1.rabiimq 安装:
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
生产者乙方:
@Autowired
private RabbitTemplate rabbitTemplate;
public void sendMsg(String msg) {
Map<String,Object> sendContext = new HashMap<>();
sendContext.put("name","张三");
sendContext.put("sex","男");
sendContext.put("msg",msg);
rabbitTemplate.convertAndSend("tulingDirectExchange","tuling.directkey",sendContext);
}
/**
* 发送到扇形交换机上
* @param msg
*/
public void sendMsg2Fanout(String msg) {
rabbitTemplate.convertAndSend("tulingFanoutExchange","aaaabbdd",msg);
}
消费者乙方:
@Component public class TulingConsumer { @RabbitListener(queues ="DirectQueue") public void consumerMsg(Message message) { System.out.println("消费消息:"+message.getPayload().toString()); }
3.配置参数:
@Bean
public MessageConverter messageConverter() {
return new Jackson2JsonMessageConverter();
}
/**
* 直接交换机
* @return
*/
@Bean
public DirectExchange tulingDirectExchange() {
//参数 durable:表示消息是否可持久化
//autoDelete:表示若没有队列和此交换机绑定 就直接删除该交换机
return new DirectExchange("tulingDirectExchange",true,false);
}
@Bean
public Queue tulingDirectQueue() {
return new Queue("tulingDirectQueue",true,false,false);
}
@Bean
public Binding tulingDq2De(){
return BindingBuilder.bind(tulingDirectQueue()).to(tulingDirectExchange()).with("tuling.directkey");
}
/**
* 扇形交换机
* @return
*/
@Bean
public FanoutExchange tulingFanoutExchange() {
return new FanoutExchange("tulingFanoutExchange",true,false);
}
@Bean
public Queue tulingfanoutQueue1() {
return new Queue("tulingFanoutQueue1",true,false,false);
}
@Bean
public Queue tulingfanoutQueue2() {
return new Queue("tulingFanoutQueue2",true,false,false);
}
@Bean
public Binding tulingBind1() {
return BindingBuilder.bind(tulingfanoutQueue1()).to(tulingFanoutExchange());
}
@Bean
public Binding tulingBind2() {
return BindingBuilder.bind(tulingfanoutQueue2()).to(tulingFanoutExchange());
}
/**
* 主题交换机
*/
@Bean
public TopicExchange tulingTopicExchange() {
return new TopicExchange("tulingTopicExchange",true,false);
}
@Bean
public Queue tulingTopicQueue() {
return new Queue("tulingTopicQueue",true,false,false);
}
@Bean
public Queue tulingTopicQueue2() {
return new Queue("tulingTopicQueue2",true,false,false);
}
@Bean
public Binding topicBind1(){
return BindingBuilder.bind(tulingTopicQueue()).to(tulingTopicExchange()).with("topic.key.#");
}
@Bean
public Binding topicBind2(){
return BindingBuilder.bind(tulingTopicQueue2()).to(tulingTopicExchange()).with("#.key");
}
4.配置连接参数:
spring:
rabbitmq:
host: 47.104.128.12
port: 5672
virtual-host: tulingVip
username: guest
password: guest
connection-timeout: 10000
template:
mandatory: true