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());
|
}
|
}
|