2018年7月24日 | Leave a comment 试验发现: rabbitmq 服务器挂掉后, actuator/health 会变成 down 试验代码: 启动 rabbitmq docker run -d -p 5672:5672 -p 15672:15672 rabbitmq:3-management 1 docker run -d -p 5672:5672 -p 15672:15672 rabbitmq:3-management pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>demo</groupId> <artifactId>rabbitmq2</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> </project> 1234567891011121314151617181920212223242526272829303132 <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>demo</groupId> <artifactId>rabbitmq2</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties></project> App.java package demo.rabbitmq2; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.core.AmqpAdmin; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.amqp.core.ExchangeTypes; import org.springframework.amqp.core.Message; import org.springframework.amqp.rabbit.annotation.Exchange; import org.springframework.amqp.rabbit.annotation.Queue; import org.springframework.amqp.rabbit.annotation.QueueBinding; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Lazy; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** * * @author 老唐 */ @SpringBootApplication @EnableScheduling public class App { final static Logger LOG = LoggerFactory.getLogger(App.class); public static void main(String[] argv) { SpringApplication.run(App.class, argv); } @Component @Lazy(false) public static class MQListener1 { private final AmqpTemplate amqp; @Autowired public MQListener1(AmqpTemplate amqp, AmqpAdmin admin) { this.amqp = amqp; } @RabbitListener(bindings = @QueueBinding( key = {"#"}, value = @Queue(value = "test-queue1", durable = "true", ignoreDeclarationExceptions = "true"), exchange = @Exchange(value = "test-1", type = ExchangeTypes.TOPIC, ignoreDeclarationExceptions = "true")) ) public void proc(String msg) { LOG.info("msg:{}", msg); } int i = 0; @Scheduled(fixedRate = 2000) public void s() { amqp.convertAndSend("test-1", null, "你好 " + (++i)); } } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 package demo.rabbitmq2; import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.amqp.core.AmqpAdmin;import org.springframework.amqp.core.AmqpTemplate;import org.springframework.amqp.core.ExchangeTypes;import org.springframework.amqp.core.Message;import org.springframework.amqp.rabbit.annotation.Exchange;import org.springframework.amqp.rabbit.annotation.Queue;import org.springframework.amqp.rabbit.annotation.QueueBinding;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Lazy;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component; /** * * @author 老唐 */@SpringBootApplication@EnableSchedulingpublic class App { final static Logger LOG = LoggerFactory.getLogger(App.class); public static void main(String[] argv) { SpringApplication.run(App.class, argv); } @Component @Lazy(false) public static class MQListener1 { private final AmqpTemplate amqp; @Autowired public MQListener1(AmqpTemplate amqp, AmqpAdmin admin) { this.amqp = amqp; } @RabbitListener(bindings = @QueueBinding( key = {"#"}, value = @Queue(value = "test-queue1", durable = "true", ignoreDeclarationExceptions = "true"), exchange = @Exchange(value = "test-1", type = ExchangeTypes.TOPIC, ignoreDeclarationExceptions = "true")) ) public void proc(String msg) { LOG.info("msg:{}", msg); } int i = 0; @Scheduled(fixedRate = 2000) public void s() { amqp.convertAndSend("test-1", null, "你好 " + (++i)); } } }