- rabbitTemplate实现自amqpTemplate接口,需引入spring-boot-starter-amqp依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
- 配置application.yml文件,我这边是配置两个mq,分别为:mq_a,mq_b
###服务启动端口号
server:
port: 8088
spring:
rabbitmq:
mq_a:
host: 15.201.x.01
port: 5672
username: admin
password: admin
mq_b:
host: 15.201.x.02
port: 60060
username: user
password: user
- 分别配置RabbitTemplate
package com.thg.config;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@Configuration
public class MqFlagByAConfig {
@Value("${spring.rabbitmq.mq_a.host}")
private String host;
@Value("${spring.rabbitmq.mq_a.port}")
private int port;
@Value("${spring.rabbitmq.mq_a.username}")
private String username;
@Value("${spring.rabbitmq.mq_a.password}")
private String password;
@Bean("mqFlagByAConfigConnectionFactory")
@Primary
public ConnectionFactory mqFlagByAConfigConnectionFactory(){
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(host);
connectionFactory.setPort(port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
return connectionFactory;
}
@Bean(name="mqaRabbitTemplate")
public RabbitTemplate mqaRabbitTemplate(@Qualifier("mqFlagByAConfigConnectionFactory") ConnectionFactory connectionFactory){
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
return rabbitTemplate;
}
}
package com.thg.config;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@Configuration
public class MqFlagByBConfig {
@Value("${spring.rabbitmq.mq_b.host}")
private String host;
@Value("${spring.rabbitmq.mq_b.port}")
private int port;
@Value("${spring.rabbitmq.mq_b.username}")
private String username;
@Value("${spring.rabbitmq.mq_b.password}")
private String password;
@Bean("mqFlagByBConfigConnectionFactory")
public ConnectionFactory mqFlagByBConfigConnectionFactory(){
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(host);
connectionFactory.setPort(port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
return connectionFactory;
}
@Bean(name="mqbRabbitTemplate")
public RabbitTemplate mqbRabbitTemplate(@Qualifier("mqFlagByBConfigConnectionFactory") ConnectionFactory connectionFactory){
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
return rabbitTemplate;
}
}
- 使用时注入自己需要的template模板,可以写一个类似这样得工具类,这样方便管理调用。
package com.thg.mq;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class SendMessage {
@Resource(name = "mqaRabbitTemplate")
private RabbitTemplate mqaRabbitTemplate;
@Resource(name = "mqbRabbitTemplate")
private RabbitTemplate mqbRabbitTemplate;
private final static String exChange_police_action ="police_action";
public void sendMessage(String message){
this.mqaRabbitTemplate.convertAndSend(
exChange_police_action,"test","A---->"+message);
this.mqbRabbitTemplate.convertAndSend("test","B---->"+message);
}
}
- 可以写个junit测试一下,我这边使用controller调用得
package com.thg.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@RestController
public class TestController {
@Autowired
private SendMessage sendMQMessage;
@RequestMapping(value = "/testMq", method = RequestMethod.POST)
public String testMq() {
sendMQMessage.sendMessage("测试测试测试!!!");
}