springcloud整合stream解决项目升级的多个消息中间件的收发问题
cloud stream
(一)简介
Spring Cloud Stream 是一个用来为微服务应用构建消息驱动能力的框架。它可以基于 Spring Boot 来创建独立的、可用于生产的 Spring 应用程序。Spring Cloud Stream 为一些供应商的消息中间件产品提供了个性化的自动化配置实现,并引入了发布-订阅、消费组、分区这三个核心概念。通过使用 Spring Cloud Stream,可以有效简化开发人员对消息中间件的使用复杂度,让系统开发人员可以有更多的精力关注于核心业务逻辑的处理。但是目前 Spring Cloud Stream 只支持 RabbitMQ 和 Kafka 的自动化配置。
(二)快速搭建
首先,我们通过一个简单的示例对 Spring Cloud Stream 有一个初步的认识。我们中间件使用 RabbitMQ,创建 spring-cloud-stream 模块
消息的发送者:springboot工程
POM
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.7.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!--spring boot热部署插件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-stream-rabbit -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
<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>
</dependencies>
application.yml文件:
server:
port: 8801
spring:
application:
name: stream-output
cloud:
stream:
bindings:
output_channel: #通道
destination: exchange-stream #交换机名称
group: queue1 #队列名称
binder: rabbit_cluster # 连接对象
binders:
rabbit_cluster:
type: rabbit
environment:
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
eureka:
client:
serviceUrl: #注册中心的注册地址
defaultZone: http://eureka-server1:8761/eureka/
instance:
prefer-ip-address: true #在eureka上显示IP地址
instance-id: stream-output #在eureka上显示的名称
启动入口:
@SpringBootApplication
@EnableEurekaClient
public class CloudStream8801Application {
public static void main(String[] args) {
SpringApplication.run(CloudStream8801Application.class,args);
}
创建一个通道:
public interface IMessageProvide {
    String OUTPUT_CHANNEL = "output_channel";
    //注解@Output表明了它是一个输出类型的通道类,名字output_channel。这一名字与app1中通道名一致,表明注入了
    //一个名字为output_channel的通道
    @Output(IMessageProvide.OUTPUT_CHANNEL)
    MessageChannel logoutput();
}
定义发送消息的方法:
@Service
@EnableBinding(IMessageProvide.class)
public class RabbitmqSender {
@Autowired
private IMessageProvide messageProvide;
// 发送消息的方法
public String sendMessage(Object message, Map<String,Object> properties){
MessageHeaders headers = new MessageHeaders(properties);
Message<Object> objectMessage = MessageBuilder.createMessage(message, headers);
boolean send = messageProvide.logoutput().send(objectMessage);
if (send)
return "完成。。";
return "00000";
}
}
测试发送消息的controller
@RequestMapping("/stream")
@RestController
public class StreamController {
    @Autowired
    private RabbitmqSender rabbitmqSender;
    @RequestMapping("/sane")
    public String saneMessage(){
        String uuid = UUID.randomUUID().toString();
        Map<String, Object> map = new HashMap<>();
        map.put("aa","11");
        map.put("bb","22");
        map.put("cc","33");
        String s = rabbitmqSender.sendMessage(uuid, map);
        return s;
    }
}
消息消费的工程:
POM:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.7.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!--spring boot热部署插件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-stream-rabbit -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
<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>
</dependencies>
和发送消息的工程没差别
application.yml
server:
port: 8802
spring:
application:
name: cloud-input
cloud:
stream:
binders:
rabbit_cluster:
type: rabbit
environment:
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
bindings:
input_channel:
destination: exchange-stream
group: queue1
binder: rabbit_cluster
consumer:
concurrency: 1 #每次从队列中获取一个消息消费
eureka:
client:
serviceUrl: #注册中心的注册地址
defaultZone: http://eureka-server1:8761/eureka/
instance:
prefer-ip-address: true #在eureka上显示IP地址
instance-id: cloud-input #在eureka上显示的名称
启动的方法:
@SpringBootApplication
@EnableEurekaClient
public class CloudStream8802Application {
public static void main(String[] args) {
SpringApplication.run(CloudStream8802Application.class,args);
}
}
监听队列的service类:
监听的通道:
public interface Imessage {
    String INPUT_CHANNEL = "input_channel";
    //注解@Input声明了它是一个输入类型的通道,名字是input_channel
    @Input(Imessage.INPUT_CHANNEL)
    SubscribableChannel loginput();
}
消费消息的类:
@EnableBinding(Imessage.class) //绑定通道
public class RabbitmqReceiver {
@Autowired
private Imessage imessage;
@StreamListener(Imessage.INPUT_CHANNEL)
public void receiver(Message message) throws Exception {
Channel channel = (Channel)message.getHeaders().get(AmqpHeaders.CHANNEL);
Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
System.err.println("Input Stream 1 接受数据:" + message);
System.err.println("消息为 :"+message.getPayload().toString());
System.err.println("消费完毕---------------");
// channel.basicAck(deliveryTag, false);
}
}
最基础的stream整合就完成了,后面开发中高级的应用后面再补充
springcloud整合stream解决项目升级的多个消息中间件的收发问题的更多相关文章
- 将arcEngine9.3和dev9.2.4开发的项目升级成arcObject10.2和dev15.1.3过程中遇到的问题和解决
		
好久没碰.net了,arcgis更是感觉都忘干净了,今天将arcEngine9.3和dev9.2.4开发的一个项目升级成arcObject10.2和dev15.1.3过程中遇到了一系问题,留个笔记,留 ...
 - 19.SpringCloud实战项目-SpringCloud整合Alibaba-Nacos配置中心
		
SpringCloud实战项目全套学习教程连载中 PassJava 学习教程 简介 PassJava-Learning项目是PassJava(佳必过)项目的学习教程.对架构.业务.技术要点进行讲解. ...
 - [转]将某个Qt4项目升级到Qt5遇到的问题
		
原文:http://hi.baidu.com/xchinux/item/9044d8ce986accbb0d0a7b87晚上花了4个小时,将以前的一个项目从Qt 4.8.4-MinGW升级到了Qt5. ...
 - office web apps 整合Java web项目
		
之前两篇文章将服务器安装好了,项目主要的就是这么讲其整合到我们的项目中,网上大部分都是asp.net的,很少有介绍Java如何整合的,经过百度,终于将其整合到了我的项目中. 首先建个servlet拦截 ...
 - 轻松把你的项目升级到PWA
		
什么是PWA PWA(Progressive Web Apps,渐进式网页应用)是Google在2015年推出的项目,致力于通过web app获得类似native app体验的网站. 优点 1.无需客 ...
 - Webpack + Vue 多页面项目升级 Webpack 4 以及打包优化
		
0. 前言 早在 2016 年我就发布过一篇关于在多页面下使用 Webpack + Vue 的配置的文章,当时也是我在做自己一个个人项目时遇到的配置问题,想到别人也可能遇到跟我同样的问题,就把配置的思 ...
 - 【Unity】近期整理Unity4.x 项目升级Unity5.0 过程中出现的各种常见问题,与大家共享。
		
近期整理Unity4.x 项目升级Unity5.0 过程中出现的各种常见问题,与大家共享. 1:Unity4.x 项目中3D模型其材质丢失,成为"白模"? 解决方式:手 ...
 - 将vue-cli 2.x的项目升级到3.x
		
尝试将vue-cli 2.x的项目升级到3.x,记录一下升级过程,和遇到的坑 1. 直接复制替换src文件夹 2. 安装项目需要的依赖 (可以将原来package.json dependencies下 ...
 - 前端项目升级到React-router5中遇到的问题解决方案以及思路
		
我胡汉三有日子没回来写写文章了,最近一直再忙着将老项目升级,所以没时间来搞文章,今天突然感觉开了挂一样,爱因斯坦附体,把之前的bug都搞定了,在这里特意把升级中遇到的问题,记录下来,算是把这个坑填上. ...
 - VS2017项目升级 error LNK2005: "public: __thiscall ATL::CTime::
		
我是将项目升级到从VS2012 升级VS2017, 报错如下 1>atlsd.lib(atltime.obj) : error LNK2005: "public: __thiscall ...
 
随机推荐
- 【FAQ】关于无法判断和区分用户与地图交互手势类型的解决办法
			
一. 问题描述 当用户通过缩放手势.平移手势.倾斜手势和旋转手势与地图交互,控制地图移动改变其可见区域时,华为地图SDK没有提供直接获取用户手势类型的API. 二. 解决方案 华为地图SDK的地图相机 ...
 - Linux编译静态库、动态库
			
一.Linux上编译静态库 # 1.编译成.o文件 gcc -c a.c b.c // 2.编译成静态库 ar -r liba.a a.o b.o // 3.链接成可执行文件 gcc main.c - ...
 - MogDB/openGauss学习笔记-获取对象DDL
			
MogDB/openGauss 学习笔记-获取对象 DDL 本文出处:https://www.modb.pro/db/399230 内置函数 omm2=# \df *def List of funct ...
 - 直播预告丨“Hello ArkUI:初识Slider组件(JS)”周三约起
			
12月1日 19:00-20:30,Hello HarmonyOS系列课程的第三节Hello ArkUI:初识Slider组件(JS)线上直播,将手把手教你熟悉最新的ArkUI,使用JS语言编写一个包 ...
 - CentOS下修改 MySQL 的密码
			
做服务器运维,修改 MySQL 的密码是经常的需要,定期修改 MySQL 密码是网站安全的一个保证.这里记录一下修改 MySQL 密码的一些命令,方便以后查看. 修改root密码 CentOS 下 M ...
 - redis 简单整理——客户端通信协议[十五]
			
前言 简单介绍一下客户端的通信协议. 正文 第 一,客户端与服务端之间的通信协议是在TCP协议之上构建的. 第二, Redis制定了RESP(REdis Serialization Protocol, ...
 - Linux systemd 定时任务
			
哈喽大家好,我是咸鱼. 说到 Linux 定时任务,大家用得最多的就是 crond 服务,但其实 systemd 也有类似的功能.我们不但可以通过 systemd 来管理服务,还能设置定时任务,那就是 ...
 - labelme转coco数据集
			
原始labelme数据目录结构如下: |-- images | |--- 1.jpg | |--- 1.json | |--- 2.jpg | |--- 2.json | |--- ....... | ...
 - CentOS 7.9编译安装Python-3.10.13
			
目录 查看CentOS版本.系统默认gcc版本.Python版本和pip版本 部署Python-3.10.13 测试 将yum中的Python版本修改为系统原来的2.7.5版本 查看CentOS版本. ...
 - 行业 SaaS 微服务稳定性保障实战
			
简介: 对于Tob企业而言,稳定性即是生命线.那么,面对商户数目暴增, C 端场景业务不断扩展呢,F6汽车科技又是如何搭建可观测体系呢?一线负责人深度解读实际演进过程! 很多研发人员在日常工作中经常回 ...