Integration of Spring Cloud and message middleware

1. Introduction

1. Overview of Spring Cloud

Spring Cloud is an open source project that provides a series of frameworks and tools for JVM-based cloud-native applications, built on top of Spring. It provides developers with the features needed to build resilient and reliable distributed systems in complex environments, such as configuration management, service discovery, circuit breakers, and more.

2. Overview of message middleware

Message middleware is a solution for asynchronous communication in a distributed system, which is mainly used to solve problems such as high concurrency, low latency, high reliability and decoupling of the system. The common ones are Kafka, RabbitMQ, ActiveMQ, etc.

3. The background of the integration of Spring Cloud and message middleware

In the microservice architecture, inter-service communication is an essential link. As a reliable, stable and high-performance message solution, message middleware is widely adopted. Spring Cloud provides support for a variety of message middleware, making it easier and faster to integrate message middleware in the microservice architecture.

2. The integration method of Spring Cloud and message middleware

1. Introduction to Spring Cloud Stream

Spring Cloud Stream is a framework for building message-driven microservices. It defines a common development model and programming paradigm, and provides unified support for various message middleware. Using Spring Cloud Stream, messages can be sent to and received from middleware through a "message channel".

2. Adapter for message middleware

Spring Cloud Stream provides a set of abstract Binder API, through which the message channel of Spring Cloud Stream can be adapted to specific message middleware. Currently, Spring Cloud Stream already supports a variety of common message middleware, such as Kafka, RabbitMQ, ActiveMQ, etc.

3. Message Channel Binder

The message channel binder is a bridge for mapping the channel properties defined in Spring Cloud Stream with the underlying message middleware implementation. Spring Cloud Stream provides a variety of configurable message channel binder implementations, and the appropriate binder can be selected according to the actual situation.

4. Message channel interceptor

The message channel interceptor is an optional component, mainly used for message conversion and processing between message producers and consumers. Operations such as message format conversion, protocol conversion, and message encryption can be implemented through the message channel interceptor to meet business needs.

3. Use Spring Cloud Stream to integrate different message middleware

1. RabbitMQ integration

1.1 Basic dependency introduction and configuration

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
    <version>3.1.2</version>
</dependency>
spring:
  cloud:
    stream:
      bindings:
        output:
          destination: my-exchange
          content-type: application/json
          producer:
            ... # 生产者的额外配置
        input:
          destination: my-queue
          group: my-group
          content-type: application/json
          consumer:
            ... # 消费者的额外配置
      rabbit:
        bindings:
          output:
            exchangeType: fanout
            ... # Exchange的额外配置
          input:
            ... # Queue的额外配置
        binder:
          ... # 其他RabbitMQ相关配置

1.2 Application Statement

import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;

@EnableBinding(MyChannels.class)
public class MyApp {
    
    
    
    @Output("output")
    private MessageChannel output;

}

interface MyChannels {
    
    
    String OUTPUT = "output";

    @Output(OUTPUT)
    MessageChannel output();
}

1.3 Publishing and subscribing to messages

import org.springframework.messaging.support.MessageBuilder;

...

myApp.output.send(MessageBuilder.withPayload(myMessage).build());

2. Kafka integration

2.1 Basic dependency introduction and configuration

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream-binder-kafka</artifactId>
    <version>3.1.2</version>
</dependency>
spring:
  cloud:
    stream:
      bindings:
        output:
          destination: my-topic
          content-type: application/json
          producer:
            ... # 生产者的额外配置
        input:
          destination: my-topic
          group: my-group
          content-type: application/json
          consumer:
            ... # 消费者的额外配置
      kafka:
        bindings:
          output:
            ... # Producer的其他配置
          input:
            ... # Consumer的其他配置
        binder:
          ... # 其他Kafka相关配置

2.2 Application Declaration

import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;

@EnableBinding(MyChannels.class)
public class MyApp {
    
    
    
    @Output("output")
    private MessageChannel output;

}

interface MyChannels {
    
    
    String OUTPUT = "output";

    @Output(OUTPUT)
    MessageChannel output();
}

2.3 Publishing and subscribing to messages

import org.springframework.messaging.support.MessageBuilder;

...

myApp.output.send(MessageBuilder.withPayload(myMessage).build());

3. ActiveMQ integration

3.1 Basic dependency introduction and configuration

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream-binder-activemq</artifactId>
    <version>3.1.2</version>
</dependency>
spring:
  cloud:
    stream:
      bindings:
        output:
          destination: my-queue
          content-type: application/json
          producer:
            ... # 生产者的额外配置
        input:
          destination: my-queue
          group: my-group
          content-type: application/json
          consumer:
            ... # 消费者的额外配置
      activemq:
        bindings:
          output:
            ... # Producer的其他配置
          input:
            ... # Consumer的其他配置
        binder:
          ... # 其他ActiveMQ相关配置

3.2 Application Declaration

import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;

@EnableBinding(MyChannels.class)
public class MyApp {
    
    
    
    @Output("output")
    private MessageChannel output;

}

interface MyChannels {
    
    
    String OUTPUT = "output";

    @Output(OUTPUT)
    MessageChannel output();
}

3.3 Publishing and subscribing to messages

import org.springframework.messaging.support.MessageBuilder;

...

myApp.output.send(MessageBuilder.withPayload(myMessage).build());

4. Application Scenarios of Integration of Spring Cloud and Message Middleware

Under the microservice architecture, message passing is required between services to achieve business decoupling and asynchronous processing. At the same time, in order to achieve high availability and scalability, it is usually necessary to use message middleware as the infrastructure for asynchronous message delivery. Spring Cloud provides rich support and integration capabilities, allowing us to easily integrate various message middleware in Spring Cloud applications.

A. Messaging under the microservice architecture

Under the microservice architecture, due to the distributed deployment and decoupling characteristics of services, synchronous calls between services will increase the coupling of each service, which will make it difficult to expand the application. Therefore, using messaging middleware for asynchronous messaging is a better approach.

B. Realize business decoupling and asynchronous processing

The use of message middleware can turn the calls between services into asynchronous message transmission, thereby reducing the coupling between services, thereby improving the scalability and maintainability of the overall application. In addition, message middleware can also implement various message delivery modes, such as publish/subscribe, point-to-point, etc.

C. High availability and scalability implementation

Message middleware usually has high reliability and high availability, even if a service node goes down, it will not affect the normal operation of the entire system. In addition, the scalability of the message middleware is also very good, which can provide high throughput and low latency message delivery, and can be dynamically expanded.

5. Summary and review

A. Advantages of integrating Spring Cloud with message middleware

Spring Cloud provides rich support and integration capabilities, and can easily integrate various message middleware into Spring Cloud applications, so as to achieve the effect of asynchronous message delivery and service decoupling.

B. The flexibility to integrate different messaging middleware using Spring Cloud Stream

Spring Cloud Stream is a framework for building message-driven microservices. It provides a unified programming model that allows us to switch between different message middleware without modifying the application code. This means that we can choose the most suitable message middleware to meet business needs at runtime.

Guess you like

Origin blog.csdn.net/u010349629/article/details/130857745