视频链接:2020最新版SpringCloud框架开发教程-周阳 文章源码:https://github.com/geyiwei-suzhou/cloud2020/
消息驱动生产者
cloud-stream-rabbitmq-provider8801模块
1. 建module
New --> Module --> Maven[Module SDK:1.8.0_191] --> name[cloud-stream-rabbitmq-provider8801] --> Finish
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2020</artifactId>
<groupId>com.antherd.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-stream-rabbitmq-provider8801</artifactId>
<dependencies>
<!-- rabbitMQ -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</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.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 基础配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3. 写yml
server:
port: 8801
spring:
application:
name: cloud-stream-provider
cloud:
stream:
binders: # 在此处配置要绑定的rabbitMQ的服务信息
defaultRabbit: # 表示定义的名称,用于binding整合
type: rabbit # 消息组件类型
environment: # 设置rabbitmq的相关环境配置
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
bindings: # 服务的整合处理
output: # 这个名字是一个通道的名称
destination: studyExchange # 表示要使用的Exchange名称定义
content-type: application/json # 消息类型,设置为json,文本则设置"text/plain"
binder: defaultRabbit # 设置要绑定的消息服务的具体设置
eureka:
client: # 客户端进行Eureka注册的配置
service-url:
defaultZone: http://localhost:7001/eureka
instance:
lease-renewal-interval-in-seconds: 2 # 设置心跳的时间间隔(默认是30秒)
lease-expiration-duration-in-seconds: 5 # 如果现在超过了5秒的间隔(默认是90秒)
4. 主启动
新建类:com.antherd.springcloud.StreamMQMain8801
package com.antherd.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class StreamMQMain8801 {
public static void main(String[] args) {
SpringApplication.run(StreamMQMain8801.class, args);
}
}
5 写业务
新建类:com.antherd.springcloud.service.IMessageProvider
package com.antherd.springcloud.service;
public interface IMessageProvider {
public String send();
}
新建类:com.antherd.springcloud.service.impl.MessageProviderImpl
package com.antherd.springcloud.service.impl;
import com.antherd.springcloud.service.IMessageProvider;
import java.util.UUID;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
@EnableBinding(Source.class) // 定义消息的推送管道
@Slf4j
public class MessageProviderImpl implements IMessageProvider {
@Resource
private MessageChannel output; // 消息发送管道,对应yml配置中binding下的通道名称
@Override
public String send() {
String serial = UUID.randomUUID().toString();
output.send(MessageBuilder.withPayload(serial).build());
log.info("***** serial:" + serial);
return null;
}
}
新建类:com.antherd.springcloud.controller.SendMessageController
package com.antherd.springcloud.controller;
import com.antherd.springcloud.service.IMessageProvider;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SendMessageController {
@Resource
private IMessageProvider messageProvider;
@GetMapping(value = "/sendMessage")
public String sendMessage() {
return messageProvider.send();
}
}
6. 启动测试
启动:cloud-eureka-server7001,cloud-stream-rabbitmq-provider8801 两个模块
访问:http://localhost:8801/sendMessage 在后台查看8801模块打印日志,并在http://localhost:15672/#/ 网页Overview中查看Message rates曲线变化
消息驱动消费者
cloud-stream-rabbitmq-consumer8802模块
1. 建module
New --> Module --> Maven[Module SDK:1.8.0_191] --> name[cloud-stream-rabbitmq-consumer8802] --> Finish
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2020</artifactId>
<groupId>com.antherd.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-stream-rabbitmq-consumer8802</artifactId>
<dependencies>
<!-- rabbitMQ -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</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.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 基础配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
3. 写yml
server:
port: 8802
spring:
application:
name: cloud-stream-provider
cloud:
stream:
binders: # 在此处配置要绑定的rabbitMQ的服务信息
defaultRabbit: # 表示定义的名称,用于binding整合
type: rabbit # 消息组件类型
environment: # 设置rabbitmq的相关环境配置
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
bindings: # 服务的整合处理
input: # 这个名字是一个通道的名称
destination: studyExchange # 表示要使用的Exchange名称定义
content-type: application/json # 消息类型,设置为json,文本则设置"text/plain"
binder: defaultRabbit # 设置要绑定的消息服务的具体设置
eureka:
client: # 客户端进行Eureka注册的配置
service-url:
defaultZone: http://localhost:7001/eureka
instance:
lease-renewal-interval-in-seconds: 2 # 设置心跳的时间间隔(默认是30秒)
lease-expiration-duration-in-seconds: 5 # 如果现在超过了5秒的间隔(默认是90秒)
instance-id: receive-8802.com #在信息列表时显示主机名称
prefer-ip-address: true #访问的路径变成IP地址
4. 主启动
新建类:com.antherd.springcloud.StreamMQMain8802
package com.antherd.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class StreamMQMain8802 {
public static void main(String[] args) {
SpringApplication.run(StreamMQMain8802.class, args);
}
}
5 写业务
新建类:com.antherd.springcloud.controller.ReceiveMessageListenerController
package com.antherd.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
@Component
@EnableBinding(Sink.class)
@Slf4j
public class ReceiveMessageListenerController {
@Value("${server.port}")
private String serverPort;
@StreamListener(Sink.INPUT)
public void input(Message<String> message) {
log.info("消费者1号, ---> 接收到的消息:" + message.getPayload() + ",port:" + serverPort);
}
}
6. 启动测试
启动:cloud-eureka-server7001,cloud-stream-rabbitmq-provider8801,cloud-stream-rabbitmq-consumer8802 三个模块
访问:http://localhost:8801/sendMessage,查看cloud-stream-rabbitmq-consumer8802后台,发现收到消息打印出了日志
cloud-stream-rabbitmq-consumer8803模块
参照cloud-stream-rabbitmq-consumer8802模块新建cloud-stream-rabbitmq-consumer8803模块
- 启动类改为StreamMQMain8803
- 端口号改为8803
- instance-id改为receive-8803.com
启动:cloud-eureka-server7001,cloud-stream-rabbitmq-provider8801,cloud-stream-rabbitmq-consumer8802、cloud-stream-rabbitmq-consumer8803 四个模块
访问:http://localhost:8801/sendMessage,查看cloud-stream-rabbitmq-consumer8802、cloud-stream-rabbitmq-consumer8803后台,发现收到消息打印出了日志
上述的Stream消息存在的问题一:
生产者发送一条消息,两个消费者都会收到消息,存在重复消费问题
比如在如下场景中,订单系统我们做集群部署,都会从RabbitMQ中获取订单信息,那么如果一个订单同时被两个服务获取到,那么就会造成数据错误,我们得避免这种情况
这时我们就可以使用Stream中的消息分组来解决
注意在Stream中处于同一个group中的多个消费者是竞争关系,就能够保证消息只会被其中一个应用消费一次。
不同组是可以全面消费的(重复消费)
同一组内会发生竞争关系,只有其中一个可以消费
解决方案:
自定义配置分组,将两个消费者分为同一个自定义配置组
修改cloud-stream-rabbitmq-consumer8802、cloud-stream-rabbitmq-consumer8803的pom文件,分别在input节点下添加子节点配置:
group: antherdA
group: antherdB
观察RabbitMQ Exchange,发现已经studyExchange中显示了studyExchange.antherdA、studyExchange.antherdB两个Bindings
访问:http://localhost:8801/sendMessage,发现两个消费者都会收到消息
修改cloud-stream-rabbitmq-consumer8802、cloud-stream-rabbitmq-consumer8803的pom文件,将分组改为同一分组:
group: antherdA
访问:http://localhost:8801/sendMessage,发现只有一个消费者收到了消息
上述的Stream消息存在的问题二:
此时,停掉cloud-stream-rabbitmq-consumer8802、cloud-stream-rabbitmq-consumer8803两个微服务,将cloud-stream-rabbitmq-consumer8802微服务中group: antherdA
配置给注释掉,保留cloud-stream-rabbitmq-consumer8803的group: antherdA
配置
访问http://localhost:8801/sendMessage 四次,然后启动cloud-stream-rabbitmq-consumer8802,发现8802消费者没有接收/处理服务宕机这段时间丢失的消息,再启动cloud-stream-rabbitmq-consumer8803,发现8803消费者处理了服务宕机期间未处理的消息
这是因为未配置group,每次重启后会自动生成一个随机group,此时不会处理之前的消息。添加group配置可以保证消息持久化