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的更多相关文章

  1. 每天学点SpringCloud(十四):Zipkin使用SpringCloud Stream以及Elasticsearch

    在前面的文章中,我们已经成功的使用Zipkin收集了项目的调用链日志.但是呢,由于我们收集链路信息时采用的是http请求方式收集的,而且链路信息没有进行保存,ZipkinServer一旦重启后就会所有 ...

  2. Rabbitmq基本使用 SpringBoot整合Rabbit SpringCloud Stream+Rabbit

    https://blog.csdn.net/m0_37867405/article/details/80793601 四.docker中使用rabbitmq 1. 搭建和启动 使用地址:rabbitm ...

  3. 《springcloud 五》springcloud stream

    什么是消息驱动? SpringCloud Stream消息驱动可以简化开发人员对消息中间件的使用复杂度,让系统开发人员更多尽力专注与核心业务逻辑的开发.SpringCloud Stream基于Spri ...

  4. SpringCloud学习笔记(九、SpringCloud Stream)

    目录: 什么是SpringCloud Stream 如何使用SpringCloud Stream 消息分流 什么是SpringCloud Stream: SpringCloud Stream是一个用于 ...

  5. SpringCloud Stream使用案例

    官方定义 Spring Cloud Stream 是一个构建消息驱动微服务的框架. 应用程序通过 inputs 或者 outputs 来与 Spring Cloud Stream 中binder 交互 ...

  6. SpringCloud Stream 消息驱动

    1.什么是消息驱动 SpringCloud Stream消息驱动可以简化开发人员对消息中间件的使用复杂度,让系统开发人员更多尽力专注与核心业务逻辑的开发.SpringCloud Stream基于Spr ...

  7. SpringCloud系列之SpringCloud Stream

    SpringCloud Stream SpringCloud Config SpringCloud Gatewa SpringCloud Hystrix SpringCloud 第一部分 文章目录 S ...

  8. 九. SpringCloud Stream消息驱动

    1. 消息驱动概述 1.1 是什么 在实际应用中有很多消息中间件,比如现在企业里常用的有ActiveMQ.RabbitMQ.RocketMQ.Kafka等,学习所有这些消息中间件无疑需要大量时间经历成 ...

  9. 使用SpringCloud Stream结合rabbitMQ实现消息消费失败重发机制

    前言:实际项目中经常遇到消息消费失败了,要进行消息的重发.比如支付消息消费失败后,要分不同时间段进行N次的消息重发提醒. 本文模拟场景 当金额少于100时,消息消费成功 当金额大于100,小于200时 ...

随机推荐

  1. Codeforces Round #651 (Div. 2) D. Odd-Even Subsequence(二分)

    题目链接:https://codeforces.com/contest/1370/problem/D 题意 给出一个含有 $n$ 个数的数组 $a$,从中选出 $k$ 个数组成子序列 $s$,使得 $ ...

  2. 【noi 2.6_9283】&【poj 3088】Push Botton Lock(DP--排列组合 Stirling数)

    题意:N个编号为1~N的数,选任意个数分入任意个盒子内(盒子互不相同)的不同排列组合数. 解法:综合排列组合 Stirling(斯特林)数的知识进行DP.C[i][j]表示组合,从i个数中选j个数的方 ...

  3. 【noi 2.6_2421】Exchange Rates(DP)

    题意:起始有1000元美元,给出N天美元与加拿大元的汇率.问N天内可以不停的兑换,每次兑换需要收取3%的手续费,问可以得到的最大的美元数. 解法:直接用2个变量存第 i 天时手中是美元和加拿大元的最大 ...

  4. 【noi 2.6_666】放苹果 & 【noi 2.6_8467】鸣人的影分身(DP)

    这题其实在2.6前面的专题也有出现过,我还以为我有写,结果发现,并没有.于是就现在写了.这2题其实重复了......我就按放苹果的来说. 题意:把N个苹果放在M个盘子里,允许有的盘子空着不放,问共有多 ...

  5. Eureka详解系列(五)--Eureka Server部分的源码和配置

    简介 按照原定的计划,我将分三个部分来分析 Eureka 的源码: Eureka 的配置体系(已经写完,见Eureka详解系列(三)--探索Eureka强大的配置体系): Eureka Client ...

  6. k8s二进制部署 - coredns安装

    coredns的资源清单文件rabc.yaml apiVersion: v1 kind: ServiceAccount metadata: name: coredns namespace: kube- ...

  7. ELK Stack 介绍 & Logstash 日志收集

    ELK Stack 组成 Software Description Function E:Elasticsearch Java 程序 存储,查询日志 L:Logstash Java 程序 收集.过滤日 ...

  8. 缓冲区溢出实验 5 Snprintf

    实验环境.代码.及准备 https://www.cnblogs.com/lqerio/p/12870834.html vul5 Snprintf函数,百度百科: 将可变个参数(...)按照format ...

  9. codeforces 1009D Relatively Prime Graph【欧拉函数】

    题目:戳这里 题意:要求构成有n个点,m条边的无向图,满足每条边上的两点互质. 解题思路: 显然1~n这n个点能构成边的条数,就是2~n欧拉函数之和(x的欧拉函数值代表小于x且与x互质的数的个数. 因 ...

  10. Java 对象的哈希值是每次 hashCode() 方法调用重计算么?

    对于没有覆盖hashCode()方法的对象 如果没有覆盖 hashCode() 方法,那么哈希值为底层 JDK C++ 源码实现,实例每次调用hashcode()方法,只有第一次计算哈希值,之后哈希值 ...