1
zhang
10 天以前 e38716f1fa4cfe37a045d90077681ea07ba7554a
1
6个文件已添加
3个文件已修改
226 ■■■■■ 已修改文件
component/component-Influxdb/pom.xml 60 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
component/component-Influxdb/src/main/java/com/zy/influxdb/config/InfluxDBAutoConfiguration.java 23 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
component/component-Influxdb/src/main/java/com/zy/influxdb/config/InfluxDBProperties.java 29 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
component/component-Influxdb/src/main/java/com/zy/influxdb/service/InfluxDBService.java 77 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
component/component-Influxdb/src/main/resources/META-INF/spring.factories 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
component/pom.xml 24 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pom.xml 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
zy-acs-gateway/pom.xml 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
zy-acs-gateway/src/main/resources/application.yml 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
component/component-Influxdb/pom.xml
New file
@@ -0,0 +1,60 @@
<?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.zy</groupId>
        <artifactId>component</artifactId>
        <version>1.0.0</version>
    </parent>
    <artifactId>component-Influxdb</artifactId>
    <properties>
        <!-- Project Properties -->
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!-- Dependency Versions -->
        <influxdb-java.version>1.7.0</influxdb-java.version>
        <lombok.version>1.18.20</lombok.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.influxdb</groupId>
            <artifactId>influxdb3-java</artifactId>
            <version>${influxdb-java.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
component/component-Influxdb/src/main/java/com/zy/influxdb/config/InfluxDBAutoConfiguration.java
New file
@@ -0,0 +1,23 @@
package com.zy.influxdb.config;
import com.influxdb.v3.client.InfluxDBClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnProperty(value = "influxdb3.enabled", havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(InfluxDBProperties.class)
public class InfluxDBAutoConfiguration {
    private static final Logger logger = LoggerFactory.getLogger(InfluxDBAutoConfiguration.class);
    @Bean(destroyMethod = "close")
    public InfluxDBClient influxDBClient(InfluxDBProperties influxDBProperties) throws Exception{
        return InfluxDBClient.getInstance(influxDBProperties.getInfluxDbUrl(), influxDBProperties.getToken().toCharArray(), influxDBProperties.getDatabase());
    }
}
component/component-Influxdb/src/main/java/com/zy/influxdb/config/InfluxDBProperties.java
New file
@@ -0,0 +1,29 @@
package com.zy.influxdb.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
 * InfluxDB Client Properties
 */
@Data
@ConfigurationProperties(prefix = "influxdb3")
public class InfluxDBProperties {
    /**
     * InfluxDB http url
     */
    private String url;
    /**
     * token
     */
    private String token;
    /**
     * database for http
     */
    private String database;
}
component/component-Influxdb/src/main/java/com/zy/influxdb/service/InfluxDBService.java
New file
@@ -0,0 +1,77 @@
package com.zy.influxdb.service;
import com.influxdb.v3.client.InfluxDBClient;
import com.influxdb.v3.client.Point;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
public class InfluxDBService  {
    private static final Logger logger = LoggerFactory.getLogger(InfluxDBService.class);
    private final InfluxDBClient influxDBClient;
    // 构造函数注入客户端
    public InfluxDBService(InfluxDBClient  influxDBClient  ) {
        this.influxDBClient =  influxDBClient  ;
    }
    /**
     * 写入数据
     * @param measurement 表名
     * @param tags 标签
     * @param fields 字段
     */
    public void writeData(String measurement, Map<String,String> tags,Map<String,Object> fields) {
        Point point = Point.measurement(measurement)
                .setTags(tags)
                .setFields(fields)
                .setTimestamp(Instant.now().minusSeconds(10));
        try {
            influxDBClient.writePoint(point);
            System.out.println("Data written to the database.");
        }
        catch (Exception e) {
            System.err.println("Failed to write data to the database.");
            e.printStackTrace();
        }
    }
    /**
     * 查询数据
     * @param sql sql语句
     * @return 查询结果列表
     */
    public List<Map<String, Object>> queryData(String sql) {
        try {
            // 执行查询
            Stream<Object[]> query = influxDBClient.query(sql);
            // 转换为 Map 列表(便于后续处理)
            List<Map<String, Object>> collect = query.map(record -> {
                        Map<String, Object> map = new LinkedHashMap<>();
                        for (int i = 0; i < record.length; i += 2) {
                            map.put((String) record[i], record[i + 1]);
                        }
                        return map;
                    })
                    .collect(Collectors.toList());
            return collect;
        } catch (Exception e) {
            System.err.println("Failed to query data from the database.");
            e.printStackTrace();
        }
        return null;
    }
}
component/component-Influxdb/src/main/resources/META-INF/spring.factories
New file
@@ -0,0 +1,2 @@
com.zy.influxdb.service.InfluxDBService
component/pom.xml
New file
@@ -0,0 +1,24 @@
<?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.zy</groupId>
        <artifactId>acs</artifactId>
        <version>1.0.0</version>
    </parent>
    <artifactId>component</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>component-Influxdb</module>
    </modules>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>
pom.xml
@@ -24,6 +24,7 @@
        <module>zy-acs-manager</module>
        <module>zy-acs-fake</module>
        <module>zy-acs-cv</module>
        <module>component</module>
    </modules>
    <properties>
zy-acs-gateway/pom.xml
@@ -43,6 +43,11 @@
            <artifactId>acs-common</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.zy</groupId>
            <artifactId>component-Influxdb</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
    <build>
zy-acs-gateway/src/main/resources/application.yml
@@ -45,3 +45,8 @@
  rcvbuf: 2097152
  #上下行日志打印
  printPacLog: true
influxdb3:
  url: http://localhost:8086
  token: xltys1995
  database: rcs_gateway