SpringCloud Stream使用解析
SpringCloudStream
官方定義Spring Cloud Stream 是一個構(gòu)建消息驅(qū)動微服務(wù)的框架。
應(yīng)用通過inputs和outputs來與Spring Cloud Stream中binder對象交互。通過我們配置來binding(綁定),而Spring Cloud Stream中的binder對象負(fù)責(zé)與消息中間件交互。所以,我們只需要搞清楚如何與Spring Cloud Stream 交互就可以方便使用消息驅(qū)動的方式。
通過使用Spring Integration來連接消息代理中間件以及實現(xiàn)消息事件驅(qū)動。
目前僅支持RabbitMQ和kafka
下面用RabbitMQ來說明使用!
案例之消息驅(qū)動之生產(chǎn)者
1.建一個項目,并添加如下的依賴:
<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.編寫下面的yml文件
server:
port: 8801
spring:
application:
name: cloud-stream-provider
rabbitmq:
host: 192.168.43.76
port: 5672
username: guest
password: guest
cloud:
stream:
binders: # 在此處配置要綁定的rabbitmq的服務(wù)信息;
defaultRabbit: # 表示定義的名稱,用于于binding整合
type: rabbit # 消息組件類型
# environment: # 設(shè)置rabbitmq的相關(guān)的環(huán)境配置,(本機(jī)方式)
# spring:
# rabbitmq:
# host: localhost
# port: 5672
# username: guest
# password: guest
bindings: # 服務(wù)的整合處理
output: # 這個名字是一個通道的名稱
destination: studyExchange # 表示要使用的Exchange名稱定義
content-type: application/json # 設(shè)置消息類型,本次為json,文本則設(shè)置“text/plain”
binder: defaultRabbit # 設(shè)置要綁定的消息服務(wù)的具體設(shè)置(爆紅不要管)
eureka:
client: # 客戶端進(jìn)行Eureka注冊的配置
service-url:
defaultZone: http://localhost:7001/eureka
instance:
lease-renewal-interval-in-seconds: 2 # 設(shè)置心跳的時間間隔(默認(rèn)是30秒)
lease-expiration-duration-in-seconds: 5 # 如果現(xiàn)在超過了5秒的間隔(默認(rèn)是90秒)
instance-id: send-8801.com # 在信息列表時顯示主機(jī)名稱
prefer-ip-address: true # 訪問的路徑變?yōu)镮P地址
3.編寫service,下面僅展示實現(xiàn)類:
import org.lzl.springcloud.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,因為該service是跟rabbitmq打交道的
@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;
}
}
4.編寫controller
import org.lzl.springcloud.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(value = "/sendMessage")
public String sendMessage(){
return messageProvider.send();
}
}
案例之消息驅(qū)動消費者
1.寫pom,加上下面的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-starter-actuator</artifactId>
</dependency>
2.編寫yml,和生產(chǎn)者唯一的區(qū)別在于,下面是input
server:
port: 8802
spring:
application:
name: cloud-stream-consumer
rabbitmq:
host: 192.168.43.76
port: 5672
username: guest
password: guest
cloud:
stream:
binders: # 在此處配置要綁定的rabbitmq的服務(wù)信息;
defaultRabbit: # 表示定義的名稱,用于于binding整合
type: rabbit # 消息組件類型
# environment: # 設(shè)置rabbitmq的相關(guān)的環(huán)境配置
# spring:
# rabbitmq:
# host: localhost
# port: 5672
# username: guest
# password: guest
bindings: # 服務(wù)的整合處理
input: # 這個名字是一個通道的名稱
destination: studyExchange # 表示要使用的Exchange名稱定義
content-type: application/json # 設(shè)置消息類型,本次為對象json,如果是文本則設(shè)置“text/plain”
binder: defaultRabbit # 設(shè)置要綁定的消息服務(wù)的具體設(shè)置
eureka:
client: # 客戶端進(jìn)行Eureka注冊的配置
service-url:
defaultZone: http://localhost:7001/eureka
instance:
lease-renewal-interval-in-seconds: 2 # 設(shè)置心跳的時間間隔(默認(rèn)是30秒)
lease-expiration-duration-in-seconds: 5 # 如果現(xiàn)在超過了5秒的間隔(默認(rèn)是90秒)
instance-id: receive-8802.com # 在信息列表時顯示主機(jī)名稱
prefer-ip-address: true # 訪問的路徑變?yōu)镮P地址
3.編寫controller,該controller不向外界暴露端口,起到實時監(jiān)控消息管道的作用!
import lombok.extern.slf4j.Slf4j;
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;
import javax.annotation.Resource;
@Component
@EnableBinding(Sink.class)
public class ReceiveMessageListenerController {
@Value("${server.port}")
private String serverPort;
@StreamListener(Sink.INPUT)//只要8801發(fā)送消息,8802就會接收到8801的消息
public void input(Message<String> message){
System.out.println("消費者1號--------》接收到的消息:"+message.getPayload()+"\t port: "+serverPort);
}
}
測試
啟動rabbitMQ和上面的兩個項目,訪問http://localhost:8801/sendMessage
在消費者的控制臺中就會出現(xiàn)下面的訂單流水號:

補(bǔ)充說明
我們打開rabbitmq的監(jiān)控界面:發(fā)現(xiàn)默認(rèn)是幫我們分組的

想要自定義分組只需要在消費者的yml文件中加上下面的一行:

如果有多個消費者,為了避免出現(xiàn)重復(fù)消費的問題,應(yīng)將組名設(shè)置成一樣的。一個組的成員輪循消費,不同組的成員進(jìn)行相同的消費。
到此這篇關(guān)于SpringCloud Stream介紹的文章就介紹到這了,更多相關(guān)SpringCloud Stream介紹內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java?中很好用的數(shù)據(jù)結(jié)構(gòu)(你絕對沒用過)
今天跟大家介紹的就是?java.util.EnumMap,也是?java.util?包下面的一個集合類,同樣的也有對應(yīng)的的?java.util.EnumSet,對java數(shù)據(jù)結(jié)構(gòu)相關(guān)知識感興趣的朋友一起看看吧2022-05-05
SpringMVC解析JSON請求數(shù)據(jù)問題解析
這篇文章主要介紹了SpringMVC解析JSON請求數(shù)據(jù)問題解析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
出現(xiàn)次數(shù)超過一半(50%)的數(shù)
給出n個數(shù),需要我們找出出現(xiàn)次數(shù)超過一半的數(shù),下面小編給大家分享下我的實現(xiàn)思路及關(guān)鍵代碼,感興趣的朋友一起學(xué)習(xí)吧2016-07-07
SSH框架網(wǎng)上商城項目第27戰(zhàn)之申請域名空間和項目部署及發(fā)布
這篇文章主要為大家詳細(xì)介紹了SSH框架網(wǎng)上商城項目第26戰(zhàn)之申請域名空間和項目部署及發(fā)布,感興趣的小伙伴們可以參考一下2016-06-06
springboot配置文件如何實現(xiàn)多個yml相互讀取問題
在SpringBoot應(yīng)用中,可以通過多種方式實現(xiàn)多個YAML配置文件的相互讀取和組合,SpringBoot2.4及以上版本可以使用spring.config.import屬性導(dǎo)入其他配置文件,@PropertySource注解雖不支持YAML2024-11-11
RedisTemplate中opsForValue和opsForList方法的使用詳解
這篇文章主要介紹了RedisTemplate中opsForValue和opsForList方法的使用詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
Spring Cloud 網(wǎng)關(guān)服務(wù) zuul 動態(tài)路由的實現(xiàn)方法
網(wǎng)關(guān)服務(wù)是流量的唯一入口。不能隨便停服務(wù)。所以動態(tài)路由就顯得尤為必要。這篇文章主要介紹了Spring Cloud 網(wǎng)關(guān)服務(wù) zuul 三 動態(tài)路由的相關(guān)知識,需要的朋友可以參考下2019-10-10
多jdk環(huán)境下指定springboot外部配置文件詳解
這篇文章主要為大家介紹了多jdk環(huán)境下指定springboot外部配置文件詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03

