请参考 : docker 安装 rocketmq(namesrv broker 与可视化控制台 console)
请参考 : SpringCloud H系列 alibaba 2.1.1 (一) nacos1.X 注册中心 服务端
pom.xml
com.alibaba
spring-cloud-hoxton
1.0
4.0.0
alibaba-stream-rocketmq-produce
jar
com.alibaba.cloud
spring-cloud-starter-stream-rocketmq
org.springframework.boot
spring-boot-starter-web
application.yml 配置文件
application:
name: alibaba-stream-rocketmq-produce
cloud:
stream:
rocketmq:
binder:
# rocketmq 地址
name-server: 192.168.101.11:9876
bindings:
output:
content-type: application/json
# 用来指定topic
destination: stream-test-topic
StreamRocketMQProduceApplication.java 启动类 增加 @EnableBinding(Source.class) 注解
/**
* @author Lion Li
*/
@SpringBootApplication
@EnableBinding(Source.class)
public class StreamRocketMQProduceApplication {
public static void main(String[] args) {
SpringApplication.run(StreamRocketMQProduceApplication.class, args);
}
}
创建消息体 TestMessaging.java 这里使用了lombok
/**
* @author Lion Li
*/
@Data
@Accessors(chain = true)
public class TestMessaging {
/**
* 消息id
*/
private String msgId;
/**
* 消息内容
*/
private String msgText;
}
创建消息投递方法 TestController.java
/**
* @author Lion Li
*/
@RestController
public class TestController {
@Autowired
private Source source;
@GetMapping("/stream-test-msg")
public String streamTestMsg(String msg){
// 构建消息对象
TestMessaging testMessaging = new TestMessaging()
.setMsgId(UUID.randomUUID().toString())
.setMsgText(msg);
source.output().send(
MessageBuilder.withPayload(testMessaging).build());
return "投递消息 => " + msg + " => 成功";
}
}
启动消息生产端 访问 http://localhost:8000/stream-test-msg?msg=Lion Li
查看rocketmq控制台
查看消息内容
投递成功
pom.xml
com.alibaba
spring-cloud-hoxton
1.0
4.0.0
alibaba-stream-rocketmq-consumer
jar
com.alibaba.cloud
spring-cloud-starter-stream-rocketmq
org.springframework.boot
spring-boot-starter-web
application.yml 配置文件
server:
port: 8002
spring:
application:
name: alibaba-stream-rocketmq-consumer
cloud:
stream:
rocketmq:
binder:
# rocketmq 地址
name-server: 192.168.101.11:9876
bindings:
input:
content-type: application/json
# 定义 topic
destination: stream-test-topic
# group一定要写
group: test-group
StreamRocketMQConsumerApplication.java 启动类 增加 @EnableBinding(Sink.class) 注解
/**
* @author Lion Li
*/
@SpringBootApplication
@EnableBinding(Sink.class)
public class StreamRocketMQConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(StreamRocketMQConsumerApplication.class, args);
}
}
消息体 TestMessaging.java
/**
* @author Lion Li
*/
@Data
@Accessors(chain = true)
public class TestMessaging {
/**
* 消息id
*/
private String msgId;
/**
* 消息内容
*/
private String msgText;
}
创建消息监听 TestStreamConsumer.java 包含全局异常处理
@Slf4j
@Service
public class TestStreamConsumer {
@StreamListener(Sink.INPUT)
public void receive(TestMessaging testMessaging){
log.warn("通过stream消费到消息 => {}",testMessaging.toString());
// if (testMessaging.getMsgText().equals("测试异常")){
// throw new RuntimeException("测试全局异常拦截");
// }
}
/**
* 全局异常处理
*/
@StreamListener("errorChannel")
public void error(Message> message){
ErrorMessage errorMessage = (ErrorMessage) message;
log.error("发生异常 => {}",errorMessage);
}
}
启动消息消费端 查看是否可以消费到消息
消费成功
项目已上传到gitee
地址: spring-cloud-alibaba-H-demo
如果帮到您了,请帮忙点个star