No, beep, code
Mainly for consumers to add configuration
1.Put the configuration in the configuration center (it can also be put on the consumer service)
listener.direct.auto -Startup is set to false,
Then add rabbitmq.start As startup property
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
listener:
direct:
auto-startup: false
rabbitmq:
start: true
Then in the consumer service startup class xxxxxApplication.class Add in
package com.test.service1;
import com.test.rabbitmq.RabbitmqApplication;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import javax.annotation.Resource;
@SpringBootApplication
@EnableEurekaClient
public class Service1Application {
public static void main(String[] args) {
ApplicationContext context=SpringApplication.run(Service1Application.class, args);
RabbitMQStart rabbitMQRun = context.getBean(RabbitMQStart.class);
rabbitMQRun.start();
}
@Bean
public RabbitMQStart rabbitMQRun() {
return new RabbitMQStart();
}
private static class RabbitMQStart {
//In order to use the @value annotation in the static method in main only this way can be used
@Value("${rabbitmq.start}")
private Boolean rabbitmqStart;
@Resource
RabbitListenerEndpointRegistry rabbitListenerEndpointRegistry;
public void start() {
if(rabbitmqStart)
rabbitListenerEndpointRegistry.start();
else
rabbitListenerEndpointRegistry.stop();
System.out.println("=================== Rabbitmq:"+rabbitmqStart+"===================");
}
}
}
2.Test:
rabbitmq.start =When true
rabbitmq.start =When false