目录:

  • 什么是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. (办公)mysql索引

    举个例子:20多w的数据,查询语句,什么都没有查到,既没有走到主键索引,普通索引,什么都没走,走的就非常慢. 下面要加索引,并了解mysql索引的作用,以及如何使用他们索引. 介绍MysqlMySQL ...

  2. linux环境搭配

    1.linuxLinux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和Unix的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的Unix工具软件.应用程序和网络协议 ...

  3. element-ui中的hover 光标移入某一个具体的td 有hover效果

    <template> <div> <el-table :data="tableData" style="width: 100%"& ...

  4. WPF 精修篇 用户控件

    原文:WPF 精修篇 用户控件 增加用户控件 数据绑定还是用依赖属性 使用的事件 就委托注册一下 public delegate void ButtonClick(object b,EventArgs ...

  5. Django restful framework中自动生成API文档

    自动生成api文档(不管是函数视图还是类视图都能显示) 1.安装rest_framework_swagger库 pip install django-rest-swagger 2.在项目下的 urls ...

  6. Codeforces Round #603 (Div. 2) D. Secret Passwords 并查集

    D. Secret Passwords One unknown hacker wants to get the admin's password of AtForces testing system, ...

  7. php获取url中的参数

    // 获取url参数值function is_set_param($param){ $current_url = $_SERVER["QUERY_STRING"]; $arr = ...

  8. idea配置pyspark

    默认python已经配好,并已经导入idea,只剩下pyspark的安装 1.解压spark-2.1.0-bin-hadoop2.7放入磁盘目录 D:\spark-2.1.0-bin-hadoop2. ...

  9. QOS限速

    XX涉及的QOS限速主要有两种: 第一种是针对一个端口下双向IP互访: 第二种是针对多个端口下双向IP互访:(聚合car) 聚合car:是指能够对多个业务使用同一个car进行流量监控,即如果多个端口应 ...

  10. 定位表和索引使用的Page

    数据存储的基本单元是Page,每个Page是8KB,数据文件(mdf和ndf)占用的硬盘空间,逻辑上按照PageNumber进行划分,也就是说,可以把数据文件看作是PageNumber 从0到n的连续 ...