前文总结了,本文基于这套框架,引入RabbitMQ消息队列。
1.在linux环境使用docker安装RabbitMQ
//拉取镜像
docker pull rabbitmq:management
//启动镜像
docker run -di --name myrabbit -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin -p 15672:15672 -p5672:5672 -p 25672:25672 -p 61613:61613 -p 1883:1883 rabbitmq:management
安装成功后登陆控制台
2.生产者
pom.xml中引入RabbitMQ依赖
!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-amqp --
dependency
groupIdorg.springframework.boot/groupId
artifactIdspring-boot-starter-amqp/artifactId
version1.3.5.RELEASE/version
/dependency
配置RabbitMQ服务端连接,和交换机名称
application.yml
spring:
rabbitmq:
port: 5672
username: admin
password: admin
host: 192.168.200.128
virtual-host: /
order:
fanout:
exchange: order.fanout.exchange
生产者向MQ发送消息
Producer.java
package com.cott.gmail.bootuserserviceprovider.mq;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.UUID;
@Component
public class Producer {
@Autowired
private RabbitTemplate rabbitTemplate;
@Value("${order.fanout.exchange}")
private String exchangeName;
public void sendMsg() {
String orderId = UUID.randomUUID().toString();
String message = "你的订单信息是:" + orderId + ",日期是:" + new Date();
rabbitTemplate.convertAndSend(exchangeName, message);
}
}
在用户接口的实现类中,调用生产者的方法
UserServiceImpl.java
package com.cott.gmail.bootuserserviceprovider.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.cott.gmail.bean.UserAddress;
import com.cott.gmail.bootuserserviceprovider.mq.Producer;
import com.cott.gmail.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
@Service
@Component
public class UserServiceImpl implements UserService {
@Autowired
Producer producer;
@Override
public ListUserAddress getAddress(String uesrId) {
producer.sendMsg();
UserAddress userAddress1 = new UserAddress();
userAddress1.setId(1);
userAddress1.setUserAddress("1");
userAddress1.setUserId("1");
UserAddress userAddress2 = new UserAddress();
userAddress2.setId(2);
userAddress2.setUserAddress("2");
userAddress2.setUserId("2");
return Arrays.asList(userAddress1, userAddress2);
}
}
3.消费者
pom.xml中引入RabbitMQ依赖
!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-amqp --
dependency
groupIdorg.springframework.boot/groupId
artifactIdspring-boot-starter-amqp/artifactId
version1.3.5.RELEASE/version
/dependency
配置RabbitMQ服务端信息,和交换机以及队列名称
application.yml
spring:
rabbitmq:
port: 5672
username: admin
password: admin
host: 192.168.200.128
virtual-host: /
order:
fanout:
exchange: order.fanout.exchange
queue: order.fanout.queue
@RabbitListener注解,绑定队列,交换机,以及选择交换模式FANOUT
@RabbitListener 标注在类上面表示当有收到消息的时候,就交给 @RabbitHandler 的方法处理,具体使用哪个方法处理,根据 MessageConverter 转换后的参数类型
Consumer.java(消费者)
package com.cott.gmail.bootorderserviceconsumer.service.impl;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(bindings = @QueueBinding(value = @Queue(value = "${order.fanout.queue}", autoDelete = "true"), exchange =
@Exchange(value = "${order.fanout.exchange}", type = ExchangeTypes.FANOUT)))
public class Consumer {
@RabbitHandler
public void processMessage(String msg) {
System.out.format("Receiving Message: -----[%s]----- n.", msg);
}
}
消费者中有调用生产者getAddress()的方法,生产者向MQ中发送一条消息,同时消费者监听MQ,消费消息。
OrderServiceImpl.java
package com.cott.gmail.bootorderserviceconsumer.service.impl;
import com.alibaba.dubbo.config.annotation.Reference;
import com.cott.gmail.bean.UserAddress;
import com.cott.gmail.service.OrderService;
import com.cott.gmail.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class OrderServiceImpl implements OrderService {
@Reference
UserService userService;
@Override
public ListUserAddress initOrder(String id) {
System.out.println("id= " + id);
ListUserAddress list = userService.getAddress("1");
for (UserAddress user : list
) {
System.out.println(user.getUserAddress());
}
return list;
}
}
4.验证
先后启动生产者和消费者,在浏览器发起请求
消费者控制台输出
同时在RabbitMQ控制台查看监控日志
END
Java面试题专栏
我知道你 “在看”
原文始发于微信公众号(Java知音):