试验发现:
rabbitmq 服务器挂掉后, actuator/health 会变成 down
试验代码:
启动 rabbitmq
1 |
docker run -d -p 5672:5672 -p 15672:15672 rabbitmq:3-management |
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
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)); } } } |