Spring Cloud Alibaba 异步通信 - RocketMQ 生产者

文章介绍了如何使用SpringCloudStream框架与RocketMQ集成,通过Binder抽象来简化消息中间件的使用,以及解决在Docker环境下RocketMQ连接超时的问题。在项目中添加了spring-cloud-starter-stream-rocketmq依赖,并配置了broker.conf,创建消息生产者服务,实现了消息的发送。
摘要由CSDN通过智能技术生成

概述

RocketMQ 是一款开源的分布式消息系统,基于高可用分布式集群技术,提供低延时的、高可靠的消息发布与订阅服务。

Spring Cloud Stream

Spring Cloud Stream 是一个用于构建基于消息的微服务应用框架。它基于 Spring Boot 来创建具有生产级别的单机 Spring 应用,并且使用 Spring Integration 与 Broker 进行连接。

Spring Cloud Stream 提供了消息中间件配置的统一抽象,推出了 publish-subscribeconsumer groupspartition 这些统一的概念。

Spring Cloud Stream 内部有两个概念:

  • Binder: 跟外部消息中间件集成的组件,用来创建 Binding,各消息中间件都有自己的 Binder 实现。
  • Binding: 包括 Input Binding 和 Output Binding。

Binding 在消息中间件与应用程序提供的 Provider 和 Consumer 之间提供了一个桥梁,实现了开发者只需使用应用程序的 Provider 或 Consumer 生产或消费数据即可,屏蔽了开发者与底层消息中间件的接触。

解决连接超时问题

在之前的 基于 Docker 安装 RocketMQ章节中,我们采用 Docker 部署了 RocketMQ 服务,此时 RocketMQ Broker 暴露的地址和端口(10909,10911)是基于容器的,会导致我们开发机无法连接,从而引发 org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException: sendDefaultImpl call timeout 异常

注意下图中的 IP 地址,这个是容器的 IP,开发机与容器不在一个局域网所以无法连接。

解决方案是在 broker.conf 配置文件中增加 brokerIP1=宿主机IP 即可

POM

主要增加了 org.springframework.cloud:spring-cloud-starter-stream-rocketmq 依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>cn.dandelioncloud</groupId>
  7. <artifactId>hello-spring-cloud-alibaba-dependencies</artifactId>
  8. <version>1.0.0-SNAPSHOT</version>
  9. <relativePath>../hello-spring-cloud-alibaba-dependencies/pom.xml</relativePath>
  10. </parent>
  11. <artifactId>hello-spring-cloud-alibaba-rocketmq-provider</artifactId>
  12. <packaging>jar</packaging>
  13. <name>hello-spring-cloud-alibaba-rocketmq-provider</name>
  14. <url>http://www.dandelioncloud.cn</url>
  15. <inceptionYear>2018-Now</inceptionYear>
  16. <dependencies>
  17. <!-- Spring Boot Begin -->
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-web</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-actuator</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-test</artifactId>
  29. <scope>test</scope>
  30. </dependency>
  31. <!-- Spring Boot End -->
  32. <!-- Spring Cloud Begin -->
  33. <dependency>
  34. <groupId>org.springframework.cloud</groupId>
  35. <artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
  36. </dependency>
  37. <!-- Spring Cloud End -->
  38. </dependencies>
  39. <build>
  40. <plugins>
  41. <plugin>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-maven-plugin</artifactId>
  44. <configuration>
  45. <mainClass>cn.dandelioncloud.hello.spring.cloud.alibaba.rocketmq.provider.RocketMQProviderApplication</mainClass>
  46. </configuration>
  47. </plugin>
  48. </plugins>
  49. </build>
  50. </project>

消息生产者服务

  1. package cn.dandelioncloud.hello.spring.cloud.alibaba.rocketmq.provider.service;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.messaging.MessageChannel;
  4. import org.springframework.messaging.support.MessageBuilder;
  5. import org.springframework.stereotype.Service;
  6. @Service
  7. public class ProviderService {
  8. @Autowired
  9. private MessageChannel output;
  10. public void send(String message) {
  11. output.send(MessageBuilder.withPayload(message).build());
  12. }
  13. }

Application

配置 Output(Source.class) 的 Binding 信息并配合 @EnableBinding 注解使其生效

  1. package cn.dandelioncloud.hello.spring.cloud.alibaba.rocketmq.provider;
  2. import cn.dandelioncloud.hello.spring.cloud.alibaba.rocketmq.provider.service.ProviderService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.boot.CommandLineRunner;
  5. import org.springframework.boot.SpringApplication;
  6. import org.springframework.boot.autoconfigure.SpringBootApplication;
  7. import org.springframework.cloud.stream.annotation.EnableBinding;
  8. import org.springframework.cloud.stream.messaging.Source;
  9. @SpringBootApplication
  10. @EnableBinding({Source.class})
  11. public class RocketMQProviderApplication implements CommandLineRunner {
  12. @Autowired
  13. private ProviderService providerService;
  14. public static void main(String[] args) {
  15. SpringApplication.run(RocketMQProviderApplication.class, args);
  16. }
  17. /**
  18. * 实现了 CommandLineRunner 接口,只是为了 Spring Boot 启动时执行任务,不必特别在意
  19. * @param args
  20. * @throws Exception
  21. */
  22. @Override
  23. public void run(String... args) throws Exception {
  24. providerService.send("Hello RocketMQ");
  25. }
  26. }

application.yml

  1. spring:
  2. application:
  3. name: rocketmq-provider
  4. cloud:
  5. stream:
  6. rocketmq:
  7. binder:
  8. # RocketMQ 服务器地址
  9. namesrv-addr: 192.168.10.149:9876
  10. bindings:
  11. # 这里是个 Map 类型参数,{} 为 YAML 中 Map 的行内写法
  12. output: {destination: test-topic, content-type: application/json}
  13. server:
  14. port: 9093
  15. management:
  16. endpoints:
  17. web:
  18. exposure:
  19. include: '*'

运行成功后即可在 RocketMQ 控制台的 消息 列表中选择 test-topic 主题即可看到发送的消息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智慧浩海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值