My latest and most complete articles are in the pumpkin slow say www.pkslow.com, welcome to tea!

1 introduction

Spring Cloud Stream is designed for event-driven microservice systems that use messaging middleware to send and receive information. Using Spring Cloud Stream allows you to focus on business development without having to worry too much about the interaction between your application and MQ. Also, you don’t need to make many code changes after switching to MQ.

This article will integrate Spring Cloud Stream and RabbitMQ for messaging.

2 Integration Process

2.1 Adding a Dependency

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
Copy the code

Different MQ uses different dependencies, making it easy to switch.

2.2 Define methods for processing sending and receiving

Queues are all about sending and receiving, so we need to define how to send and receive.

Send a message:

@Bean
public Supplier<String> pkslowSource(a) {
  return () -> {
    String message = "www.pkslow.com";
    log.info("Sending value: " + message);
    return message;
  };
}
Copy the code

Only one String is sent, and the common business is usually the Entity class. The content sent here is also fixed. The actual business can obtain data sources by checking databases and reading files.

Receiving messages:

@Bean
public Consumer<String> pkslowSink(a) {
  return message -> {
    log.info("Received message " + message);
  };
}
Copy the code

The message can be printed directly, and the logic in the project can be implemented according to the specific business.

2.3 Configuring Attributes

Configure the RabbitMQ:

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: pkslow
    password: 123456
Copy the code

Configure Spring Cloud Stream related items:

spring:
  cloud:
    stream:
      function:
        definition: pkslowSource; pkslowSink
      bindings:
        pkslowSource-out-0:
         destination: pkslow-topic
        pkslowSink-in-0:
          destination: pkslow-topic
      poller:
        fixed-delay: 500
Copy the code

Spring. Cloud. Stream. The function. The definition will define treatment methods, such as the paper method of sending and receiving messages.

Bindings configuration corresponding function; Destination points to the topic of MQ;

There is a poller that sends messages every 500ms.

2.4 run

Start with RabbitMQ:

docker run \ -e RABBITMQ_DEFAULT_USER=pkslow \ -e RABBITMQ_DEFAULT_PASS=123456 \ -p 5672:5672 -p 15672:15672 \ The rabbitmq: 3.8 - managementCopy the code

After running the program, it will create the theme, send the message, receive the message:

Run logs are as follows:

It can be seen that each send/receive is about 500ms apart, but of course it cannot be exactly 500ms.

3 summary

Please check the code: github.com/LarryDpk/pk…


Welcome to pay attention to the wechat public number “Pumpkin slow Talk”, will continue to update for you…

Read more and share more; Write more. Organize more.