記錄:這兩天在寫mq的東西,就順便記錄下
注意的是mqtt不能儲存資料,就算儲存也是只能儲存最後一條,所以當服務端斷了不能及時接收資料時容易造成資料丟失,但mqtt的優點就是省流量
1.匯入依賴
>
>
org.eclipse.pahogroupid
>
>
org.eclipse.paho.client.mqttv3artifactid
>
>
1.2.0version
>
dependency
>
>
>
org.fusesource.mqtt-clientgroupid
>
>
mqtt-clientartifactid
>
>
1.14version
>
dependency
>
#mqtt配置資訊
#mqtt-使用者名稱
spring.mqtt.username
: (賬號)
#mqtt-密碼
spring.mqtt.password
: (密碼)
#mqtt-伺服器連線位址,如果有多個,用逗號隔開,如:tcp:
spring.mqtt.url
: tcp://ip:1883(埠預設1883)
#mqtt-連線伺服器預設客戶端id
spring.mqtt.client.id:1
#mqtt-訊息訂閱主題,實際可在呼叫介面時指定,如果有多個,用逗號隔開,
spring.mqtt.topic
: /ksp/test
#連線超時
spring.mqtt.completiontimeout
:3000
3.編寫配置類@component
public
class
mqttcofigbean")
private string username;
@value
("$"
)private string password;
@value
("$"
)private string hosturl;
@value
("$"
)private string clientid;
@value
("$"
)private string msgtopic;
@value
("$"
)private
int completiontimeout;
//連線超時
/** * 獲取使用者名稱
* @return
*/public string getusername()
/** * 獲取密碼
* @return
*/public string getpassword()
/** * 獲取伺服器連線位址
* @return
*/public string gethosturl()
/** * 獲取客戶端id
* @return
*/public string getclientid()
/** * 獲取預設主題
* @return
*/public string[
]getmsgtopic()
/***
* 獲取連線超時時間
* @return
*/public
intgetcompletiontimeout()
}
4.編寫客戶端@component
public
class
mqttconsumer
implements
/** * 用來連線伺服器
*/private
void
connect()
throws exception
client.
setcallback
(new
topmsgcallback
(client,options,msgtopic,qos));
client.
connect
(options)
; client.
subscribe
(msgtopic,qos)
; logger.
info
("mqtt連線成功:"
+mqttcofigbean.
getclientid()
+":"
+client);}
catch
(exception e)
}/**
* 重連
* @throws exception
*/public
void
reconnect()
throws exception
}/**
* 訂閱某個主題
* @param topic
* @param qos
*/public
void
subscribe
(string topic,
int qos)
catch
(mqttexception e)
}public mqttclient getclient()
public
void
setclient
(mqttclient client)
public mqtttopic getmqtttopic()
public
void
setmqtttopic
(mqtttopic mqtttopic)
}
5.編寫服務端@component
public
class
topmsgcallback
implements
mqttcallback
public
topmsgcallback
(mqttclient client, mqttconnectoptions options,string[
] topic,
int[
] qos)
/** * 斷開重連
*/public
void
connectionlost
(throwable cause)
catch
(exception e)}}
/** * 接收到訊息呼叫令牌中呼叫
*/public
void
deliverycomplete
(imqttdeliverytoken token)
/** * 訊息處理
*/public
void
messagearrived
(string topic, mqttmessage message)
throws exception
//物件轉化為位元組碼
public
byte
getbytesfromobject
(serializable obj)
throws exception
bytearrayoutputstream bo =
newbytearrayoutputstream()
; objectoutputstream oo =
newobjectoutputstream
(bo)
; oo.
writeobject
(obj)
;return bo.
tobytearray()
;}}
Springboot整合Redis簡單教程
1.將框架jar包整合到pom中 spring redis redis.clients jedis 3.0.1 2.寫乙個redis的工具類 用來將redis的池初始化到spring容器中 import redis.clients.jedis.jedis import redis.clients.j...
springboot整合redis簡單實現
1.引入jar包 org.springframework.boot spring boot starter data redis 2.1.10.release org.apache.commons commons pool2 2.4.3 2.properties 引數配置 spring.redis....
Vue與Springboot的簡單整合
概述 本文主要講述vue springboot jpa實現登入註冊的方法,springboot 2.1.3.release vue 3.5.5 均為當前最新版本,其中首頁和登入效果如圖所示 1 安裝node和npm 安裝方法參考官網 2 安裝vue和vue cli 1 方式一 npm install...