centOS docker install - TuPengXiong/TuPengXiong.github.io GitHub Wiki

centOS docker 安装

  • yum 更新
 sudo yum update
  • Add the yum repo
sudo tee /etc/yum.repos.d/docker.repo <<-'EOF'
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF
  • Install the Docker package
sudo yum install docker-engine
  • 启动 docker service.
sudo systemctl enable docker.service
  • 启动 docker
sudo systemctl start docker

docker rabbitmq mqtt 服务

# 下载 rabbitmq 镜像 端口映射 fork 运行
docker run -itd -p 8883:8883 -p 1883:1883 -p 5617:5671 -p 5672:5672 -p 4369:4369 -p 25672:25672 docker.io/rabbitmq
  • docker ps
1b7b52b7fb40        docker.io/rabbitmq   "docker-entrypoint.sh"   2 minutes ago       Up 2 minutes        0.0.0.0:1883->1883/tcp, 0.0.0.0:4369->4369/tcp, 0.0.0.0:5672->5672/tcp, 0.0.0.0:8883->8883/tcp, 0.0.0.0:25672->25672/tcp, 0.0.0.0:5617->5671/tcp   sad_kare
  • 加载rabbitmq的mqtt插件 1b7b52b7fb40 为容器id
docker exec -it  1b7b52b7fb40 rabbitmq-plugins enable rabbitmq_mqtt

java client 使用 eclipse.paho 测试

  • jar 包依赖
<dependency>
 <groupId>org.eclipse.paho</groupId>
 <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
 <version>1.1.0</version>
</dependency>

测试代码

package tpx.rabbitmq;
  import org.eclipse.paho.client.mqttv3.MqttClient;
        import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
        import org.eclipse.paho.client.mqttv3.MqttException;
        import org.eclipse.paho.client.mqttv3.MqttMessage;
        import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

        public class MqttPublishSample {

        public static void main(String[] args) {

            String topic        = "MQTT Examples";
            String content      = "Message from MqttPublishSample";
            int qos             = 0;
            String broker       = "tcp://192.168.0.13:1883";
            String clientId     = "JavaSample";
            MemoryPersistence persistence = new MemoryPersistence();

            try {
                MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
                MqttConnectOptions connOpts = new MqttConnectOptions();
                connOpts.setCleanSession(true);
                System.out.println("Connecting to broker: "+broker);
                sampleClient.connect(connOpts);
                System.out.println("Connected");
                System.out.println("Publishing message: "+content);
                MqttMessage message = new MqttMessage(content.getBytes());
                message.setQos(qos);
                sampleClient.publish(topic, message);
                System.out.println("Message published");
                sampleClient.disconnect();
                System.out.println("Disconnected");
                System.exit(0);
            } catch(MqttException me) {
                System.out.println("reason "+me.getReasonCode());
                System.out.println("msg "+me.getMessage());
                System.out.println("loc "+me.getLocalizedMessage());
                System.out.println("cause "+me.getCause());
                System.out.println("excep "+me);
                me.printStackTrace();
            }
        }
    }
⚠️ **GitHub.com Fallback** ⚠️