SpringBoot集成mqtt的多模块项目配置详解

前言

近期为了准备毕设,准备使用SpringBoot搭建mqtt后端,本篇主要记录了在IDEA中搭建SpringBoot mqtt的多模块项目的过程

开发工具及系统环境

  • IDE:IntelliJ IDEA 2020.2
  • 操作系统:Windows 10 2004
  • Java Version:1.8
  • SpringBoot Version:2.1.17.RELEASE

项目路径

Study
    |----study-common        # 存放公共类
    |----study-mapper        # mapper层
    |----study-mqtt          # mqtt相关配置文件及接口
    |----study-service       # service层
    |----study-serviceimpl   # service的实现类
    |----study-web           # web层
	|----pom.xml

配置过程

1. 搭建父项目

在IDEA中新建一个SpringBoot工程

这里我使用了阿里云的启动服务,正常使用可以直接用原本的启动服务

根据你的需求选择Java版本,初始化类型并配置groupID和artifactId,我这里配置成我的域名的反写,并将artifactId定义成Study。配置完成后单击Next

这一步选择你需求的SpringBoot版本,我这里选择的是2.1.17.RELEASE,然后单击NEXT。在这一步中不需要勾选任何依赖。

选择保存的路径,点击Finish完成创建。

删除不需要的文件。将目录下的src/,HELP.md,mvnw,mvnw.cmd等文件全部删除(不包括.gitigore

至此,父项目一级已经创建完成,最后项目目录如下:

2. 搭建子项目

右键项目根目录->新建->新模块

选择Maven,单击Next

配置父项,项目名称以及构建坐标,完成后单击Finish。这里以study-common模块为例

以此类推创建整个项目,项目目录如图所示

至此,我们搭建完所有的子项目,接下来开始配置各个项目的pom文件

3. 配置各个模块

1. 配置父项目

配置父项目的pom.xml文件

父项目的pom.xml主要是对子项目的引用起到一个约束作用,固在父项目的pom.xml需要使用dependencyManagement这一项来约束子项目中各个依赖的版本。在父项目中可以引用子项目都用得到的引用。父项目的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这一项指向父项目即可,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>

创建所需要的类和接口

配置完pom.xml后,就可以在目录下创建所需要的类以及接口。在本项目中我分别创建了作为pojo类的UserBean和作为json返回结果的JsonResult类作为通用类,项目结构如下:

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模块对应的是mapper层,也就是我们常说的DAO层,用于与数据库进行通信,读写操作。这里我们用的持久层框架是Mybatis,连接的数据库是mysql数据库。同时需要common模块中的各个pojo类,这里需要引入各个引用。操作步骤如下:

配置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>
        <!-- 内部引用 -->
        <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文件夹下创建mapper目录,并编写用于映射的xml文件。同时创建对应的接口,本项目工程目录大致如下:

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 id="getAll" resultType="com.itbu.study.common.bean.UserBean">
        select * from mqtt.user_table
    </select>
</mapper>

4. 配置service模块

service模块也就是service层,主要是一些服务接口方便给controller层调用。步骤如下:

配置pom.xml文件

service层需要用到common模块中的pojo类,这里需要对该模块添加依赖项

<?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各个接口

这里接口与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接口的各个实现类,实现步骤如下:

配置pom.xml文件

serviceimpl需要mapper层的支持,需要实现service层的各个接口,也需要引用到common模块中的pojo类,我们直接加上这三项的依赖

<?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>

根据service各个接口编写实现类

UserServiceimpl.java 注意在实现类上要加上@Service标注以用于SpringBoot框架识别

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;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<UserBean> getAll() {
        return userMapper.getAll();
    }
}

6. 配置web模块

接下来就到了核心部分,也就是与不分模块配置差异较大的部分。Web层需要配置启动类以及配置文件,内容较多,配置步骤如下:

配置pom.xml文件

Web层将直接引用Service层的各个接口,Common模块的各个类,这里我们直接加上依赖

<?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>

编写各个Controller以及配置文件

先根据以下的项目结构创建对应的文件,项目结构如下:

study-web
	|----pom.xml
	|----src
		|----test
			|----java
				|----com.itbu.study.web
					|----WebApplicationTests.java  #测试类
		|----main
			|----java
				|----com.itbu.study.web
					|----WebApplication.java
					|----controller
						|----ApiController.java
         	|----resources
         		|----config
         			|----application.yml
         			|----application-dev.yml

编写启动类,记得加上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();
    }
}

在配置文件中设置数据源和mapper映射文件以及监听端口

application-dev.xml

server:
  port: 10000
mybatis:
  mapper-locations: classpath*:mapper/*.xml  #注意此处与未分模块的区别
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. 测试

至此,我们以及配置完了一个多模块的MQTT后端基本项目,接下来进行简单测试

运行项目并在浏览器中输入http://localhost:10000/api/index, 返回以下结果表明测试成功

4. 配置MQTT模块

前面我们完成了SpringBoot基础项目的配置,接下来我们将mqtt也做成模块,步骤如下:

配置pom.xml文件

我们集成mqtt功能主要使用了spring-integration-mqtt这个jar包,所以我们在pom中添加对这个包的依赖,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各个配置类以及方法

项目结构如下:

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")
@Component
public 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);
}

在启动类中添加@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") //这里添加,不然无法自动注入
public class WebApplication {
    public static void main(String[] args) {
        SpringApplication.run(WebApplication.class, args);
    }
}

编写对应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");
    }

}

测试

首先我们运行后端项目,可以看到日志打出以下输出,说明后端项目正常启动

然后我们使用mqtt.fx这个软件往订阅主题default发送helloworld

发现日志打印如下信息,表面输入通道正常:

然后我们在浏览器中输入http://localhost:10000/api/mqtt?msg=1234556 并按下回车,浏览器显示如下:

可以看到日志中打印了如下内容:

因为我们订阅的主题也是default,所以也收到了生产者发送的信息,我们打开mqtt.fx, 订阅default主题,可以收到如下信息:

测试成功

最后放上工程的源码:GitHub

到此这篇关于SpringBoot集成mqtt的多模块项目配置详解的文章就介绍到这了,更多相关SpringBoot mqtt多模块内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • SpringBoot+MQTT+apollo实现订阅发布功能的示例

    由于最近公司在开发一款后台与安卓的更新系统,经过再三研究之后,也是选择Mqtt这个目前流行的框架.为了能够让项目运营起来,最终虽说是选择ActiveMQ.但在这个过程中,也是发现Apollo作为服务器也是相当不错.当然对于后者已经被apace放弃,不过今天还是和大家整理一下SpringBoot+MQTT+apollo实现订阅发布功能的全过程. 对于项目首先需要用到的前提东西,比如Apollo如何下载,以及MQTT测试工具在这里就不多说.如果真的不懂私聊Damon吧,在这里就不浪费时间. 对于项目

  • springboot集成mqtt的实践开发

    序 MQTT(Message Queuing Telemetry Transport)是基于二进制消息的发布/订阅编程模式的消息协议,非常适合需要低功耗和网络带宽有限的IoT场景.这里简单介绍一下如何在springboot中集成. maven <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-integration</arti

  • springboot 实现mqtt物联网的示例代码

    Springboot整合mybatisPlus+mysql+druid+swaggerUI+ mqtt 整合mqtt整合druid整合mybatis-plus完整pom完整yml整合swaggerUi整合log4j MQTT 物联网系统基本架构本物联网系列 mqtt) 整合mqtt <!--mqtt依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>

  • SpringBoot2.0集成MQTT消息推送功能实现

    这几天在弄后端管理系统向指定的Android客户端推送消息的功能模块,查阅了网上很多博客介绍的许多方式,最终选择基于MQTT协议来实现,MQTT是一个轻量级的消息发布/订阅协议,它是实现基于手机客户端的消息推送服务器的理想解决方案. 实现MQTT协议的中间件有很多,我用的是Apollo服务器,如何搭建MQTT服务器,请查阅其他资料.这里,主要介绍SpringBoot2.0集成MQTT实现消息推送的功能.好,正式开始: 本文采用Gateway绑定的方式,网上也有介绍但不全面,还有其他采用Paho

  • 如何在Spring Boot中使用MQTT

    为什么选择MQTT MQTT的定义相信很多人都能讲的头头是道,本文章也不讨论什么高大上的东西,旨在用最简单直观的方式让每一位刚接触的同行们可以最快的应用起来 先从使用MQTT需要什么开始分析: 消息服务器 不同应用/设备之间的频繁交互 可能涉及一对多的消息传递 根据上面列举的这三点,我们大概可以了解到, MQTT最适合的场景是消息做为系统的重要组成部分,且参与着系统关键业务逻辑的情形 MQTT, 启动! 既然决定使用它,我们首先要研究的是如何让MQTT正常工作,毕竟它不是简单的在maven里加入

  • SpringBoot集成mqtt的多模块项目配置详解

    前言 近期为了准备毕设,准备使用SpringBoot搭建mqtt后端,本篇主要记录了在IDEA中搭建SpringBoot mqtt的多模块项目的过程 开发工具及系统环境 IDE:IntelliJ IDEA 2020.2 操作系统:Windows 10 2004 Java Version:1.8 SpringBoot Version:2.1.17.RELEASE 项目路径 Study |----study-common # 存放公共类 |----study-mapper # mapper层 |--

  • SpringBoot集成JWT实现登陆验证的方法详解

    1:首先,我们需要在项目中导入两个依赖: <dependency> <groupId>com.auth0</groupId> <artifactId>java-jwt</artifactId> <version>3.10.3</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifa

  • Spring-boot集成pg、mongo多数据源过程详解

    这篇文章主要介绍了Spring-boot集成pg.mongo多数据源过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 修改POM文件,增加相应Jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </

  • SpringBoot中shiro过滤器的重写与配置详解

    目录 问题 解决方案 实现代码 1.重写shiro 登录 过滤器 2.重写role权限 过滤器 3.配置过滤器 问题 遇到问题:在前后端分离跨域访问的项目中shiro进行权限拦截失效 (即使有正确权限的访问也会被拦截) 时造成302重定向错误等问题报错:Response for preflight is invalid (redirect) 1.302原因:使用ajax访问后端项目时无法识别重定向操作 2.shiro拦截失效原因:跨域访问时有一种带预检访问的跨域,即访问时先发出一条methods

  • SpringBoot集成本地缓存性能之王Caffeine示例详解

    目录 引言 Spring Cache 是什么 集成 Caffeine 核心原理 引言 使用缓存的目的就是提高性能,今天码哥带大家实践运用 spring-boot-starter-cache 抽象的缓存组件去集成本地缓存性能之王 Caffeine. 大家需要注意的是:in-memeory 缓存只适合在单体应用,不适合与分布式环境. 分布式环境的情况下需要将缓存修改同步到每个节点,需要一个同步机制保证每个节点缓存数据最终一致. Spring Cache 是什么 不使用 Spring Cache 抽象

  • SpringBoot集成P6Spy实现SQL日志的记录详解

    目录 P6Spy简介 应用场景 pom application.yml entity Mapper 启动类 测试类 P6Spy入门使用 spy.properties P6SPYConfig application.yml P6Spy简介 P6Spy是一个可以用来在应用程序中拦截和修改数据操作语句的开源框架. 通过P6Spy可以对SQL语句进行拦截,相当于一个SQL语句的记录器,这样我们可以用它来作相关的分析,比如性能分析. 应用场景 pom <dependencies> <depende

  • React 模块联邦多模块项目实战详解

    目录 前提: 1. 修改webpack增加ModuleFederationPlugin 2.本地开发测试 3.根据路由变化自动加载对应的服务入口 4.线上部署 5.问题记录 前提: 老项目是一个多模块的前端项目,有一个框架层级的前端服务A,用来渲染界面的大概样子,其余各个功能模块前端定义自己的路由信息与组件.本地开发时,通过依赖框架服务A来启动项目,在线上部署时会有一个总前端的应用,在整合的时候,通过在获取路由信息时批量加载各个功能模块的路由信息,来达到服务整合的效果. // config.js

  • springboot集成spark并使用spark-sql的示例详解

    首先添加相关依赖: <?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

  • springboot 集成redission 以及分布式锁的使用详解

    目录 springboot集成redission及分布式锁的使用 1.引入jar包 2.增加Configuration类 3.使用redission分布式锁 Springboot整合Redisson 锁 一.依赖 二.配置文件 三.锁的使用 四.分布式秒杀 五.redis锁 单机版可用,分布式用Redisson springboot集成redission及分布式锁的使用 1.引入jar包 <dependency> <groupId>org.redisson</groupId&

  • SpringBoot集成Druid监控页面最小化配置操作

    在项目中使用阿里的druid连接池,pom文件配置: <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId&g

随机推荐