RabbitMQ简介与springboot整合

// 创建队列

@Bean

public Queue topicQueue() {

return new Queue(“topic.mess”);

}

}

2.生产者

直接配置一个队列,然后调用API发送消息就可以了。

public class ProducerController {

@Autowired

private RabbitTemplate rabbitTemplate;

@GetMapping(“/sendMessage”)

public Object sendMessage() {

new Thread(() -> {

//for (int i = 0; i < 100; i++) {

Date date = new Date();

String value = new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”).format(date);

System.out.println(“send message {}” + value);

City city= new City();

city.setCityName(“aaaa”);

city.setDescription(“bbb”);

city.setProvinceId((long)111);

rabbitTemplate.convertAndSend(“topic.mess”, city); //使用默认的队列

//}

}).start();

return “ok”;

}

}

3.消费者

消费者直接使用就可以了(可以传对象 基本类型)。需要@RabbitListener注解。

@Component

@RabbitListener(queues = “topic.mess”) //topic交换机

public class Consumer2 {

@RabbitHandler

public void consumeMessage(City city) {

System.out.println(“consume message {} 2222222:” + city);

}

}

2.topic交换机
1.新建队列与绑定关系

声名两个队列和一个topic交换器,然后通过路由键绑定他们之间的关系,路由键和队列名相同就能匹配,但是topic可以模糊匹配 #可以代替一段字符。

@Configuration

public class RabbitMQConfig {

// -------------------------topic队列

// 创建队列

@Bean

public Queue topicQueue() {

return new Queue(“topic.mess”);

}

@Bean

public Queue topicQueue2() {

return new Queue(“topic.mess2”);

}

// 创建 topic 类型的交换器

@Bean

public TopicExchange topicExchange() {

return new TopicExchange(“topic”);

}

// 使用路由键(routingKey)把队列(Queue)绑定到交换器(Exchange) Topic交换器通过routingKey与队列绑定

@Bean

public Binding bindingA(Queue topicQueue, TopicExchange topicExchange) {

return BindingBuilder.bind(topicQueue).to(topicExchange).with(“topic.mess”);

}

@Bean

public Binding bindingB(Queue topicQueue2, TopicExchange topicExchange) {

return BindingBuilder.bind(topicQueue2).to(topicExchange).with(“topic.#”);

}

}

2.生产者

直接调用API发送消息。消费者发送到队列,因为有模糊匹配的规则,topic.mess可以匹配 topic.mess和topic.mess2队列 而topic.mess2只能匹配到topic.#。

@RestController

public class ProducerController {

@Autowired

private RabbitTemplate rabbitTemplate;

@GetMapping(“/sendMessage”)

public Object sendMessage() {

new Thread(() -> {

//for (int i = 0; i < 100; i++) {

Date date = new Date();

String value = new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”).format(date);

System.out.println(“send message {}” + value);

City city= new City();

city.setCityName(“aaaa”);

city.setDescription(“bbb”);

city.setProvinceId((long)111);

rabbitTemplate.convertAndSend(“topic”, “topic.mess”, city);

rabbitTemplate.convertAndSend(“topic”, “topic.mess2”, city);

//}

}).start();

return “ok”;

}

}

3.消费者

消费者直接接收。

@Component

@RabbitListener(queues = “topic.mess”) //topic交换机

public class Consumer2 {

@RabbitHandler

public void consumeMessage(City city) {

System.out.println(“consume message {} 2222222:” + city);

}

}

3.Fanout Exchange 广播
1.新建队列与绑定关系

在配置文件中声名队列和交换器,然后绑定。

// --------FanoutExchange绑定

// -------------------------Fanout 队列

@Bean

FanoutExchange fanoutExchange() {

return new FanoutExchange(“fanoutExchange”);

}

@Bean

public Queue fanoutQueue() {

return new Queue(“fanoutqueue”);

}

@Bean

public Queue fanoutQueue2() {

return new Queue(“fanoutqueue2”);

}

@Bean

public Binding bindingC(Queue fanoutQueue, FanoutExchange fanoutExchange) {

return BindingBuilder.bind(fanoutQueue).to(fanoutExchange);

}

@Bean

public Binding bindingD(Queue fanoutQueue2, FanoutExchange fanoutExchange) {

return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);

}

2.生产者

调用API发送消息。

@RestController

public class ProducerController {

@Autowired

private RabbitTemplate rabbitTemplate;

@GetMapping(“/sendMessage”)

public Object sendMessage() {

new Thread(() -> {

//for (int i = 0; i < 100; i++) {

Date date = new Date();

String value = new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”).format(date);

System.out.println(“send message {}” + value);

City obj = new City();

obj.setCityName(“aaaa”);

obj.setDescription(“bbb”);

obj.setProvinceId((long)111);

rabbitTemplate.convertAndSend(“fanoutExchange”,“”, value); //使用默认的队列

//}

}).start();

return “ok”;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值