目录
消息总线实例github的地址:https://github.com/kevin-lihongmin/spring-cloud-project-kevin/tree/master/bus-demo中的bus-sender和bus-receiver。
之前的时候从网上搜索Spring Cloud Bus总会出现Spring Cloud Config相关的东西,以为就只是为了降低配置服务端的调用量而出现了,其实这只是其中的一个重要经常,Spring Cloud Bus应该是对所有的应用的消息的管理。所以,先创建一个bus-demo的Spring Boot项目,在项目下创建两个子应用。bus-demo的dependencies如下:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-bus</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream-binder-rabbit</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
1、bus-sender
1)、启动类中添加一个web可以访问的入口
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class BusSenderApplication {
private static final Logger log = LoggerFactory.getLogger(Sender.class);
public static void main(String[] args) {
SpringApplication.run(BusSenderApplication.class, args);
}
@Autowired
private Sender sender;
@RequestMapping("send")
public String send() {
for (int i = 0; i < 1000; i++) {
sender.send(i);
}
return "success!";
}
}
2)、添加一个消息发送类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Sender {
private static final Logger log = LoggerFactory.getLogger(Sender.class);
@Autowired
private AmqpTemplate amqpTemplate;
public void send(int i) {
String message = "hello world " + i + " ! ";
log.info("Sender message : " + message);
this.amqpTemplate.convertAndSend("hello", message);
}
}
3)、配置信息
application.properties
# 服务名称 spring.application.name=bus-sender # 配置服务端口 server.port=9501
bootstrap.properties
# spring cloud bus 刷新配置 spring.rabbitmq.host=127.0.0.1 spring.rabbitmq.port=5672 spring.rabbitmq.password=guest spring.rabbitmq.username=guest spring.cloud.bus.enabled=true spring.cloud.bus.trace.enabled=true spring.cloud.bus.virtual-host=/
2、bus-receiver
1)、启动类中注册一个Queue的Bean
import org.springframework.amqp.core.Queue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class BusReceiverApplication {
@Bean
public Queue helloQueue(){
return new Queue("hello");
}
public static void main(String[] args) {
SpringApplication.run(BusReceiverApplication.class, args);
}
}
2)、添加消息监听
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "hello")
public class Receiver {
private static final Logger log = LoggerFactory.getLogger(Receiver.class);
@RabbitHandler
public void process(String hello) {
log.info("Receiver info : {}", hello);
}
}
3)、配置信息
使用Spring Profiles启动两个客户端去接受消息
receiver-1
application-receiver-1.properties
# 配置服务端口 server.port=9502 # 服务名称 spring.application.name=bus-receiver-1
bootstrap-receiver-1.properties
# spring cloud bus 刷新配置 spring.rabbitmq.host=127.0.0.1 spring.rabbitmq.port=5672 spring.rabbitmq.password=guest spring.rabbitmq.username=guest spring.cloud.bus.enabled=true spring.cloud.bus.trace.enabled=true spring.cloud.bus.virtual-host=/
receiver-2
application-receiver-2.properties
# 配置服务端口 server.port=9503 # 服务名称 spring.application.name=bus-receiver-2
bootstrap-receiver-2.properties
# spring cloud bus 刷新配置 spring.rabbitmq.host=127.0.0.1 spring.rabbitmq.port=5672 spring.rabbitmq.password=guest spring.rabbitmq.username=guest spring.cloud.bus.enabled=true spring.cloud.bus.trace.enabled=true spring.cloud.bus.virtual-host=/
3、启动三个服务
启动服务后,在浏览器中输入: http://127.0.0.1:9501/send, 调用发送信息。
Rabbitmq connections
Rabbitmq exchange
Rabbitmq queue
两个客户端分别进行消息的消费:
2019-06-13 12:50:41.903 INFO 12252 --- [ntContainer#0-1] com.kevin.bus.receiver.Receiver : Receiver info : hello world 0 !
2019-06-13 12:50:41.906 INFO 12252 --- [ntContainer#0-1] com.kevin.bus.receiver.Receiver : Receiver info : hello world 2 !
2019-06-13 12:50:41.925 INFO 7420 --- [ntContainer#0-1] com.kevin.bus.receiver.Receiver : Receiver info : hello world 1 !
2019-06-13 12:50:41.926 INFO 7420 --- [ntContainer#0-1] com.kevin.bus.receiver.Receiver : Receiver info : hello world 3 !