首先安装好ELK,我使用的版本是5.6.10,HEAD,RabbitMQ,在RabbitMQ管理界面,创建Exchange :ex_logstash,Queue:q_logstash,建立绑定,routing key是你的服务名称。当然也可以通过程序创建Exchange,Queue和绑定关系。然后按照如下步骤操作。
一,在Spring Boot 2.0 项目pom.xml里添加RabbitMQ依赖,如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
二,在logback.xml里配置如下
<appender name="AMQP" class="org.springframework.amqp.rabbit.logback.AmqpAppender">
<layout>
<pattern>
<![CDATA[%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}.%method %line - %msg%n ]]>
</pattern>
</layout>
<host>rabbitmq address</host>
<port>5672</port>
<username>user</username>
<password>password</password>
<applicationId>credit-card-bill</applicationId>
<routingKeyPattern>credit-card-bill</routingKeyPattern>
<declareExchange>true</declareExchange>
<exchangeType>direct</exchangeType>
<exchangeName>ex_logstash</exchangeName>
<generateId>true</generateId>
<charset>UTF-8</charset>
<durable>true</durable>
<deliveryMode>PERSISTENT</deliveryMode>
</appender>
<root level="INFO">
<appender-ref ref="console" />
<appender-ref ref="InfoRollingFile" />
<appender-ref ref="AMQP" />
</root>
三,在logstash安装目录新建logstash.conf,配置如下
input {
rabbitmq {
host => "10.139.60.131"
port => 5672
user => kkuser
password => kkuser123
durable => true
queue => "q_logstash"
codec => plain
}
}
output {
elasticsearch {
hosts => ["http://127.0.0.1:9200"]
}
}
四,启动ELK,通过Head可以看到Logstash创建了以logstash-为前缀,后跟日期的索引。发起请求,产生日志,在Kibana界面中就能查看到日志了。