package com.forezp;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.messaging.MessageChannel;
import org.springframework.web.client.RestTemplate;
import com.forezp.service.PushCallback;
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}
@Bean
public MessageChannel mqttInputChannel() {
return new DirectChannel();
}
@Bean
public MqttPahoClientFactory mqttClientFactory() {
DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
factory.setServerURIs("tcp://127.0.0.1:61613");
factory.setUserName("admin");
factory.setPassword("password");
return factory;
}
@Bean
public MqttClient start() {
try {
// host为主机名,clientid即连接MQTT的客户端ID,一般以唯一标识符表示,MemoryPersistence设置clientid的保存形式,默认为以内存保存
MqttClient client = new MqttClient("tcp://127.0.0.1:61613", "testClient1", new MemoryPersistence());
// MQTT的连接设置
MqttConnectOptions options = new MqttConnectOptions();
// 设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接
options.setCleanSession(true);
// 设置连接的用户名
options.setUserName("admin");
// 设置连接的密码
options.setPassword("password".toCharArray());
// 设置超时时间 单位为秒
options.setConnectionTimeout(10);
// 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制
options.setKeepAliveInterval(20);
// 设置回调
client.setCallback(new PushCallback());
// MqttTopic topic = client.getTopic(TOPIC);
//setWill方法,如果项目中需要知道客户端是否掉线可以调用该方法。设置最终端口的通知消息
// options.setWill(topic, "will>>>>>>close".getBytes(), 2, true);
client.connect(options);
//订阅消息
int[] Qos = {1};
String[] topic1 = {"testTopic2"};
client.subscribe(topic1, Qos);
return client;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
SpringCloudMQTT.zip (81个子文件)
service-hi
pom.xml 2KB
target
test-classes
com
forezp
ServiceHiApplicationTests.class 625B
classes
META-INF
maven
com.forezp
service-hi
pom.xml 2KB
pom.properties 235B
MANIFEST.MF 336B
com
forezp
ServiceHiApplication$MyGateway.class 422B
ServiceHiApplication.class 3KB
bootstrap.yml 161B
mvnw 7KB
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.common.project.facet.core.xml 170B
org.eclipse.core.resources.prefs 155B
org.eclipse.wst.common.project.facet.core.prefs.xml 172B
org.eclipse.jdt.core.prefs 430B
src
test
java
com
forezp
ServiceHiApplicationTests.java 330B
main
resources
bootstrap.yml 161B
java
com
forezp
ServiceHiApplication.java 3KB
.project 976B
.classpath 1KB
.gitignore 249B
.mvn
wrapper
maven-wrapper.jar 48KB
maven-wrapper.properties 110B
mvnw.cmd 5KB
service-ribbon
pom.xml 3KB
target
test-classes
com
forezp
ServiceRibbonApplicationTests.class 637B
classes
META-INF
maven
com.forezp
service-ribbon
pom.xml 3KB
pom.properties 247B
MANIFEST.MF 347B
application.yml 159B
com
forezp
ServiceRibbonApplication.class 3KB
web
HelloControler.class 944B
service
PushCallback.class 2KB
HelloService.class 1KB
mvnw 7KB
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.common.project.facet.core.xml 170B
org.eclipse.core.resources.prefs 155B
org.eclipse.wst.common.project.facet.core.prefs.xml 172B
org.eclipse.jdt.core.prefs 430B
src
test
java
com
forezp
ServiceRibbonApplicationTests.java 334B
main
resources
application.yml 159B
java
com
forezp
ServiceRibbonApplication.java 3KB
web
HelloControler.java 574B
service
HelloService.java 438B
PushCallback.java 2KB
.project 980B
.classpath 1KB
.gitignore 249B
.mvn
wrapper
maven-wrapper.jar 48KB
maven-wrapper.properties 110B
mvnw.cmd 5KB
eureka-server
pom.xml 2KB
.springBeans 471B
target
test-classes
com
forezp
EurekaServerApplicationTests.class 634B
classes
META-INF
spring-configuration-metadata.json 189B
maven
com.forezp
eureka-server
pom.xml 2KB
pom.properties 244B
MANIFEST.MF 345B
application.yml 250B
com
forezp
EurekaServerApplication.class 791B
service
ServerMQTT.class 3KB
listener
EurekaStateChangeListener.class 1KB
.factorypath 22KB
mvnw 7KB
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.common.project.facet.core.xml 170B
org.eclipse.core.resources.prefs 155B
org.eclipse.wst.common.project.facet.core.prefs.xml 172B
org.eclipse.jdt.apt.core.prefs 68B
org.eclipse.jdt.core.prefs 488B
src
test
java
com
forezp
EurekaServerApplicationTests.java 333B
main
resources
application.yml 250B
java
com
forezp
EurekaServerApplication.java 409B
service
ServerMQTT.java 3KB
listener
EurekaStateChangeListener.java 970B
.project 979B
.classpath 1KB
.gitignore 249B
.mvn
wrapper
maven-wrapper.jar 48KB
maven-wrapper.properties 110B
mvnw.cmd 5KB
.apt_generated
共 81 条
- 1
资源评论
- imephone112018-11-05值得参考!!不错
- rockemd2019-10-08已下载,学习学习
- autowanglei2020-04-10不错的资源
涛哥是个大帅比
- 粉丝: 287
- 资源: 31
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功