目录:

  • 什么是SpringCloud Stream
  • 如何使用SpringCloud Stream
  • 消息分流

什么是SpringCloud Stream:

SpringCloud Stream是一个用于构建消息驱动的微服务应用框架。它通过注入,输入、输出通道来与外界通信;因此它很容易实现消息的中转,并且在更换消息中间件的时候不需要该代码,仅需要修改配置即可。支持的消息中间件如RabbitMQ、Kafka等等。

如何使用SpringCloud Stream(以RabbitMQ为例):

1、增加maven依赖

 <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-test-support</artifactId>
<scope>test</scope>
</dependency>

2、增加properties配置

 spring.application.name=stream
server.port=7070 # rabbitmq
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest # stream
spring.cloud.stream.bindings.input.destination=customer
spring.cloud.stream.bindings.output.destination=customer

3、启动类加上本工程的消息代理类型

@EnableBinding({Processor.class})

@EnableBinding分为三种类型

)org.springframework.cloud.stream.messaging.Processor:接收和发送消息

)org.springframework.cloud.stream.messaging.Source:仅支持发送消息

)org.springframework.cloud.stream.messaging.Sink:仅支持接收消息

4、加上Controller及Service

 @RestController
@AllArgsConstructor
public class ProcessorController { private final ProcessorService processorService; @GetMapping("/testProcessor/{message}")
public boolean testProcessor(@PathVariable("message") String message) {
return processorService.send(message);
}
}
 @Service
@AllArgsConstructor
public class ProcessorService { private final Processor processor; public boolean send(String message) {
return processor.output().send(MessageBuilder.withPayload(message).build());
} public boolean subscribe(MessageHandler handler) {
return processor.input().subscribe(handler);
}
}

5、在任意bean下写上接收逻辑或另起一个工程(另一个工程的mq需要配成一个哦)

 @StreamListener(Sink.INPUT)
public void receive(String message) {
System.err.println("receive message: " + message);
}

然后我们启动项目,访问http://localhost:7070/testProcessor/hello,此时就会在控制台看到receive message: hello的字样。

消息分流(kafka特性):

 @GetMapping("/testMessageShunt/{type}")
public boolean testMessageShunt(@PathVariable("type") String type) {
String header = "a".equalsIgnoreCase(type) ? "msg1" : "msg2";
return processorService.send(type, header);
}
 /**
* RabbitMQ不支持消息分流
*/
@StreamListener(value = Sink.INPUT, condition = "headers['contentType']=='mgs1'")
public void receiveMessage1(@Payload Message<String> message) {
System.err.println("receive message1: " + message.getPayload());
} /**
* RabbitMQ不支持消息分流
*/
@StreamListener(value = Sink.INPUT, condition = "headers['contentType']=='mgs2'")
public void receiveMessage2(@Payload Message<String> message) {
System.err.println("receive message2: " + message.getPayload());
}

SpringCloud学习笔记(九、SpringCloud Stream)的更多相关文章

  1. SpringCloud学习笔记:SpringCloud简介(1)

    1. 微服务 微服务具有的特点: ◊ 按照业务划分服务 ◊ 每个微服务都有独立的基础组件,如:数据库.缓存等,且运行在独立的进程中: ◊ 微服务之间的通讯通过HTTP协议或者消息组件,具有容错能力: ...

  2. SpringCloud学习笔记(5):Hystrix Dashboard可视化监控数据

    简介 上篇文章中讲了使用Hystrix实现容错,除此之外,Hystrix还提供了近乎实时的监控.本文将介绍如何进行服务监控以及使用Hystrix Dashboard来让监控数据图形化. 项目介绍 sc ...

  3. SpringCloud学习笔记(2):使用Ribbon负载均衡

    简介 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡工具,在注册中心对Ribbon客户端进行注册后,Ribbon可以基于某种负载均衡算法,如轮询(默认 ...

  4. SpringCloud学习笔记(3):使用Feign实现声明式服务调用

    简介 Feign是一个声明式的Web Service客户端,它简化了Web服务客户端的编写操作,相对于Ribbon+RestTemplate的方式,开发者只需通过简单的接口和注解来调用HTTP API ...

  5. SpringCloud学习笔记(4):Hystrix容错机制

    简介 在微服务架构中,微服务之间的依赖关系错综复杂,难免的某些服务会出现故障,导致服务调用方出现远程调度的线程阻塞.在高负载的场景下,如果不做任何处理,可能会引起级联故障,导致服务调用方的资源耗尽甚至 ...

  6. SpringCloud学习笔记(6):使用Zuul构建服务网关

    简介 Zuul是Netflix提供的一个开源的API网关服务器,SpringCloud对Zuul进行了整合和增强.服务网关Zuul聚合了所有微服务接口,并统一对外暴露,外部客户端只需与服务网关交互即可 ...

  7. SpringCloud学习笔记(7):使用Spring Cloud Config配置中心

    简介 Spring Cloud Config为分布式系统中的外部化配置提供了服务器端和客户端支持,服务器端统一管理所有配置文件,客户端在启动时从服务端获取配置信息.服务器端有多种配置方式,如将配置文件 ...

  8. SpringCloud学习笔记:服务支撑组件

    SpringCloud学习笔记:服务支撑组件 服务支撑组件 在微服务的演进过程中,为了最大化利用微服务的优势,保障系统的高可用性,需要通过一些服务支撑组件来协助服务间有效的协作.各个服务支撑组件的原理 ...

  9. 多线程学习笔记九之ThreadLocal

    目录 多线程学习笔记九之ThreadLocal 简介 类结构 源码分析 ThreadLocalMap set(T value) get() remove() 为什么ThreadLocalMap的键是W ...

  10. MDX导航结构层次:《Microsoft SQL Server 2008 MDX Step by Step》学习笔记九

    <Microsoft SQL Server 2008 MDX Step by Step>学习笔记九:导航结构层次   SQL Server 2008中SQL应用系列及BI笔记系列--目录索 ...

随机推荐

  1. SSH agent 的使用 - 资料摘录

    下面是一些ssh agent的资料简要摘录,网路上的相关的文章已经很多了: ssh 推荐的登录方式是使用私钥登录.但是如果生成私钥的时候,设置了口令(passphrase),每次登录时需要输入口令也很 ...

  2. Scrapy中的Request和日志分析

    Scrapy.http.Request 自动去重,根据url的哈希值,进行去重 属性 meta(dict)  在不同的请求之间传递数据,dict priority(int)  此请求的优先级(默认为0 ...

  3. You Are Given a Decimal String... CodeForces - 1202B [简单dp][补题]

    补一下codeforces前天教育场的题.当时只A了一道题. 大致题意: 定义一个x - y - counter :是一个加法计数器.初始值为0,之后可以任意选择+x或者+y而我们由每次累加结果的最后 ...

  4. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) D2. Optimal Subsequences (Hard Version) 数据结构 贪心

    D2. Optimal Subsequences (Hard Version) This is the harder version of the problem. In this version, ...

  5. 终端中的 zsh 和 bash-魔法切换

    常用ubuntu,这两个终端都装了,平时使用zsh比较方便,可是,有时候出现了问题,不知道是谁的问题时候,还要做一下切换操作的,怎么才能迅速切换呢? 要切换,首先要知道你现在使用的是什么,请看第一个命 ...

  6. 感受一把面试官通过一道题目引出的关于 TCP 的 5 个连环炮!

    面试现场:从 URL 在浏览器被被输入到页面展现的过程中发生了什么? 相信大多数准备过的同学都能回答出来,但是如果继续问:收到的 HTML 如果包含几十个图片标签,这些图片是以什么方式什么顺序下载?建 ...

  7. js常用但是不容易记住的代码

    <!-- iframe 自适应高度度 --><iframe src="__CONTROLLER__/showlist" frameborder="0&q ...

  8. php使用inotify扩展监控文件或目录的变化

    一.安装inotify扩展 1.下载inotify扩展源码 https://pecl.php.net/package/inotify 对于php7以上版本,请下载 inotify-2.0.0.tgz. ...

  9. .net core下使用DbProviderFactories.GetFactory("")无法创建工厂的解决方案

    前言:我们有时候会有一种需求,需要连接很多的数据库,如:mysql,sqlserver,oracle等等,需要把这些数据库里的数据抽取出来加工后,返回给客户端使用. 在.net framework中是 ...

  10. wpf datagrid field to image converter 字段 图片转化器

    <DataGridTemplateColumn Header="Binding"> <DataGridTemplateColumn.CellTemplate> ...