最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Cloud Stream簡(jiǎn)單用法

 更新時(shí)間:2021年07月23日 14:50:36   作者:微瞰技術(shù)  
Spring cloud stream是為構(gòu)建微服務(wù)消息驅(qū)動(dòng)而產(chǎn)生的一種框架。Spring Cloud Stream基于Spring boot的基礎(chǔ)上,可創(chuàng)建獨(dú)立的、生產(chǎn)級(jí)別的Spring應(yīng)用,并采用Spring Integration來(lái)連接消息中間件提供消息事件驅(qū)動(dòng),一起看看吧

Spring Cloud Stream對(duì)Spring Cloud體系中的Mq進(jìn)⾏了很好的上層抽象,可以讓我們與具體消息中間件解耦合,屏蔽掉了底層具體MQ消息中間件的細(xì)節(jié)差異,就像Hibernate屏蔽掉了具體數(shù)據(jù)庫(kù)(Mysql/Oracle⼀樣)。如此⼀來(lái),我們學(xué)習(xí)、開(kāi)發(fā)、維護(hù)MQ都會(huì)變得輕松。⽬前Spring Cloud Stream原生⽀持RabbitMQ和Kafka,阿里在這個(gè)基礎(chǔ)上提供了RocketMQ的支持

簡(jiǎn)單使用Spring Cloud Stream 構(gòu)建基于RocketMQ的生產(chǎn)者和消費(fèi)者

生產(chǎn)者

pom文件中加入依賴(lài)

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>

    </dependencies>

配置文件中增加關(guān)于Spring Cloud Stream binder和bindings的配置

spring:
  application:
    name: zhao-cloud-stream-producer
  cloud:
    stream:
      rocketmq:
        binder:
          name-server: 127.0.0.1:9876
        bindings:
          output:
            producer:
              group: test
              sync: true
      bindings:
        output:
          destination: stream-test-topic
          content-type: text/plain # 內(nèi)容格式。這里使用 JSON

其中destination代表生產(chǎn)的數(shù)據(jù)發(fā)送到的topic 然后定義一個(gè)channel用于數(shù)據(jù)發(fā)送

import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;

public interface TestChannel {
    @Output("output")
    MessageChannel output();
}

最后構(gòu)造數(shù)據(jù)發(fā)送的接口

@Controller
public class SendMessageController {
    @Resource
    private TestChannel testChannel;

    @ResponseBody
    @RequestMapping(value = "send", method = RequestMethod.GET)
    public String sendMessage() {
        String messageId = UUID.randomUUID().toString();
        Message<String> message = MessageBuilder
                .withPayload("this is a test:" + messageId)
                .setHeader(MessageConst.PROPERTY_TAGS, "test")
                .build();
        try {
            testChannel.output().send(message);
            return messageId + "發(fā)送成功";
        } catch (Exception e) {
            return messageId + "發(fā)送失敗,原因:" + e.getMessage();
        }
    }
}

消費(fèi)者

消費(fèi)者的pom引入與生產(chǎn)者相同,在此不再贅述,配置時(shí)需要將stream的output修改為input并修改對(duì)應(yīng)屬性

spring:
  application:
    name: zhao-cloud-stream-consumer
  cloud:
    stream:
      rocketmq:
        binder:
          name-server: 127.0.0.1:9876
        bindings:
          input:
            consumer:
              tags: test
      bindings:
        input:
          destination: stream-test-topic
          content-type: text/plain # 內(nèi)容格式。這里使用 JSON
          group: test

另外關(guān)于channel的構(gòu)造也要做同樣的修改

import org.springframework.cloud.stream.annotation.Input;
import org.springframework.messaging.SubscribableChannel;

public interface TestChannel {
    @Input("input")
    SubscribableChannel input();
}

最后我在啟動(dòng)類(lèi)中對(duì)收到的消息進(jìn)行了監(jiān)聽(tīng)

   @StreamListener("input")
    public void receiveInput(@Payload Message message) throws ValidationException {
        System.out.println("input1 receive: " + message.getPayload() + ", foo header: " + message.getHeaders().get("foo"));
    }

測(cè)試結(jié)果

file

file

Stream其他特性

消息發(fā)送失敗的處理

消息發(fā)送失敗后悔發(fā)送到默認(rèn)的一個(gè)“topic.errors"的channel中(topic是配置的destination)。要配置消息發(fā)送失敗的處理,需要將錯(cuò)誤消息的channel打開(kāi) 消費(fèi)者配置如下

spring:
  application:
    name: zhao-cloud-stream-producer
  cloud:
    stream:
      rocketmq:
        binder:
          name-server: 127.0.0.1:9876
        bindings:
          output:
            producer:
              group: test
              sync: true
      bindings:
        output:
          destination: stream-test-topic
          content-type: text/plain # 內(nèi)容格式。這里使用 JSON
          producer:
            errorChannelEnabled: true

在啟動(dòng)類(lèi)中配置錯(cuò)誤消息的Channel信息

 @Bean("stream-test-topic.errors")
    MessageChannel testoutPutErrorChannel(){
        return new PublishSubscribeChannel();
    }

新建異常處理service

import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Service;

@Service
public class ErrorProducerService {

    @ServiceActivator(inputChannel = "stream-test-topic.errors")
    public void receiveProducerError(Message message){
        System.out.println("receive error msg :"+message);
    }
}

當(dāng)發(fā)生異常時(shí),由于測(cè)試類(lèi)中已經(jīng)將異常捕獲,處理發(fā)送異常主要是在這里進(jìn)行。模擬,應(yīng)用與rocketMq斷開(kāi)的場(chǎng)景??梢?jiàn)

file file

消費(fèi)者錯(cuò)誤處理

首先增加配置為

spring:
  application:
    name: zhao-cloud-stream-producer
  cloud:
    stream:
      rocketmq:
        binder:
          name-server: 127.0.0.1:9876
        bindings:
          output:
            producer:
              group: test
              sync: true
      bindings:
        output:
          destination: stream-test-topic
          content-type: text/plain # 內(nèi)容格式。這里使用 JSON
          producer:
            errorChannelEnabled: true

增加相應(yīng)的模擬異常的操作

 @StreamListener("input")
    public void receiveInput(@Payload Message message) throws ValidationException {
        //System.out.println("input1 receive: " + message.getPayload() + ", foo header: " + message.getHeaders().get("foo"));
        throw new RuntimeException("oops");
    }
    @ServiceActivator(inputChannel = "stream-test-topic.test.errors")
    public void receiveConsumeError(Message message){
        System.out.println("receive error msg"+message.getPayload());
    }

file

代碼地址https://github.com/zhendiao/deme-code/tree/main/zp

到此這篇關(guān)于Spring Cloud Stream簡(jiǎn)單用法的文章就介紹到這了,更多相關(guān)Spring Cloud Stream使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java如何通過(guò)File類(lèi)方法刪除指定文件夾中的全部文件

    Java如何通過(guò)File類(lèi)方法刪除指定文件夾中的全部文件

    這篇文章主要給大家介紹了關(guān)于Java如何通過(guò)File類(lèi)方法刪除指定文件夾中的全部文件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • 一文帶你了解Java8?Stream流處理中的收集器技巧

    一文帶你了解Java8?Stream流處理中的收集器技巧

    Java?8?引入的?Stream?極大地簡(jiǎn)化了集合數(shù)據(jù)的處理,提供了一種現(xiàn)代、函數(shù)式的方式來(lái)處理數(shù)據(jù),本文將深入探討?Java?8?Stream?中的收集器,希望對(duì)大家有所幫助
    2023-08-08
  • Spring超出最大會(huì)話數(shù)(Max?sessions?limit?reached:?10000)

    Spring超出最大會(huì)話數(shù)(Max?sessions?limit?reached:?10000)

    在Spring系統(tǒng)中遇到的Maxsessionslimitreached:10000錯(cuò)誤,該錯(cuò)誤由于會(huì)話數(shù)超過(guò)默認(rèn)限制10000而觸發(fā),下面就來(lái)介紹一下解決方法,感興趣的可以了解一下
    2024-12-12
  • 什么是 Java 的 CyclicBarrier(代碼示例)

    什么是 Java 的 CyclicBarrier(代碼示例)

    CyclicBarrier 是多線程協(xié)同的利器,適合需要多次同步的場(chǎng)景,本文通過(guò)代碼示例講解什么是 Java 的 CyclicBarrier,感興趣的朋友一起看看吧
    2025-03-03
  • java仿windows記事本小程序

    java仿windows記事本小程序

    這篇文章主要為大家詳細(xì)介紹了java仿windows記事本小程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • java拼接字符串時(shí)去掉最后一個(gè)多余逗號(hào)的方法

    java拼接字符串時(shí)去掉最后一個(gè)多余逗號(hào)的方法

    這篇文章主要介紹了java拼接字符串時(shí)去掉最后一個(gè)多余逗號(hào)的方法,實(shí)例分析了java操作字符串的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-03-03
  • Java方法能定義多少個(gè)參數(shù)你知道嗎

    Java方法能定義多少個(gè)參數(shù)你知道嗎

    這篇文章主要給大家介紹了關(guān)于Java方法能定義多少個(gè)參數(shù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • Java讀寫(xiě)pdf文件的詳細(xì)實(shí)現(xiàn)方法

    Java讀寫(xiě)pdf文件的詳細(xì)實(shí)現(xiàn)方法

    最近公司的項(xiàng)目中需要操作pdf文件,所以這里給大家總結(jié)下方法,這篇文章主要給大家介紹了關(guān)于Java讀寫(xiě)pdf文件的詳細(xì)實(shí)現(xiàn)方法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-09-09
  • 詳解Springboot對(duì)多線程的支持

    詳解Springboot對(duì)多線程的支持

    Spring是通過(guò)任務(wù)執(zhí)行器(TaskExecutor)來(lái)實(shí)現(xiàn)多線程和并發(fā)編程,使用ThreadPoolTaskExecutor來(lái)創(chuàng)建一個(gè)基于線城池的TaskExecutor。這篇文章給大家介紹Springboot對(duì)多線程的支持,感興趣的朋友一起看看吧
    2018-07-07
  • java導(dǎo)出excel 瀏覽器直接下載或者或以文件形式導(dǎo)出

    java導(dǎo)出excel 瀏覽器直接下載或者或以文件形式導(dǎo)出

    這篇文章主要介紹了java導(dǎo)出excel 瀏覽器直接下載或者或以文件形式導(dǎo)出方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06

最新評(píng)論

上犹县| 德安县| 镇赉县| 克什克腾旗| 铜梁县| 惠安县| 沁源县| 乌兰浩特市| 麟游县| 阳江市| 云浮市| 宁化县| 安顺市| 水城县| 中卫市| 乌兰县| 白城市| 察隅县| 东乌| 五河县| 玉田县| 黑河市| 大荔县| 黑龙江省| 西盟| 蒙城县| 洛宁县| 上犹县| 灵丘县| 浦北县| 绥阳县| 黎城县| 永泰县| 丹寨县| 基隆市| 会泽县| 保亭| 军事| 台中县| 上犹县| 郁南县|