整合spring boot-生产者-Fanout模式
1.新建springboot项目 选择 springweb和rabbitmq依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</build>
2.service层
package com.tian.demo.service;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.UUID;
@Service
public class OrderService {
@Autowired
private RabbitTemplate rabbitTemplate;
public void makeOrder(String userid,String productid,int num){
String orderId= UUID.randomUUID().toString();
System.out.println("订单生产成功:"+orderId);
String exchangeName="fanout_order_exchange";
String routingKey="";
rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId);
}
}
3.config层
package com.tian.demo.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMqConfiguration {
@Bean
public FanoutExchange fanoutExchange(){
return new FanoutExchange("fanout_order_exchange",true,false);
}
@Bean
public Queue smsQueue(){
return new Queue("sms.fanout.queue",true);
}
@Bean
public Queue duanxinQueue(){
return new Queue("duanxin.fanout.queue",true);
}
@Bean
public Queue emailQueue(){
return new Queue("email.fanout.queue",true);
}
@Bean
public Binding smsBingding(){
return BindingBuilder.bind(smsQueue()).to(fanoutExchange());
}
@Bean
public Binding emailBingding(){
return BindingBuilder.bind(emailQueue()).to(fanoutExchange());
}
@Bean
public Binding duanxinBingding(){
return BindingBuilder.bind(duanxinQueue()).to(fanoutExchange());
}
}
4.application.yml
server:
port: 8080
spring:
rabbitmq:
username: admin
password: admin
virtual-host: /
host: 192.168.1.150
port: 5672
5.测试运行
@SpringBootTest
class DemoApplicationTests {
@Autowired
private OrderService orderService;
@Test
void contextLoads() {
orderService.makeOrder("1","1",12);
}
}
6.查看网页
整合spring boot-消费者-Fanout模式
1.新建springboot 勾选上springweb和rabbitmq依赖 同上
2.application.yml同上
3.service层
@Service
@RabbitListener(queues = {"duanxin.fanout.queue"})
public class FanoutDuanxinConsumer {
@RabbitHandler
public void receiveMessage(String message){
System.out.println("duanxin fanout--->接收到了订单信息是->"+message);
}
}
@Service
@RabbitListener(queues = {"email.fanout.queue"})
public class FanoutEmailConsumer {
@RabbitHandler
public void receiveMessage(String message){
System.out.println("email fanout--->接收到了订单信息是->"+message);
}
}
@Service
@RabbitListener(queues = {"sms.fanout.queue"})
public class FanoutSMSConsumer {
@RabbitHandler
public void receiveMessage(String message){
System.out.println("sms fanout--->接收到了订单信息是->"+message);
}
}
4.主启动类运行 控制台收到消息
sms fanout--->接收到了订单信息是->2531c6ab-18be-4d5f-b880-835533c41dd4
email fanout--->接收到了订单信息是->2531c6ab-18be-4d5f-b880-835533c41dd4
duanxin fanout--->接收到了订单信息是->2531c6ab-18be-4d5f-b880-835533c41dd4
整合spring boot-Direct模式
consumer config层
package com.tian.demo.config;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DirectRabbitMqConfiguration {
@Bean
public DirectExchange directExchange(){
return new DirectExchange("direct_order_exchange",true,false);
}
@Bean
public Queue directsmsQueue(){
return new Queue("sms.direct.queue",true);
}
@Bean
public Queue directduanxinQueue(){
return new Queue("duanxin.direct.queue",true);
}
@Bean
public Queue directemailQueue(){
return new Queue("email.direct.queue",true);
}
@Bean
public Binding directsmsBingding(){
return BindingBuilder.bind(directsmsQueue()).to(directExchange()).with("sms");
}
@Bean
public Binding directemailBingding(){
return BindingBuilder.bind(directemailQueue()).to(directExchange()).with("email");
}
@Bean
public Binding directduanxinBingding(){
return BindingBuilder.bind(directduanxinQueue()).to(directExchange()).with("duanxin");
}
}
service层
@Service
@RabbitListener(queues = {"duanxin.direct.queue"})
public class DirectDuanxinConsumer {
@RabbitHandler
public void receiveMessage(String message){
System.out.println("duanxin direct--->接收到了订单信息是->"+message);
}
}
@Service
@RabbitListener(queues = {"email.direct.queue"})
public class DirectEmailConsumer {
@RabbitHandler
public void receiveMessage(String message){
System.out.println("email direct--->接收到了订单信息是->"+message);
}
}
@Service
@RabbitListener(queues = {"sms.direct.queue"})
public class DirectSMSConsumer {
@RabbitHandler
public void receiveMessage(String message){
System.out.println("sms direct--->接收到了订单信息是->"+message);
}
}
生产者 service层
public void makeOrderDirect(String userid,String productid,int num){
String orderId= UUID.randomUUID().toString();
System.out.println("订单生产成功:"+orderId);
String exchangeName="direct_order_exchange";
rabbitTemplate.convertAndSend(exchangeName,"email",orderId);
rabbitTemplate.convertAndSend(exchangeName,"duanxin",orderId);
}
测试访问
@Autowired
private OrderService orderService;
@Test
void contextLoads1() {
orderService.makeOrderDirect("1","1",12);
}
只有 邮件和短信收到服务
整合spring boot-Topic模式
@Service
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "duanxin.topic.queue",durable = "true",autoDelete = "false"),
exchange = @Exchange(value = "topic_order_exchange",type = ExchangeTypes.TOPIC),
key = "#.duanxin.#"
))
public class TopicDuanxinConsumer {
@RabbitHandler
public void receiveMessage(String message){
System.out.println("duanxin topic--->接收到了订单信息是->"+message);
}
}
@Service
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "email.topic.queue",durable = "true",autoDelete = "false"),
exchange = @Exchange(value = "topic_order_exchange",type = ExchangeTypes.TOPIC),
key = "*.email.#"
))
public class TopicEmailConsumer {
@RabbitHandler
public void receiveMessage(String message){
System.out.println("email topic--->接收到了订单信息是->"+message);
}
}
@Service
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "sms.topic.queue",durable = "true",autoDelete = "false"),
exchange = @Exchange(value = "topic_order_exchange",type = ExchangeTypes.TOPIC),
key = "com.#"
))
public class TopicSMSConsumer {
@RabbitHandler
public void receiveMessage(String message){
System.out.println("sms topic--->接收到了订单信息是->"+message);
}
}
producer端 service层
public void makeOrderTopic(String userid,String productid,int num){
String orderId= UUID.randomUUID().toString();
System.out.println("订单生产成功:"+orderId);
String exchangeName="topic_order_exchange";
String routingKey="com.duanxin";
rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId);
测试
@Autowired
private OrderService orderService;
@Test
void contextLoads2() {
orderService.makeOrderTopic("1","1",12);
}
只有sms 和 短信接收到服务
|