[RabbitMq] 在 spring boot中使用RabbitMq

 

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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值