1 配置RabbitMQ连接信息
spring boot遵循约定大于配置的原则,在内部它默认配置了RabbitMQ 服务端的信息(RabbitProperties .java类中)。其中默认host为localhost。下面是springboot提供的默认配置:
'org.springframework.boot:spring-boot-starter-amqp',
@ConfigurationProperties(prefix = "spring.rabbitmq")
public class RabbitProperties {
/**
* RabbitMQ host.
*/
private String host = "localhost";
/**
* RabbitMQ port.
*/
private int port = 5672;
/**
* Login user to authenticate to the broker.
*/
private String username = "guest";
/**
* Login to authenticate against the broker.
*/
private String password = "guest";
}
如果们需要自己配置的,可以在application.properties 中:
spring.rabbitmq.host=192.168.1.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
#spring.rabbitmq.virtual-host=/
2 创建配置类
@Configuration public class RabbitmqConfig { //队列bean的名称 public static final String QUEUE_CMS_POSTPAGE = "queue_cms_postpage"; //交换机的名称 public static final String EX_ROUTING_CMS_POSTPAGE="ex_routing_cms_postpage"; //队列的名称 @Value("${xysc.mq.queue}") public String queue_cms_postpage_name; //routingKey 即站点Id @Value("${xysc.mq.routingKey}") public String routingKey; @Bean(QUEUE_CMS_POSTPAGE) public Queue queue() { return new Queue(queue_cms_postpage_name); } @Bean(EX_ROUTING_CMS_POSTPAGE) public Exchange exchange() { return ExchangeBuilder.directExchange(EX_ROUTING_CMS_POSTPAGE).durable(true).build(); } /** * 绑定队列到交换机 * @param queue * @param exchange * @return */ @Bean public Binding bind(@Qualifier(QUEUE_CMS_POSTPAGE) Queue queue, @Qualifier(EX_ROUTING_CMS_POSTPAGE) Exchange exchange){ return BindingBuilder.bind(queue).to(exchange).with(routingKey).noargs(); }
@Bean("customContainerFactory") public SimpleRabbitListenerContainerFactory containerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) { SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); factory.setConcurrentConsumers(processThreads); factory.setMaxConcurrentConsumers(processThreads); configurer.configure(factory, connectionFactory); return factory; } }
3 生产者
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import java.util.concurrent.atomic.AtomicInteger;
public class TopicSender {
//由RabbitTemplate 发送消息到交换机 @Autowired
private RabbitTemplate template;
//创建定时任务,500毫秒后,每隔1秒发送一条消息
@Scheduled(fixedDelay = 1000, initialDelay = 500)
public void send() {
Map<String, String> msg = new HashMap<>();
msg.put("pageId", cmsPage.getPageId());
String jsonString = JSON.toJSONString(msg);
template.convertAndSend(RabbitmqConfig.EX_ROUTING_CMS_POSTPAGE, “5ee5e7b147b6b660b41b3ad1”, jsonString);
}
}
4 消费者
@Component
public class ConsumerPostPage {
@RabbitListener(queues = {"${xysc.mq.queue}"}, containerFactory = "customContainerFactory")
public void postPage(String msg){
Map map = JSON.parseObject(msg, Map.class);
String pageId = (String) map.get("pageId");
System.out.println("ConsumerPostPage::postPage pageId:" + pageId);
}
}