SpringBoot停止启动时测试检查rabbitmq操作
程序员文章站
2022-06-23 23:35:22
目录springboot停止启动时测试检查rabbitmqrabbitmq的简单使用的demospringboot停止启动时测试检查rabbitmq问题在springboot项目中配置rabbitmq...
springboot停止启动时测试检查rabbitmq
问题
在springboot项目中配置rabbitmq后,总是在每次启动时自动测试mq的连接,如果测试不通过,就一直重连,导致项目无法正常启动。自己在开发与mq无关的功能时,无法正常进行,十分耽误时间。如下所示:
org.springframework.amqp.amqpconnectexception: java.net.connectexception: connection timed out: connect at org.springframework.amqp.rabbit.support.rabbitexceptiontranslator.convertrabbitaccessexception(rabbitexceptiontranslator.java:62) ~[spring-rabbit-2.0.6.release.jar:2.0.6.release] at org.springframework.amqp.rabbit.connection.abstractconnectionfactory.createbareconnection(abstractconnectionfactory.java:484) ~[spring-rabbit-2.0.6.release.jar:2.0.6.release] at org.springframework.amqp.rabbit.connection.cachingconnectionfactory.createconnection(cachingconnectionfactory.java:628) ~[spring-rabbit-2.0.6.release.jar:2.0.6.release] at org.springframework.amqp.rabbit.connection.connectionfactoryutils.createconnection(connectionfactoryutils.java:240) ~[spring-rabbit-2.0.6.release.jar:2.0.6.release] at org.springframework.amqp.rabbit.core.rabbittemplate.doexecute(rabbittemplate.java:1816) ~[spring-rabbit-2.0.6.release.jar:2.0.6.release] at org.springframework.amqp.rabbit.core.rabbittemplate.execute(rabbittemplate.java:1790) ~[spring-rabbit-2.0.6.release.jar:2.0.6.release] at org.springframework.amqp.rabbit.core.rabbittemplate.execute(rabbittemplate.java:1771) ~[spring-rabbit-2.0.6.release.jar:2.0.6.release] at org.springframework.amqp.rabbit.core.rabbitadmin.getqueueproperties(rabbitadmin.java:345) ~[spring-rabbit-2.0.6.release.jar:2.0.6.release] at org.springframework.amqp.rabbit.listener.abstractmessagelistenercontainer.redeclareelementsifnecessary(abstractmessagelistenercontainer.java:1604) ~[spring-rabbit-2.0.6.release.jar:2.0.6.release] at org.springframework.amqp.rabbit.listener.simplemessagelistenercontainer$asyncmessageprocessingconsumer.run(simplemessagelistenercontainer.java:995) [spring-rabbit-2.0.6.release.jar:2.0.6.release] at java.lang.thread.run(thread.java:748) [na:1.8.0_231] caused by: java.net.connectexception: connection timed out: connect at java.net.dualstackplainsocketimpl.waitforconnect(native method) ~[na:1.8.0_231] at java.net.dualstackplainsocketimpl.socketconnect(dualstackplainsocketimpl.java:85) ~[na:1.8.0_231] at java.net.abstractplainsocketimpl.doconnect(abstractplainsocketimpl.java:350) ~[na:1.8.0_231] at java.net.abstractplainsocketimpl.connecttoaddress(abstractplainsocketimpl.java:206) ~[na:1.8.0_231] at java.net.abstractplainsocketimpl.connect(abstractplainsocketimpl.java:188) ~[na:1.8.0_231] at java.net.plainsocketimpl.connect(plainsocketimpl.java:172) ~[na:1.8.0_231] at java.net.sockssocketimpl.connect(sockssocketimpl.java:392) ~[na:1.8.0_231] at java.net.socket.connect(socket.java:606) ~[na:1.8.0_231] at com.rabbitmq.client.impl.socketframehandlerfactory.create(socketframehandlerfactory.java:60) ~[amqp-client-5.4.1.jar:5.4.1] at com.rabbitmq.client.connectionfactory.newconnection(connectionfactory.java:1102) ~[amqp-client-5.4.1.jar:5.4.1] at com.rabbitmq.client.connectionfactory.newconnection(connectionfactory.java:1054) ~[amqp-client-5.4.1.jar:5.4.1] at com.rabbitmq.client.connectionfactory.newconnection(connectionfactory.java:994) ~[amqp-client-5.4.1.jar:5.4.1] at org.springframework.amqp.rabbit.connection.abstractconnectionfactory.createbareconnection(abstractconnectionfactory.java:457) ~[spring-rabbit-2.0.6.release.jar:2.0.6.release] ... 9 common frames omitted
查找了很多地方,这里记录一下,如何在springboot启动项目时,如何关闭rabbitmq的连接测试。
解决
在配置文件里增加
spring.rabbitmq.listener.direct.auto-startup= false spring.rabbitmq.listener.simple.auto-startup= false
这样启动时就不会每次都检查rabbitmq是否可以连接了
rabbitmq的简单使用的demo
当前使用的版本:rabbitmq-server-windows-3.8.3
1.声明
当前内容用于本人学习和使用rabbitmq这个消息队列,当前内容为springboot操作rabbitmq的基本操作
本人使用windows10方式使用rabbitmq
2.创建一个测试账户
注意这里的can access virtual hosts必须修改为/,否则可能出现连接失败,这里使用账户为root
3.pom依赖
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.5.2.release</version> <relativepath /> </parent> <groupid>springboot-rabbitmq</groupid> <artifactid>springboot-rabbitmq</artifactid> <version>0.0.1-snapshot</version> <packaging>jar</packaging> <name>springboot-rabbitmq</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding>utf-8</project.reporting.outputencoding> <java.version>1.8</java.version><!-- 更新springcloud的版本 --> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-amqp</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-actuator</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build>
4.配置文件application.properties
spring.application.name=rabbitmq-hello server.port=8080 spring.rabbitmq.host spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=root spring.rabbitmq.password=root
5.创建入口类
@restcontroller @springbootapplication public class rabbitmqapplication { @autowired amqptemplate amqptemplate; @requestmapping("/send") public string send(string msg) { amqptemplate.convertandsend("test", msg); return "发送消息成功:===>" + msg; } public static void main(string[] args) { springapplication.run(rabbitmqapplication.class, args); } @bean queue queue() { return new queue("test"); } @component @rabbitlistener(queues = "test") class msgcustomer { @rabbithandler public void handlermsg(string msg) { system.out.println("消息消费者消费===>" + msg); } } }
6.测试
7.总结
1.通过使用springboot操作rabbitmq的时候非常简单,直接使用amqptemplate即可
2.通过@rabbitlistener指定需要监听的队列,通过@rabbithandler方式处理监听后的数据
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。