1、安装解压
2、emq启动
./bin/emqx start //守护进程模式启动
3、客户端编程初步
3.1 //消息订阅的客户端例子
package com.zkhuashui.support.mqtt;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import java.text.MessageFormat;
public class SubscribeSample { //消息订阅的客户端例子
public static void main(String[] args) {
String broker = "tcp://localhost:1883"; //连接服务器
String clientId = "JavaSample"; //客户端id
//Use the memory persistence
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");
String topic = "testTopic";
System.out.println("Subscribe to topic:" + topic);
sampleClient.subscribe(topic);
sampleClient.setCallback(new MqttCallback() { //设置回调实例
public void messageArrived(String topic, MqttMessage message) throws Exception {
String theMsg = MessageFormat.format("{0} is arrived for topic {1}.", new String(message.getPayload()), topic);
System.out.println(theMsg);
}
public void deliveryComplete(IMqttDeliveryToken token) {
}
public void connectionLost(Throwable throwable) {
}
});
} 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();
}
}
}
3.2 //消息发布的客户端例子
package com.zkhuashui.support.mqtt;
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 PublishSample { //消息发布的客户端例子
public static void main(String[] args) {
String topic = "testTopic"; //topic
String content = "I Love Emq";
int qos = 1; //mqtt协议的主要特性,QoS 1:至少一次传送 (确认数据交付)
String broker = "tcp://127.0.0.1:1883"; //服务器
String userName = "admin";
String password = "public";
String clientId = "pubClient";
// 内存存储
MemoryPersistence persistence = new MemoryPersistence();
try {
// 创建客户端
MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
// 创建链接参数
MqttConnectOptions connOpts = new MqttConnectOptions();
// 在重新启动和重新连接时记住状态
connOpts.setCleanSession(false);
// 设置连接的用户名
connOpts.setUserName(userName);
connOpts.setPassword(password.toCharArray());
// 建立连接
sampleClient.connect(connOpts);
// 创建消息
MqttMessage message = new MqttMessage(content.getBytes()); //传入的参数字节
// 设置消息的服务质量
message.setQos(qos);
// 发布消息
sampleClient.publish(topic, message);
// 断开连接
sampleClient.disconnect();
// 关闭客户端
sampleClient.close();
} 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();
}
}
}
运行结果如下:订阅客户端始终在运行,等待消息的到来
4、emq浏览器界面
默认的用户名:admin 密码:public
1、通过浏览器访问 http://127.0.0.1:18083,您将看到一个 EMQ X 登录界面:
2、更改 Dashboard 界面语言
ADMIN -> Settings 菜单,您可以改变 Dashboard 使用的语言,点击 Apply 后生效。
目前 EMQ X 支持中文和英文
。
3、修改默认登录名和密码:
etc/plugins/emqx_dashboard.conf
dashboard.default_user.login = admin
dashboard.default_user.password = public
4 、ADMIN -> Users 菜单下,您可以修改用户密码和增加 / 删除用户。admin 用户只能修改密码,不能被删除。
5、端口的配置
在安装以后,EMQ X 默认会使用以下端口:
1883: MQTT 协议端口
8883: MQTT/SSL 端口
8083: MQTT/WebSocket 端口
8080: HTTP API 端口
18083: Dashboard 管理控制台端口
'etc/emqx.conf' //修改端口
listener.tcp.external = 0.0.0.0:1883
listener.ssl.external = 8883
listener.ws.external = 8083
修改 HTTP API 端口请
'etc/plugins/emqx_management.conf'
management.listener.http = 8080
修改 Dashboard 管理控制台端口请编辑 emqx_dashboard 插件的配置文件
'etc/plugins/emqx_dashboard.conf', 找到下述行,并按需修改端口号:
dashboard.listener.http = 18083
6、插件的启用和停止
插件是 EMQ X 的重要部分,EMQ X 的扩展功能基本都是通过插件实现的。包括 Dashbard 也是插件实现。您可以通过随软件附带的命令行工具 emqx_ctl 来启动和停止各个插件
bin/emqx_ctl plugins load plugin_name //启动插件
bin/emqx_ctl plugins unload plugin_name //停止插件
可以在 Dashboard 的 MANAGEMENT -> plugins 菜单下启动和停止插件
7、修改 Erlang 虚拟机启动参数etc/emqx.conf’中有两个限定了虚拟机允许的最大连接数
node.process_limit //Erlang 虚拟机允许的最大进程数,EMQ X 一个连接会消耗 2 个 Erlang 进程;
node.max_ports //Erlang虚拟机允许的最大 Port 数量,EMQ X 一个连接消耗 1 个 Port
//Erlang虚拟机允许的最大 Port 数量,EMQ X 一个连接消耗 1 个 Port
这连个参数可以设置为:
- node.process_limit: 大于最大允许连接数
- 2 node.max_ports: 大于最大允许连接数