说明
创建生产者子模块
在父项目中,创建生产者子模块:direct_producer
略
pom.xml
由于父项目已引入所有依赖包,所以pom.xml中不做任何修改
application.yml
server:
port: 8081
spring:
application:
#服务名称
name: rabbitmq-topic-producer
#RabbitMQ配置
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
#虚拟host 可以不设置,使用server默认host
# virtual-host: RabbitMqHost
RabbitMQ配置类
功能如下:
- 创建 交换机
- 创建 2个队列 topic_notice、topic_sms
- 将 交换机 分别与 两个 队列绑定,绑定时指定 routing key,含有通配符
package top.malaoshi.config;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
public static final String QUEUE_NOTICE="topic_notice";
public static final String QUEUE_SMS ="topic_sms";
public static final String EXCHANGE="topic_exchange";
public static final String KEY_NOTICE="key_notice.*";
public static final String KEY_NOTICE1="key_notice.abc";
public static final String KEY_NOTICE2="key_notice.abc.def";
public static final String KEY_SMS ="key_notice.#";
//1.创建Exchange交换器-Topic类型
@Bean
public Exchange createExchange(){
return new TopicExchange(EXCHANGE);
}
//2.创建推送消息队列
@Bean
public Queue createNoticeQueue(){
return new Queue(QUEUE_NOTICE);
}
//3.创建短信消息队列
@Bean
public Queue createOrderQueue(){
return new Queue(QUEUE_SMS);
}
//4.创建绑定对象,将 推送消息队列 与 交换器 进行绑定
@Bean
public Binding bindNotice(){
return new Binding(QUEUE_NOTICE,//队列名称
Binding.DestinationType.QUEUE,//目标类型为队列
EXCHANGE,//交换器名称
KEY_NOTICE,//还有通配符的routingKey
null);//其他参数可以不填
}
//5.创建绑定对象,将 短信队列 与 交换器 进行绑定
@Bean
public Binding bindOrder(){
return new Binding(QUEUE_SMS,//队列名称
Binding.DestinationType.QUEUE,//目标类型为队列
EXCHANGE,//交换器名称
KEY_SMS,//还有通配符的routingKey
null);//其他参数可以不填
}
}
controller
提供 2 个接口,一个是 发 推送消息,一个是 短信
package top.malaoshi.ctrl;
import com.std.config.RabbitMQConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class TestCtrl {
@Resource
private RabbitTemplate rabbitTemplate;
@RequestMapping("/sendNotice")
public String sendNotice(String msg){
//将消息发送给交换机
rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE,RabbitMQConfig.KEY_NOTICE1, msg);
return "【" + msg + "】 已发送!";
}
@RequestMapping("/sendNotice2")
public String sendNotice2(String msg){
//将消息发送给交换机
rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE,RabbitMQConfig.KEY_NOTICE2, msg);
return "【" + msg + "】 已发送!";
}
}
注意: 这里调用 rabbitTemplate.convertAndSend() 需要传 3个参数
主启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
启动服务
启动主启动类
访问 rabbitmq web管理页面
访问 http://localhost:15672/ RabbitMQ管理页面,此时 没有 消息队列
这是因为 懒加载 方式,没不会创建队列
发送消息
访问 http://localhost:8081/sendNotice?msg=lucy关注了你 ,会发送消息,消息内容是 lucy关注了你
访问 http://localhost:8081/sendNotice2?msg=lili关注了你 ,会发送消息,消息内容是 lili关注了你
访问 rabbitmq web管理页面
可以看到消息队列、交换器
