rabbitmq应用实现

案例一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队列名+valuemsg

    @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队列名+valuemsg

    @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队列名+valuemsg

    @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队列名+valuemsg

    @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队列名+valuemsg

    @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队列名+valuemsg

    @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队列名+valuemsg

    @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队列名+valuemsg

    @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队列名+valuemsg

    @RabbitHandler

    public void process(String msg){

         System.out.println("Info...Recevier:"+msg);

         /*try {

            

         } catch (Exception e) {

             e.printStackTrace();

         }*/

         throw new RuntimeException();

    }

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值