成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解

瀏覽:145日期:2023-03-16 17:24:34
前言

近期為了準(zhǔn)備畢設(shè),準(zhǔn)備使用SpringBoot搭建mqtt后端,本篇主要記錄了在IDEA中搭建SpringBoot mqtt的多模塊項(xiàng)目的過(guò)程

開發(fā)工具及系統(tǒng)環(huán)境 IDE:IntelliJ IDEA 2020.2 操作系統(tǒng):Windows 10 2004 Java Version:1.8 SpringBoot Version:2.1.17.RELEASE項(xiàng)目路徑

Study |----study-common# 存放公共類 |----study-mapper# mapper層 |----study-mqtt # mqtt相關(guān)配置文件及接口 |----study-service # service層 |----study-serviceimpl # service的實(shí)現(xiàn)類 |----study-web # web層 |----pom.xml配置過(guò)程1. 搭建父項(xiàng)目

在IDEA中新建一個(gè)SpringBoot工程

SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解

這里我使用了阿里云的啟動(dòng)服務(wù),正常使用可以直接用原本的啟動(dòng)服務(wù)

根據(jù)你的需求選擇Java版本,初始化類型并配置groupID和artifactId,我這里配置成我的域名的反寫,并將artifactId定義成Study。配置完成后單擊Next

SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解

這一步選擇你需求的SpringBoot版本,我這里選擇的是2.1.17.RELEASE,然后單擊NEXT。在這一步中不需要勾選任何依賴。

SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解

選擇保存的路徑,點(diǎn)擊Finish完成創(chuàng)建。

SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解

刪除不需要的文件。將目錄下的src/,HELP.md,mvnw,mvnw.cmd等文件全部刪除(不包括.gitigore)

SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解

至此,父項(xiàng)目一級(jí)已經(jīng)創(chuàng)建完成,最后項(xiàng)目目錄如下:

SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解

2. 搭建子項(xiàng)目

右鍵項(xiàng)目根目錄->新建->新模塊

SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解

選擇Maven,單擊Next

SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解

配置父項(xiàng),項(xiàng)目名稱以及構(gòu)建坐標(biāo),完成后單擊Finish。這里以study-common模塊為例

SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解

以此類推創(chuàng)建整個(gè)項(xiàng)目,項(xiàng)目目錄如圖所示

SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解

至此,我們搭建完所有的子項(xiàng)目,接下來(lái)開始配置各個(gè)項(xiàng)目的pom文件

3. 配置各個(gè)模塊

1. 配置父項(xiàng)目

配置父項(xiàng)目的pom.xml文件

父項(xiàng)目的pom.xml主要是對(duì)子項(xiàng)目的引用起到一個(gè)約束作用,固在父項(xiàng)目的pom.xml需要使用dependencyManagement這一項(xiàng)來(lái)約束子項(xiàng)目中各個(gè)依賴的版本。在父項(xiàng)目中可以引用子項(xiàng)目都用得到的引用。父項(xiàng)目的pom.xml文件如下:

<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <groupId>com.itbu</groupId> <artifactId>study</artifactId> <packaging>pom</packaging> <version>1.0.0</version> <modules><module>study-common</module><module>study-service</module><module>study-serviceimpl</module><module>study-web</module><module>study-mapper</module><module>study-mqtt</module> </modules> <name>study</name> <description>Demo project for Spring Boot</description> <parent><artifactId>spring-boot-starter-parent</artifactId><groupId>org.springframework.boot</groupId><version>2.1.17.RELEASE</version><relativePath/> </parent> <properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-boot.version>2.1.17.RELEASE</spring-boot.version><mybatis.boot.starter.version>2.1.4</mybatis.boot.starter.version><mysql.connector.java.version>8.0.22</mysql.connector.java.version><druid.version>1.2.0</druid.version><integration.version>2.3.7.RELEASE</integration.version><stream.integration.version>5.4.2</stream.integration.version><mqtt.integration.version>5.4.2</mqtt.integration.version> </properties> <dependencies><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope></dependency> </dependencies> <dependencyManagement><dependencies> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope> </dependency> <dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>${mybatis.boot.starter.version}</version> </dependency> <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.connector.java.version}</version> </dependency> <dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>${druid.version}</version> </dependency></dependencies> </dependencyManagement> <build><plugins> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.1.17.RELEASE</version> </plugin></plugins> </build></project>

2. 配置common模塊

配置pom.xml文件

common模塊主要包括一些通用的類和接口,固這里只需要配置下parent這一項(xiàng)指向父項(xiàng)目即可,common模塊的pom.xml配置文件如下:

<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd'> <parent><artifactId>study</artifactId><groupId>com.itbu</groupId><version>1.0.0</version> </parent> <groupId>com.itbu.study</groupId> <artifactId>common</artifactId> <version>1.0.0</version> <modelVersion>4.0.0</modelVersion> <packaging>jar</packaging> <properties><java.version>1.8</java.version> </properties> <build><plugins> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId> </plugin></plugins> </build></project>

創(chuàng)建所需要的類和接口

配置完pom.xml后,就可以在目錄下創(chuàng)建所需要的類以及接口。在本項(xiàng)目中我分別創(chuàng)建了作為pojo類的UserBean和作為json返回結(jié)果的JsonResult類作為通用類,項(xiàng)目結(jié)構(gòu)如下:

study-common|----pom.xml|----src|----test|----main|----resources|----java|----com.itbu.study.common|----bean|----UserBean.java|----result|----JsonResult.java

UserBean.java文件

package com.itbu.study.common.bean;public class UserBean { private int id; private String username; private String password; public int getId() {return id; } public String getPassword() {return password; } public String getUsername() {return username; } public void setPassword(String password) {this.password = password; } public void setId(int id) {this.id = id; } public void setUsername(String username) {this.username = username; } public UserBean(){ } public UserBean(String username, String password){this.username = username;this.password = password; }}

JsonResult.java文件

package com.itbu.study.common.result;public class JsonResult<T> { private int code; private String msg; private T data; public JsonResult(int Code,String msg){this.code = Code;this.msg = msg; } public JsonResult(T data) {this.data = data;this.code = 0;this.msg = '操作成功!'; } public JsonResult(T data, String msg) {this.data = data;this.code = 0;this.msg = msg; } public String getMsg() {return msg; } public int getCode() {return code; } public T getData() {return data; } public void setData(T data) {this.data = data; } public void setMsg(String msg) {this.msg = msg; } public void setCode(int code) {this.code = code; }}

3. 配置mapper模塊

mapper模塊對(duì)應(yīng)的是mapper層,也就是我們常說(shuō)的DAO層,用于與數(shù)據(jù)庫(kù)進(jìn)行通信,讀寫操作。這里我們用的持久層框架是Mybatis,連接的數(shù)據(jù)庫(kù)是mysql數(shù)據(jù)庫(kù)。同時(shí)需要common模塊中的各個(gè)pojo類,這里需要引入各個(gè)引用。操作步驟如下:

配置pom.xml文件

這里我們需要引入mybatis,druid和mysql支持,固配置文件編寫如下:

<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <groupId>com.itbu.study</groupId> <artifactId>mapper</artifactId> <version>1.0.0</version> <name>mapper</name> <description>Demo project for Spring Boot</description> <packaging>jar</packaging> <parent><artifactId>study</artifactId><groupId>com.itbu</groupId><version>1.0.0</version><relativePath>../pom.xml</relativePath> </parent> <properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-boot.version>2.1.17.RELEASE</spring-boot.version> </properties> <dependencies><!-- 內(nèi)部引用 --><dependency> <groupId>com.itbu.study</groupId> <artifactId>common</artifactId> <version>1.0.0</version></dependency><!-- 外部引用 --><dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId></dependency><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId></dependency><dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId></dependency> </dependencies> <build><plugins> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId> </plugin></plugins> </build></project>

編寫接口和映射文件

和不分模塊的操作一樣,在resource文件夾下創(chuàng)建mapper目錄,并編寫用于映射的xml文件。同時(shí)創(chuàng)建對(duì)應(yīng)的接口,本項(xiàng)目工程目錄大致如下:

study-mapper|----pom.xml|----src|----test|----main|----java|----com.itbu.study.mapper|----UserMapper.java|----resources|----mapper|----UserMapper.xml

UserMapper.java文件

package com.itbu.study.mapper;import com.itbu.study.common.bean.UserBean;import java.util.List;public interface UserMapper { List<UserBean> getAll();}

UserMapper.xml

<?xml version='1.0' encoding='UTF-8' ?><!DOCTYPE mapper PUBLIC '-//mybatis.org//DTD Mapper 3.0//EN' 'http://mybatis.org/dtd/mybatis-3-mapper.dtd' ><mapper namespace='com.itbu.study.mapper.UserMapper'> <select resultType='com.itbu.study.common.bean.UserBean'>select * from mqtt.user_table </select></mapper>

4. 配置service模塊

service模塊也就是service層,主要是一些服務(wù)接口方便給controller層調(diào)用。步驟如下:

配置pom.xml文件

service層需要用到common模塊中的pojo類,這里需要對(duì)該模塊添加依賴項(xiàng)

<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd'> <parent><artifactId>study</artifactId><groupId>com.itbu</groupId><version>1.0.0</version><relativePath>../pom.xml</relativePath> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.itbu.study</groupId> <artifactId>service</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>service</name> <properties><java.version>1.8</java.version> </properties> <dependencies><dependency> <groupId>com.itbu.study</groupId> <artifactId>common</artifactId> <version>1.0.0</version></dependency> </dependencies> <build><plugins> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId> </plugin></plugins> </build></project>

編寫service各個(gè)接口

這里接口與mapper層類似,就不一一贅述,這里直接放代碼

UserService.java

package com.itbu.study.service;import com.itbu.study.common.bean.UserBean;import java.util.List;public interface UserService { List<UserBean> getAll();}

5. 配置serviceimpl模塊

serviceimpl即service接口的各個(gè)實(shí)現(xiàn)類,實(shí)現(xiàn)步驟如下:

配置pom.xml文件

serviceimpl需要mapper層的支持,需要實(shí)現(xiàn)service層的各個(gè)接口,也需要引用到common模塊中的pojo類,我們直接加上這三項(xiàng)的依賴

<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <groupId>com.itbu.study</groupId> <artifactId>serviceimpl</artifactId> <version>1.0.0</version> <name>serviceimpl</name> <description>Demo project for Spring Boot</description> <packaging>jar</packaging> <parent><artifactId>study</artifactId><groupId>com.itbu</groupId><version>1.0.0</version> </parent> <properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-boot.version>2.1.17.RELEASE</spring-boot.version> </properties> <dependencies><dependency> <groupId>com.itbu.study</groupId> <artifactId>common</artifactId> <version>1.0.0</version></dependency><dependency> <groupId>com.itbu.study</groupId> <artifactId>service</artifactId> <version>1.0.0</version></dependency><dependency> <groupId>com.itbu.study</groupId> <artifactId>mapper</artifactId> <version>1.0.0</version></dependency> </dependencies> <build><plugins> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId> </plugin></plugins> </build></project>

根據(jù)service各個(gè)接口編寫實(shí)現(xiàn)類

UserServiceimpl.java 注意在實(shí)現(xiàn)類上要加上@Service標(biāo)注以用于SpringBoot框架識(shí)別

package com.itbu.study.serviceimpl;import com.itbu.study.common.bean.UserBean;import com.itbu.study.mapper.UserMapper;import com.itbu.study.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public List<UserBean> getAll() {return userMapper.getAll(); }}

6. 配置web模塊

接下來(lái)就到了核心部分,也就是與不分模塊配置差異較大的部分。Web層需要配置啟動(dòng)類以及配置文件,內(nèi)容較多,配置步驟如下:

配置pom.xml文件

Web層將直接引用Service層的各個(gè)接口,Common模塊的各個(gè)類,這里我們直接加上依賴

<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <groupId>com.itbu.study</groupId> <artifactId>serviceimpl</artifactId> <version>1.0.0</version> <name>serviceimpl</name> <description>Demo project for Spring Boot</description> <packaging>jar</packaging> <parent><artifactId>study</artifactId><groupId>com.itbu</groupId><version>1.0.0</version> </parent> <properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-boot.version>2.1.17.RELEASE</spring-boot.version> </properties> <dependencies><dependency> <groupId>com.itbu.study</groupId> <artifactId>common</artifactId> <version>1.0.0</version></dependency><dependency> <groupId>com.itbu.study</groupId> <artifactId>service</artifactId> <version>1.0.0</version></dependency><dependency> <groupId>com.itbu.study</groupId> <artifactId>mapper</artifactId> <version>1.0.0</version></dependency> </dependencies> <build><plugins> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId> </plugin></plugins> </build></project>

編寫各個(gè)Controller以及配置文件

先根據(jù)以下的項(xiàng)目結(jié)構(gòu)創(chuàng)建對(duì)應(yīng)的文件,項(xiàng)目結(jié)構(gòu)如下:

study-web|----pom.xml|----src|----test|----java|----com.itbu.study.web|----WebApplicationTests.java #測(cè)試類|----main|----java|----com.itbu.study.web|----WebApplication.java|----controller|----ApiController.java |----resources |----config |----application.yml |----application-dev.yml

編寫啟動(dòng)類,記得加上MapperScan

WebApplication.java

package com.itbu.study.web;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication(scanBasePackages = {'com.itbu.study.*'})@MapperScan('com.itbu.study.mapper')public class WebApplication { public static void main(String[] args) {SpringApplication.run(WebApplication.class, args); }}

編寫Controller層

ApiController.java

package com.itbu.study.web.controller;import com.itbu.study.common.bean.UserBean;import com.itbu.study.common.result.JsonResult;import com.itbu.study.service.UserService;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import javax.xml.ws.RequestWrapper;import java.util.List;@RestController@RequestMapping('/api')public class ApiController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); public ApiController(UserService userService){this.userService = userService; } private final UserService userService; @RequestMapping('/index') public List<UserBean> index(){return userService.getAll(); }}

在配置文件中設(shè)置數(shù)據(jù)源和mapper映射文件以及監(jiān)聽端口

application-dev.xml

server: port: 10000mybatis: mapper-locations: classpath*:mapper/*.xml #注意此處與未分模塊的區(qū)別spring: datasource: username: root password: 123456 url: jdbc:mysql://192.168.28.88:10090/mqtt?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC driver-class-name: com.mysql.cj.jdbc.Driver

7. 測(cè)試

至此,我們以及配置完了一個(gè)多模塊的MQTT后端基本項(xiàng)目,接下來(lái)進(jìn)行簡(jiǎn)單測(cè)試

運(yùn)行項(xiàng)目并在瀏覽器中輸入http://localhost:10000/api/index, 返回以下結(jié)果表明測(cè)試成功

SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解

4. 配置MQTT模塊

前面我們完成了SpringBoot基礎(chǔ)項(xiàng)目的配置,接下來(lái)我們將mqtt也做成模塊,步驟如下:

配置pom.xml文件

我們集成mqtt功能主要使用了spring-integration-mqtt這個(gè)jar包,所以我們?cè)趐om中添加對(duì)這個(gè)包的依賴,pom.xml文件如下:

<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd'> <parent><artifactId>study</artifactId><groupId>com.itbu</groupId><version>1.0.0</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>com.itbu.study</groupId> <artifactId>mqtt</artifactId> <packaging>jar</packaging> <properties><java.version>1.8</java.version> </properties> <dependencies><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-integration</artifactId></dependency><dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-stream</artifactId></dependency><dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-mqtt</artifactId></dependency> </dependencies> <build><plugins> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId> </plugin></plugins> </build></project>

在study-web的配置文件中添加如下配置:

mqtt: enabled: true username: root password: 123456 url: tcp://192.168.28.88:15005 producer: clientId: server defaultTopic: default consumer: clientId: client defaultTopic: default

編寫MQTT各個(gè)配置類以及方法

項(xiàng)目結(jié)構(gòu)如下:

study-mqtt|----pom.xml|----src|----test|----main|----resources|----java|----com.itbu.study.mqtt|----MqttBaseConfig.java|----MqttInConfig.java|----MqttOutConfig.java|----MqttMessageReceiver.java|----MqttMessageSender.java

MqttBaseConfig.java

package com.itbu.study.mqtt;import org.eclipse.paho.client.mqttv3.MqttConnectOptions;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;import org.springframework.integration.mqtt.core.MqttPahoClientFactory;@Configuration@ConditionalOnProperty(value = 'mqtt.enabled', havingValue = 'true')public class MqttBaseConfig { @Value('${mqtt.url:#{null}}') private String[] url; @Value('${mqtt.username:}') private String username; @Value('${mqtt.password:}') private String password; @Bean public MqttPahoClientFactory factory(){DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();MqttConnectOptions options = new MqttConnectOptions();if(username != null) options.setUserName(username);if(password != null) options.setPassword(password.toCharArray());options.setServerURIs(url);factory.setConnectionOptions(options);return factory; }}

MqttInConfig.java

package com.itbu.study.mqtt;import com.itbu.study.mqtt.MqttMessageReceiver;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.integration.annotation.ServiceActivator;import org.springframework.integration.channel.DirectChannel;import org.springframework.integration.core.MessageProducer;import org.springframework.integration.mqtt.core.MqttPahoClientFactory;import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;import org.springframework.messaging.MessageChannel;import org.springframework.messaging.MessageHandler;@Configuration@ConditionalOnProperty(value = 'mqtt.enabled', havingValue = 'true')public class MqttInConfig { private final MqttMessageReceiver mqttMessageReceiver; public MqttInConfig(MqttMessageReceiver mqttMessageReceiver){this.mqttMessageReceiver = mqttMessageReceiver; } @Value('${mqtt.producer.clientId:') private String clientId; @Value('${mqtt.producer.defaultTopic}') private String topic; @Bean public MessageChannel mqttInputChannel(){return new DirectChannel(); } @Bean public MessageProducer channelInbound(MessageChannel mqttInputChannel, MqttPahoClientFactory factory){MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(clientId, factory, topic);adapter.setCompletionTimeout(5000);adapter.setConverter(new DefaultPahoMessageConverter());adapter.setQos(2);adapter.setOutputChannel(mqttInputChannel);return adapter; } @Bean @ServiceActivator(inputChannel = 'mqttInputChannel') public MessageHandler mqttMessageHandler(){return this.mqttMessageReceiver; }}

MqttOutConfig.java

package com.itbu.study.mqtt;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.integration.annotation.ServiceActivator;import org.springframework.integration.channel.DirectChannel;import org.springframework.integration.mqtt.core.MqttPahoClientFactory;import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;import org.springframework.messaging.MessageChannel;import org.springframework.messaging.MessageHandler;@Configuration@ConditionalOnProperty(value = 'mqtt.enabled', havingValue = 'true')public class MqttOutConfig { @Value('${mqtt.consumer.clientId:}') private String clientId; @Value('${mqtt.consumer.defaultTopic}') private String topic; @Bean public MessageChannel mqttOutputChannel(){return new DirectChannel(); } @Bean @ServiceActivator(inputChannel = 'mqttOutputChannel') public MessageHandler mqttOutBound(MqttPahoClientFactory factory){MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(clientId, factory);messageHandler.setAsync(true);messageHandler.setDefaultQos(2);messageHandler.setDefaultTopic(topic);return messageHandler; }}

MqttMessageReceiver.java

package com.itbu.study.mqtt;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;import org.springframework.context.annotation.ComponentScan;import org.springframework.integration.mqtt.support.MqttHeaders;import org.springframework.messaging.Message;import org.springframework.messaging.MessageHandler;import org.springframework.messaging.MessagingException;import org.springframework.stereotype.Component;@Component@ConditionalOnProperty(value = 'mqtt.enabled',havingValue = 'true')public class MqttMessageReceiver implements MessageHandler { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Override public void handleMessage(Message<?> message) throws MessagingException {String topic = String.valueOf(message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC));String payload = String.valueOf(message.getPayload());logger.info('接收到 mqtt消息, 主題:{} 消息:{}', topic, payload); }}

MqttMessageSender.java

package com.itbu.study.mqtt;import org.springframework.integration.annotation.MessagingGateway;import org.springframework.integration.mqtt.support.MqttHeaders;import org.springframework.messaging.handler.annotation.Header;import org.springframework.stereotype.Component;@MessagingGateway(defaultRequestChannel = 'mqttOutputChannel')@Componentpublic interface MqttMessageSender { void sendToMqtt(String data); void sendToMqtt(@Header(MqttHeaders.TOPIC) String topic, String payload); void sendToMqtt(@Header(MqttHeaders.TOPIC) String topic, @Header(MqttHeaders.QOS) int qos, String payload);}

在啟動(dòng)類中添加@IntegrationComponentScan注解

package com.itbu.study.web;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.integration.annotation.IntegrationComponentScan;@SpringBootApplication(scanBasePackages = {'com.itbu.study.*','com.itbu.study.mqtt'})@MapperScan('com.itbu.study.mapper')@IntegrationComponentScan(basePackages = 'com.itbu.study.mqtt') //這里添加,不然無(wú)法自動(dòng)注入public class WebApplication { public static void main(String[] args) {SpringApplication.run(WebApplication.class, args); }}

編寫對(duì)應(yīng)Controller,我這里直接在ApiController上修改了

package com.itbu.study.web.controller;import com.itbu.study.common.bean.UserBean;import com.itbu.study.common.result.JsonResult;import com.itbu.study.mqtt.MqttMessageSender;import com.itbu.study.service.UserService;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import javax.xml.ws.RequestWrapper;import java.util.List;@RestController@RequestMapping('/api')public class ApiController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); private final MqttMessageSender mqttMessageSender; public ApiController(MqttMessageSender mqttMessageSender, UserService userService){this.mqttMessageSender = mqttMessageSender;this.userService = userService; } private final UserService userService; @RequestMapping('/index') public List<UserBean> index(){return userService.getAll(); } @RequestMapping('/mqtt') public JsonResult<?> mqtt_sender(@RequestParam('msg')String msg){logger.info('Send mqtt msg: {}', msg);mqttMessageSender.sendToMqtt(msg);logger.info('Send successfully!');return new JsonResult<>(0,'Send Successfully'); }}

測(cè)試

首先我們運(yùn)行后端項(xiàng)目,可以看到日志打出以下輸出,說(shuō)明后端項(xiàng)目正常啟動(dòng)

SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解

然后我們使用mqtt.fx這個(gè)軟件往訂閱主題default發(fā)送helloworld

SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解

發(fā)現(xiàn)日志打印如下信息,表面輸入通道正常:

SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解

然后我們?cè)跒g覽器中輸入http://localhost:10000/api/mqtt?msg=1234556 并按下回車,瀏覽器顯示如下:

SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解

可以看到日志中打印了如下內(nèi)容:

SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解

因?yàn)槲覀冇嗛喌闹黝}也是default,所以也收到了生產(chǎn)者發(fā)送的信息,我們打開mqtt.fx, 訂閱default主題,可以收到如下信息:

SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解

測(cè)試成功

最后放上工程的源碼:GitHub

到此這篇關(guān)于SpringBoot集成mqtt的多模塊項(xiàng)目配置詳解的文章就介紹到這了,更多相關(guān)SpringBoot mqtt多模塊內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久
欧美一区二区三区成人| av激情亚洲男人天堂| 国产成人自拍在线| 日本韩国精品一区二区在线观看| 又紧又大又爽精品一区二区| 在线播放豆国产99亚洲| 国产精品天天看| 欧美特黄一级| 国产精品午夜免费| 国产精品a级| 国产精品第五页| 亚洲精品乱码久久久久久蜜桃麻豆 | 精品福利av导航| 国产精品性做久久久久久| 欧美日韩1区2区3区| 国产精品三级av| 亚洲三级观看| 亚洲国产日韩一级| 久久一区二区三区av| 日本怡春院一区二区| 欧美视频一区二区| 国产一区二区网址| 日韩欧美国产三级| jlzzjlzz亚洲女人18| 久久先锋影音av| 色综合婷婷久久| 国产欧美一区二区精品性色超碰| 午夜久久影院| 亚洲另类中文字| 亚洲永久视频| 日韩国产欧美在线播放| 欧美午夜寂寞影院| 国产一区二区免费在线| 欧美成人bangbros| 欧美一区高清| 亚洲欧美日韩国产另类专区| 国产精品区一区| 日本在线播放一区二区三区| 欧美日韩精品欧美日韩精品一| 国产福利一区二区三区在线视频| 精品国产91亚洲一区二区三区婷婷| 91碰在线视频| 综合亚洲深深色噜噜狠狠网站| 国产九区一区在线| 免费成人在线播放| 日韩限制级电影在线观看| 91麻豆swag| 国产精品免费av| 久久国产精品一区二区三区| 精品一区二区三区久久| 精品国产乱码久久久久久蜜臀| 国产精品v欧美精品v日本精品动漫| 亚洲天堂久久久久久久| 色婷婷一区二区三区四区| 国产精品91xxx| 国产调教视频一区| 一区在线视频观看| 天天操天天干天天综合网| 91麻豆精品国产| 欧美三级免费| 午夜一区二区三区在线观看| 欧美老肥妇做.爰bbww视频| 久久99国产乱子伦精品免费| 2021中文字幕一区亚洲| 亚洲国产激情| 奇米综合一区二区三区精品视频| 日韩欧美国产一区二区在线播放| 雨宫琴音一区二区在线| 六月婷婷色综合| 国产日韩欧美精品一区| 久久福利精品| 国产成人免费视| 亚洲天堂av一区| 欧美艳星brazzers| 91丝袜美腿高跟国产极品老师| 亚洲一区二三区| 欧美一区二区黄| 亚洲高清激情| 韩国一区二区三区| 亚洲国产精品av| 日本精品裸体写真集在线观看| av在线不卡电影| 亚洲成人手机在线| 欧美精品v国产精品v日韩精品| 午夜天堂精品久久久久| 日韩va欧美va亚洲va久久| 精品国产伦理网| 午夜一级久久| 白白色亚洲国产精品| 亚洲午夜电影在线| 精品噜噜噜噜久久久久久久久试看| 一区二区三区国产在线| 国产成人精品影院| 亚洲国产视频在线| 久久影院视频免费| 91久久免费观看| 国模精品娜娜一二三区| 精品一区二区三区在线播放视频| 国产精品不卡在线观看| 欧美精选午夜久久久乱码6080| 亚洲国产精品123| 国产精品自拍毛片| 一区二区三区精品视频| 日韩欧美激情在线| 先锋a资源在线看亚洲| 97精品超碰一区二区三区| 日本午夜一区二区| 中文一区二区在线观看| 欧美日韩国产中文| 国产精品女主播一区二区三区| av一二三不卡影片| 日韩国产精品大片| 精品国产1区二区| 欧美性生活久久| 亚洲二区视频| 成人18视频日本| 日本不卡一区二区| 自拍偷拍亚洲激情| 久久亚洲精品小早川怜子| 欧美性猛交xxxxxx富婆| 日韩视频在线观看国产| 91香蕉视频在线| 国产一区在线精品| 亚洲不卡在线观看| 亚洲欧洲国产日韩| 精品国产91九色蝌蚪| 欧美日韩国产首页| 久久精品主播| 一区二区三区我不卡| www.日韩av| 韩国精品免费视频| 午夜精品久久久久久久| 国产精品欧美经典| www成人在线观看| 欧美一级在线免费| 欧美日韩激情一区二区三区| 免费一区视频| 亚洲人成人一区二区三区| 欧美在线网站| 成人av一区二区三区| 黄页网站大全一区二区| 亚洲大片在线观看| 一区二区三区丝袜| 国产精品传媒入口麻豆| 久久久久9999亚洲精品| 日韩欧美在线影院| 欧美性淫爽ww久久久久无| 久久高清免费观看| 国产午夜精品一区二区三区欧美 | 亚洲超丰满肉感bbw| 亚洲黄色免费电影| 亚洲欧美日韩小说| 成人欧美一区二区三区黑人麻豆 | 欧美精品在线一区二区| 在线一区二区三区四区五区| 欧美专区18| 美女被久久久| 99精品热视频只有精品10| aaa欧美日韩| yourporn久久国产精品| 国产凹凸在线观看一区二区| 国模大尺度一区二区三区| 精品在线观看免费| 精品亚洲porn| 国产尤物一区二区| 韩国v欧美v亚洲v日本v| 国产在线精品视频| 国产伦精品一区二区三区免费迷| 久久99国产精品免费网站| 免费一级欧美片在线观看| 免费观看一级特黄欧美大片| 日本中文字幕不卡| 日韩精品成人一区二区在线| 日韩高清一级片| 秋霞午夜鲁丝一区二区老狼| 麻豆国产91在线播放| 国内精品伊人久久久久av一坑| 国产综合一区二区| 懂色av中文一区二区三区| 99久久免费视频.com| 91麻豆swag| 亚洲高清毛片| 国产日产精品一区二区三区四区的观看方式 | 91视频你懂的| 91伊人久久大香线蕉| 欧美精品免费观看二区| 狠狠综合久久| 国产精品久久一区二区三区| 久久久久se| 在线观看网站黄不卡| 欧美日韩在线播放三区四区| 欧美高清www午色夜在线视频| 91精品中文字幕一区二区三区| 日韩久久久精品| 欧美激情资源网| 亚洲欧美日韩中文字幕一区二区三区| 亚洲国产wwwccc36天堂| 免费精品视频在线| 黄一区二区三区| 99在线精品免费|