rabbit-socket容器创建及使用

2018-09-09  本文已影响0人  毛豆豆豆豆子
启容器rabbit
docker run -d --name some-rabbit -p 5672:5672 -p 15674:15674  -p 8030:15672 -p 15670:15670  rabbitmq:3-management
进入容器启动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

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

上一篇下一篇

猜你喜欢

热点阅读