Centos7最小版安装rabbitmq

一。安装

1 下载安装包

wget https://www.rabbitmq.com/releases/erlang/erlang-19.0.4-1.el7.centos.x86_64.rpm
wget https://www.rabbitmq.com/releases/rabbitmq-server/v3.6.15/rabbitmq-server-3.6.15-1.el7.noarch.rpm

2 执行安装命令

yum install -y vim wget lrzsz socat
yum install -y erlang-19.0.4-1.el7.centos.x86_64.rpm
yum install -y rabbitmq-server-3.6.15-1.el7.noarch.rpm

3 设置开机启动

systemctl enable rabbitmq-server

4启动/停止/重起

service rabbitmq-server start/stop/restart

5.关闭防火墙

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动

6.测试

rabbitmqctl list_users
sudo rabbitmqctl add_user gzz gzz
sudo rabbitmqctl  set_permissions -p /  gzz '.*'  '.*'  '.*' 

二。springboot 2.1.11程序中使用

pom.xml



    4.0.0
    com.gzz
    60-springboot-rabbitmq
    0.0.1-SNAPSHOT
    jar
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.11.RELEASE
         
    
    
        UTF-8
        UTF-8
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-amqp
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


application.yml

spring: 
   application:
      name: rabbitmq-hello
   rabbitmq: 
      host: 192.168.145.138
      port: 5672
      username: gzz
      password: gzz

RabbitConfig.java

package com.gzz.config;

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    @Bean
    public Queue helloQueue() {
        return new Queue("hello");
    }

}

HelloApplication.java

package com.gzz;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloApplication.class, args);
    }
}

Receiver.java

package com.gzz;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
@RabbitListener(queues = "hello")
public class Receiver {
    private Logger log = LoggerFactory.getLogger(getClass());

    @RabbitHandler
    public void process(String hello) {
        log.info("Receiver : " + hello);
    }

}

Sender.java

package com.gzz;

import java.util.Date;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Sender {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send() {
        this.rabbitTemplate.convertAndSend("hello", "我的测试实例:" + new Date());
    }

}

Tests.java

package com.gzz;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Tests {
    @Autowired
    private Sender sender;

    @Test
    public void hello() throws Exception {
        sender.send();
    }
}

源码下载

三。常用命令

添加用户

sudo rabbitmqctl add_user gzz gzz

删除用户

sudo rabbitmqctl delete_user  

修改用户密码

sudo rabbitmqctl change_password   

清除用户密码(该用户将不能使用密码登陆,但是可以通过SASL登陆如果配置了SASL认证)

sudo rabbitmqctl clear_password 

设置用户tags(相当于角色,包含administrator,monitoring,policymaker,management)

sudo rabbitmqctl set_user_tags  

列出所有用户

sudo rabbitmqctl list_users 

创建一个vhosts

sudo rabbitmqctl add_vhost  

删除一个vhosts

sudo rabbitmqctl delete_vhost  

列出vhosts

sudo rabbitmqctl list_vhosts [ ...] 

针对一个vhosts给用户赋予相关权限;

sudo rabbitmqctl set_permissions [-p ]     

清除一个用户对vhosts的权限;

sudo rabbitmqctl clear_permissions [-p ]  

列出哪些用户可以访问该vhosts;

sudo rabbitmqctl list_permissions [-p ]  

列出用户访问权限;

sudo rabbitmqctl list_user_permissions 

你可能感兴趣的:(Centos7最小版安装rabbitmq)