一)实现消息的分组
根据入门案例发现和消息中间件实现了耦合,可以自由切换消息中间件的类型,但是这还存在一个的问题,那就是对集群的支持,假如现在有个订单业务,当用户下完订单后要给用户的手机发送短信告知,由于短信服务压力过于大我们对其进行了集群的处理,但是此时如果使用Stream进行消息的推送,是将的对集群汇总所有的节点都进行通知,此时用户将要收到和集群数量想同的信息,这样肯定是不符合逻辑的。那么如何解决呢?
我们可以使用消息分组解决
1)创建消息的提供者
2.pom文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sxt</groupId>
<artifactId>12-springcloud-stream-group-send</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>12-springcloud-stream-group-send</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-test-support</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.修改全局配置文件
spring.application.name=10-springcloud-stream-send
server.port=7070
eureka.client.service-url.defaultZone=http://peer1:8081/eureka/,http://peer2:8082/eureka/,http://peer3:8083/eureka/
#添加RabbitMQ信息
spring.rabbitmq.host=192.168.177.140
spring.rabbitmq.port=5672
spring.rabbitmq.username=oldlu
spring.rabbitmq.password=123456
spring.rabbitmq.virtual-host=/
#设置MQ内的交换请名称 outputProduct自定义在发送接口获取的值为outputProduct
spring.cloud.stream.bindings.outputProduct.destination=outputProduct
4.创建发送接口
public interface ISenderMessage
{
String OUTPUT="outputProduct";
/**
* @param null
* @description: 消息发送的接口方法
* 注解:@Output:是值的是交换器的名字
* 方法的返回值必须是SubscribableChannel类型
* @return:
* @author: shinelon
* @time: 2019/9/10:11:03
*/
@Output(OUTPUT)
public SubscribableChannel send();
}
5.修改启动类
@SpringBootApplication
@EnableEurekaClient
@EnableBinding(value = {ISenderMessage.class})
public class Application
{
public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
}
}
2)创建消费者
1.创建消费者
2.pom文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sxt</groupId>
<artifactId>13-springcloud-stream-group-customer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>13-springcloud-stream-group-customer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-test-support</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.修改全局配置文件
spring.application.name=12-springcloud-stream-group-send
server.port=6060
eureka.client.service-url.defaultZone=http://peer1:8081/eureka/,http://peer2:8082/eureka/,http://peer3:8083/eureka/
#添加RabbitMQ信息
spring.rabbitmq.host=192.168.177.140
spring.rabbitmq.port=5672
spring.rabbitmq.username=oldlu
spring.rabbitmq.password=123456
spring.rabbitmq.virtual-host=/
#设置MQ内的交换请名称 inputProduct自定义在发送接口获取的值为outputProduct
spring.cloud.stream.bindings.inputProduct.destination=outputProduct
#设置持久化 注意这个的名字要和的上面的队列的键名一致
spring.cloud.stream.bindings.inputProduct.group=groupProduct
4.添加消息的消费者接口
public interface IReceiveService
{
String INPUT= "inputProduct";
/**
* @param null
* @description:消息的接收者方法
* @Inpot:表示从指定交换器路由到指定的队列获取消息
* @return:
* @author: shinelon
* @time: 2019/9/10:11:38
*/
@Input(INPUT)
public SubscribableChannel receive();
}
5.添加具体的实现类
@Service
@EnableBinding(value = {IReceiveService.class}) //绑定的消息的接收者接口,将接口IReceiveService和的ReceiveService绑定起来
public class ReceiveService
{
/**
* @param message:接收消息对列的信息
* @description: 该方法监听指定的队列的信息
* @return:
* @author: shinelon
* @time: 2019/9/10:11:41
*/
@StreamListener(IReceiveService.INPUT)
public void onReceive(byte[] message){
System.out.println("stream ....rabbitmq"+new String(message));
}
}
6.修改测试类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ApplicationTests
{
@Autowired
private ISenderMessage iSenderMessage;
@Test
public void contextLoads()
{
String msg = "oldlu....";
Message message = MessageBuilder.withPayload(msg.getBytes()).build();
this.iSenderMessage.send().send(message);
}
}
以上就是的分组配置
消息分组的特点:
1.从生产者发送的消息以随机的方式将的消息发送到集群中的节点
2.如果是多个消息同样也是分散的进行消息的分配
消息能够发送到
使用这个的才才能进行发送不知道为什么,无法使用提供者进行提供测试消息