spring boot的环境怎么搭建这边就不提了, 这里引入spring boot -AMQP的依赖
mq使用场景
异步处理
同一个应用中可以使用异步任务,但是不通的应用中就没办法解决
应用解耦
流量肖锋
比如双十一时,这几天政府发的消费券,在线抢的人不再少数
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
配置连接,创建交换机,队列
首先 我和上面一样 先要配置连接信息这里 可以选用yml的方式 也可以选用javaConfig的方式 这里两种方式我都贴出来 你们自己选
yml: 参数什么意思刚刚介绍过了 这里吧你自己的参数填进去就好了
yml 方式
rabbitmq:
host: Ip
port: 5672
username: test
password: test
virtual-host: test
这样 spring boot 会帮你把rabbitmq其他相关的东西都自动装备好
Hello
生产者
@SpringBootTest(classes = EcsApplication.class)
@RunWith(SpringRunner.class)
public class Provider {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void send(){
rabbitTemplate.convertAndSend("hello","springboot hello 模型发送消息");
}
消费者
@Component
//@RabbitListener(queuesToDeclare = @Queue(value = "hello",autoDelete = "false",durable = "false"))
@RabbitListener(queuesToDeclare = @Queue("hello"))
public class HelloConsumer {
@RabbitHandler
public void hello(String msg){
System.out.println("========================springboot hello "+msg);
}
}
work
生成者
@Test
public void sendwork(){
for (int i = 0; i < 10; i++) {
rabbitTemplate.convertAndSend("work","springboot hello 模型发送消息");
}
}
消费者
@Component
public class WorkConsumer {
@RabbitListener(queuesToDeclare = @Queue("work"))
public void rece(String msg) throws InterruptedException {
Thread.sleep(100);
System.out.println("消费者1=============="+ msg);
}
@RabbitListener(queuesToDeclare = @Queue("work"))
public void rece2(String msg) throws InterruptedException {
Thread.sleep(100);
System.out.println("消费者2======="+ msg);
}
}
fanout
生成者
@Test
public void sendfanout(){
rabbitTemplate.convertAndSend("logs","","fanout 模型消息");
}
消费者
@Component
public class FanoutConsumer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(value = "logs",type = "fanout")
)
})
public void rece(String msg){
System.out.println(msg);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(value = "logs",type = "fanout")
)
})
public void rece2(String msg){
System.out.println(msg);
}
}
direct
生成者
@Test
public void senddirect(){
rabbitTemplate.convertAndSend("directs","user.save","directs 模型消息 user");
rabbitTemplate.convertAndSend("directs","order.save","directs 模型消息 order");
}
消费者
@Component
public class DirectConsumer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(name = "directs",type = "direct"),
key = {"user.save"}
)
})
public void rece(String msg){
System.out.println("消费者1:===="+msg);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(name = "directs",type = "direct"),
key = {"order.save"}
)
})
public void rece1(String msg){
System.out.println("消费者2:===="+msg);
}
}
topic
生成者
@Test
public void sendtopic(){
rabbitTemplate.convertAndSend("topics","user.save","topic 模型消息 user");
rabbitTemplate.convertAndSend("topics","order.save","topic 模型消息 order");
}
消费者
@Component
public class TopicConsumer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(name = "topics",type = "topic"),
key = {"user.save","user.*"}
)
})
public void rece(String msg){
System.out.println("消费者1:===="+msg);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(name = "topics",type = "topic"),
key = {"user.*","order.*"}
)
})
public void rece1(String msg){
System.out.println("消费者2:===="+msg);
}
}
配置方式
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost",5672);
//我这里直接在构造方法传入了
// connectionFactory.setHost();
// connectionFactory.setPort();
connectionFactory.setUsername("admin");
connectionFactory.setPassword("admin");
connectionFactory.setVirtualHost("testhost");
//是否开启消息确认机制
//connectionFactory.setPublisherConfirms(true);
return connectionFactory;
}
配置完连接之后 我们就可以开始发送消息和接收消息了(因为我们上面刚刚测试rabbitmq的时候创建过队列和交换机等等这种东西了 当然 spring boot也可以创建)
spring boot创建交换机 队列 并绑定:
@Bean
public DirectExchange defaultExchange() {
return new DirectExchange("directExchange");
}
@Bean
public Queue queue() {
//名字 是否持久化
return new Queue("testQueue", true);
}
@Bean
public Binding binding() {
//绑定一个队列 to: 绑定到哪个交换机上面 with:绑定的路由建(routingKey)
return BindingBuilder.bind(queue()).to(defaultExchange()).with("direct.key");
}
发送消息:
发送消息比较简单, spring 提供了一个RabbitTemplate 来帮助我们完成发送消息的操作
如果想对于RabbitTemplate 这个类进行一些配置(至于有哪些配置我们后面会讲到) 我们可以在config类中 吧他作为Bean new出来并配置
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
//注意 这个ConnectionFactory 是使用javaconfig方式配置连接的时候才需要传入的 如果是yml配置的连接的话是不需要的
RabbitTemplate template = new RabbitTemplate(connectionFactory);
return template;
}
@Component
public class TestSend {
@Autowired
RabbitTemplate rabbitTemplate;
public void testSend() {
//至于为什么调用这个API 后面会解释
//参数介绍: 交换机名字,路由建, 消息内容
rabbitTemplate.convertAndSend("directExchange", "direct.key", "hello");
}
}
我们只需要写一个类 然后交给spring 管理 在类里面注入RabbitTemplate 就可以直接调用api来发送消息了
接受消息:
这里我新建了一个项目(最好新建一个 当然 不新建也没关系) 来接收信息(之前的配置这里就不贴出来了 和上面基本一样),
@Component
public class TestListener {
@RabbitListener(queues = "testQueue")
public void get(String message) throws Exception{
System.out.println(message);
}
}