centos7 rabbitmq安裝以及應用

安裝單機rabbitmq
 
1.安裝erlang
cd /usr.local
yum install wget
yum install net-tools
wget http://erlang.org/download/otp_src_19.3.tar.g
解壓
tar -xvzf otp_src_19.3.tar.gz
 
erlang在安裝前須要先安裝下它的依賴工具:
yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-devel unixODBC-devel
 
而後進入解壓文件對erlang進行安裝環境的配置(爲了之後升級版本,此處就繼續使用加壓文件的名字了,至少我看得懂。。。。)
./configure --prefix=/usr/erlang --without-javac 
 
編譯make
 
安裝 make install
 
驗證是否安裝成功
./bin/erl
 
配置環境變量
vi /etc/profile
在最後加:export PATH=$PATH:/usr/local/otp_src_19.3/bin
source /etc/profile
 
2.安裝RabbitMQ
 
yum install rabbitmq-server-3.6.6-1.el7.noarch.rpm
 
可能會報錯
erlang >= R16B-03
解決方法
yum -y install socat
yum -y install epel-release
再進入 usr/local
yum install rabbitmq-server-3.6.6-1.el7.noarch.rpm
 
完成後啓動服務:
service rabbitmq-server start
能夠查看服務狀態
service rabbitmq-server status
 
 
這裏能夠看到log文件的位置,轉到文件位置,打開文件:
 
這裏顯示的是沒有找到配置文件,咱們能夠本身建立這個文件
 
cd /etc/rabbitmq/ vi rabbitmq.config
 
編輯內容以下:
[{rabbit, [{loopback_users, []}]}].
 
保存配置後重啓服務:
service rabbitmq-server stop service rabbitmq-server start
 
 
開啓管理UI:
rabbitmq-plugins enable rabbitmq_management firewall-cmd --zone=public --add-port=15672/tcp --permanent firewall-cmd --reload
 
在Windows下打開地址:
 
 
 
至此 rabbitmq安裝成功
 
應用
 
首頁看下目錄結構
 
這是個springboot項目,
spring-rabbitmq.xml:配置mq的全部基本信息
rabbit文件夾下的xml即配置的exchange,routingkey以及對應綁定的對列名
listener包下是所須要的監聽類
主要是如下內容
@RabbitListener(queues = "attendance.q") :監聽的隊列名
@RabbitHandler 對應監聽的方法
如何去發送:定義exchange和routeingkey就ok了
 
public class RabbitUtil {

    @Autowired
    private ConnectionFactory connectionFactory;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    private static RabbitUtil rabbitUtil;

    @PostConstruct
    private void init() {
        rabbitUtil = this;
        rabbitUtil.rabbitTemplate = this.rabbitTemplate;
        rabbitUtil.connectionFactory = this.connectionFactory;
    }

    public static RabbitTemplate getRabbitTemplate() {
        return rabbitUtil.rabbitTemplate;
    }

    public static ConnectionFactory getConnectionFactory() {
        return rabbitUtil.connectionFactory;
    }

    /**
     * 發送RMQ消息
     *
     * @param message
     * @throws AmqpException
     */
    public void convertAndSend(String message) throws AmqpException {
        convertAndSend(null, null, message, false);
    }

    /**
     * 發送RMQ消息
     *
     * @param routingKey
     * @param message
     * @throws AmqpException
     */
    public void convertAndSend(String routingKey, String message) throws AmqpException {
        convertAndSend(null, routingKey, message, false);
    }

    /**
     * 發送RMQ消息
     *
     * @param exchange
     * @param routingKey
     * @param message
     * @throws AmqpException
     */
    public static void convertAndSend(String exchange, String routingKey, String message) throws AmqpException {
        convertAndSend(exchange, routingKey, message, false);
    }

    private static void convertAndSend(String exchange, String routingKey, Object message, boolean waitForAck)
            throws AmqpException {
        if (waitForAck) {

        } else {
            if (StringUtils.isNotEmpty(exchange) && StringUtils.isNotEmpty(routingKey)) {
                getRabbitTemplate().convertAndSend(exchange, routingKey, message);
            } else if (StringUtils.isNotEmpty(routingKey)) {
                getRabbitTemplate().convertAndSend(routingKey, message);
            } else {
                getRabbitTemplate().convertAndSend(message);
            }
        }
    }

    /**
     * 查詢隊列消息數量
     * @param queue
     * @throws Exception
     */
    public static long getMessageCount(String queue) throws Exception {
        Connection connection = null;
        Channel channel = null;
        try {
            ConnectionFactory connectionFactory = getConnectionFactory();
            connection = connectionFactory.createConnection();
            channel = connection.createChannel(false);
            return channel.messageCount(queue);
        } finally {
            if(channel != null){
                channel.close();
            }
            if(connection != null){
                connection.close();
            }
        }

    }
相關文章
相關標籤/搜索