SpringCloudStream 使用总结

目录

一、集成RabbitMQ的使用案例

1、消息生产者

2、消息消费者

3、初步测试

二、集成Kafka的实现案例

三、集成RocetMQ实现案例

1、导入依赖

2、生产者

3、消费者

4、配置文件

5、运行效果


项目结构

消费者配置:

server:
  port: 8002

spring:
  cloud:
    stream:
      bindings: # 消费者配置
        input:
          destination: scstreamExchange  # 目标topic
          group: stream # 目标群

生产者配置:

server:
  port: 8001
spring:
  cloud:
    stream:
      bindings:
        output:  # 生产者配置
          destination: scstreamExchange

一、集成RabbitMQ的使用案例

父工程添加依赖如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.testpro</groupId>
    <artifactId>springcloudstreamTestPro</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>scs-common</module>
        <module>scs-consumer</module>
        <module>scs-producer</module>
    </modules>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <version>2.3.12.RELEASE</version>
        <artifactId>spring-boot-starter-parent</artifactId>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
        <com-alibaba-cloud.version>2.2.7.RELEASE</com-alibaba-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


</project>

1、消息生产者

启动类

package com.testPro;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;

@SpringBootApplication
@EnableBinding({Source.class})
public class ProducerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProducerApplication.class,args);
    }
}

生产者类:

package com.testPro.rabbit.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SendMessageController {

    @Autowired
    private Source source;

    @GetMapping("/test/{message}")
    public Object send(@PathVariable String message) {
        MessageBuilder<String> messageBuilder = MessageBuilder.withPayload(message);
        source.output().send(messageBuilder.build());
        return "Message sended " + message;
    }
}

2、消息消费者

启动类

package com.testPro;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Sink;

@SpringBootApplication
@EnableBinding({Sink.class})
public class ConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class,args);
    }
}

消费监听

package com.testPro.rabbit.listener;

import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.stereotype.Component;

@Component
public class MessageReceiver {

    @StreamListener(Sink.INPUT)
    public void process(Object message) {
        System.out.println("received message: " + message);
    }
}

3、初步测试

二、集成Kafka的实现案例

与RabbitMQ时代码相同,此时仅需更改依赖即可:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-kafka</artifactId>
        </dependency>

三、集成RocetMQ实现案例

与上面类似

1、导入依赖

<!--  SpringCloudStream 集成RocketMQ  -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
        </dependency>

注意:这里有个坑,如果你使用的springcloudalibaba对应的rocketmq版本大于4.4.0集成是会出问题的,高版本不要用springcloudstream集成rocketmq,仅限4.4.0一下版本,可以考虑我使用的版本:alibaba: 2.2.6.RELEASE cloud和boot都有其对应的版本。

2、生产者

package com.testPro.component;

import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
public class MyProducer {

    @Resource
    private Source source;

    public void sendMessage(String message){
        //封装消息头
        Map<String,Object> headers=new HashMap<>();
        headers.put(MessageConst.PROPERTY_TAGS,"TagA");
        MessageHeaders messageHeaders=new MessageHeaders(headers);
        //创建消息对象
        Message<String> message1 = MessageBuilder.createMessage(message, messageHeaders);
        //发送消息
        source.output().send(message1);
    }

    @GetMapping("/test/{s}")
    public void testSendMessage(@PathVariable String s){
        sendMessage(s);
    }
}

启动类:

package com.testPro;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;

@SpringBootApplication
@EnableBinding(Source.class)
public class ProducerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProducerApplication.class,args);
    }
}

3、消费者

package com.testPro.component;

import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.stereotype.Component;

@Component
public class MyListener {

    @StreamListener(Sink.INPUT)
    public void processMessage(String message){
        System.out.println("接收到的消息是:"+message);
    }
}

启动类:

package com.testPro;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Sink;

/**
 * author: Dragon Wu
 * date: 2022-09-10 12:51
 */
@SpringBootApplication
@EnableBinding(Sink.class)
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class,args);
    }
}

4、配置文件

生产者配置:

server:
  port: 8001
spring:
  cloud:
    stream:
      bindings:
        output:
          # 生产者目的地topic
          destination: TestTopic
      # rocket-mq连接的broker地址
      rocketmq:
        binder:
          name-server: localhost:9876

消费者配置:

server:
  port: 8888
spring:
  cloud:
    stream:
      bindings:
        input:  # 对应消费者
          destination: TestTopic
          group: scGroup  #消费者组
      #配置name-srv地址
      rocketmq:
        binder:
          name-server: localhost:9876

5、运行效果

 

 监听成功!

集成更多问题见我的解决博客:

SpringCloudStream集成Rocketmq踩坑笔记_Dragon Wu的博客-CSDN博客

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【2021年,将Spring全家桶系列课程进行Review,修复顺序等错误。进入2022年,将Spring的课程进行整理,整理为案例精讲的系列课程,并新增高级的Spring Security等内容,通过手把手一步步教你从零开始学会应用Spring,课件将逐步进行上传,敬请期待】 本课程是Spring案例精讲课程的第四部分Spring CloudSpring案例精讲课程以真实场景、项目实战为导向,循序渐进,深入浅出的讲解Java网络编程,助力您在技术工作中更进一步。 本课程聚焦Spring Cloud的核心知识点:注册中心、服务提供者与消费者、服务的调用OpenFeign、Hystrix监控、服务网关gateway、消息驱动的微服务Spring Cloud Stream、分布式集群、分布式配置中心的案例介绍, 快速掌握Spring Cloud的核心知识,快速上手,为学习及工作做好充足的准备。 由于本课程聚焦于案例,即直接上手操作,对于Spring的原理等不会做过多介绍,希望了解原理等内容的需要通过其他视频或者书籍去了解,建议按照该案例课程一步步做下来,之后再去进一步回顾原理,这样能够促进大家对原理有更好的理解。【通过Spring全家桶,我们保证你能收获到以下几点】 1、掌握Spring全家桶主要部分的开发、实现2、可以使用Spring MVC、Spring BootSpring CloudSpring Data进行大部分的Spring开发3、初步了解使用微服务、了解使用Spring进行微服务的设计实现4、奠定扎实的Spring技术,具备了一定的独立开发的能力  【实力讲师】 毕业于清华大学软件学院软件工程专业,曾在Accenture、IBM等知名外企任管理及架构职位,近15年的JavaEE经验,近8年的Spring经验,一直致力于架构、设计、开发及管理工作,在电商、零售、制造业等有丰富的项目实施经验  【本课程适用人群】如果你是一定不要错过!  适合于有JavaEE基础的,如:JSP、JSTL、Java基础等的学习者没有基础的学习者跟着课程可以学习,但是需要补充相关基础知识后,才能很好的参与到相关的工作中。 【Spring全家桶课程共包含如下几门】 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值