Spring Cloud_29_消息驱动/整合SpringCloud

本文介绍如何在SpringCloud集群环境中实现消息驱动开发,包括使用RabbitMQ和Kafka作为消息中间件的具体步骤。通过示例代码展示了生产者与消费者的实现方式,并介绍了Spring Cloud Stream简化开发的Sink、Source和Processor接口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

消息驱动/整合SpringCloud

  • 如何在SpringCloud集群中进行消息开发?

1、准备工作

  • Eureka服务器
  • Eureka客户端,Producer:用于生产消息,Consumer:用于消费消息
  • RabbitMQ作为服务在window系统中运行,所以暂用RabbitMQ进行搭建测试,之后再更换为kafka
server:
 port: 9000
spring:
 application:
  name: atm-msg-consumer
 #rabbitmq:
 # host: localhost
 # port: 5762
 # username: guest
 # password: guest
 # 均使用默认的配置,所以可以无需配置,需要修改时,再配置

2、生产者/RabbitMQ

2.1、引入依赖

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

2.2、SendMessageInterface

package com.atm.cloud;

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

/**
 * 发送消息的服务接口,用来绑定Topic
 */
public interface SendMessageInterface {

    /**
     * 声明一个方法用来订阅渠道,使用output注解,声明渠道名称,从这里输出一个消息
     */
    @Output("myInput")
    SubscribableChannel sendMsg();
}

2.3、SendMessageController

package com.atm.cloud;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 使用SendMessageInterface中的消息渠进行发送消息
 */
@RestController
public class SendMessageController {

    @Autowired
    private SendMessageInterface sendMessageInterface;

    @GetMapping("/send")
    public String sendMsg() {

        Message msg = MessageBuilder.withPayload("Hello World".getBytes())
                .build();

        sendMessageInterface.sendMsg().send(msg);
        return "Success";
    }
}

2.4、MsgProducerApp

package com.atm.cloud;

import java.util.Scanner;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.stream.annotation.EnableBinding;

@SpringBootApplication
@EnableEurekaClient
@EnableBinding(SendMessageInterface.class)//开启绑定
public class MsgProducerApp {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String port = scanner.nextLine();

        new SpringApplicationBuilder(MsgProducerApp.class).properties(
                "server.port=" + port).run(args);
    }
}

3、消费者/RabbitMQ

3.1、引入依赖

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

3.2、ReadMessageInterface

package com.atm.cloud;

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

/**
 * 用于接收消息
 */
public interface ReadMessageInterface {

    // 绑定myInput的渠道
    @Input("myInput")
    SubscribableChannel readMsg();
}

3.3、MsgConsumerApp

package com.atm.cloud;

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.StreamListener;

@SpringBootApplication
@EnableDiscoveryClient
@EnableBinding(ReadMessageInterface.class)
public class MsgConsumerApp {

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

    /**
     * 用于监听接收的消息
     */
    @StreamListener("myInput")
    public void onListen(byte[] msg) {
        System.out.println("接收到的消息:" + new String(msg));
    }
}

  • 我们无需关心消息如何发送与接收,我们只需要知道其中的内容是什么
  • 目前我们已经使用RabbitMQ发送和接收消息了,接下来我们要使用kafka
  • 只需要更换绑定器即可,SpringCloud默认提供了两个绑定器,一个绑定器是绑定了RabbitMQ,一个是绑定kafka,所有调用消息中间件的代码都已经在绑定器中实现了,绑定期类似与适配器,使用不同的消息代理中间件就使用不同的绑定器

4、生产者/消费者-Kafka

  • 只需要修改依赖即可,更换绑定器
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-stream-kafka</artifactId>
</dependency>

5、消费者组

spring:
  application:
    name: spring-msg-consumer3
  ##配置消费者组
  cloud:
    stream:
      bindings:
        myInput:
          group: groupB
  • 同一消费者组,其中一个处理
  • 不同消费者组,广播分配

5.1、Sink、Source、Processor

  • 为了简化开发,SpringCloud Stream内置了三个接口,Sink、Source、Processor
  • Processor继承了Sink和Source,实际应用中可以考虑只使用Processor
public interface Sink {

    String INPUT = "input";

    @Input(Sink.INPUT)
    SubscribableChannel input();

}
public interface Source {

    String OUTPUT = "output";

    @Output(Source.OUTPUT)
    SubscribableChannel output();

}
  • 根据这两个接口可知,实际上帮我们内置了”input”和“output”两个通道,那么在大多数情况下,我们就可以不必编写服务接口,甚至不必使用@input和@output两个注解,在绑定通道时加入Sink.class
/**
 * 用于接收消息,可以不需要了
 */
public interface ReadMessageInterface {

    // 绑定myInput的渠道
    @Input("myInput")
    SubscribableChannel readMsg();
}

/**
 * 发送消息的服务接口,用来绑定Topic
 */
public interface SendMessageInterface {

    /**
     * 声明一个方法用来订阅渠道,使用output注解,声明渠道名称,从这里输出一个消息
     */
    @Output("input")
    SubscribableChannel sendMsg();
}
package com.atm.cloud;

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.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.cloud.stream.messaging.Source;

@SpringBootApplication
@EnableDiscoveryClient
//@EnableBinding(ReadMessageInterface.class)
@EnableBinding(value={Sink.class})
public class MsgConsumerApp {

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

    /**
     * 用于监听接收的消息
     */
    //@StreamListener("myOutput)
    @StreamListener(Sink.INPUT)
    public void onListen(byte[] msg) {
        System.out.println("A:接收到的消息:" + new String(msg));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

'嗯哼。

生生不息,“折腾”不止,Thx

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值