SpringCloud Stream message driver

Why is this technology available? ? ?

1. 这个stream是操作消息队列的,简化,学习消息队列的成本降低。
2. 可操作rabbitMQ兔子message queue,kafaka,可理解为jdbc可操作oracle, mysql..
3. spring家的技术学就完了。。

Concepts you need to understand about message-driven

(1) Website document
https://spring.io/projects/spring-cloud-stream#overview

https://cloud.spring.io/spring-cloud-static/spring-cloud-stream/3.0.1.RELEASE/reference/html/


(2) Introduction

什么是SpringCloudStream
官方定义Spring Cloud Stream是一个构建消息驱动微服务的框架。
应用程序通过inputs或者outputs与Spring Cloud Stream中binder对象交互。
通过我们配置来binding(绑定),
而Spring Cloud Stream的binder对象负责与消息中间件交互。
所以,我们只需要搞清楚如何与Spring Cloud Stream
交互就可以访便使用消息驱动的方式。
通过使用Spring Integration来连接消息代理中间件以实现消息事件驱动。
Spring Cloud Stream为一些供应商的消息中间件产品提供了个性化的自动化配置实现,
引用了发布-订阅、消费组、分区的三个核心概念。
目前仅支持RabbitMQ、Kafka.

(3) Standard mq

  • Message

    • Producers/consumers rely on message media to transfer information content
  • Message Channel

    • The message must go through a specific channel
  • The sub-interface SubscribableChannel of the message channel MessageChannel, subscribed by the MessageHandler message processor

    • How are the messages in the message channel consumed? Who is responsible for receiving and sending them?

(4) Why use Cloud Stream

1 Why can the binding stream unify the underlying differences?

  • In the absence of binder when the concept of situation, our SpringBoot application to exchange information directly with the messaging middleware,
    in various messaging middleware to build a different mind, there will be a greater difference on the details of their implementation
    by Define the binder as the middle layer, which perfectly realizes the isolation between the application and the details of the message middleware.
    By exposing the unified Channel to the application, the application does not need to consider various message middleware implementations.

2 binder architecture

Insert picture description here

INPUT对应于消费者
OUTPUT对应于生产者

(5) The message communication method in Stream follows the publish-subscribe model

Topic主题进行广播
在RabbitMQ就是Exchange 交换机
在kafka中就是Topic

(6) Common api, annotation

Insert picture description here

Message-driven producers, consumers

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

abstain
server:
  port: 8802

spring:
  application:
    name: cloud-stream-consumer
  cloud:
    stream:
      binders: # 在此处配置要绑定的rabbitmq的服务信息;
        defaultRabbit: # 表示定义的名称,用于于binding整合
          type: rabbit # 消息组件类型
          environment: # 设置rabbitmq的相关的环境配置
            spring:
              rabbitmq:
                host: localhost
                port: 5672
                username: guest
                password: guest
      bindings: # 服务的整合处理
        input: # 这个名字是一个通道的名称
          destination: studyExchange # 表示要使用的Exchange名称定义
          content-type: application/json # 设置消息类型,本次为json,文本则设置“text/plain”
          binder: defaultRabbit  # 设置要绑定的消息服务的具体设置

eureka:
  client: # 客户端进行Eureka注册的配置
    service-url:
      defaultZone: http://localhost:7001/eureka
  instance:
    lease-renewal-interval-in-seconds: 2 # 设置心跳的时间间隔(默认是30秒)
    lease-expiration-duration-in-seconds: 5 # 如果现在超过了5秒的间隔(默认是90秒)
    instance-id: receive-8802.com  # 在信息列表时显示主机名称
    prefer-ip-address: true     # 访问的路径变为IP地址
 

Send message interface, realize

public interface IMessageProvider
{
    
    
    public String send();
}
 
 
/// 实现 
import com.atguigu.springcloud.service.IMessageProvider;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.integration.support.MessageBuilderFactory;
import org.springframework.messaging.MessageChannel;
import org.springframework.integration.support.MessageBuilder;
import javax.annotation.Resource;
import org.springframework.cloud.stream.messaging.Source;

import javax.annotation.Resource;
import java.util.UUID;


@EnableBinding(Source.class) //定义消息的推送管道
public class MessageProviderImpl implements IMessageProvider
{
    
    
    @Resource
    private MessageChannel output; // 消息发送管道

    @Override
    public String send()
    {
    
    
        String serial = UUID.randomUUID().toString();
        output.send(MessageBuilder.withPayload(serial).build());
        System.out.println("*****serial: "+serial);
        return null;
    }
}
 
  
 

  1. At the same time consumer yaml, pom, yaml must change the port.
    Define the controller to receive messages
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.Message;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.stereotype.Component;

@Component
@EnableBinding(Sink.class)
public class ReceiveMessageListenerController {
    
    
    @Value("${server.port}")
    private String serverPort;

    @StreamListener(Sink.INPUT)
    public void input(Message<String> message) {
    
    
        System.out.println("消费者1号,接受:"+message.getPayload()+"\t port:"+serverPort);
    }

}

Build another service, message consumer, problem

1. 消息重复。发送者发送消息,两个消费者都会接收到消息,如果是支付多个模块,
收到一条消息,多个模块会收到坏账,需要分组,只对一个支付模块发消息.
2. 消息持久化。当关掉消费者,消息丢失。

a. New group configuration, custom group

group: damn

b. Persistence, the service is down, to ensure that the message is not lost

Page number configuration grouping solution

Guess you like

Origin blog.csdn.net/qq_44783283/article/details/111397952