集群之 -- rabbitmq

24 篇文章 0 订阅
5 篇文章 0 订阅
下载地址:
http://www.rabbitmq.com/download.html 


window安装后,
安装过程:
配置环境:RABBITMQ_SERVER  C:\Program Files\RabbitMQ Server\rabbitmq_server-3.6.12
path  %RABBITMQ_SERVER%\sbin;
进入到sbin目录中,运行
rabbitmq-plugins.bat enable rabbitmq_management
接着去win 运行中:分别运行  
rabbitmq-service stop
rabbitmq-service install
rabbitmq-service start


进入:

http://localhost:15672

用户密码:guest/guest



见博文配置:http://blog.csdn.net/sharetop/article/details/49716897


pom.xml配置
  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>


application配置:
spring.application.name=mymq
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.virtual-host=/




1、竞争消费
生产者代码
package com.my.busi;
 import org.springframework.amqp.rabbit.core.RabbitTemplate;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;


 @Component
 public class Produce {


 @Autowired
 private RabbitTemplate rabbitTemplate;


     public void produce(String s) {
        this.rabbitTemplate.convertAndSend("mymq", s);
     }
 }




 package com.my.controller;
import com.my.busi.Produce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class MqController {


    @Autowired
    private Produce produce;
    @GetMapping("/mymq")
    public String send(@RequestParam("s")String s) {
        produce.send(s);
        return "success";
    }
}




 消费者代码
 package com.my.busi;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.amqp.core.Queue;
 import org.springframework.amqp.rabbit.annotation.RabbitHandler;
 import org.springframework.amqp.rabbit.annotation.RabbitListener;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.messaging.handler.annotation.Payload;


 @Configuration
 @RabbitListener(queues = "mymq")


 public class Customer {




    @Bean
     public Queue fooQueue() {
         return new Queue("mymq");
     }


 @RabbitHandler
     public void process(@Payload String mymq) {
        System.out.println("Listener: " + mymq);
     }
 }


 如果多个消费者,只能有一个运行,就算消费了


  运行:http://127.0.0.1:1111/mymq?s=1111122222



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值