Browse Source

新增 influxdb组件

guoenzhou 2 years ago
parent
commit
c741322982

+ 29 - 0
base-modules/service-system/service-system-biz/src/main/java/com/usky/system/controller/web/TestController.java

@@ -0,0 +1,29 @@
+package com.usky.system.controller.web;
+
+import com.usky.common.influx.core.InfluxDbUtils;
+import org.influxdb.dto.QueryResult;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+
+/**
+ * 租户管理-管理员配置
+ *
+ * @author yq
+ */
+@RestController
+@RequestMapping("/test")
+public class TestController extends BaseController {
+
+    @Autowired
+    private InfluxDbUtils influxDbUtils;
+
+    @GetMapping("select")
+    public QueryResult select(){
+        String command="SELECT * FROM sp_d868474049166828 WHERE time > now() - 7d order by time desc";
+        QueryResult query = influxDbUtils.query(command);
+        return query;
+    }
+
+
+}

+ 6 - 0
usky-common/usky-common-core/pom.xml

@@ -108,6 +108,12 @@
             <version>2.2.10.RELEASE</version>
         </dependency>
 
+        <dependency>
+            <groupId>org.influxdb</groupId>
+            <artifactId>influxdb-java</artifactId>
+            <version>2.15</version>
+        </dependency>
+
     </dependencies>
 
 </project>

+ 45 - 0
usky-common/usky-common-core/src/main/java/com/usky/common/influx/config/InfluxDbConfig.java

@@ -0,0 +1,45 @@
+package com.usky.common.influx.config;
+
+import com.usky.common.core.util.StringUtils;
+import com.usky.common.influx.core.InfluxDbUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ *  配置方式:
+ * spring:
+ *     influx:
+ *         url: http://172.16.120.69:8086
+ *         password: root
+ *         user: root
+ *         database: USKTSDB
+ */
+@Configuration
+@Slf4j
+public class InfluxDbConfig {
+
+    @Value("${spring.influx.url:''}")
+    private String influxDBUrl;
+
+    @Value("${spring.influx.user:''}")
+    private String userName;
+
+    @Value("${spring.influx.password:''}")
+    private String password;
+
+    @Value("${spring.influx.database:''}")
+    private String database;
+
+    @Bean
+    public InfluxDbUtils influxDbUtils() {
+        log.info("influx config info:{},{}",influxDBUrl,database);
+        if(influxDBUrl!= null && !influxDBUrl.equals("''") ) {
+            log.warn("influx 组件启动");
+            return new InfluxDbUtils(userName, password, influxDBUrl, database, "");
+        }
+        log.warn("influx 配置缺失,未启动");
+        return null;
+    }
+}

+ 69 - 0
usky-common/usky-common-core/src/main/java/com/usky/common/influx/core/InfluxDbUtils.java

@@ -0,0 +1,69 @@
+package com.usky.common.influx.core;
+
+import lombok.Data;
+import org.influxdb.InfluxDB;
+import org.influxdb.InfluxDBFactory;
+import org.influxdb.dto.Query;
+import org.influxdb.dto.QueryResult;
+
+@Data
+public class InfluxDbUtils {
+
+    private String userName;
+    private String password;
+    private String url;
+    public String database;
+    private String retentionPolicy;
+    // InfluxDB实例
+    private InfluxDB influxDB;
+
+    // 数据保存策略
+    public static String policyNamePix = "logRetentionPolicy_";
+
+
+
+    public InfluxDbUtils(String userName, String password, String url, String database,
+                         String retentionPolicy) {
+        this.userName = userName;
+        this.password = password;
+        this.url = url;
+        this.database = database;
+        this.retentionPolicy = retentionPolicy == null || "".equals(retentionPolicy) ? "autogen" : retentionPolicy;
+        this.influxDB = influxDbBuild();
+    }
+
+    /**
+     * 连接数据库 ,若不存在则创建
+     *
+     * @return influxDb实例
+     */
+    private InfluxDB influxDbBuild() {
+        if (influxDB == null) {
+            influxDB = InfluxDBFactory.connect(url, userName, password);
+        }
+        try {
+//            createDB(database);
+            influxDB.setDatabase(database);
+        } catch (Exception e) {
+            e.printStackTrace();
+//          log.error("create influx db failed, error: {}", e.getMessage());
+        } finally {
+            influxDB.setRetentionPolicy(retentionPolicy);
+        }
+        influxDB.setLogLevel(InfluxDB.LogLevel.BASIC);
+        return influxDB;
+    }
+
+    /**
+     * 查询
+     * @param command
+     * 查询语句
+     * @return
+     */
+    public QueryResult query(String command) {
+        return influxDB.query(new Query(command, database));
+    }
+
+
+
+}