RabbitMQ-Springboot整合(三)
一、发布/订阅模式(fanout整合)
依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <!--测试用--> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit-test</artifactId> <scope>test</scope> </dependency>
1、提供者
application.yml配置文件
spring: application: name: fanout-consumer rabbitmq: host: 192.168.232.119 port: 5672 username: test password: test12 virtual-host: test_mq #不配置的话默认/
配置类,声明交换机模式及交换机名、声明队列、队列绑定交换机
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.beans.factory.annotation.Configurable; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @ClassName MyRabbitMqConfig * @Author ZC * @Date 2022/7/5 11:35 * @Version 1.0 * @Description */ @Configuration public class MyRabbitMqConfig { /** * 声明创建交换机 */ @Bean public FanoutExchange fanoutExchange(){ return new FanoutExchange("fanout_exchange"); } /** * 声明队列,模拟短息消息 * @return */ @Bean public Queue smsqueue(){ return new Queue("sms.fanout.queue"); } /** * 声明队列,模拟邮件消息 * @return */ @Bean public Queue emailqueue(){ return new Queue("email.fanout.queue"); } /** * 短信队列绑定交换机 * @return */ @Bean public Binding bingsmsqueue(){ return BindingBuilder.bind(smsqueue()).to(fanoutExchange()); } @Bean public Binding bingemailqueuq(){ return BindingBuilder.bind(emailqueue()).to(fanoutExchange()); } }
发送消息
import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Date; /** * @ClassName FanoutService * @Author ZC * @Date 2022/7/5 11:53 * @Version 1.0 * @Description */ @Service public class FanoutService { private final static String EXCHANGE_NAME = "fanout_exchange"; private final static String ROUTING_KEY =""; @Autowired private RabbitTemplate rabbitTemplate; public void sendMessage(){ String content = "测试fanout"+new Date(); rabbitTemplate.convertAndSend(EXCHANGE_NAME,ROUTING_KEY,content); System.out.println("send[x]:"+content); } }
测试发送消息
import com.example.provider.service.FanoutService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @ClassName SendMessageController * @Author ZC * @Date 2022/7/5 12:05 * @Version 1.0 * @Description */ @RestController public class SendMessageController { @Autowired private FanoutService service; @RequestMapping("send") public String sendMessage(){ service.sendMessage(); return "发送成功"; } }
2、消费者
application.yml配置文件
server: port: 8011 spring: application: name: fanout-consumer rabbitmq: host: 192.168.232.119 port: 5672 username: test password: test12 virtual-host: test_mq #不配置的话默认/
编写消费者、监听消息队列
消费者1
import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Service; /** * @ClassName SmsConsumer * @Author ZC * @Date 2022/7/5 11:48 * @Version 1.0 * @Description * @RabbitListener 监听队列 */ @RabbitListener(queues = "sms.fanout.queue") @Service public class SmsConsumer { /** * 接收消息的回调方法 * @param message 需要接收消息的类型 */ @RabbitHandler public void receiveMessage(String message){ System.out.println("短息服务接收到消息:"+message); } }
消费者2
import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Service; /** * @ClassName EmailConsumer * @Author ZC * @Date 2022/7/5 11:51 * @Version 1.0 * @Description */ @RabbitListener(queues = "email.fanout.queue") @Service public class EmailConsumer { @RabbitHandler public void receiveEmailMessage(String message){ System.out.println("邮件服务接收到消息:"+message); } }
ps:发布订阅模式,所有的队列都消费
二、Routing模式(direct整合)
1、提供者
队列绑定交换机时候增加routingkey
with(routing key)
return BindingBuilder.bind(emailqueue()).to(directExchange()).with("email");
配置文件
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyRabbitMqConfig {
@Bean
public DirectExchange directExchange(){
return new DirectExchange("direct_exchange");
}
/**
* 声明队列,模拟短息消息
* @return
*/
@Bean
public Queue smsqueue(){
return new Queue("sms.direct.queue");
}
/**
* 声明队列,模拟邮件消息
* @return
*/
@Bean
public Queue emailqueue(){
return new Queue("email.direct.queue");
}
/**
* 短信队列绑定交换机
* @return
*/
@Bean
public Binding bingsmsqueue(){
return BindingBuilder.bind(smsqueue()).to(directExchange()).with("sms");
}
@Bean
public Binding bingemailqueuq(){
return BindingBuilder.bind(emailqueue()).to(directExchange()).with("email");
}
}
发送消息到指定队列-
相对于发布/订阅指定了一个routingkey
import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Date; @Service public class FanoutService { private final static String EXCHANGE_NAME = "direct_exchange"; private final static String ROUTING_KEY ="sms"; @Autowired private RabbitTemplate rabbitTemplate; public void sendMessage(){ String content = "测试"+new Date(); rabbitTemplate.convertAndSend(EXCHANGE_NAME,ROUTING_KEY,content); System.out.println("send[x]:"+content); } }
2、消费者
编写消费者、监听消息队列
消费者1
import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Service; @RabbitListener(queues = "sms.direct.queue") @Service public class SmsConsumer { /** * 接收消息的回调方法 * @param message 需要接收消息的类型 */ @RabbitHandler public void receiveMessage(String message){ System.out.println("短息服务接收到消息:"+message); } }
消费者2
import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Service; @RabbitListener(queues = "email.direct.queue") @Service public class EmailConsumer { @RabbitHandler public void receiveEmailMessage(String message){ System.out.println("邮件服务接收到消息:"+message); } }
ps: 两个消息队列都监听了,但提供者指定routingkey,通过routingk找到队列的才会消费消息
三、Topics模式(topic整合)
1、提供者修改
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyRabbitMqConfig {
/**
* 声明创建交换机
*/
// @Bean
// public FanoutExchange fanoutExchange(){
// return new FanoutExchange("fanout_exchange");
// }
// @Bean
// public DirectExchange directExchange(){
// return new DirectExchange("direct_exchange");
// }
@Bean
public TopicExchange topicExchange(){
return new TopicExchange("topic_exchange");
}
/**
* 声明队列,模拟短息消息
* @return
*/
@Bean
public Queue smsqueue(){
return new Queue("sms.topic.queue");
}
/**
* 声明队列,模拟邮件消息
* @return
*/
@Bean
public Queue emailqueue(){
return new Queue("email.topic.queue");
}
/**
* 短信队列绑定交换机
* @return
*/
@Bean
public Binding bingsmsqueue(){
return BindingBuilder.bind(smsqueue()).to(topicExchange()).with("*.stu");
}
@Bean
public Binding bingemailqueuq(){
return BindingBuilder.bind(emailqueue()).to(topicExchange()).with("#.stu.*");
}
}
发送消息
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
@Service
public class FanoutService {
private final static String EXCHANGE_NAME = "direct_exchange";
private final static String ROUTING_KEY ="sms";
@Autowired
private RabbitTemplate rabbitTemplate;
public void sendMessage(){
String exchange_name = "topic_exchange";
/**
* 短息: *.stu
* 邮件: #.stu.*
*/
String routing_key = "top.stu";
String content = "测试topic";
rabbitTemplate.convertAndSend(exchange_name,routing_key,content);
System.out.println("send[x]:"+content);
}
}