美文网首页中间件
RabbitMQ入门(简介-安装-代码实战)

RabbitMQ入门(简介-安装-代码实战)

作者: 欧阳馒头 | 来源:发表于2019-02-26 22:10 被阅读112次

    1.简介

    RabbitMQ是一个由erlang开发的基于AMQP(Advanced Message Queue)协议的开源实现。用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面都非常的优秀。是当前最主流的消息中间件之一。

    2.下载erlang/rabbitmq-server

    wget --content-disposition https://packagecloud.io/rabbitmq/erlang/packages/el/6/erlang-21.2.6-1.el6.x86_64.rpm/download.rpm

    wget --content-disposition https://packagecloud.io/rabbitmq/rabbitmq-server/packages/el/6/rabbitmq-server-3.7.12-1.el6.noarch.rpm/download.rpm

    3.安装

    rpm -ivh erlang-21.2.6-1.el6.x86_64.rpm

    rpm -ivh rabbitmq-server-3.7.12-1.el6.noarch.rpm

    4.启动

    service rabbitmq-server start (启动rabbitmq服务)
    rabbitmqctl status(查看rabbitmq状态)

    5.配置web 管理插件

    rabbitmq-plugins enable rabbitmq_management
    
    [root@VM_0_7_centos software]# rabbitmq-plugins enable rabbitmq_management
    Enabling plugins on node rabbit@VM_0_7_centos:
    rabbitmq_management
    The following plugins have been configured:
      rabbitmq_management
      rabbitmq_management_agent
      rabbitmq_web_dispatch
    Applying plugin configuration to rabbit@VM_0_7_centos...
    The following plugins have been enabled:
      rabbitmq_management
      rabbitmq_management_agent
      rabbitmq_web_dispatch
    
    started 3 plugins.
    

    6.访问

    http://IP:15672/

    image.png

    7.创建用户

    用户:guest guest (只能本地使用)
    创建用户

    rabbitmqctl add_user admin 123456  #添加用户名和密码
    rabbitmqctl set_permissions -p / admin ".*" ".*" ".*" #修改权限
    rabbitmqctl set_user_tags admin administrator  #添加用户角色
    
    登录后查看web端: MQ web端

    8.常见问题

    1.版本过高报错

    #】 rpm -ivh erlang-21.0.6-1.el7.centos.x86_64.rpm
    warning: erlang-21.0.6-1.el7.centos.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 6026dfca: NOKEY
    error: Failed dependencies:
        libc.so.6(GLIBC_2.14)(64bit) is needed by erlang-21.0.6-1.el7.centos.x86_64
        libc.so.6(GLIBC_2.15)(64bit) is needed by erlang-21.0.6-1.el7.centos.x86_64
        libcrypto.so.10(OPENSSL_1.0.2)(64bit) is needed by erlang-21.0.6-1.el7.centos.x86_64
    

    centos 7才支持这个版本,centos 6 需要降低erlang的安装版本

    9.代码使用

    spring boot 中应用

    1.maven依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    

    2.配置
    application-dev.properties

    spring.rabbitmq.host=IP
    spring.rabbitmq.port=5672
    spring.rabbitmq.username=xx
    spring.rabbitmq.password=pwd
    
    @Configuration
    public class RabbitConfig {
        @Bean
        public Queue helloQueue() {
            return new Queue("hello");
        }
    }
    

    3.发送与监听

    @Component
    public class MQSender {
    
        @Autowired
        private AmqpTemplate rabbitTemplate;
    
        public void send() {
            String context = "hello " + DateUtilS.getNowTimeString();
            System.out.println("Sender : " + context);
            this.rabbitTemplate.convertAndSend("hello", context);
        }
    }
    
    @Component
    @RabbitListener(queues = "hello")
    public class MQReceiver {
    
        @RabbitHandler
        public void process(String hello) {
            System.out.println("Receiver : " + hello);
        }
    }
    

    4.测试
    4.1 监听MQ消息
    启动项目进行,看到如下打印说明成功连接到MQ

    2019-02-26 13:21:57.412 [SimpleAsyncTaskExecutor-1] INFO  org.springframework.amqp.rabbit.connection.CachingConnectionFactory -Created new connection: SimpleConnection@55100446 [delegate=amqp://xx@IP:5672/, localPort= 58664]
    

    4.2 发送MQ消息

    @RunWith(SpringRunner.class)
    @SpringBootTest
    @ComponentScan("com.byron")
    @ActiveProfiles("dev")
    public class MQTest{
    
        @Autowired
        private MQSender sender;
    
        @Test
        public void hello() throws Exception {
            sender.send();
        }
    
    }
    

    4.3结果
    监听控制台打印如下

    Receiver : hello 2019-02-26 13:23:46
    
    web界面看MQ队列: MQ队列

    10.参考站点

    10.1 官方

    10.2 博客

    相关文章

      网友评论

        本文标题:RabbitMQ入门(简介-安装-代码实战)

        本文链接:https://www.haomeiwen.com/subject/agcmyqtx.html