|
@@ -0,0 +1,126 @@
|
|
|
+package com.ws.fastapi.util;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.log.StaticLog;
|
|
|
+import com.influxdb.client.InfluxDBClient;
|
|
|
+import com.influxdb.client.InfluxDBClientFactory;
|
|
|
+import com.influxdb.client.InfluxDBClientOptions;
|
|
|
+import com.influxdb.client.domain.Bucket;
|
|
|
+import com.influxdb.client.domain.Organization;
|
|
|
+import okhttp3.OkHttpClient;
|
|
|
+
|
|
|
+import cn.hutool.core.io.file.FileReader;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.ws.fastapi.entity.InfluxDBProperties;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class InfluxDBClientUtil {
|
|
|
+
|
|
|
+ private InfluxDBProperties properties;
|
|
|
+
|
|
|
+ private InfluxDBClient client;
|
|
|
+ public InfluxDBClientUtil initClient(){
|
|
|
+ return initClient(null);
|
|
|
+ }
|
|
|
+ public InfluxDBClientUtil initClient(JSONObject configerJson){
|
|
|
+ if(configerJson == null){
|
|
|
+ initInfluxDBProperties();
|
|
|
+ }else {
|
|
|
+ initInfluxDBProperties(configerJson);
|
|
|
+ }
|
|
|
+ OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder()
|
|
|
+ .readTimeout(properties.getReadTimeout(), TimeUnit.SECONDS)
|
|
|
+ .writeTimeout(properties.getWriteTimeout(), TimeUnit.SECONDS)
|
|
|
+ .connectTimeout(properties.getConnectTimeout(), TimeUnit.SECONDS);
|
|
|
+ // 设置客户端信息
|
|
|
+ InfluxDBClientOptions options = InfluxDBClientOptions.builder()
|
|
|
+ .okHttpClient(okHttpClientBuilder)
|
|
|
+ .url(properties.getUrl())
|
|
|
+ .authenticateToken(properties.getToken().toCharArray())
|
|
|
+ .org(properties.getOrg())
|
|
|
+ .build();
|
|
|
+ // 创建客户端
|
|
|
+ client = InfluxDBClientFactory.create(options);
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initInfluxDBProperties(JSONObject configerJson){
|
|
|
+ JSONObject output = configerJson.getJSONObject("output");
|
|
|
+ Integer type = output.getInt("type");
|
|
|
+ String url = output.getStr("url");
|
|
|
+ String organization = output.getStr("organization");
|
|
|
+ String token = output.getStr("token");
|
|
|
+ String bucket = output.getStr("bucket");
|
|
|
+ properties = new InfluxDBProperties();
|
|
|
+ properties.setReadTimeout(3600);
|
|
|
+ properties.setWriteTimeout(3600);
|
|
|
+ properties.setConnectTimeout(3600);
|
|
|
+ properties.setSubType(type);
|
|
|
+ properties.setUrl(url);
|
|
|
+ properties.setOrg(organization);
|
|
|
+ properties.setToken(token);
|
|
|
+ properties.setBucket(bucket);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initInfluxDBProperties(){
|
|
|
+ String path = ConfUtil.getPath();
|
|
|
+ if(FileUtil.exist(path + "/configer.json")){
|
|
|
+ FileReader reader = new FileReader(path + "/configer.json");
|
|
|
+ if(StrUtil.isNotEmpty(reader.readString())){
|
|
|
+ JSONObject json = JSONUtil.parseObj(reader.readString());
|
|
|
+ initInfluxDBProperties(json);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean checkBucketExist(String bucket) {
|
|
|
+ try {
|
|
|
+ Bucket find = client.getBucketsApi().findBucketByName(bucket);
|
|
|
+ return find != null;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean createBucket(String bucket) {
|
|
|
+ try {
|
|
|
+ // 查找org组织
|
|
|
+ Organization organization = client.getOrganizationsApi()
|
|
|
+ .findOrganizations().stream()
|
|
|
+ .filter(it -> properties.getOrg().equals(it.getName()))
|
|
|
+ .findFirst()
|
|
|
+ .orElseThrow(IllegalStateException::new);
|
|
|
+ // 创建bucket
|
|
|
+ client.getBucketsApi().createBucket(bucket, organization);
|
|
|
+ StaticLog.info(bucket + "分库创建成功...");
|
|
|
+ return true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean checkAndCreateBucket(String bucket) {
|
|
|
+ if(client == null){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (!checkBucketExist(bucket)) {
|
|
|
+ return createBucket(bucket);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean checkAndCreateBucket() {
|
|
|
+ Date date = DateUtil.date();
|
|
|
+ String bucket = properties.getBucket() + "_" + DateUtil.year(date);
|
|
|
+ return checkAndCreateBucket(bucket);
|
|
|
+ }
|
|
|
+}
|