Spring Cloud Bus Message Bus RabbitMQ

Original link: http://www.cnblogs.com/luangeng/p/7291770.html

Spring Cloud Bus system distributed nodes connected by a lightweight message broker. Thereby achieving a state change such as a broadcast (e.g., a configuration change) command, or other management. The only implementation is to use AMQP as a transport agent object.

Spring Cloud Bus is also known as message bus, responsible for the management and dissemination of all distributed system message broadcast mechanism to achieve integration through MQ, it is currently used Kafka and RabbitMQ. Use bus mechanism can do a lot of things, where the configuration center client refresh is a typical application scenarios. And using the message bus, the configuration may be achieved when the center of the refresh command is to send to each end of a configuration using the radio. 

Spring has completed most of the work in the Config package, we only need to center on the basis of the previous configuration, modified as follows:

1. To the config-server config-client, and increased reliance:

<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

2. Give config-server and config-client configuration file to increase MQ

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=lg
spring.rabbitmq.password=lg

3. turn starts RabbitMQ, Eureka, config-server, config-client1, config-client2

4. Modify profile on GitHub, refresh config-client configuration view, are not updated at this time

Then perform curl -X POST http: // localhost: After 8011 / bus / refresh, see the config-client configuration again, this time has been updated 

5. Log RabbitMQ management page, you can view to connecting, queue and other information.

 

Sometimes may need to refresh part, that is only refresh the specified config-client, spring also support, only need to call the transfer destination argument when / bus / refresh interfaces, such as:

/bus/refresh?destination=config-client:8031 或 /bus/refresh?destination=config-client:**

 

Custom message and sends messages via the bus

On the basis of several projects before, we make the following modifications:

1.config-server-based class increased Sender

@Component
public class Sender {

    private static AtomicInteger count = new AtomicInteger(1);

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send() {
        String msg = "msg" + count.getAndIncrement() + " " + new Date();
        System.out.println("Sender : " + msg);
        this.rabbitTemplate.convertAndSend("hello", msg);
    }
}

 

2.config-server-based timer task increases, to the main class annotated @EnableScheduling, send a message every 3 seconds:

@Component
public class ScheduledTasks {

    @Autowired
    Private Broadcaster sends;

    @Scheduled(initiaDelay=5000, fixedRate = 3000)
    public void reportCurrentTime() {
        sender.send ();
    }

}

 

3, respectively, and a config-client1 config-client2 increasing acceptance class Receiver

@Component
@RabbitListener(queues = "hello")
public class Receiver {

    @RabbitHandler
    public void process(String msg) {
        System.out.println("Receiver : " + msg);
    }

}

 

4. Start sequentially eureka config-server config-client

5. Observe console output as follows:

config-server:

Sender : msg1 Sun Aug 06 21:56:03 CST 2017
Sender : msg2 Sun Aug 06 21:56:06 CST 2017
Sender : msg3 Sun Aug 06 21:56:09 CST 2017
Sender : msg4 Sun Aug 06 21:56:12 CST 2017
Sender : msg5 Sun Aug 06 21:56:15 CST 2017
Sender : msg6 Sun Aug 06 21:56:18 CST 2017
Sender : msg7 Sun Aug 06 21:56:21 CST 2017
Sender : msg8 Sun Aug 06 21:56:24 CST 2017
Sender : msg9 Sun Aug 06 21:56:27 CST 2017

config-client1:

Receiver : msg2 Sun Aug 06 21:56:06 CST 2017
Receiver : msg4 Sun Aug 06 21:56:12 CST 2017
Receiver : msg6 Sun Aug 06 21:56:18 CST 2017
Receiver : msg8 Sun Aug 06 21:56:24 CST 2017

config-client2:

Receiver : msg1 Sun Aug 06 21:56:03 CST 2017
Receiver : msg3 Sun Aug 06 21:56:09 CST 2017
Receiver : msg5 Sun Aug 06 21:56:15 CST 2017
Receiver : msg7 Sun Aug 06 21:56:21 CST 2017
Receiver : msg9 Sun Aug 06 21:56:27 CST 2017

Visible from the client to take a single message queue sequentially

 

 

Reference: http://projects.spring.io/spring-cloud/spring-cloud.html 

 

end

Reproduced in: https: //www.cnblogs.com/luangeng/p/7291770.html

Guess you like

Origin blog.csdn.net/weixin_30515513/article/details/94786679