Sping Cloud Alibaba - 5、Stream & RabbitMQ

1、RabbitMQ 环境安装

链接(linux):https://www.linuxprobe.com/install-rabbitmq-on-centos-7.html
链接(win):https://www.cnblogs.com/JustinLau/p/11738511.html

2、Producer 生产者

  • main 服务启动类
package com.wyh;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

    @RestController
    static class TestController {

        @Autowired
        private TestTopic testTopic;

        @GetMapping("/sendMessage")
        public String messageWithMQ(@RequestParam String message) {
            testTopic.output().send(MessageBuilder.withPayload(message).setHeader("version", "1.0").build());
            testTopic.output().send(MessageBuilder.withPayload(message).setHeader("version", "2.0").build());
            return "ok";
        }

    }


    interface TestTopic {

        String OUTPUT = "example-topic-output";

        @Output(OUTPUT)
        MessageChannel output();

/*
        如果存在多个通道的话可以这样使用
        String OUTPUT2 = "example-topic-output";

        String INPUT2 = "example-topic-input";

        @Output(OUTPUT)
        MessageChannel output2();

        @Input(INPUT)
        SubscribableChannel input2();*/

    }
}

  • application.yml
server:
  port: 8006
spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: admin
    password: admin
  application:
    name: rabbitmq-provider   #本服务的名称
  cloud:                           #注册到nacos
    nacos:
      discovery:
        server-addr: localhost:8848
    stream:
      binders:  #需要绑定的rabbitmq的服务信息
        defaultRabbit: #定义的名称,用于binding整合
          type: rabbit #消息组件类型
      bindings: # 服务的整合处理(这里需要注意:如果存在多条管道,那么需要多个绑定)
        example-topic-output:
          destination: test-topic
management:
  endpoints:
    web:
      exposure:
        include: "*"
  • pom.xml
<?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">
    <parent>
        <artifactId>myspring-cloud</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>producer</artifactId>

    <dependencies>

        <!-- Spring Cloud Stream RabbitMQ-->

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
        </dependency>

        <!--  SpringCloud alibaba nacos    -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!--  web组件      -->
        <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.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>


    </dependencies>
</project>

3、consumer-01 消费者

  • main 服务启动类
package com.wyh;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.Input;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.SubscribableChannel;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

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


    /**
     * 消息消费逻辑
     */
    @Slf4j
    @Component
    static class TestListener {

        @StreamListener(value = TestTopic.INPUT, condition = "headers['version']=='1.0'")
        public void receiveV1(String payload, @Header("version") String version) {
            log.info("Received v1 com-01: " + payload + ", " + version);
        }

        @StreamListener(value = TestTopic.INPUT, condition = "headers['version']=='2.0'")
        public void receiveV2(String payload, @Header("version") String version) {
            log.info("Received v2 com-01: " + payload + ", " + version);
        }

    }

    interface TestTopic {

        String INPUT = "example-topic-input";

        @Input(INPUT)
        SubscribableChannel input();


    }

}

  • application.yml
server:
  port: 8004
spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: admin
    password: admin
  application:
    name: consumer-01   #本服务的名称
  cloud:                           #注册到nacos
    nacos:
      discovery:
        server-addr: localhost:8848
    stream:
      binders:  #需要绑定的rabbitmq的服务信息
        defaultRabbit: #定义的名称,用于binding整合
          type: rabbit #消息组件类型
      bindings: # 服务的整合处理(这里需要注意:如果存在多条管道,那么需要多个绑定)
        example-topic-input:
          destination: test-topic  #exchange名称,交换模式默认是topic
          group: stream-content-route #分组
management:
  endpoints:
    web:
      exposure:
        include: "*"
  • pom.xml
<?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">
    <parent>
        <artifactId>myspring-cloud</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consumer-01</artifactId>

    <dependencies>

        <!-- Spring Cloud Stream RabbitMQ-->

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
        </dependency>

        <!--  SpringCloud alibaba nacos    -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!--  web组件      -->
        <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.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>


    </dependencies>
</project>

4、consumer-02 消费者

  • 01copy一份即可,端口号修改一下
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值