Explorar el Código

引入demo模块

xucaiqin hace 1 año
padre
commit
a9876ed394

+ 4 - 0
iot-framework/iot-starter-web/pom.xml

@@ -22,6 +22,10 @@
             <groupId>com.middle.platform</groupId>
             <artifactId>iot-common-core</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-autoconfigure</artifactId>
+        </dependency>
         <!-- Spring Boot 配置所需依赖 -->
         <dependency>
             <groupId>org.springframework.boot</groupId>

+ 35 - 0
iot-module/iot-module-demo/pom.xml

@@ -0,0 +1,35 @@
+<?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">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>com.middle.platform</groupId>
+        <artifactId>iot-module</artifactId>
+        <version>1.0.0</version>
+    </parent>
+
+    <artifactId>iot-module-demo</artifactId>
+
+    <properties>
+        <maven.compiler.source>17</maven.compiler.source>
+        <maven.compiler.target>17</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+    <dependencies>
+        <dependency>
+            <groupId>com.middle.platform</groupId>
+            <artifactId>iot-common-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.middle.platform</groupId>
+            <artifactId>iot-starter-kafka</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.middle.platform</groupId>
+            <artifactId>iot-starter-web</artifactId>
+        </dependency>
+
+    </dependencies>
+
+</project>

+ 16 - 0
iot-module/iot-module-demo/src/main/java/com/middle/platform/demo/DemoApplication.java

@@ -0,0 +1,16 @@
+package com.middle.platform.demo;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+/**
+ * @author xucaiqin
+ * @date 2024-04-02 15:11:55
+ */
+@SpringBootApplication
+public class DemoApplication {
+
+    public static void main(String[] args) {
+        SpringApplication.run(DemoApplication.class, args);
+    }
+}

+ 67 - 0
iot-module/iot-module-demo/src/main/java/com/middle/platform/demo/controller/TestController.java

@@ -0,0 +1,67 @@
+package com.middle.platform.demo.controller;
+
+import com.middle.platform.demo.service.KafkaService;
+import jakarta.annotation.Resource;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * @author xucaiqin
+ * @date 2024-04-02 15:17:47
+ */
+@RestController
+@RequestMapping("/kafka")
+public class TestController {
+
+    @Resource
+    private KafkaService kafkaService;
+
+    /**
+     * 发送文本消息
+     *
+     * @param msg
+     * @return
+     */
+    @GetMapping("/send/{msg}")
+    public String send(@PathVariable String msg) {
+        kafkaService.tet(msg);
+        return "";
+    }
+
+//    /**
+//     * 发送JSON数据
+//     * @return
+//     */
+//    @GetMapping("/send2")
+//    public String send2() {
+//        Message message = new Message();
+//        message.setId(System.currentTimeMillis());
+//        message.setMsg("生产者发送消息到topic1: " + UUID.getUUID32());
+//        message.setSendTime(new Date());
+//
+//        String value = JSON.toJSONString(message);
+//        log.info("生产者发送消息到topic1 message = {}", value);
+//
+//        kafkaService.send(kafkaConfiguration.getMyTopic1(),value);
+//        return value;
+//    }
+//    /**
+//     * 发送JSON数据
+//     * @return
+//     */
+//    @GetMapping("/send3")
+//    public String send3() {
+//        Message message = new Message();
+//        message.setId(System.currentTimeMillis());
+//        message.setMsg("生产者发送消息到topic2: " + UUID.getUUID32());
+//        message.setSendTime(new Date());
+//
+//        String value = JSON.toJSONString(message);
+//        log.info("生产者发送消息到topic2 message = {}", value);
+//
+//        kafkaService.send(kafkaConfiguration.getMyTopic2(),value);
+//        return value;
+//    }
+}

+ 22 - 0
iot-module/iot-module-demo/src/main/java/com/middle/platform/demo/service/KafkaService.java

@@ -0,0 +1,22 @@
+package com.middle.platform.demo.service;
+
+import com.alibaba.fastjson.JSONObject;
+import lombok.RequiredArgsConstructor;
+import org.springframework.kafka.core.KafkaTemplate;
+import org.springframework.stereotype.Service;
+
+/**
+ * @author xucaiqin
+ * @date 2024-04-02 15:19:51
+ */
+@Service
+@RequiredArgsConstructor
+public class KafkaService {
+    private final KafkaTemplate<String, Object> kafkaTemplate;
+
+    public void tet(String s) {
+        JSONObject jsonObject = new JSONObject();
+        jsonObject.put("123", s);
+        kafkaTemplate.send("topic", jsonObject.toJSONString());
+    }
+}

+ 20 - 0
iot-module/iot-module-demo/src/main/java/com/middle/platform/demo/service/Test.java

@@ -0,0 +1,20 @@
+package com.middle.platform.demo.service;
+
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.springframework.kafka.annotation.KafkaListener;
+import org.springframework.stereotype.Component;
+
+/**
+ * @author xucaiqin
+ * @date 2024-04-02 15:16:48
+ */
+@Component
+public class Test {
+    @KafkaListener(topics = "topic")
+    public void listen(ConsumerRecord<?,String> record) {
+        System.out.println(record);
+        String value = record.value();
+        System.out.println("消费者1接收到消息:" + value);
+
+    }
+}

+ 7 - 0
iot-module/iot-module-demo/src/main/resources/application.yml

@@ -0,0 +1,7 @@
+server:
+  port: 8080
+spring:
+  kafka:
+    bootstrap-servers: localhost:9092
+    consumer:
+      group-id: list

+ 1 - 0
iot-module/pom.xml

@@ -16,6 +16,7 @@
         <module>iot-module-system</module>
         <module>iot-module-data</module>
         <module>iot-module-manage</module>
+        <module>iot-module-demo</module>
     </modules>
     <properties>
         <maven.compiler.source>17</maven.compiler.source>