linux运维

Centos7最小版安装rabbitmq

2019-10-12  本文已影响0人  不知不怪

一。安装

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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.gzz</groupId>
    <artifactId>60-springboot-rabbitmq</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.11.RELEASE</version>
        <relativePath/> 
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

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 <username> 

修改用户密码

sudo rabbitmqctl change_password <username> <newpassword> 

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

sudo rabbitmqctl clear_password <username>

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

sudo rabbitmqctl set_user_tags <username> <tag>

列出所有用户

sudo rabbitmqctl list_users 

创建一个vhosts

sudo rabbitmqctl add_vhost <vhostpath> 

删除一个vhosts

sudo rabbitmqctl delete_vhost <vhostpath> 

列出vhosts

sudo rabbitmqctl list_vhosts [<vhostinfoitem> ...] 

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

sudo rabbitmqctl set_permissions [-p <vhostpath>] <user> <conf> <write> <read> 

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

sudo rabbitmqctl clear_permissions [-p <vhostpath>] <username> 

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

sudo rabbitmqctl list_permissions [-p <vhostpath>]  

列出用户访问权限;

sudo rabbitmqctl list_user_permissions <username>
上一篇下一篇

猜你喜欢

热点阅读