spring boot @value_spring-boot使用rabbitmq

v2-ae66ff851b773eda2931259fd094f88c_1440w.jpg?source=172ae18b

docker安装rabbitmq

Docker Hub​hub.docker.com
  • docker pull rabbitmq:3.8.3-beta.2-management-alpine
management带后台 alpine版(微型包)
  • docker run -d --hostname my-rabbit --name some-rabbit -p 15672:15672 -p 5672:5672 -e RABBITMQ_DEFAULT_USER=user -e RABBITMQ_DEFAULT_PASS=admin123 rabbitmq:3.8.3-beta.2-management-alpine
修改用户名密码
RABBITMQ_DEFAULT_USER
RABBITMQ_DEFAULT_PASS

spring-boot引入rabbitmq

  • 引入amqp包
<dependency>
   

rabbitmq基本使用

  • 配置环境 (application.properties)
#rabbitmq配置
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=user
spring.rabbitmq.password=admin123
  • RabbitConfiguration
import 
  • UserController
@RestController

rabbitmq topic使用

  • 配置环境 (application.properties)
#rabbitmq配置
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=user
spring.rabbitmq.password=admin123

spring.rabbitmq.config.queue.info=topic-info-queue
spring.rabbitmq.config.queue.warn=topic-warn-queue
spring.rabbitmq.config.queue.exchange=topic-queue-exchange
  • RabbitListenInfoTopic
import 
  • RabbitListenWarnTopic
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(bindings = {@QueueBinding(value = @Queue(value = "${spring.rabbitmq.config.queue.warn}",autoDelete = "false"),
        exchange=@Exchange(value = "${spring.rabbitmq.config.queue.exchange}",type = ExchangeTypes.TOPIC),
        key = "*.log.warn")})
public class RabbitListenWarnTopic {

    @RabbitHandler
    public void process(String msg){
        System.out.println("Warn ............receiver" +msg);
    }
}
  • controller
/**
     * rabbitmq2 topic模式
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "rabbitmq2", notes = "")
    @RequestMapping(value = "rabbitmq2", method = RequestMethod.POST)
    public String rabbitmq2() throws Exception{
        rabbitTemplate.convertAndSend("topic-queue-exchange","order.log.info","order info send ...");
        rabbitTemplate.convertAndSend("topic-queue-exchange","order.log.warn","order warn send ...");
        rabbitTemplate.convertAndSend("topic-queue-exchange","sku.log.info","sku info send ...");
        rabbitTemplate.convertAndSend("topic-queue-exchange","sku.log.warn","sku warn send ...");
        return null;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值