前言
一直没机会做spring生态圈的框架,公司选择的是一些小众的微服务,鉴于此考虑,丰富自己的技术栈,花了两天时间从网上各网站上学习了springboot一些基础知识。
本章只介绍springboot微服务集成RabbitMQ,用于通过消息中间件给其他微服务发送消息。
环境准备
- IntelliJ IDEA
- 前一章中搭建的微服务框架
开始集成
-
pom.xml中增加依赖包
依赖包.png
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
-
在application.yml中增加rabbit服务端信息:
yml配置.png
spring:
#...
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
-
增加rabbit的配置类,定义所需要绑定的queue,exchange,routingkey,binding等
RabbitConfig.png
package com.example.demo;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 类功能描述:<br>
* Broker:它提供一种传输服务,它的角色就是维护一条从生产者到消费者的路线,保证数据能按照指定的方式进行传输,
* Exchange:消息交换机,它指定消息按什么规则,路由到哪个队列。
* Queue:消息的载体,每个消息都会被投到一个或多个队列。
* Binding:绑定,它的作用就是把exchange和queue按照路由规则绑定起来.
* Routing Key:路由关键字,exchange根据这个关键字进行消息投递。
* vhost:虚拟主机,一个broker里可以有多个vhost,用作不同用户的权限分离。
* Producer:消息生产者,就是投递消息的程序.
* Consumer:消息消费者,就是接受消息的程序.
* Channel:消息通道,在客户端的每个连接里,可建立多个channel.
* <ul>
* <li>类功能描述1<br>
* <li>类功能描述2<br>
* <li>类功能描述3<br>
* </ul>
* 修改记录:<br>
* <ul>
* <li>修改记录描述1<br>
* <li>修改记录描述2<br>
* <li>修改记录描述3<br>
* </ul>
*
* @author xuefl
* @version 5.0 since 2020-01-02
*/
@Configuration
public class RabbitConfig {
//队列名
public static final String FANOUT_QUEUE_NAME = "test_fanout_queue";
public static final String FANOUT_QUEUE_NAME1 = "test_fanout_queue1";
public static final String TEST_FANOUT_EXCHANGE = "testFanoutExchange";
public static final String DIRECT_QUEUE_NAME = "test_direct_queue";
public static final String TEST_DIRECT_EXCHANGE = "testDirectExchange";
public static final String DIRECT_ROUTINGKEY = "test";
public static final String TOPIC_QUEUE_NAME = "test_topic_queue";
public static final String TEST_TOPIC_EXCHANGE = "testTopicExchange";
public static final String TOPIC_ROUTINGKEY = "test.*";
//创建队列
@Bean
public Queue createFanoutQueue() {
return new Queue(FANOUT_QUEUE_NAME);
}
//创建队列
@Bean
public Queue createFanoutQueue1() {
return new Queue(FANOUT_QUEUE_NAME1);
}
//创建队列
@Bean
public Queue createDirectQueue() {
return new Queue(DIRECT_QUEUE_NAME);
}
//创建队列
@Bean
public Queue createTopicQueue() {
return new Queue(TOPIC_QUEUE_NAME);
}
//创建交换机
@Bean
public FanoutExchange defFanoutExchange() {
return new FanoutExchange(TEST_FANOUT_EXCHANGE);
}
//队列与交换机进行绑定
@Bean
Binding bindingFanout() {
return BindingBuilder.bind(createFanoutQueue()).
to(defFanoutExchange());
}
//队列与交换机进行绑定
@Bean
Binding bindingFanout1() {
return BindingBuilder.bind(createFanoutQueue1()).
to(defFanoutExchange());
}
@Bean
DirectExchange directExchange(){
return new DirectExchange(TEST_DIRECT_EXCHANGE);
}
@Bean
Binding bindingDirect() {
return BindingBuilder.bind(createDirectQueue()).
to(directExchange()).
with(DIRECT_ROUTINGKEY);
}
@Bean
TopicExchange defTopicExchange(){
return new TopicExchange(TEST_TOPIC_EXCHANGE);
}
@Bean
Binding bindingTopic() {
return BindingBuilder.bind(createTopicQueue()).
to(defTopicExchange()).
with(TOPIC_ROUTINGKEY);
}
}
此处定义了三种类型的queue,exchange,和routingkey,预先定义,并且将队列,绑定到指定Exchange上,定义其routingKey。并使用@Bean注解定义为实体
-
在demo下新增rabbitmq包,在其下新建MsgProducer和MsgConsumer类,定义消费者和生产者,用于消费队列和发送消息(此处为了自测,自己给自己发送消息,并由自己消费消息)
消费者.png
生产者.png
package com.example.demo.rabbitmq;
import com.example.demo.RabbitConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
/**
* 类功能描述:<br>
* <ul>
* <li>类功能描述1<br>
* <li>类功能描述2<br>
* <li>类功能描述3<br>
* </ul>
* 修改记录:<br>
* <ul>
* <li>修改记录描述1<br>
* <li>修改记录描述2<br>
* <li>修改记录描述3<br>
* </ul>
*
* @author xuefl
* @version 5.0 since 2020-01-02
*/
@Component
@Slf4j
public class MsgConsumer {
@RabbitListener(
bindings =
{
@QueueBinding(value = @Queue(value = RabbitConfig.FANOUT_QUEUE_NAME, durable = "true"),
exchange = @Exchange(value = RabbitConfig.TEST_FANOUT_EXCHANGE, type = "fanout"))
})
@RabbitHandler
public void processFanoutMsg(Message massage) {
String msg = new String(massage.getBody(), StandardCharsets.UTF_8);
log.info("received Fanout message : " + msg);
}
@RabbitListener(
bindings =
{
@QueueBinding(value = @Queue(value = RabbitConfig.FANOUT_QUEUE_NAME1, durable = "true"),
exchange = @Exchange(value = RabbitConfig.TEST_FANOUT_EXCHANGE, type = "fanout"))
})
@RabbitHandler
public void processFanout1Msg(Message massage) {
String msg = new String(massage.getBody(), StandardCharsets.UTF_8);
log.info("received Fanout1 message : " + msg);
}
@RabbitListener(
bindings =
{
@QueueBinding(value = @Queue(value = RabbitConfig.DIRECT_QUEUE_NAME, durable = "true"),
exchange = @Exchange(value = RabbitConfig.TEST_DIRECT_EXCHANGE),
key = RabbitConfig.DIRECT_ROUTINGKEY)
})
@RabbitHandler
public void processDirectMsg(Message massage) {
String msg = new String(massage.getBody(), StandardCharsets.UTF_8);
log.info("received Direct message : " + msg);
}
@RabbitListener(
bindings =
{
@QueueBinding(value = @Queue(value = RabbitConfig.TOPIC_QUEUE_NAME, durable = "true"),
exchange = @Exchange(value = RabbitConfig.TEST_TOPIC_EXCHANGE, type = "topic"),
key = RabbitConfig.TOPIC_ROUTINGKEY)
})
@RabbitHandler
public void processTopicMsg(Message massage) {
String msg = new String(massage.getBody(), StandardCharsets.UTF_8);
log.info("received Topic message : " + msg);
}
}
package com.example.demo.rabbitmq;
import com.example.demo.RabbitConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 类功能描述:<br>
* <ul>
* <li>类功能描述1<br>
* <li>类功能描述2<br>
* <li>类功能描述3<br>
* </ul>
* 修改记录:<br>
* <ul>
* <li>修改记录描述1<br>
* <li>修改记录描述2<br>
* <li>修改记录描述3<br>
* </ul>
*
* @author xuefl
* @version 5.0 since 2020-01-02
*/
@Component
public class MsgProducer {
@Autowired
private RabbitTemplate rabbitTemplate;
public void send2FanoutTestQueue(String massage){
rabbitTemplate.convertAndSend(RabbitConfig.TEST_FANOUT_EXCHANGE,
"", massage);
}
public void send2DirectTestQueue(String massage){
rabbitTemplate.convertAndSend(RabbitConfig.TEST_DIRECT_EXCHANGE,
RabbitConfig.DIRECT_ROUTINGKEY, massage);
}
public void send2TopicTestAQueue(String massage){
rabbitTemplate.convertAndSend(RabbitConfig.TEST_TOPIC_EXCHANGE,
"test.aaa", massage);
}
public void send2TopicTestBQueue(String massage){
rabbitTemplate.convertAndSend(RabbitConfig.TEST_TOPIC_EXCHANGE,
"test.bbb", massage);
}
}
消费者类中通过@RabbitListener和@RabbitHandler注解将一个方法定义为消息监听的方法,使用方法如下
注解.png
@RabbitListener可以通过定义bindings={@QueueBinding},@QueueBinding可以通过赋值value=@Queue(value = RabbitConfig.FANOUT_QUEUE_NAME, durable = "true")定义出消息队列名,@QueueBinding也可以通过赋值exchange=@Exchange(value = RabbitConfig.TEST_FANOUT_EXCHANGE, type = "fanout")来定义当前方法所监听的Exchange,以及类型,类型默认是direct
-
定义controller,在controller下新建RabbitMQController类,定义几个接口
RabbitMQController.png
package com.example.demo.controller;
import com.example.demo.rabbitmq.MsgProducer;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* 类功能描述:<br>
* <ul>
* <li>类功能描述1<br>
* <li>类功能描述2<br>
* <li>类功能描述3<br>
* </ul>
* 修改记录:<br>
* <ul>
* <li>修改记录描述1<br>
* <li>修改记录描述2<br>
* <li>修改记录描述3<br>
* </ul>
*
* @author xuefl
* @version 5.0 since 2020-01-02
*/
@RequestMapping("/rabbit")
@Controller
@Slf4j
@Api(value = "SwaggerValue", tags={"RabbitMQController"},description = "swagger应用", produces = MediaType.APPLICATION_JSON_VALUE)
public class RabbitMQController {
@Autowired
private MsgProducer msgProducer;
@GetMapping(value = "/sendFanout")
@ResponseBody
@Transactional(rollbackFor = Exception.class)
public void sendMsg(){
msgProducer.send2FanoutTestQueue("this is a test fanout message!");
}
@GetMapping(value = "/sendDirect")
@ResponseBody
@Transactional(rollbackFor = Exception.class)
public void sendDirectMsg(){
msgProducer.send2DirectTestQueue("this is a test direct message!");
}
@GetMapping(value = "/sendDirectA")
@ResponseBody
@Transactional(rollbackFor = Exception.class)
public void sendTopicAMsg(){
msgProducer.send2TopicTestAQueue("this is a test topic aaa message!");
}
@GetMapping(value = "/sendTopicB")
@ResponseBody
@Transactional(rollbackFor = Exception.class)
public void sendTopicBMsg(){
msgProducer.send2TopicTestBQueue("this is a test topic bbb message!");
}
}
此接口用于自测试,测试消息发送。
- 启动服务,通过接口开始测试,打开swagger界面,http://localhost:8081/api
运行日志.png
此教程仅提供了rabbitmq的初级用法。
网友评论