baoshiwei
2025-04-23 c2375c2bcc0bf9e6a3af7f9776d5a0eb14370b40
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.zhitan.engine.config;
 
import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.InfluxDBClientOptions;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
/**
 * InfluxDB配置类
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "influxdb")
public class InfluxDBConfig {
 
    /**
     * influxDB连接地址
     */
    private String host;
 
    /**
     * 用户名
     */
    private String username;
 
    /**
     * 密码
     */
    private String password;
 
    /**
     * 数据库名
     */
    private String bucket;
 
    /**
     * 保留策略
     */
    private String retentionPolicy;
    
    /**
     * InfluxDB 2.x的认证令牌
     */
    private String token;
    
    /**
     * InfluxDB 2.x的组织
     */
    private String org;
 
    /**
     * 是否启用InfluxDB
     */
    private boolean enable;
    
    /**
     * 测量名称
     */
    private String measurement;
    
    /**
     * 创建连接
     */
    @Bean
    public InfluxDBClient influxDBClient() {
        InfluxDBClientOptions.Builder optionsBuilder = InfluxDBClientOptions.builder()
                .url(host)
                .bucket(bucket);
        
        // 使用token认证方式(InfluxDB 2.x推荐)
        if (token != null && !token.isEmpty()) {
            optionsBuilder.authenticateToken(token.toCharArray());
        }
        
        // 设置组织
        if (org != null && !org.isEmpty()) {
            optionsBuilder.org(org);
        }
                
        return InfluxDBClientFactory.create(optionsBuilder.build());
    }
}