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解决项目升级的多个消息中间件的收发问题的更多相关文章

  1. 将arcEngine9.3和dev9.2.4开发的项目升级成arcObject10.2和dev15.1.3过程中遇到的问题和解决

    好久没碰.net了,arcgis更是感觉都忘干净了,今天将arcEngine9.3和dev9.2.4开发的一个项目升级成arcObject10.2和dev15.1.3过程中遇到了一系问题,留个笔记,留 ...

  2. 19.SpringCloud实战项目-SpringCloud整合Alibaba-Nacos配置中心

    SpringCloud实战项目全套学习教程连载中 PassJava 学习教程 简介 PassJava-Learning项目是PassJava(佳必过)项目的学习教程.对架构.业务.技术要点进行讲解. ...

  3. [转]将某个Qt4项目升级到Qt5遇到的问题

    原文:http://hi.baidu.com/xchinux/item/9044d8ce986accbb0d0a7b87晚上花了4个小时,将以前的一个项目从Qt 4.8.4-MinGW升级到了Qt5. ...

  4. office web apps 整合Java web项目

    之前两篇文章将服务器安装好了,项目主要的就是这么讲其整合到我们的项目中,网上大部分都是asp.net的,很少有介绍Java如何整合的,经过百度,终于将其整合到了我的项目中. 首先建个servlet拦截 ...

  5. 轻松把你的项目升级到PWA

    什么是PWA PWA(Progressive Web Apps,渐进式网页应用)是Google在2015年推出的项目,致力于通过web app获得类似native app体验的网站. 优点 1.无需客 ...

  6. Webpack + Vue 多页面项目升级 Webpack 4 以及打包优化

    0. 前言 早在 2016 年我就发布过一篇关于在多页面下使用 Webpack + Vue 的配置的文章,当时也是我在做自己一个个人项目时遇到的配置问题,想到别人也可能遇到跟我同样的问题,就把配置的思 ...

  7. 【Unity】近期整理Unity4.x 项目升级Unity5.0 过程中出现的各种常见问题,与大家共享。

    近期整理Unity4.x 项目升级Unity5.0 过程中出现的各种常见问题,与大家共享. 1:Unity4.x 项目中3D模型其材质丢失,成为"白模"?       解决方式:手 ...

  8. 将vue-cli 2.x的项目升级到3.x

    尝试将vue-cli 2.x的项目升级到3.x,记录一下升级过程,和遇到的坑 1. 直接复制替换src文件夹 2. 安装项目需要的依赖 (可以将原来package.json dependencies下 ...

  9. 前端项目升级到React-router5中遇到的问题解决方案以及思路

    我胡汉三有日子没回来写写文章了,最近一直再忙着将老项目升级,所以没时间来搞文章,今天突然感觉开了挂一样,爱因斯坦附体,把之前的bug都搞定了,在这里特意把升级中遇到的问题,记录下来,算是把这个坑填上. ...

  10. VS2017项目升级 error LNK2005: "public: __thiscall ATL::CTime::

    我是将项目升级到从VS2012 升级VS2017, 报错如下 1>atlsd.lib(atltime.obj) : error LNK2005: "public: __thiscall ...

随机推荐

  1. 基于EtherNet/IP实现欧姆龙NX系列PLC通信

    1.引言 工业以太网协议 (Ethernet/IP) 是由ODVA所开发并得到了罗克韦尔自动化的强大支持.它使用已用于ControlNet和DeviceNet的控制和信息协议 (CIP) 为应用层协议 ...

  2. Linux Ubuntu配置国内源

    配置国内源 因为众所周知的原因,国外的很多网站在国内是访问不了或者访问极慢的,这其中就包括了Ubuntu的官方源. 所以,想要流畅的使用apt安装应用,就需要配置国内源的镜像. 市面上Ubuntu的国 ...

  3. 家庭实验室系列文章-如何迁移树莓派系统到更大的 SD 卡?

    前言 其实这个专题很久很久之前就想写了,但是一直因为各种原因拖着没动笔. 因为没有资格,也没有钱在一线城市买房 (); 但是在要结婚之前,婚房又是刚需. 我和太太最终一起在一线城市周边的某二线城市买了 ...

  4. 聊聊 Redis Stream

    Redis Stream 是 Redis 5.0 版本中引入的一种新的数据结构,它用于实现简单但功能强大的消息传递模式. 这篇文章,我们聊聊 Redis Stream 基本用法 ,以及如何在 Spri ...

  5. 升级Django项目过程中问题记录

    升级内容: python版本:3.8.4升到3.10.7 Django版本:2.2.13升到4.2 所遇问题: 1. error in anyjson setup command: use_2to3 ...

  6. NodeJS安装cnpm

    介绍: NPM(Node Package Manager):Node的包管理器. CNPM(Chinese CPM):中国的NPM(国内使用,网速较快). 配置步骤 用npm安装cnpm npm in ...

  7. 介绍一个气缸控制的FB程序块

    关键词: 气缸,双控.单控.电磁阀.感应器.初始位置(简称"始位").末端位置(简称"端位").屏蔽功能.延时功能.报警功能 正文: 1.为什么要做气缸FB功能 ...

  8. 浏览器端实现类似input限制输入两位小数,输入时光标从输入位置移动到最后

    1.问题描述展示 示例代码所做限制为不允许输入字母d,其他限制规则可以根据需求自己调整,使用React编写,其他框架或原生均可根据该代码理解原理进行转变,特意使用了中文键盘可以看到输入框下面白色框闪出 ...

  9. 【笔记】go语言--go语言的依赖管理

    [笔记]go语言--go语言的依赖管理 GO语言的依赖管理 依赖的概念,依赖就是第三方的库,即别人已经做好的库 依赖管理的三个阶段 GOPATH,GOVENDOR, go mod 三个阶段 - GOP ...

  10. What is an HL7 ADT Message?

    Patient Admission Discharge and Transfer (ADT) messages are used to exchange the patient state withi ...