屏蔽底层消息中间件的差异,降低切换成本 , 统一消息的编程模型。

通过定义绑定器Binder 作为中间件。 实现应用程序与消息中间件的细节之间的隔离。

消息发送端:

<dependencies>
<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>
<!--基础配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
主要是这个

消息发送实现类:

@EnableBinding(Source.class)
public class MessageProviderImpl implements IMessageProvider {
@Resource
private MessageChannel messageChannel;
@Override
public void send(String message) {
Message<String> message1 = MessageBuilder.withPayload(message).build();
messageChannel.send(message1);
}
}
server:
port: 8801 spring:
application:
name: cloud-stream-provider
cloud:
stream:
binders: # 在此处配置要绑定的rabbitmq的服务信息
defaultRabbit:
type: rabbit # 消息组件类型
environment: # 设置rabbitmq的相关的环境配置
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
bindings: # 服务的整合处理
output: # 这个名字是一个通道的名称
destination: studyExchange # 表示要使用的Exchange名称定义
content-type: application/json # 设置消息类型,本次为json,文本则设置“text/plain”
binder: defaultRabbit # 设置要绑定的消息服务的具体设置 eureka:
client: # 客户端进行Eureka注册的配置
service-url:
defaultZone: http://localhost:7001/eureka
instance:
lease-renewal-interval-in-seconds: 2 # 设置心跳的时间间隔(默认是30秒)
lease-expiration-duration-in-seconds: 5 # 如果现在超过了5秒的间隔(默认是90秒)
prefer-ip-address: true # 访问的路径变为IP地址
public interface IMessageProvider {
void send(String message);
}
@RestController
@Slf4j
public class SendMessageController {
@Autowired
private IMessageProvider iMessageProvider; @GetMapping("send")
public String sendmessage(String message){
iMessageProvider.send(message);
return "success";
}
}

消息接收端:

 <dependencies>
<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>
<!--基础配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
server:
port: 8803 spring:
application:
name: cloud-stream-consumer
cloud:
stream:
binders: # 在此处配置要绑定的rabbitmq的服务信息
defaultRabbit:
type: rabbit # 消息组件类型
environment: # 设置rabbitmq的相关的环境配置
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
bindings: # 服务的整合处理
input: # 这个名字是一个通道的名称
destination: studyExchange # 表示要使用的Exchange名称定义
content-type: application/json # 设置消息类型,本次为对象json,如果是文本则设置“text/plain”
binder: defaultRabbit # 设置要绑定的消息服务的具体设置 eureka:
client: # 客户端进行Eureka注册的配置
service-url:
defaultZone: http://localhost:7001/eureka
instance:
lease-renewal-interval-in-seconds: 2 # 设置心跳的时间间隔(默认是30秒)
lease-expiration-duration-in-seconds: 5 # 如果现在超过了5秒的间隔(默认是90秒)
prefer-ip-address: true # 访问的路径变为IP地址
@Slf4j
@Component
@EnableBinding(Sink.class)
public class ReceiveMessageListener {
@StreamListener(Sink.INPUT)
public void input(Message<String> message){
String payload = message.getPayload();
log.info("消费者 接受信息------->" + payload);
}
}
@SpringBootApplication
@Slf4j
@EnableEurekaClient
public class StreamConsumerMain8803 {
public static void main(String[] args) {
SpringApplication.run( StreamConsumerMain8803.class,args);
log.info("****************StreamConsumerMain8803启动 ************ ");
}
}

Spring Cloud Stream 消息驱动的更多相关文章

  1. spring cloud 2.x版本 Spring Cloud Stream消息驱动组件基础教程(kafaka篇)

    本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server.eureka-client.eureka-ri ...

  2. Spring Cloud Stream消息驱动之RocketMQ入门(一)

    SpringCloudStream目前支持的中间件有RabbitMQ.Kafka,还有我最近在学习的RocketMQ,以下是我学习的笔记 学习Spring cloud Stream 可以先学习一下了解 ...

  3. Spring Cloud Stream消息驱动@SendTo和消息降级

    参考程序员DD大佬的文章,自己新建demo学习学习,由于需要消息回执,看到了@SendTo这个注解能够实现,下面开始学习demo,新建两个项目cloud-stream-consumer消费端 和 cl ...

  4. Spring Cloud Stream消息总线

    Springcloud 里面对于MQ的整合一个是前一篇的消息总线一个是本文介绍的消息驱动 大体要学习这么几个知识点: 课题:SpringCloud消息驱动Stream1.什么是SpringCloud消 ...

  5. 使用 Spring Cloud Stream 构建消息驱动微服务

    相关源码: spring cloud demo 微服务的目的: 松耦合 事件驱动的优势:高度解耦 Spring Cloud Stream 的几个概念 Spring Cloud Stream is a ...

  6. 第十章 消息驱动的微服务: Spring Cloud Stream

    Spring Cloud Stream 是一个用来为微服务应用构建消息驱动能力的框架. 它可以基于Spring Boot 来创建独立的. 可用于生产的 Spring 应用程序. 它通过使用 Sprin ...

  7. 消息驱动式微服务:Spring Cloud Stream & RabbitMQ

    1. 概述 在本文中,我们将向您介绍Spring Cloud Stream,这是一个用于构建消息驱动的微服务应用程序的框架,这些应用程序由一个常见的消息传递代理(如RabbitMQ.Apache Ka ...

  8. SpringCloud之Spring Cloud Stream:消息驱动

    Spring Cloud Stream 是一个构建消息驱动微服务的框架,该框架在Spring Boot的基础上整合了Spring Integrationg来连接消息代理中间件(RabbitMQ, Ka ...

  9. Spring Cloud Alibaba学习笔记(12) - 使用Spring Cloud Stream 构建消息驱动微服务

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

  10. 「 从0到1学习微服务SpringCloud 」08 构建消息驱动微服务的框架 Spring Cloud Stream

    系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现 「 从0到1学习微服务S ...

随机推荐

  1. Flaks框架(g对象,session,数据库连接池,信号,flask-script,SQLAlchemy(ORM))

    目录 一:g对象 简介 1.g对象和session的区别 2.g对象实战代码 二:flask-session(借助于第三方插件连接redis保存session ) 1.方式一: 2.方式二(flask ...

  2. 铁威马NAS如何开启二次验证提高系统安全性

    想到登录TNAS时更安全?直接开启OTP二次验证,通过 TNAS mobile生成的一次性密码登录NAS存储,简单设置,提升TOS系统访问安全性给你TNAS双重保护. 1.首先,确认你的TOS系统在5 ...

  3. 重学c#系列——linq(3) [二十九]

    前言 继续介绍一些复杂的linq. 正文 groupjoin 这个函数: 有department public class Deployment { public string Id { get; s ...

  4. 电脑无法自动获取ip地址

    1.按下win+r,输入cmd,打开命令提示符;2.执行ipconfig命令看下能否获取到ip地址:3.若不能,执行ipconfig /renew命令重新获取ip:4.执行ipconfig命令看下能否 ...

  5. Spark框架下均值漂移算法对舆情聚类的分析

    知网链接 原文链接 张京坤,  王怡怡 软件导刊   2022年21卷第6期 页码:141-146 DOI:10.11907/rjdk.211889    中图分类号:TP274 纸质出版日期:202 ...

  6. 微软拼音长句模式恢复工具支持Win10 1803

    4月份就有人留言旧微软拼音恢复工具不支持Win10 1803了,我自己也遇到了,但因为没时间搞,勉为其难使用了词组模式的微软拼音几个月,终于在八月份抽个空研究了下,解决了. 这次是因为傻逼大微软改了 ...

  7. [C++标准模板库:自修教程与参考手册]关于deque

    Deque的基本概念 deque和vector很相似,也提供随机存取的能力,将元素都动态地存储在一个数组中,最大的区别就是:deque的头和尾都是开放的,非常方便在头尾两端进行一些操作.(重点在于:d ...

  8. flutter 1.升级2.X在模型类中序列化JSON报错Non-nullable instance field 'title' must be initialized.

    flutter 1.升级2.X在模型类中序列化JSON报错 Non-nullable instance field 'title' must be initialized. Try adding an ...

  9. Vue3+TypeScript 项目中,配置 ESLint 和 Prettier

    接上篇:从0搭建vite-vue3-ts项目框架:配置less+svg+pinia+vant+axios 文档同步项目gitee:https://gitee.com/lixin_ajax/vue3-v ...

  10. 标准if-else语句-扩展if-else语句

    标准if-else语句 if语句第二种格式: if...else if(关系表达式) { 语句体1; }else { 语句体2; } 执行流程 首先判断关系表达式看其结果是true还是false 如果 ...