zoukankan      html  css  js  c++  java
  • 有关RabbitMq基操

    RabbitMQ一些积累记录

    一、JAVA直接调用RabbitMQ的API获取队列、用户、虚拟机等信息。

    @scheduled(cron = "* 0/2 * * * ?")
        public void startMonitor() throws CLientProtocolException, IOException {
            String rabbitmqMsg = getRabbitmqMsg("/api/queues", username.password, host, port);
            List<JSONObject> parseArray = JSONArray.parseArray(rabbitmqMsg, JSONObject.class);
            if (parseArray == null || parseArray.size() == 0) {
                return;
            }
            for (int i = 0; i < parseArray.size(); i++) {
                //获取队列里的消息数量
                BigDecimal messages = parseArray.get(i).getBigDecimal("messages");
                //获取虚拟机名
                String vhost = parseArray.get(i).getString("vhost");
                //获取队列名
                String name = parseArray.get(i).getString("name");
                System.out.println(vhost + name + "消息数量" + messages);
            }
        }
    
        /**
         * @param tailUrl  尾缀
         * @param userName 较大权限的用户名(可查看队列信息)
         * @param password 较大权限的用户的密码
         * @param hostIp   访问的主机IP
         * @param port     访问的rabbitmq端口(一定是web管理端口,默认15672,不是连接端口5672)
         * @return
         * @throws CLientProtocolException
         * @throws IOException
         */
        public String getRabbitmqMsg(String tailUrl, String userName, String password, String hostIp, int port)
                throws CLientProtocolException, IOException {
            HostPost host = new HostPost(hostIp, port);
            HttpGet httGet = new HttpGet(tailUrl);
            //开始基于Bisc模式认证用户名密码
            BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(new AuthScope(hostIp.port),
                    new UsernamePasswordCredentials(userName, password));
            DefaultHttpClient client = new DefaultHttpClient();
            client.setCredentialsProvider(credentialsProvider);
            HttpResponse execute = client.execute(host, httGet);
            HttpEntity entity = execute.getEntity();
            return EntityUtils.toString(entity);
        }

    有关于RabbitMQ的API使用,可查看rabbitmq的web管理页ip:15672/api查看。更多信息也可查看json字符串后自行截取。

    二、springboot连接rabbitmq发生socket closed异常

    该情况有几种排查方案。

    1.检查配置文件端口是否是5672,因为rabbitmq连接的不是web管理端,很多时候粗心出现这个错误。只有访问Web API时才设定15672.

    2.不管是集群还是单机模式,一般都会配置下hosts文件的主机名。检查下host是否被篡改导致的。

    3.在web管理页检查配置的用户是否有操作当前队列的权限。如未绑定虚拟机、交换机权限。

    以下图片从网络寻找。

     三、springboot使用rabbitmq配置集群

    package com.chx.util;
    
    import com.rabbitmq.client.ConnectionFactory;
    import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class RabbitmqConfig {
        private String hosts = "127.0.0.1:5672,127.0.0.2:5672";
    
        @Bean
        public ConnectionFactory connectionFactory(){
            CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
            connectionFactory.setAddresses(hosts);
            connectionFactory.setUsername("");
            connectionFactory.setPassword("");
            connectionFactory.setVirtualHost("");
        }
    }
  • 相关阅读:
    apply call this arguments caller callee
    JavaScript 小刮号
    asp.net底层http管道
    JavaScript 使用方括号([])引用对象的属性和方法 createDelegate
    MSIL条件跳转(简单注释)
    微软MVP手把手教你如何修改.NET Framework
    简单操作IL文件
    Remoting入门实例
    AutoResetEvent和ManualResetEvent用法示例
    AutoResetEvent和ManualResetEvent用法
  • 原文地址:https://www.cnblogs.com/chxwkx/p/14065747.html
Copyright © 2011-2022 走看看