案例一direct:
生产者
spring.application.name=rabbits-provider #rabbits spring.rabbitmq.host=127.0.0.1 spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest
#exchange name mq.config.exchange=log.direct mq.config.queue.info.routing.key=log.info.routing.key mq.config.queue.error.routing.key=log.error.routing.key
|
@Component public class Sender {
@Autowired private AmqpTemplate rabbitTmplate;
@Value("${mq.config.exchange}") private String exchange;
@Value("${mq.config.queue.info.routing.key}") private String infoRouting;
@Value("${mq.config.queue.error.routing.key}") private String errorRouting;
public void send(String msg){ System.out.println("发送到队列。。。。"); //这个是重载的方法 this.rabbitTmplate.convertAndSend(exchange, infoRouting, "发送info"); } }
|
消费者
#rabbits spring.rabbitmq.host=127.0.0.1 spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest
#exchange name mq.config.exchange=log.direct #queue name and rout-key mq.config.queue.info=log.info mq.config.queue.info.routing.key=log.info.routing.key
mq.config.queue.error=log.error mq.config.queue.error.routing.key=log.error.routing.key |
@Component @RabbitListener( bindings=@QueueBinding( value=@Queue(value="${mq.config.queue.info}",autoDelete="true"), exchange = @Exchange(value="${mq.config.exchange}",type=ExchangeTypes.DIRECT), key="${mq.config.queue.info.routing.key}" ) ) public class InfoRecevier {
/** * 接收消息的方法,采用消息队列监听器 * */ //queues队列名+value值msg @RabbitHandler public void process(String msg){ System.out.println("Info...Recevier:"+msg); } }
|
@Component @RabbitListener( bindings=@QueueBinding( value=@Queue(value="${mq.config.queue.error}",autoDelete="true"), exchange = @Exchange(value="${mq.config.exchange}",type=ExchangeTypes.DIRECT), key="${mq.config.queue.error.routing.key}" ) ) public class ErrorRecevier {
/** * 接收消息的方法,采用消息队列监听器 * */ //queues队列名+value值msg @RabbitHandler public void process(String msg){ System.out.println("Error...Recevier:"+msg); } }
|
案例二:topic
生产者:3条服务到交换器
#rabbits spring.rabbitmq.host=127.0.0.1 spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest
#exchange name mq.config.exchange=log.topic |
@Component public class UserSender {
@Autowired private AmqpTemplate rabbitTmplate;
@Value("${mq.config.exchange}") private String exchange;
public void send(String msg){ System.out.println("发送到队列。。。。"); //这个是重载的方法 //发送的路由键,根据topic规则去匹配 写死了路由键 this.rabbitTmplate.convertAndSend(exchange, "user.log.dubug", "发送user.log.dubug"); this.rabbitTmplate.convertAndSend(exchange, "user.log.info", "发送user.log.info"); this.rabbitTmplate.convertAndSend(exchange, "user.log.warn", "发送user.log.warn"); this.rabbitTmplate.convertAndSend(exchange, "user.log.error", "发送user.log.error"); } }
|
@Component public class OrderSender {
@Autowired private AmqpTemplate rabbitTmplate;
@Value("${mq.config.exchange}") private String exchange;
public void send(String msg){ System.out.println("发送到队列。。。。"); //这个是重载的方法 //发送的路由键,根据topic规则去匹配 this.rabbitTmplate.convertAndSend(exchange, "order.log.dubug", "发送order.log.dubug"); this.rabbitTmplate.convertAndSend(exchange, "order.log.info", "发送order.log.info"); this.rabbitTmplate.convertAndSend(exchange, "order.log.warn", "发送order.log.warn"); this.rabbitTmplate.convertAndSend(exchange, "order.log.error", "发送order.log.error"); } } |
@Component public class ProductSender {
@Autowired private AmqpTemplate rabbitTmplate;
@Value("${mq.config.exchange}") private String exchange;
public void send(String msg){ System.out.println("发送到队列。。。。"); //这个是重载的方法 //发送的路由键,根据topic规则去匹配 this.rabbitTmplate.convertAndSend(exchange, "productor.log.dubug", "发送productor.log.dubug"); this.rabbitTmplate.convertAndSend(exchange, "productor.log.info", "发送productor.log.info"); this.rabbitTmplate.convertAndSend(exchange, "productor.log.warn", "发送productor.log.warn"); this.rabbitTmplate.convertAndSend(exchange, "productor.log.error", "发送productor.log.error"); } }
|
消费者:从3条队列中取出消息
spring.application.name=rabbits-consumer
#rabbits spring.rabbitmq.host=127.0.0.1 spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest
#exchange name mq.config.exchange=log.topic
mq.config.queue.info=log.info mq.config.queue.error=log.error mq.config.queue.logs=log.all
|
@Component @RabbitListener( bindings=@QueueBinding( value=@Queue(value="${mq.config.queue.error}",autoDelete="true"), exchange = @Exchange(value="${mq.config.exchange}",type=ExchangeTypes.TOPIC), key="*.log.error" ) ) public class ErrorRecevier {
/** * 接收消息的方法,采用消息队列监听器 * */ //queues队列名+value值msg @RabbitHandler public void process(String msg){ System.out.println("Error...Recevier:"+msg); } }
|
@Component @RabbitListener( bindings=@QueueBinding( value=@Queue(value="${mq.config.queue.info}",autoDelete="true"), exchange = @Exchange(value="${mq.config.exchange}",type=ExchangeTypes.TOPIC), key="*.log.info" ) ) public class InfoRecevier {
/** * 接收消息的方法,采用消息队列监听器 * */ //queues队列名+value值msg @RabbitHandler public void process(String msg){ System.out.println("Info...Recevier:"+msg); } }
|
这个路由键可以匹配多个
@Component @RabbitListener( bindings=@QueueBinding( value=@Queue(value="${mq.config.queue.logs}",autoDelete="true"), exchange = @Exchange(value="${mq.config.exchange}",type=ExchangeTypes.TOPIC), key="*.log.*" ) ) public class logsRecevier {
/** * 接收消息的方法,采用消息队列监听器 * */ //queues队列名+value值msg @RabbitHandler public void process(String msg){ System.out.println("all...Recevier:"+msg); } }
|
案例三:fanout广播模式
生产者
spring.application.name=rabbits-provider
#rabbits spring.rabbitmq.host=127.0.0.1 spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest
#exchange name mq.config.exchange=order.fanout |
不需要路由键
@Component public class Sender {
@Autowired private AmqpTemplate rabbitTmplate;
@Value("${mq.config.exchange}") private String exchange;
public void send(String msg){ System.out.println("发送到队列。。。。"); //这个是重载的方法 this.rabbitTmplate.convertAndSend(exchange,"", "发送info"); } }
|
消费者
一个队列对应一个服务
#rabbits spring.rabbitmq.host=127.0.0.1 spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest
#exchange name mq.config.exchange=order.fanout #queue name and rout-key mq.config.queue.sms=order.sms mq.config.queue.push=order.push |
不需要指定路由键
@Component @RabbitListener( bindings=@QueueBinding( value=@Queue(value="${mq.config.queue.push}",autoDelete="true"), exchange = @Exchange(value="${mq.config.exchange}",type=ExchangeTypes.FANOUT) ) ) public class PushRecevier {
/** * 接收消息的方法,采用消息队列监听器 * */ //queues队列名+value值msg @RabbitHandler public void process(String msg){ System.out.println("push...Recevier:"+msg); } } |
@Component @RabbitListener( bindings=@QueueBinding( value=@Queue(value="${mq.config.queue.sms}",autoDelete="true"), exchange = @Exchange(value="${mq.config.exchange}",type=ExchangeTypes.FANOUT) ) ) public class SmsRecevier {
/** * 接收消息的方法,采用消息队列监听器 * */ //queues队列名+value值msg @RabbitHandler public void process(String msg){ System.out.println("SMS...Recevier:"+msg); } } |
案例四:持久化
在消费者端不删去队列,这样可以保证消息不丢失
@Component @RabbitListener( bindings=@QueueBinding( value=@Queue(value="${mq.config.queue.info}",autoDelete="false"), exchange = @Exchange(value="${mq.config.exchange}",type=ExchangeTypes.DIRECT), key="${mq.config.queue.info.routing.key}" ) ) public class InfoRecevier {
/** * 接收消息的方法,采用消息队列监听器 * */ //queues队列名+value值msg @RabbitHandler public void process(String msg){ System.out.println("Info...Recevier:"+msg); } }
|
生产者端的代码配置不变
案例五:消息确认ack
为了保证能正确处理消息,消费者处理成功返回给发送者一个ack信息,当处理不成功,生产者会再次不停地发送消息;也就是消息永远不丢失的设计;
为了避免消息的死循环
生产者端配置不变
#rabbits spring.rabbitmq.host=127.0.0.1 spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest
#exchange name mq.config.exchange=log.direct mq.config.queue.info.routing.key=log.info.routing.key mq.config.queue.error.routing.key=log.error.routing.key |
消费者端配置
spring.application.name=rabbits-consumer
#rabbits spring.rabbitmq.host=127.0.0.1 spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest
#exchange name mq.config.exchange=log.direct #queue name and rout-key mq.config.queue.info=log.info mq.config.queue.info.routing.key=log.info.routing.key
mq.config.queue.error=log.error mq.config.queue.error.routing.key=log.error.routing.key
#consumer start retry number spring.rabbitmq.listener.retry.enabled=true spring.rabbitmq.listener.retry.max-attempts=5
|
消费者端代码或者try catch或者配置方式
@Component @RabbitListener( bindings=@QueueBinding( value=@Queue(value="${mq.config.queue.info}",autoDelete="true"), exchange = @Exchange(value="${mq.config.exchange}",type=ExchangeTypes.DIRECT), key="${mq.config.queue.info.routing.key}" ) ) public class InfoRecevier {
/** * 接收消息的方法,采用消息队列监听器 * */ //queues队列名+value值msg @RabbitHandler public void process(String msg){ System.out.println("Info...Recevier:"+msg); /*try {
} catch (Exception e) { e.printStackTrace(); }*/ throw new RuntimeException(); } }
|