SpringCloud Stream
1.介绍
官网:https://www.springcloud.cc/spring-cloud-dalston.html#_spring_cloud_stream
1.1定义
是一个构建消息驱动微服务的框架,主要是屏蔽底层消息中间件的差异,降低切换成本,统一消息的编程模型。目前只支持RabbitMQ和Kafka。
1.2设计思想
通过定义绑定器(Binder)作为中间层,实现了应用程序与消息中间件细节之间的隔离。inout对应消费者,output对应生产者。通信方式采用了发布-订阅的模式。
2.项目开发
源代码:https://github.com/zhongyushi-git/cloud-stream.git
2.1环境搭建
1)创建一个SpringBoot的项目,导入相关的依赖。详见源码
2)添加一个服务注册中心eureka。详见源码
3)需要依赖RabbitMQ,因此需要先安装它。
2.2消息驱动之生产者8801
1)创建模块stream-rabbitmq-provider8801,导入依赖
<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.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
2)创建启动类
3)yml配置
server:
port: 8801 spring:
application:
name: cloud-stream-provider
cloud:
stream:
# 在此处配置要绑定的rabbitMQ的服务信息
binders:
# 表示定义的名称,用于binding的整合
defaultRabbit:
# 消息中间件类型
type: rabbit
# 设置rabbitMQ的相关环境配置
environment:
spring:
rabbitmq:
host: 192.168.51.5
port: 5672
username: guest
password: guest
# 服务的整合处理
bindings:
# 这个名字是一个通道的名称
output:
# 表示要使用的exchange名称定义
destination: studyExchange
# 设置消息类型,本次为json,文本则设为text/plain
content-type: application/json
# 设置要绑定的消息服务的具体设置
binder: defaultRabbit eureka:
client:
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: send-8001.com # 主机名
prefer-ip-address: true # 显示ip
4)创建发送消息接口
package com.zys.cloud.service;
public interface IMessageProvider {
/**
* 消息发送
* @return
*/
String send();
}
5)创建发送接口实现类
package com.zys.cloud.service.impl;
import com.zys.cloud.service.IMessageProvider;
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;
import javax.annotation.Resource;
import java.util.UUID; //这不是传统的service,这是和rabbitmq打交道的,不需要加注解@Service
//信道channel和exchange绑定在一起
@EnableBinding(Source.class)
public class MessageProviderImpl implements IMessageProvider { /**
* 消息发送管道
*/
@Resource
private MessageChannel output; @Override
public String send() {
String serial = UUID.randomUUID().toString();
output.send(MessageBuilder.withPayload(serial).build());
System.out.println("serial = " + serial);
return null;
}
}
6)创建controller
package com.zys.cloud.controller;
import com.zys.cloud.service.IMessageProvider;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; @RestController
public class SendMessageController { @Resource
private IMessageProvider messageProvider; @GetMapping("/sendMessage")
public String sendMessage(){
return messageProvider.send();
}
}
7)测试
先启动7001,然后是RabbitMQ,再启动8801,访问http://localhost:8801/sendMessage可以在控制台看到输出的内容。
2.3消息驱动之消费者8803
1)创建模块stream-rabbitmq-,导入依赖
<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.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
2)创建启动类
3)yml配置
server:
port: 8803 spring:
application:
name: cloud-stream-consumer
cloud:
stream:
binders: # 在此处配置要绑定的rabbitMQ的服务信息
defaultRabbit: # 表示定义的名称,用于binding的整合
type: rabbit # 消息中间件类型
environment: # 设置rabbitMQ的相关环境配置
spring:
rabbitmq:
host: 192.168.51.5
port: 5672
username: guest
password: guest
bindings: # 服务的整合处理
input: # 这个名字是一个通道的名称
destination: studyExchange # 表示要使用的exchange名称定义
content-type: application/json # 设置消息类型,本次为json,文本则设为text/plain
binder: defaultRabbit # 设置要绑定的消息服务的具体设置 eureka:
client:
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)创建controller
package com.zys.cloud.controller;
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;
import org.springframework.web.bind.annotation.RestController; @Component
@EnableBinding(Sink.class)
public class ReceiveMessageListenerController {
@Value("${server.port}")
private String serverPort; @StreamListener(Sink.INPUT)
public void input(Message<String> message){
System.out.println("消费者1号,------->接收到的消息: "+message.getPayload()+"\t port: "+serverPort);
}
}
5)测试
先启动7001,然后是RabbitMQ,再启动8801,最后启动8803,访问http://localhost:8801/sendMessage可以在控制台看到8801,8803输出的内容
按照8803再创建8804模块,配置同上。
2.4服务重复消费
1)在8804和8804启动后,发现他们都在消费消息,但是一般消息在被消费后就不能再被消费了。
解决办法很简单,只需要配置相同分组即可。
SpringCloud Stream的更多相关文章
- 每天学点SpringCloud(十四):Zipkin使用SpringCloud Stream以及Elasticsearch
在前面的文章中,我们已经成功的使用Zipkin收集了项目的调用链日志.但是呢,由于我们收集链路信息时采用的是http请求方式收集的,而且链路信息没有进行保存,ZipkinServer一旦重启后就会所有 ...
- Rabbitmq基本使用 SpringBoot整合Rabbit SpringCloud Stream+Rabbit
https://blog.csdn.net/m0_37867405/article/details/80793601 四.docker中使用rabbitmq 1. 搭建和启动 使用地址:rabbitm ...
- 《springcloud 五》springcloud stream
什么是消息驱动? SpringCloud Stream消息驱动可以简化开发人员对消息中间件的使用复杂度,让系统开发人员更多尽力专注与核心业务逻辑的开发.SpringCloud Stream基于Spri ...
- SpringCloud学习笔记(九、SpringCloud Stream)
目录: 什么是SpringCloud Stream 如何使用SpringCloud Stream 消息分流 什么是SpringCloud Stream: SpringCloud Stream是一个用于 ...
- SpringCloud Stream使用案例
官方定义 Spring Cloud Stream 是一个构建消息驱动微服务的框架. 应用程序通过 inputs 或者 outputs 来与 Spring Cloud Stream 中binder 交互 ...
- SpringCloud Stream 消息驱动
1.什么是消息驱动 SpringCloud Stream消息驱动可以简化开发人员对消息中间件的使用复杂度,让系统开发人员更多尽力专注与核心业务逻辑的开发.SpringCloud Stream基于Spr ...
- SpringCloud系列之SpringCloud Stream
SpringCloud Stream SpringCloud Config SpringCloud Gatewa SpringCloud Hystrix SpringCloud 第一部分 文章目录 S ...
- 九. SpringCloud Stream消息驱动
1. 消息驱动概述 1.1 是什么 在实际应用中有很多消息中间件,比如现在企业里常用的有ActiveMQ.RabbitMQ.RocketMQ.Kafka等,学习所有这些消息中间件无疑需要大量时间经历成 ...
- 使用SpringCloud Stream结合rabbitMQ实现消息消费失败重发机制
前言:实际项目中经常遇到消息消费失败了,要进行消息的重发.比如支付消息消费失败后,要分不同时间段进行N次的消息重发提醒. 本文模拟场景 当金额少于100时,消息消费成功 当金额大于100,小于200时 ...
随机推荐
- Educational Codeforces Round 67 E.Tree Painting (树形dp)
题目链接 题意:给你一棵无根树,每次你可以选择一个点从白点变成黑点(除第一个点外别的点都要和黑点相邻),变成黑点后可以获得一个权值(白点组成连通块的大小) 问怎么使权值最大 思路:首先,一但根确定了, ...
- A. Vitaly and Strings
Vitaly is a diligent student who never missed a lesson in his five years of studying in the universi ...
- codeforces 580D. Kefa and Dishes
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【Azure 微服务】基于已经存在的虚拟网络(VNET)及子网创建新的Service Fabric并且为所有节点配置自定义DNS服务
问题描述 创建新的Service Fabric集群,可以通过门户,Powershell命令,或者是ARM模板.但是通过门户和PowerShell命令时,创建的SF集群都会自动新建一个虚拟网络而无法使用 ...
- OpenStack Train版-4.安装placement放置服务
安装placement放置服务 创建placement数据库 mysql -uroot CREATE DATABASE placement; GRANT ALL PRIVILEGES ON place ...
- CPU饥饿与线程饥饿
线程饥饿: 进程无法得到资源,(cpu或者io资源或者别的什么资源),所以无法进行下去 比如说读者写者问题,如果读者优先,那么写者可能会饿死. 又比如操作系统概念的一道习题. 用broadcast可能 ...
- C语言中函数的调用方式
第一眼看到这样一个题目的我,心想除了如下的直接调用还能怎么调用呢? 1 void fun(void) 2 { 3 ...... 4 //你的代码 5 ..... 6 } 7 int main(void ...
- 【rocketmq学习笔记】rocketmq入门学习
基本介绍 rocketmq是阿里巴巴团队使用java语言开发的一款基于发布订阅模型的分布式消息队列中间件,是一款低延迟,高可用,拥有海量消息堆积能力和灵活拓展性的消息队列. 特点 可以实现集群无单点故 ...
- macOS & Catalina vs Big Sur
macOS & Catalina vs Big Sur 乍一看,macOS的色彩更加丰富,最大的变化就是明亮,略带卡通风格的iOS形状的图标. 一切都变得更加圆润,感觉一切都变得更大了. 这可 ...
- image auto downloader
image auto downloader icons killer / js crawler http only + same-origin OK "use strict"; / ...