Kaynağa Gözat

添加路灯服务

邵洋 1 yıl önce
ebeveyn
işleme
cae3efce95

+ 9 - 0
lamp-service/Dockerfile

@@ -0,0 +1,9 @@
+# 基础镜像
+FROM openjdk:11.0-jre-buster
+# 设定时区
+ENV TZ=Asia/Shanghai
+RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
+# 拷贝jar包
+COPY lamp-service.jar /app.jar
+# 入口
+ENTRYPOINT ["java", "-Xms256m", "-Xmx512m", "-jar", "/app.jar"]

+ 79 - 0
lamp-service/pom.xml

@@ -0,0 +1,79 @@
+<?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>
+    <parent>
+        <groupId>com.zksy</groupId>
+        <artifactId>dh-server-micro</artifactId>
+        <version>1.0.0</version>
+    </parent>
+    <artifactId>lamp-service</artifactId>
+    <groupId>com.zksy.lamp</groupId>
+    <properties>
+        <maven.compiler.source>11</maven.compiler.source>
+        <maven.compiler.target>11</maven.compiler.target>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
+    <dependencies>
+        <!--common-->
+        <dependency>
+            <groupId>com.zksy</groupId>
+            <artifactId>zksy-common</artifactId>
+            <version>1.0.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+        <!--数据库-->
+        <dependency>
+            <groupId>mysql</groupId>
+            <artifactId>mysql-connector-java</artifactId>
+        </dependency>
+        <!--mybatis-->
+        <dependency>
+            <groupId>com.baomidou</groupId>
+            <artifactId>mybatis-plus-boot-starter</artifactId>
+        </dependency>
+        <!--nacos 服务注册发现-->
+        <dependency>
+            <groupId>com.alibaba.cloud</groupId>
+            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
+        </dependency>
+        <!--负载均衡-->
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
+        </dependency>
+        <!--统一配置管理-->
+        <dependency>
+            <groupId>com.alibaba.cloud</groupId>
+            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
+        </dependency>
+        <!--加载bootstrap-->
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-bootstrap</artifactId>
+        </dependency>
+        <!--redis-->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-redis</artifactId>
+        </dependency>
+        <!--sentinel-->
+        <dependency>
+            <groupId>com.alibaba.cloud</groupId>
+            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <finalName>${project.artifactId}</finalName>
+        <plugins>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+            </plugin>
+        </plugins>
+    </build>
+</project>

+ 21 - 0
lamp-service/src/main/java/com/zksy/lamp/LampApplication.java

@@ -0,0 +1,21 @@
+package com.zksy.lamp;
+
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+/**
+ * @author Administrator
+ * @version 1.0
+ * @project dh-server-micro
+ * @description
+ * @date 2025/2/10 14:51:32
+ */
+@SpringBootApplication
+@MapperScan(basePackages = "com.zksy.lamp.mapper")
+public class LampApplication {
+    public static void main(String[] args) {
+        SpringApplication.run(LampApplication.class);
+        System.out.println("路灯服务启动成功");
+    }
+}

+ 27 - 0
lamp-service/src/main/java/com/zksy/lamp/controller/TestController.java

@@ -0,0 +1,27 @@
+package com.zksy.lamp.controller;
+
+import com.zksy.lamp.server.ExecutionServer;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * @author Administrator
+ * @version 1.0
+ * @project dh-server-micro
+ * @description
+ * @date 2025/2/10 15:00:46
+ */
+@RequestMapping("/test")
+@RestController
+public class TestController {
+    @Autowired
+    private ExecutionServer server;
+    @GetMapping("/a")
+    public String test(){
+        String data = "";
+        String msg = server.ExecutionData(data);
+        return msg;
+    }
+}

+ 40 - 0
lamp-service/src/main/java/com/zksy/lamp/server/ExecutionServer.java

@@ -0,0 +1,40 @@
+package com.zksy.lamp.server;
+
+import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+
+/**
+ * @author Administrator
+ * @version 1.0
+ * @project dh-server-micro
+ * @description 指令执行
+ * @date 2025/2/10 16:25:14
+ */
+@Component
+public class ExecutionServer {
+    private BufferedReader reader;
+    private BufferedWriter writer;
+
+    public String ExecutionData(String data) {
+        try {
+            String msg = "AT+STACH1=1\r\n";// \r\n(回车换行)
+            System.out.println("第1路闭合,发送指令:AT+STACH1=1\\r\\n");
+            //发送数据
+            writer.write(msg);
+            writer.flush();
+            //读数据
+            msg = reader.readLine();
+            msg = msg.replace("\r\n", "\\r\\n");
+            System.out.println("设备应答:" + msg);
+            System.out.println("指令执行成功!");
+            Thread.sleep(100);
+            return msg;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return "发送指令:"+data+"失败";
+    }
+}

+ 65 - 0
lamp-service/src/main/java/com/zksy/lamp/server/TcpServer.java

@@ -0,0 +1,65 @@
+package com.zksy.lamp.server;
+
+import cn.hutool.core.lang.func.VoidFunc0;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import java.io.*;
+import java.net.*;
+
+@Component
+public class TcpServer {
+    private int type = 3; // 建议使用小写字母开头以符合Java命名规范
+    private BufferedReader reader;
+    private BufferedWriter writer;
+    private ServerSocket server;
+    private Socket client;
+    private int serverPort = 15678;
+
+    @PostConstruct
+    public void init(){
+        Runnable func0 = this::startServer;
+        Thread thread = new Thread(func0);
+        thread.start();
+    }
+
+    @PreDestroy
+    public void closeResource(){
+        try {
+            if (reader != null) reader.close();
+            if (writer != null) writer.close();
+            if (client != null) client.close();
+            if (server != null) server.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * 启动服务器并等待客户端连接
+     */
+    public void startServer() {
+        try {
+            System.out.println("本机作为服务端");
+            System.out.println("本机端口:" + serverPort);
+            server = new ServerSocket(serverPort);
+
+            System.out.println("等待客户机的连接");
+            client = server.accept();
+            System.out.println("连接成功");
+
+            String clientAddr = client.getRemoteSocketAddress().toString().substring(1);
+            String[] clientAddrs = clientAddr.split(":");
+            System.out.println("客户端IP:" + clientAddrs[0] + " 端口:" + clientAddrs[1]);
+
+            reader = new BufferedReader(new InputStreamReader(client.getInputStream(), "GB2312"));
+            writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream(), "GB2312"));
+
+            System.out.println("测试完成!");
+
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 17 - 0
lamp-service/src/main/resources/application-dev.yaml

@@ -0,0 +1,17 @@
+zksy:
+  db:
+    host: 192.168.110.30
+    un: root
+    pw: 123
+    port: 3307
+    database: zhongkeshengyang
+spring:
+  redis:
+    host: 192.168.110.30
+    port: 6379
+  cloud:
+    nacos:
+      discovery:
+        server-addr: 192.168.110.30:8848
+      config:
+        server-addr: 192.168.110.30:8848

+ 19 - 0
lamp-service/src/main/resources/application-prod.yaml

@@ -0,0 +1,19 @@
+zksy:
+  db:
+    host: 172.16.102.51
+    un: root
+    pw: d$3%#*(jnhUDGHB]z0x876c~
+    port: 3306
+    database: zhongkeshengyang
+spring:
+  redis:
+    host: 172.16.102.52
+    port: 6379
+minio:
+  endpoint: http://172.16.102.52:9000
+  accessKey: minio
+  secretKey: minio123
+  bucket: test
+  readPath: http://172.16.102.52:9000
+redisson:
+  host: redis://172.16.102.52:6379

+ 31 - 0
lamp-service/src/main/resources/bootstrap.yaml

@@ -0,0 +1,31 @@
+spring:
+  application:
+    name: lamp-service
+  profiles:
+    active: dev
+  servlet:
+    multipart:
+      max-file-size: 100MB
+      max-request-size: 100MB
+  main:
+    allow-bean-definition-overriding: true
+  cloud:
+    sentinel:
+      transport:
+      #  dashboard: 172.16.102.52:8090
+        dashboard: 192.168.110.30:8090
+      http-method-specify: true
+    nacos:
+      discovery:
+      #  server-addr: 172.16.102.52:8848
+      #  namespace: 99a7b129-faed-4b32-bcbf-54acd7ffd372
+         server-addr: 192.168.110.30:8848
+      config:
+      #  server-addr: 172.16.102.52:8848
+      #  namespace: 99a7b129-faed-4b32-bcbf-54acd7ffd372
+        server-addr: 192.168.110.30:8848
+        file-extension: yaml
+        shared-configs:
+          - dataId: lamp-service.yaml
+          - dataId: zksy-shared-jdbc.yaml
+          - dataId: zksy-shared-log.yaml

+ 1 - 0
pom.xml

@@ -16,6 +16,7 @@
         <module>environment-data-service</module>
         <module>pole-service</module>
         <module>park-overview-service</module>
+        <module>lamp-service</module>
     </modules>
 
     <parent>