Message middleware is of great help to decoupling and peak removal between our systems. spring boot also integrates the contents of this part, and the easiest thing to integrate is rabbitmq. Today we will use rabbitmq as an example.
The old rules, let's take a look at the pom first
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
AMQP, or Advanced Message Queuing Protocol, an application-level standard advanced message queue protocol that provides unified messaging services, is an open standard of the application-level protocol and is designed for message-oriented middleware. Clients and message middleware based on this protocol can pass messages, and are not restricted by different products and development languages of clients/middleware. Spring-boot-starter-amqp introduces rabbitmq. There is a prerequisite: first install rabbitmq server on your machine, and then execute rabbitmq-server server and start it. After starting, we can configure our client program. First look at our configuration file
spring.application.name: spirng-boot-rabbitmqspring.rabbitmq.host: 127.0.0.1spring.rabbitmq.port: 5672spring.rabbitmq.username: guestspring.rabbitmq.password: guest
The server's IP, port, username, password and other basic information are configured to ensure that we can connect to the server.
Add a Rabbitmq configuration class
package com.shuqi;import org.springframework.amqp.core.Queue;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class RabbitConfig { @Bean public Queue Queue() { return new Queue("hello"); }}A queue named hello is created, and the producer can put data into the hello queue, and the consumer can consume data from the hello queue. Check out the producer's handler
package com.shuqi.controller;import org.springframework.amqp.core.AmqpTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class HelloController { @Autowired private AmqpTemplate rabbitTemplate; @RequestMapping("/hello") public String hello(@RequestParam String name){ rabbitTemplate.convertAndSend("hello","hello "+name); return "Message sent successfully"; }}Production of messages through controller and sending messages through AmqpTemplate. With producers, let's take a look at consumers
package com.shuqi.consumer;import lombok.extern.slf4j.Slf4j;import org.springframework.amqp.rabbit.annotation.RabbitHandler;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.stereotype.Component;@Component@RabbitListener(queues = "hello")@Slf4jpublic class HelloConsumer { @RabbitHandler public void process(String hello) { log.info("Received message: message:{}",hello); }}@RabbitListener(queues = "hello") means that it is a Rabbitmq listener. The queue name of the listener is hello, which means that the data will definitely come. When the data comes, the data will be processed through the @RabbitHandler modification method. Print it. Let's start the project and see the results.
Enter http://localhost:8080/hello?name=shuqi in your browser to see the following results
Check out the logs output from the console
2018-03-25 16:24:32.752 INFO 4987 --- [cTaskExecutor-1] com.shuqi.consumer.HelloConsumer: Received message: message:hello shuqi
This means that the message has been received and processed by the consumer. You can play with it.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.