spring cloud stream rabbit 4.0示例

参考链接

疑问

这里配置生产者、消费者 每一个都需要在yml配置,看起来很复杂,不知道有没有简单的配置方法

pom 添加依赖

方式一

<!--cloud rabbitMq 依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
            <version>4.1.0</version>
        </dependency>

方式二

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-stream-rabbit -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
    <version>4.1.0</version>
</dependency>

yml 配置文件

rabbitmq 配置

spring:
  cloud:
    stream:
      binders:
        rabbit:
          type: rabbit
          environment: #配置rabbitmq连接环境
            spring:
              rabbitmq:
                host: ip
                username: admin
                password: admin
                virtual-host: my_vhost

这里我把生产者 消费者放在一个项目测试,可以在不同想目放生产者、消费者

消费者 配置


spring:
  cloud:
    stream:
      bindings:
        # in 消费者
        test-in-0:
          content-type: application/json
          destination: test-destination
          group: test-group
          binder: rabbit
        test1-in-0:
          content-type: application/json
          destination: test1-destination
          group: test1-group
          binder: rabbit
        test2-in-0:
          content-type: application/json
          destination: test2-destination
          group: test2-group  # 队列
          binder: rabbit
    function:
      definition: test;test1;test2

消费者

@Component
public class ConsumerTest {


    /**
     * 注意方法名称 demo 要与配置文件中的spring.cloud.stream.bindings.demo-in-0 保持一致
     * 其中 -in-0 是固定写法,in 标识消费者类型,0是消费者索引
     */
    @Bean
    public Consumer<Person> test() {
        return person -> {
            System.out.println("Received: " + person);
        };
    }

    @Bean
    public Consumer<String> test1() {
        return msg -> {
            System.out.println("Received: " + msg);
        };
    }

    @Bean
    public Consumer<Person> test2() {
        return msg -> {
            System.out.println("Received: " + msg);
        };
    }


}

public class Person {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String toString() {
        return this.name;
    }
}

启动后可以在 rabbitmq 控制台看到 生成的 topicqueue

在这里插入图片描述

在这里插入图片描述

配置生产者

yml

spring:
  cloud:
    stream:
      bindings:
        # 生产者
        test-out-0:
          content-type: application/json
          destination: test-destination  # topic
          binder: rabbit
        test1-out-0:
          content-type: application/json
          destination: test1-destination
          binder: rabbit
        test2-out-0:
          content-type: application/json
          destination: test2-destination
          binder: rabbit

测试代码

package com.example.demorabbit;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class ProducerController {

    private final StreamBridge streamBridge;


    @GetMapping("sendMsg")
    public String sendMsg(int delay, String name) {

        Person person = new Person();
        person.setName(name);
//        Message<SpringcloudstreamDemo1Application.Person> message = MessageBuilder.withPayload(person)
//                .setHeader("x-delay", delay).build();
//        // 发送延时消息
//        streamBridge.send("demo2-out-0", message);

        streamBridge.send("test1-out-0", person);

        streamBridge.send("test-out-0", person);

        return "发送成功";
    }
}




测试发送消息
http://localhost:5656/sendMsg?delay=10000&name=zhangsan

发送接收成功
在这里插入图片描述

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值