docker安装rabbitmq
Docker Hubhub.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;
}