zoukankan      html  css  js  c++  java
  • springboot之rabbitmq安装与实践

    环境:腾讯云centos7 

    注意:rabbitmq安装插件,可能会报错。本人是主机名的问题,所以修改了主机名。
    
    vim /etc/hosts
    
    vim /etc/hostname
    
    修改这两个文件,并重启主机,然后再运行rabbitmq,没有问题了。

    1、下载安装包

    https://www.rabbitmq.com/install-generic-unix.html

    2、解压

    安装xz :yum -y install xz
    xz -d rabbitmq-server-generic-unix-3.7.17.tar.xz tar -xvf rabbitmq-server-generic-unix-3.7.17.tar

    3、安装

    mkdir /usr/java/
    
    mv rabbitmq-server-generic-unix-3.7.17 /usr/java/rabbitmq-server
    
    cd /usr/java/rabbitmq-server/sbin

    4、安装erlang

    由于rabbitmq是erlang语言开发,所以需要该语言环境
        yum install erlang -y 

    5、安装管理界面插件

    rabbitmq-plugins enable     rabbitmq_management

    6、运行mq

    启动服务:rabbitmq-server -detached  或者 rabbitmq-server    start  
    查看状态:rabbitmqctl status
    关闭服务:rabbitmqctl stop    
    查看插件列表:abbitmq-plugins list        
    新增用户:rabbitmqctl  add_user  Username  Password
    删除用户:rabbitmqctl  delete_user  Username
    修改用户密码:rabbitmqctl  change_password  Username  Newpassword
    查看当前用户列表:rabbitmqctl  list_users

    7、验证是否启动

    ss -tnalp 查看端口,如果有15672,则运行成功。

    8、进入web界面

    http://ip:15672/    初始账号/密码:guest/guest  

    登录报错:User can only log in via localhost 
      原因:rabbitmq从3.3.0开始禁止使用guest/guest权限通过除localhost外的访问
    解决方法:找到ebin/rabbit.app文件,将 {loopback_users, [<<"guest">>]},  改为{loopback_users, []}

     完毕!

    #######springboot集成实践######

    1、pom.xml文件添加依赖

     <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-amqp -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-amqp</artifactId>
                <version>2.1.3.RELEASE</version>
            </dependency>

    2、在yml文件中添加服务ip

    #默认使用配置  #activemq
    spring:
      rabbitmq:
        host: 132.232.44.82

    3、RabbitMQConfig.java 这个文件不能少,否则启动报错(所有的消息的topic都要在这个文件进行注册)

    package com.cn.commodity.config;
    
    import org.springframework.amqp.core.Queue;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class RabbitMQConfig {
        //direct模式,直接根据队列名称投递消息
        @Bean
        public Queue logOpQueue(){
            return new Queue("oplog");
        }
    
        @Bean
        public Queue logErrQueue(){
            return new Queue("errlog");
        }
    
        //不要偷懒 一定要声明
        @Bean
        public Queue chatMessageQueue(){
            return new Queue("yang");
        }
    
        @Bean
        public Queue mapMessageQueue(){
            return new Queue("rabbitmq_map");
        }
    }

    4、RabbitmqProducer.java

    package com.cn.commodity.controller;
    
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class RabbitmqProducer {
        @Autowired
        private RabbitTemplate rabbitTemplate;
        @RequestMapping(value = "testSend")
        public void testSend(String msg){
            rabbitTemplate.convertAndSend("yang","rabbitmq 发送消息:"+msg);
        }
        @RequestMapping(value = "testSendMap")
        public void testSendMap(String msg){
            rabbitTemplate.convertAndSend("rabbitmq_map","rabbitmq 发送消息:"+msg);
        }
    
    }

    5、RabbitmqConsumer.java

    package com.cn.commodity.controller;
    
    import org.springframework.amqp.rabbit.annotation.RabbitHandler;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.stereotype.Component;
    
    import java.util.HashMap;
    import java.util.Map;
    
    @Component
    public class RabbitmqConsumer {
        @RabbitHandler
        @RabbitListener(queues="yang" )
        public void showMessage(String message){
            System.out.println("yangwj接收到消息:"+message);
        }
    
        @RabbitHandler
        @RabbitListener(queues="rabbitmq_map" )
        public void showMessage(){
            Map map = new HashMap();
            map.put("1","大爷你好");
            System.out.println("yangwj接收到消息:"+map);
        }
    }

    启动运行,完毕!

  • 相关阅读:
    oracle 视图views
    5分钟把任意网站变成桌面软件
    Angular http跨域
    jQuery版本升级踩坑大全
    redis 模糊删除key
    jedisCluster 报错: redis.clients.jedis.exceptions.JedisClusterException: No way to dispatch this command to Redis Cluster because keys have different slots.
    mac电脑复制键失灵
    java8 for ,forEach ,lambda forEach , strean forEach , parller stream forEach, Iterator性能对比
    linux光标操作
    redis hashmap数据结构分析
  • 原文地址:https://www.cnblogs.com/ywjfx/p/11310043.html
Copyright © 2011-2022 走看看