فهرست منبع

使用spring的influx配置

欧阳劲驰 1 ماه پیش
والد
کامیت
744f702e2b

+ 0 - 38
iot-common/iot-common-core/src/main/java/com/shkpr/iot/common/core/config/InfluxDBConfig.java

@@ -1,38 +0,0 @@
-package com.shkpr.iot.common.core.config;
-
-import com.shkpr.iot.common.core.constants.InfluxdbMetadata;
-import com.shkpr.iot.common.core.properties.InfluxdbDBProperties;
-import org.influxdb.InfluxDB;
-import org.influxdb.InfluxDBFactory;
-import org.influxdb.dto.Query;
-import org.springframework.boot.context.properties.EnableConfigurationProperties;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-/**
- * influxdb配置
- *
- * @author 欧阳劲驰
- * @since 0.0.1-dev
- */
-@Configuration
-@EnableConfigurationProperties(InfluxdbDBProperties.class)
-public class InfluxDBConfig {
-
-    /**
-     * influxdb客户端
-     *
-     * @param properties 属性
-     * @return influxdb客户端
-     */
-    @Bean
-    public InfluxDB influxDB(InfluxdbDBProperties properties) {
-        //连接数据库
-        InfluxDB influxDB = InfluxDBFactory.connect(properties.getUrl(), properties.getUsername(), properties.getPassword());
-        //创建数据库
-        influxDB.query(new Query(InfluxdbMetadata.CREATE_DATABASE + properties.getDatabase()));
-        //设置要使用的数据库
-        influxDB.setDatabase(properties.getDatabase());
-        return influxDB;
-    }
-}

+ 2 - 14
iot-common/iot-common-core/src/main/java/com/shkpr/iot/common/core/properties/InfluxdbDBProperties.java

@@ -12,20 +12,8 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
  */
 @Getter
 @Setter
-@ConfigurationProperties(prefix = "influxdb")
-public class InfluxdbDBProperties {
-    /**
-     * 地址
-     */
-    private String url;
-    /**
-     * 用户名
-     */
-    private String username;
-    /**
-     * 密码
-     */
-    private String password;
+@ConfigurationProperties(prefix = "spring.influx")
+public class InfluxDbProperties {
     /**
      * 数据库
      */

+ 16 - 17
iot-common/iot-common-core/src/main/java/com/shkpr/iot/common/core/util/InfluxDBUtil.java

@@ -1,13 +1,15 @@
 package com.shkpr.iot.common.core.util;
 
+import com.shkpr.iot.common.core.constants.InfluxdbMetadata;
+import com.shkpr.iot.common.core.properties.InfluxDbProperties;
 import lombok.extern.slf4j.Slf4j;
 import org.influxdb.InfluxDB;
 import org.influxdb.dto.BatchPoints;
 import org.influxdb.dto.Point;
+import org.influxdb.dto.Query;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.stereotype.Component;
 
-import javax.annotation.PreDestroy;
-
 /**
  * influxdb工具类
  *
@@ -16,12 +18,18 @@ import javax.annotation.PreDestroy;
  **/
 @Component
 @Slf4j
-public class InfluxDBUtil {
+@EnableConfigurationProperties(InfluxDbProperties.class)
+public class InfluxDbUtil {
     final
-    InfluxDB influxDB;
+    InfluxDB influxDb;
+
+    public InfluxDbUtil(InfluxDbProperties properties, InfluxDB influxDb) {
+        //创建数据库
+        influxDb.query(new Query(InfluxdbMetadata.CREATE_DATABASE + properties.getDatabase()));
+        //设置要使用的数据库
+        influxDb.setDatabase(properties.getDatabase());
 
-    public InfluxDBUtil(InfluxDB influxDB) {
-        this.influxDB = influxDB;
+        this.influxDb = influxDb;
     }
 
     /**
@@ -32,7 +40,7 @@ public class InfluxDBUtil {
      */
     public Boolean insert(Point point) {
         try {
-            influxDB.write(point);
+            influxDb.write(point);
             return true;
         } catch (Exception e) {
             log.error("插入InfluxDB失败", e);
@@ -48,20 +56,11 @@ public class InfluxDBUtil {
      */
     public Boolean insertBatch(BatchPoints points) {
         try {
-            influxDB.write(points);
+            influxDb.write(points);
             return true;
         } catch (Exception e) {
             log.error("插入InfluxDB失败", e);
             return false;
         }
     }
-
-    /**
-     * 关闭
-     */
-    @PreDestroy
-    private void shutdown() {
-        influxDB.close();
-    }
-
 }

+ 5 - 5
iot-server/iot-server-data/src/main/java/com/shkpr/iot/server/data/service/impl/DataServiceImpl.java

@@ -3,7 +3,7 @@ package com.shkpr.iot.server.data.service.impl;
 import com.shkpr.iot.common.core.constants.InfluxdbMetadata;
 import com.shkpr.iot.common.core.domain.po.DataInfo;
 import com.shkpr.iot.common.core.domain.po.DataValue;
-import com.shkpr.iot.common.core.util.InfluxDBUtil;
+import com.shkpr.iot.common.core.util.InfluxDbUtil;
 import com.shkpr.iot.server.data.service.DataService;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
@@ -25,10 +25,10 @@ import java.util.concurrent.TimeUnit;
 @Service
 public class DataServiceImpl implements DataService {
     final
-    InfluxDBUtil influxDBUtil;
+    InfluxDbUtil influxDbUtil;
 
-    public DataServiceImpl(InfluxDBUtil influxDBUtil) {
-        this.influxDBUtil = influxDBUtil;
+    public DataServiceImpl(InfluxDbUtil influxDbUtil) {
+        this.influxDbUtil = influxDbUtil;
     }
 
     /**
@@ -62,7 +62,7 @@ public class DataServiceImpl implements DataService {
                     .build();
             log.info("插入点位数据:{}", point);
             //插入点
-            result = influxDBUtil.insert(point);
+            result = influxDbUtil.insert(point);
         }
         return result;
     }

+ 5 - 5
iot-server/iot-server-data/src/main/resources/application-baokang.yml

@@ -4,8 +4,8 @@ spring:
     password: kpr.23417.postgres
     url: jdbc:postgresql://140.246.183.164:5432/water_smart_develop_branch?useSSL=false&useAffectedRows=false&allowMultiQueries=true&rewriteBatchedStatements=true
     driver-class-name: org.postgresql.Driver
-influxdb:
-  url: http://111.170.129.106:8086
-  username: kpr
-  password: kpr.2024@117.influxdb
-  database: iot
+  influx:
+    url: http://111.170.129.106:8086
+    user: kpr
+    password: kpr.2024@117.influxdb
+    database: iot

+ 5 - 5
iot-server/iot-server-data/src/main/resources/application-local.yml

@@ -4,8 +4,8 @@ spring:
     password: Al123456
     url: jdbc:postgresql://118.178.230.53:5532/kpr-iot
     driver-class-name: org.postgresql.Driver
-influxdb:
-  url: http://118.178.230.53:8186
-  username: influxdb
-  password: Al123456
-  database: iot
+  influx:
+    url: http://118.178.230.53:8186
+    user: influxdb
+    password: Al123456
+    database: iot