提交 | 用户 | 时间
|
a955f1
|
1 |
package com.iailab.module.model.influxdb.common.config; |
D |
2 |
|
|
3 |
import com.iailab.framework.tenant.core.context.TenantContextHolder; |
|
4 |
import com.influxdb.client.InfluxDBClient; |
|
5 |
import com.influxdb.client.InfluxDBClientFactory; |
|
6 |
import com.influxdb.client.domain.Bucket; |
|
7 |
import com.influxdb.client.domain.Organization; |
|
8 |
import org.slf4j.Logger; |
|
9 |
import org.slf4j.LoggerFactory; |
|
10 |
import org.springframework.beans.factory.annotation.Value; |
|
11 |
import org.springframework.stereotype.Component; |
|
12 |
|
|
13 |
import java.util.HashSet; |
|
14 |
import java.util.Set; |
|
15 |
|
|
16 |
/** |
|
17 |
* @author PanZhibao |
|
18 |
* @Description |
|
19 |
* @createTime 2023年04月25日 17:13:00 |
|
20 |
*/ |
|
21 |
@Component |
|
22 |
public class InfluxDBInstance { |
|
23 |
|
|
24 |
@Value("${influx-db.org}") |
|
25 |
public String org; |
|
26 |
|
|
27 |
/*@Value("${influx-db.bucket}") |
|
28 |
public String bucket;*/ |
|
29 |
|
|
30 |
@Value("${influx-db.token}") |
|
31 |
public String token; |
|
32 |
|
|
33 |
@Value("${influx-db.url}") |
|
34 |
public String url; |
|
35 |
|
|
36 |
private Set<String> isExistBucket = new HashSet<>(); |
|
37 |
|
|
38 |
private final static String BUCKET_NAME = "bucket_model_"; |
|
39 |
|
|
40 |
private Logger logger = LoggerFactory.getLogger(getClass()); |
|
41 |
|
|
42 |
private InfluxDBClient client; |
|
43 |
|
|
44 |
public InfluxDBClient getClient() { |
|
45 |
try { |
|
46 |
if (client == null) { |
|
47 |
client = InfluxDBClientFactory.create(url, token.toCharArray()); |
|
48 |
} |
|
49 |
} catch (Exception ex) { |
|
50 |
ex.printStackTrace(); |
|
51 |
logger.error("创建InfluxDBClient失败!"); |
|
52 |
} |
|
53 |
return client; |
|
54 |
} |
|
55 |
|
|
56 |
public String getBucket() { |
|
57 |
String bucketName = BUCKET_NAME + TenantContextHolder.getRequiredTenantId(); |
|
58 |
// 判断Bucket是否存在,不存在则创建 |
|
59 |
if (!isExistBucket.contains(bucketName)) { |
|
60 |
Bucket bucketByName = client.getBucketsApi().findBucketByName(bucketName); |
|
61 |
if (null == bucketByName) { |
|
62 |
Organization organization = client.getOrganizationsApi().findOrganizations().stream().filter(e -> e.getName().equals(org)).findFirst().orElseThrow(() -> new RuntimeException("influxdb:org不存在,org:" + org)); |
|
63 |
client.getBucketsApi().createBucket(bucketName,organization); |
|
64 |
}else { |
|
65 |
isExistBucket.add(bucketName); |
|
66 |
} |
|
67 |
} |
|
68 |
return bucketName; |
|
69 |
} |
|
70 |
|
|
71 |
} |