Springboot集成RabbitMQ使用

RabbitMQ

优点:
	1.应用解耦
	2.异步提速
	3.削峰填谷
缺点:
	1.一致性
	2.复杂性提高
	3.可用性降低

特点:

1.可靠性
2.灵活的路由
3.高可用(集群)
4.多种协议
5.多语言客户端
6.管理界面
7.跟踪机制 

rabbitmq启动

双击rabbitmq-server.bat启动rabbitmq

访问:localhost:15672

连接端口: 5672

SpringBoot集成RabbitMQ

依赖
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
fanout模式(先在rabbitmq中新建一个virtual-host)
consumer
server:
  port: 8081

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: test
    password: 123456
    virtual-host: /test_rabbit
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyRabbitConfig {


    private static String EXCHANGE_NAME = "my_fanout";
    private static String QUEUE_NAME = "my_boot_fanout_queue";

    /**
     * 注册一个广播形式的交换机
     * @return
     */
    @Bean
    public FanoutExchange exchange(){
        return new FanoutExchange(EXCHANGE_NAME,true,false);
    }

    /**
     * 注册一个队列
     * @return
     */
    @Bean
    public Queue queue(){
        return new Queue(QUEUE_NAME,true,false,false);
    }

    /**
     * 将交换机和队列绑定起来
     * @param queue
     * @param fanoutExchange
     * @return
     */
    @Bean
    public Binding queueBinding(Queue queue,FanoutExchange fanoutExchange){
        return BindingBuilder.bind(queue).to(fanoutExchange);
    }


}

@Component
public class MyConsumer {


    @RabbitListener(queues = "my_boot_fanout_queue")
    public void process(Message message){
        byte[] body = message.getBody();
        System.out.println("接收到的消息:"+new String(body));
    }
}
producer
server:
  port: 8082

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: test
    password: 123456
    virtual-host: /test_rabbit
@Configuration
public class MyRabbitConfig {

    private static String EXCHANGE_NAME = "my_fanout";

    /**
     * fanout 广播形式
     * @return
     */
    @Bean
    public FanoutExchange exchange(){
        return new FanoutExchange(EXCHANGE_NAME,true,false);
    }
}
test类
	@Resource
    private RabbitTemplate rabbitTemplate;

    @Test
    public void sendMessage(){
        String message = "hello world rabbit";
        rabbitTemplate.convertAndSend("amq.fanout","",message);
    }
topic模式
consumer
server:
  port: 8081

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: test
    password: 123456
    virtual-host: /test_rabbit


@Configuration
public class MyRabbitConfig {


    private static String EXCHANGE_NAME = "my_boot-topic_exchange";
    private static String QUEUE_NAME = "my_boot_topic_queue";

    /**
     * 注册一个广播形式的交换机
     * @return
     */
    @Bean
    public TopicExchange exchange(){
        return new TopicExchange(EXCHANGE_NAME,true,false);
    }

    /**
     * 注册一个队列
     * @return
     */
    @Bean
    public Queue queue(){
        return new Queue(QUEUE_NAME,true,false,false);
    }

    /**
     * 将交换机和队列绑定起来
     * @param queue
     * @param fanoutExchange
     * @return
     */
    @Bean
    public Binding queueBinding(Queue queue,FanoutExchange fanoutExchange){
        return BindingBuilder.bind(queue).to(fanoutExchange).with("product.*");
    }


}

@Component
public class MyConsumer {


    @RabbitListener(queues = "my_boot_topic_queue")
    public void process(Message message){
        byte[] body = message.getBody();
        System.out.println("接收到的消息:"+new String(body));
    }
}
producer
server:
  port: 8082

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: test
    password: 123456
    virtual-host: /test_rabbit
@Configuration
public class MyRabbitConfig {

    private static String EXCHANGE_NAME = "my_boot_topic_exchange";

    /**
     * fanout 广播形式
     * @return
     */
    @Bean
    public TopicExchange exchange(){
        return new TopicExchange(EXCHANGE_NAME,true,false);
    }
}
test类
	@Resource
    private RabbitTemplate rabbitTemplate;

    @Test
    public void sendMessage(){
        String message = "hello world rabbit";
        rabbitTemplate.convertAndSend("amq.fanout","product.test",message);
    }
ack的使用
server:
  port: 8083

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: test
    password: 123456
    virtual-host: /test_rabbit
#    修改成手动ack
    listener:
      simple:
        acknowledge-mode: manual
// 消息消费方
@Component
public class MyConsumer {

    @RabbitListener(queues = "my_boot_topic_queue")
    public void process(Message message, Channel channel) throws IOException {
        System.out.println(new String(message.getBody()));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
注解形式开发
producer
spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: test
    password: 123456
    virtual-host: /test_rabbit
@RestController
public class ProducerController {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/send")
    public String sendMessage(String message){
        Map map = new HashMap();
        map.put("message",message);
        map.put("id","001");
        map.put("type","test");
        
        rabbitTemplate.convertAndSend("exchange_test","send_test", JSON.toJSONString(map));
        
        return "success";
    }
}
consumer
spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: test
    password: 123456
    virtual-host: /test_rabbit
@Component
public class RabbitmqConsumer {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "queue_test"),
            exchange = @Exchange(name = "exchange_test",type = ExchangeTypes.TOPIC),
            key = "send_test"
    ))
    public void consumer(Message message){
        String s = new String(message.getBody());
        Map map = JSON.parseObject(s, Map.class);
        map.forEach((key,value) -> System.out.println(key+ ":"+map.get(key)));
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值