springcloud/springboot 集成rabbitm
直接上干货:
环境:centeros7 docker
安装启动rabbitmq
docker内 下载rabbitmq镜像 docker pull rabbitmq:3.7.7-management
启动rabbitmq: docker run -d --name rabbitmq3.7.7 -p 5672:5672 -p 15672:15672 --hostname myRabbit -e RABBITMQ_DEFAULT_VHOST=my_vhost -e RABBITMQ_DEFAULT_USER=admin RABBITMQ_DEFAULT_PASS=admin df80af9ca0c9(这个是镜像id)
RABBITMQ_DEFAULT_USER 设置默认登陆名 RABBITMQ_DEFAULT_PASS 设置默认登陆密码 ,也可以删掉默认,rabbitmq默认用户名 guest-guest
安装启动好后访问 localhost:15672 看到解面即可成功
1、执行命令:docker pull sebp/elk 将镜像pull到本地来;
2、执行命令:docker run -p 5601:5601 -p 9200:9200 -p 5044:5044 -e ES_MIN_MEM=128m -e ES_MAX_MEM=1024m -it --name elk sebp/elk 将镜像运行为容器,由于我本机内存不符合安装要求,为了保证ELK能够正常运行,加了-e参数限制使用最小内存及最大内存。
3、打开浏览器,输入:http://<your-host>:5601,看到图形化解面即安装成功
4 配置 docker exec -it elk /bin/bash 进入后 cd etc/logstash/conf.d ,然后 vi 02-beats-input.conf
input {
rabbitmq {
type =>"all"
durable => true
exchange => "ex_logstash"
exchange_type => "direct"
key => "logstash"
host => "localhost:5672"
user => "root"
password => "admin"
queue => "faceJob-logstash"
auto_delete => false
}
}
output{
elasticsearch {
hosts => ["localhost:9200"]
}
}
保存,退出容器,重启elk。
es 查看日志:http://{ip}:9200/_search?pretty
kibana : http://192.168.72.128:5601
如果访问都有结果那么elk集成rabbitmq就大功告成。
springboot 配置 rabbitmq
maven引入:
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
logback.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!--日志存储位置(读取配置文件的路径) -->
<!-- 控制台日志 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>
<appender name="AMQP"
class="org.springframework.amqp.rabbit.logback.AmqpAppender">
<layout>
<pattern>
{
"time": "%date{ISO8601}",
"thread": "%thread",
"level":
"%level",
"class": "%logger{60}",
"message": "%msg"
}
</pattern>
</layout>
<host>192.168.72.128</host>
<port>5672</port>
<username>root</username>
<password>admin</password>
<routingKeyPattern>logstash</routingKeyPattern>
<declareExchange>false</declareExchange>
<exchangeType>direct</exchangeType>
<exchangeName>ex_logstash</exchangeName>
<generateId>true</generateId>
<charset>UTF-8</charset>
<durable>true</durable>
<deliveryMode>NON_PERSISTENT</deliveryMode>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="AMQP" />
</root>
</configuration>
启动项目即可看到效果