下载apollo 解压后放在C盘
执行cd/d C:\apache-apollo-1.7.1\bin
文件夹中多了mybroker。
进入 mybroker\bin\ 目录,在CMD输入命令:
cd C:\apache-apollo-1.7.1\bin\mybroker\bin
apollo-broker.cmd run
后台登录接口:https://127.0.0.1:61681/或http://127.0.0.1:61680/
账号admin
密码password
Mqttbox
Mqttlens
MQTT.fx
账号密码是apollo的账号密码
package com.litian.iot.ammeter.mqtt;
import com.litian.iot.ammeter.model.Ammeter;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
/**
* ServerMQTT class
*
* @author 孙林业 @Description: 服务器向多个客户端推送主题,即不同客户端可向服务端订阅相同的主题
* @date 2019/3/28
*/
public class ServerMQTT {
// tcp://MQTT安装的服务器地址:MQTT定义的端口号
public static final String HOST = "tcp://127.0.0.1:61613";
// 定义一个主题
public static final String TOPIC = "mytopic";
// 定义MQTT的ID,可以在MQTT服务配置中指定
private static final String clientid = "server11";
private MqttClient client;
private MqttTopic topic11;
private String userName = "admin";
private String passWord = "password";
private MqttMessage message;
/** @throws */
public ServerMQTT() throws MqttException {
// MemoryPersistence设置clientid的保存形式,默认为以内存保存
client = new MqttClient(HOST, clientid, new MemoryPersistence());
connect();
}
/** 用来连接服务器 */
private void connect() {
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(false);
options.setUserName(userName);
options.setPassword(passWord.toCharArray());
// 设置超时时间
options.setConnectionTimeout(10);
// 设置会话心跳时间
options.setKeepAliveInterval(20);
try {
client.setCallback(new PushCallback());
client.connect(options);
topic11 = client.getTopic(TOPIC);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param topic
* @param message
*/
public void publish(MqttTopic topic, MqttMessage message) throws MqttException {
MqttDeliveryToken token = topic.publish(message);
token.waitForCompletion();
System.out.println("message is published completely! " + token.isComplete());
}
/**
* 启动入口
*
* @param args
*/
public static void main(String[] args) throws MqttException {
ServerMQTT server = new ServerMQTT();
Ammeter ammeter =
new Ammeter(
17.83,
2.67,
0.0,
0.05,
103.9,
103.9,
0.0,
0.314,
0.0,
0.314,
0.053,
0.0274,
0.0,
0.0254,
-0.0012,
0.0146,
0.0,
-0.0158,
100.0,
87.7,
100.0,
84.9,
0.0,
0.0,
300.1,
25.3,
0.0,
205.1,
0.0,
1547691840,
0.0,
1547691840,
0.0,
1547691840,
0.0,
1547691840,
0.0,
1547691840,
16.5802,
3.5872,
4.4641,
4.3115,
4.2174);
System.out.println(ammeter.toJSONString());
server.message = new MqttMessage();
server.message.setQos(1);
server.message.setRetained(true);
server.message.setPayload(ammeter.toJSONString().getBytes());
server.publish(server.topic11, server.message);
System.out.println(server.message.isRetained() + "------ratained状态");
}
}
有梦想的马侬 发布了20 篇原创文章 · 获赞 0 · 访问量 1万+ 私信 关注