当前位置: 首页 > news >正文

Dubbo(97)如何在物联网系统中应用Dubbo?

在物联网系统中应用Dubbo,可以通过以下步骤实现:

  1. 项目结构设计:规划项目的模块结构,通常包括服务接口模块、各个服务提供者模块(如设备管理服务、数据采集服务、数据分析服务等)、服务消费者模块(如前端应用、后台管理系统等)。
  2. 服务接口定义:定义各个服务的接口,确保服务提供者和消费者能够共享相同的接口。
  3. 服务提供者实现:实现各个服务接口,并配置Dubbo提供服务。
  4. 服务消费者调用:在服务消费者中引用服务接口,并通过Dubbo调用远程服务。
  5. 服务注册与发现:配置注册中心(如Zookeeper)以实现服务注册与发现。
  6. 配置管理:管理项目的配置文件,确保服务提供者和消费者能够正确连接到注册中心并发现彼此。
  7. 测试与部署:测试服务的调用,确保服务能够正常工作,并将服务部署到生产环境。

以下是一个详细的示例,展示如何在物联网系统中应用Dubbo。

1. 项目结构设计

我们将创建一个包含多个模块的项目:dubbo-apidevice-management-servicedata-collection-servicedata-analysis-servicefrontend-application

iot-system
├── dubbo-api
│   └── src/main/java/com/example/dubbo/api
│       ├── DeviceManagementService.java
│       ├── DataCollectionService.java
│       └── DataAnalysisService.java
├── device-management-service
│   └── src/main/java/com/example/devicemanagement
│       ├── service
│       │   └── DeviceManagementServiceImpl.java
│       ├── config
│       │   └── DubboProviderConfig.java
│       └── DeviceManagementServiceApplication.java
├── data-collection-service
│   └── src/main/java/com/example/datacollection
│       ├── service
│       │   └── DataCollectionServiceImpl.java
│       ├── config
│       │   └── DubboProviderConfig.java
│       └── DataCollectionServiceApplication.java
├── data-analysis-service
│   └── src/main/java/com/example/dataanalysis
│       ├── service
│       │   └── DataAnalysisServiceImpl.java
│       ├── config
│       │   └── DubboProviderConfig.java
│       └── DataAnalysisServiceApplication.java
├── frontend-application
│   └── src/main/java/com/example/frontend
│       ├── controller
│       │   └── IotController.java
│       ├── config
│       │   └── DubboConsumerConfig.java
│       └── FrontendApplication.java
└── pom.xml

2. 服务接口定义

2.1 创建 dubbo-api 模块

创建 dubbo-api 模块的 pom.xml 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.w3.org/POM/4.0.0 http://www.w3.org/2001/04/xmldsig-more#"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>dubbo-api</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><dependencies><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo</artifactId><version>2.7.8</version></dependency></dependencies>
</project>
2.2 定义服务接口

dubbo-api/src/main/java/com/example/dubbo/api 目录下创建 DeviceManagementServiceDataCollectionServiceDataAnalysisService 接口:

package com.example.dubbo.api;public interface DeviceManagementService {String registerDevice(String deviceId, String deviceType);String getDeviceStatus(String deviceId);
}public interface DataCollectionService {String collectData(String deviceId, String data);
}public interface DataAnalysisService {String analyzeData(String data);
}

3. 服务提供者实现

3.1 创建 device-management-service 模块

创建 device-management-service 模块的 pom.xml 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.w3.org/POM/4.0.0 http://www.w3.org/2001/04/xmldsig-more#"><parent><groupId>com.example</groupId><artifactId>iot-system</artifactId><version>1.0-SNAPSHOT</version><relativePath>../pom.xml</relativePath></parent><artifactId>device-management-service</artifactId><dependencies><dependency><groupId>com.example</groupId><artifactId>dubbo-api</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>2.7.8</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.3</version></dependency></dependencies>
</project>
3.2 实现设备管理服务接口

device-management-service/src/main/java/com/example/devicemanagement/service 目录下创建 DeviceManagementServiceImpl 类:

package com.example.devicemanagement.service;import com.example.dubbo.api.DeviceManagementService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;@DubboService
public class DeviceManagementServiceImpl implements DeviceManagementService {private static final Logger logger = LoggerFactory.getLogger(DeviceManagementServiceImpl.class);@Overridepublic String registerDevice(String deviceId, String deviceType) {logger.info("Registering device ID: {}, Type: {}", deviceId, deviceType);return "Device " + deviceId + " of type " + deviceType + " registered successfully.";}@Overridepublic String getDeviceStatus(String deviceId) {logger.info("Getting status for device ID: {}", deviceId);return "Status of device " + deviceId + ": Active";}
}
3.3 配置Dubbo服务

device-management-service/src/main/java/com/example/devicemanagement/config 目录下创建 DubboProviderConfig 类:

package com.example.devicemanagement.config;import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.Configuration;@Configuration
@EnableDubbo(scanBasePackages = "com.example.devicemanagement.service")
public class DubboProviderConfig {
}
3.4 创建启动类

device-management-service/src/main/java/com/example/devicemanagement 目录下创建 DeviceManagementServiceApplication 类:

package com.example.devicemanagement;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class DeviceManagementServiceApplication {public static void main(String[] args) {SpringApplication.run(DeviceManagementServiceApplication.class, args);}
}
3.5 配置文件

device-management-service/src/main/resources 目录下创建 application.yml 配置文件:

spring:application:name: device-management-servicemain:web-application-type: nonedubbo:application:name: device-management-serviceregistry:address: zookeeper://localhost:2181protocol:name: dubboport: 20880scan:base-packages: com.example.devicemanagement.servicelogging:level:com.example.devicemanagement: INFOfile:name: logs/device-management-service.log
3.6 创建 data-collection-service 模块

创建 data-collection-service 模块的 pom.xml 文件,类似于 device-management-service 模块。

3.7 实现数据采集服务接口

data-collection-service/src/main/java/com/example/datacollection/service 目录下创建 DataCollectionServiceImpl 类:

package com.example.datacollection.service;import com.example.dubbo.api.DataCollectionService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;@DubboService
public class DataCollectionServiceImpl implements DataCollectionService {private static final Logger logger = LoggerFactory.getLogger(DataCollectionServiceImpl.class);@Overridepublic String collectData(String deviceId, String data) {logger.info("Collecting data from device ID: {}, Data: {}", deviceId, data);return "Collected data from device " + deviceId + ": " + data;}
}
3.8 配置Dubbo服务和启动类

data-collection-service 模块中配置Dubbo服务和启动类,类似于 device-management-service 模块。

3.9 创建 data-analysis-service 模块

创建 data-analysis-service 模块的 pom.xml 文件,类似于 device-management-service 模块。

3.10 实现数据分析服务接口

data-analysis-service/src/main/java/com/example/dataanalysis/service 目录下创建 DataAnalysisServiceImpl 类:

package com.example.dataanalysis.service;import com.example.dubbo.api.DataAnalysisService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;@DubboService
public class DataAnalysisServiceImpl implements DataAnalysisService {private static final Logger logger = LoggerFactory.getLogger(DataAnalysisServiceImpl.class);@Overridepublic String analyzeData(String data) {logger.info("Analyzing data: {}", data);return "Analyzed data: " + data;}
}
3.11 配置Dubbo服务和启动类

data-analysis-service 模块中配置Dubbo服务和启动类,类似于 device-management-service 模块。

4. 服务消费者调用

4.1 创建 frontend-application 模块

创建 frontend-application 模块的 pom.xml 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.w3.org/POM/4.0.0 http://www.w3.org/2001/04/xmldsig-more#"><parent><groupId>com.example</groupId><artifactId>iot-system</artifactId><version>1.0-SNAPSHOT</version><relativePath>../pom.xml</relativePath></parent><artifactId>frontend-application</artifactId><dependencies><dependency><groupId>com.example</groupId><artifactId>dubbo-api</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>2.7.8</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.3</version></dependency></dependencies>
</project>
4.2 创建控制器

frontend-application/src/main/java/com/example/frontend/controller 目录下创建 IotController 类:

package com.example.frontend.controller;import com.example.dubbo.api.DataCollectionService;
import com.example.dubbo.api.DataAnalysisService;
import com.example.dubbo.api.DeviceManagementService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class IotController {private static final Logger logger = LoggerFactory.getLogger(IotController.class);@DubboReferenceprivate DeviceManagementService deviceManagementService;@DubboReferenceprivate DataCollectionService dataCollectionService;@DubboReferenceprivate DataAnalysisService dataAnalysisService;@GetMapping("/registerDevice")public String registerDevice(@RequestParam String deviceId, @RequestParam String deviceType) {logger.info("Registering device ID: {}, Type: {}", deviceId, deviceType);return deviceManagementService.registerDevice(deviceId, deviceType);}@GetMapping("/getDeviceStatus")public String getDeviceStatus(@RequestParam String deviceId) {logger.info("Getting status for device ID: {}", deviceId);return deviceManagementService.getDeviceStatus(deviceId);}@GetMapping("/collectData")public String collectData(@RequestParam String deviceId, @RequestParam String data) {logger.info("Collecting data from device ID: {}, Data: {}", deviceId, data);return dataCollectionService.collectData(deviceId, data);}@GetMapping("/analyzeData")public String analyzeData(@RequestParam String data) {logger.info("Analyzing data: {}", data);return dataAnalysisService.analyzeData(data);}
}
4.3 配置Dubbo消费

frontend-application/src/main/java/com/example/frontend/config 目录下创建 DubboConsumerConfig 类:

package com.example.frontend.config;import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.Configuration;@Configuration
@EnableDubbo(scanBasePackages = "com.example.frontend.controller")
public class DubboConsumerConfig {
}
4.4 创建启动类

frontend-application/src/main/java/com/example/frontend 目录下创建 FrontendApplication 类:

package com.example.frontend;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class FrontendApplication {public static void main(String[] args) {SpringApplication.run(FrontendApplication.class, args);}
}
4.5 配置文件

frontend-application/src/main/resources 目录下创建 application.yml 配置文件:

spring:application:name: frontend-applicationdubbo:application:name: frontend-applicationregistry:address: zookeeper://localhost:2181protocol:name: dubboscan:base-packages: com.example.frontend.controllerlogging:level:com.example.frontend: INFOfile:name: logs/frontend-application.log

5. 根项目的 pom.xml

在根项目 iot-system 中创建 pom.xml 文件,定义模块和依赖管理:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.w3.org/POM/4.0.0 http://www.w3.org/2001/04/xmldsig-more#"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>iot-system</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules><module>dubbo-api</module><module>device-management-service</module><module>data-collection-service</module><module>data-analysis-service</module><module>frontend-application</module></modules><dependencyManagement><dependencies><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo</artifactId><version>2.7.8</version></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>2.7.8</version></dependency></dependencies></dependencyManagement><build><pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.3.4.RELEASE</version></plugin></plugins></pluginManagement></build>
</project>

6. 启动Zookeeper

确保Zookeeper在本地运行,默认端口为 2181。可以通过下载Zookeeper并运行以下命令启动Zookeeper:

bin/zkServer.sh start

7. 启动服务提供者和消费者

相关文章:

  • 【PDF拆分+提取内容改名】批量拆分PDF提取拆分后的每个PDF物流面单数据改名或导出表格,基于WPF的PDF物流面单批量处理方案
  • 【计算机视觉】3d人脸重建:3DDFA_V2:实时高精度3D人脸重建与密集对齐技术指南
  • Linux 怎么使用局域网内电脑的网络访问外部
  • Python cv2图像几何变换全攻略:从理论到实战
  • 开源模型应用落地-qwen模型小试-Qwen3-8B-快速体验-批量推理(三)
  • 【Elasticsearch入门到落地】12、索引库删除判断以及文档增删改查
  • (一)Modular Monolith Architecture(项目结构/.net项目初始化/垂直切片架构)
  • 【NLP】30. 深入理解 In-Context Learning 的核心机制与策略
  • 浅析AI大模型为何需要向量数据库?【入门基础】
  • 【全队项目】智能学术海报生成系统PosterGenius--前后端系统介绍
  • NGINX 的 ngx_http_auth_jwt_module模块
  • 《繁花》投资、交易启示及思考
  • 深入探索 Apache Spark:从初识到集群运行原理
  • 【Hive入门】Hive安全管理与权限控制:基于SQL标准的授权GRANT REVOKE深度解析
  • Python中有序序列容器的概念及其与可变性的关系
  • 【PostgreSQL数据分析实战:从数据清洗到可视化全流程】4.4 异构数据源整合(CSV/JSON/Excel数据导入)
  • Linux:深入理解数据链路层
  • NOI 2025 大纲更新:算法竞赛的新风向标
  • 算法笔记.约数个数
  • 【今日三题】小红的口罩(小堆) / 春游(模拟) / 数位染色(01背包)
  • 上海乐高乐园明天正式开售年卡,下月开启试运营
  • 上海飞银川客机触地复飞后备降西安,亲历者:不少乘客都吐了
  • 一周人物|何子彦任职光州双年展,陈意心讲述五原路往事
  • 甘肃省政府原党组成员、副省长杨子兴被提起公诉
  • 黄道炫:南京102天——黄镇球的防空日记
  • “五一”假期出入境人数达1089.6万人次,同比增长28.7%