美文网首页
rabbit-socket容器创建及使用

rabbit-socket容器创建及使用

作者: 毛豆豆豆豆子 | 来源:发表于2018-09-09 13:18 被阅读0次
    启容器rabbit
    docker run -d --name some-rabbit -p 5672:5672 -p 15674:15674  -p 8030:15672 -p 15670:15670  rabbitmq:3-management
    
    • 5672是java调用的端口 15674是js调用的端口
    • 访问localhost:8030
    • 设置用户名: -e "RABBITMQ_DEFAULT_USER=user"
    • 设置密码:-e "RABBITMQ_DEFAULT_PASS=password"
    • 默认用户是:guest guest
    进入容器启动socker的服务
    docker exec -it some-rabbit /bin/bash
    rabbitmq-plugins enable rabbitmq_web_stomp   //启动rabbitmq_web_stomp
    rabbitmq-plugins enable rabbitmq_web_stomp_examples  //启动rabbitmq_web_stomp_examples
    
    • 访问localhost:8030
    • 访问localhost:15670demo

    Java调用发送消息

    meven引入
    <!--导入rabbitmq依赖-->
            <dependency>
                <groupId>com.rabbitmq</groupId>
                <artifactId>amqp-client</artifactId>
                <version>4.1.0</version>
            </dependency>
    
    java代码
    public static boolean sendQueueer(String mes,String queueName){
            boolean flag = true;
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost(DeployUtil.RABBITMQHOST);//因为两个进程在同一个机器上
            //容器内的5672端口映射出来
            factory.setPort(5672);
            factory.setUsername("guest");
            factory.setPassword("guest");
            factory.setVirtualHost("/");
            Connection connection = null;
            Channel channel = null;
            try {
                connection = factory.newConnection();
                channel = connection.createChannel();
                //前端默认是持久化所以第二个参数应该是true否则配置不一致导致报错
                    channel.queueDeclare(queueName, true, false, false, null);
                    channel.basicPublish("", queueName, null, mes.getBytes("UTF-8"));
                channel.close();
                connection.close();
            } catch (IOException e) {
                flag = false;
                e.printStackTrace();
            } catch (TimeoutException e) {
                flag = false;
                e.printStackTrace();
            }
            finally {
                return flag;
            }
        }
    

    js监听消费消息

    参考:https://www.cnblogs.com/puyangsky/p/6666624.html

    容器相关链接:https://hub.docker.com/_/rabbitmq/
    使用相关链接:https://www.rabbitmq.com/web-stomp.html
    java发送常见错误:https://blog.csdn.net/s10461/article/details/53883846?utm_source=itdadao&utm_medium=referral
    中文文档:http://rabbitmq.mr-ping.com/installation/Installing_on_Debian_Ubuntu.html

    相关文章

      网友评论

          本文标题:rabbit-socket容器创建及使用

          本文链接:https://www.haomeiwen.com/subject/fcrbeftx.html