From 4c6b49c6a24185d35ff6f0ff3808a246997bf69f Mon Sep 17 00:00:00 2001 From: caixiang <939387484@qq.com> Date: Fri, 25 Nov 2022 15:00:33 +0800 Subject: [PATCH] mark for pull --- pom.xml | 8 +- src/main/java/com/qgs/dc/influx/Main.java | 82 +++++++++++-------- .../qgs/dc/influx/config/ConfigInjector.java | 23 ++++++ .../qgs/dc/influx/config/InfluxClient.java | 17 ++-- .../qgs/dc/influx/config/InfluxConfig.java | 21 +++++ .../influx/controller/InfluxController.java | 11 +-- .../dc/s7/my/s7connector/api/DaveArea.java | 2 +- src/main/resources/application.yml | 19 ++--- src/main/resources/logback.xml | 15 +++- 9 files changed, 135 insertions(+), 63 deletions(-) create mode 100644 src/main/java/com/qgs/dc/influx/config/ConfigInjector.java create mode 100644 src/main/java/com/qgs/dc/influx/config/InfluxConfig.java diff --git a/pom.xml b/pom.xml index ec9c664..bbf4779 100644 --- a/pom.xml +++ b/pom.xml @@ -190,12 +190,16 @@ org.springframework.boot spring-boot-starter-validation - + + org.springframework.boot + spring-boot-configuration-processor + true + com.influxdb influxdb-client-java - 6.3.0 + 6.7.0 diff --git a/src/main/java/com/qgs/dc/influx/Main.java b/src/main/java/com/qgs/dc/influx/Main.java index 6239a1a..5bf2399 100644 --- a/src/main/java/com/qgs/dc/influx/Main.java +++ b/src/main/java/com/qgs/dc/influx/Main.java @@ -2,16 +2,17 @@ package com.qgs.dc.influx; //import com.qgs.dc.influx.config.InfluxClient; -import com.influxdb.client.InfluxDBClient; -import com.influxdb.client.InfluxDBClientFactory; -import com.influxdb.client.QueryApi; -import com.influxdb.client.WriteApi; +import com.influxdb.client.*; +import com.influxdb.client.domain.WritePrecision; +import com.influxdb.client.write.Point; import com.influxdb.query.FluxRecord; import com.influxdb.query.FluxTable; +import com.qgs.dc.influx.config.InfluxClient; import com.qgs.dc.influx.param.PageInfo; import com.qgs.dc.influx.param.QueryDataParam; import com.qgs.dc.influx.param.Range; import com.qgs.dc.influx.param.Tag; +import com.qgs.dc.influx.template.Event; import java.text.SimpleDateFormat; import java.time.Instant; @@ -27,13 +28,28 @@ import java.util.List; */ public class Main { public static void main(String[] args) throws InterruptedException { - char[] token = "lkBsC27QZr1W50BSPlGxpTqNNpwuUk5uz1dZZRPSPbCG5VmNDDUo8P3UkZIhGWwfJwkuz6ZGZ7Et4_KBaG3gHw==".toCharArray(); - String org = "qgs"; - String bucket = "qgs-bucket"; - InfluxDBClient influxDBClient = InfluxDBClientFactory.create("http://192.168.0.170:8086", token, org, bucket); + char[] token = "N4yBD2iC0kDkAzlWEQ5koqJNbbd6v9PQsubEAOle1rykOIeM5zckKxUteDUsHm1LFMBnJTtfGNsRT_N54YTLcQ==".toCharArray(); + String org = "cigs"; + String bucket = "cigs-all"; + InfluxDBClient influxDBClient = InfluxDBClientFactory.create("http://172.16.21.164:8086", token, org, bucket); + Point point = Point + .measurement("fortest") + .addTag("host", "host1") + .addField("used_percent", 23.43234543) + .time(Instant.now(), WritePrecision.NS); - WriteApi writeApi = influxDBClient.makeWriteApi(); + WriteApiBlocking writeApi = influxDBClient.getWriteApiBlocking(); + writeApi.writePoint(bucket, org, point); + + Event event = new Event(); + event.setTime(Instant.now()); + event.setTransationId("asasd11"); + event.setArgName("argName11"); + event.setBatchNum("12333"); + event.setArgValue("900001"); +// Point asProcessCompleteEvent = insert(event, "ForTest"); + InfluxClient.Client.insert(event,"fortest2"); // InfluxService influxService = new InfluxService(); // Event event = new Event(); @@ -144,30 +160,30 @@ public class Main { - QueryDataParam queryDataParam = new QueryDataParam(); - queryDataParam.setBucket("qgs-bucket"); - queryDataParam.setRange(new Range(getDate().toInstant(),new Date().toInstant())); - queryDataParam.setMeasurement("Weight"); - queryDataParam.setTag(new Tag("argName","LTWeight")); -// queryDataParam.setDropedTagName("transationId"); - List dropNames = new ArrayList<>(); - dropNames.add("transationId"); - dropNames.add("inspectionSheetId"); - dropNames.add("batchNum"); - queryDataParam.setDropedTagNames(dropNames); - queryDataParam.setPageInfo(new PageInfo(1,10000)); - - List tables = query(queryDataParam,influxDBClient); -// List records1 = tables.get(0).getRecords(); -// List> lists = Utils.fixedGroup(records1, 10); - - for (FluxTable fluxTable : tables) { - List records = fluxTable.getRecords(); - for (FluxRecord fluxRecord : records) { - Instant timms = fluxRecord.getTime(); - System.out.println("time: "+fluxRecord.getTime() +" key:"+fluxRecord.getField()+" value: " + fluxRecord.getValueByKey("_value")+" measurement: " + fluxRecord.getMeasurement()); - } - } +// QueryDataParam queryDataParam = new QueryDataParam(); +// queryDataParam.setBucket("qgs"); +// queryDataParam.setRange(new Range(getDate().toInstant(),new Date().toInstant())); +// queryDataParam.setMeasurement("Weight"); +// queryDataParam.setTag(new Tag("argName","LTWeight")); +//// queryDataParam.setDropedTagName("transationId"); +// List dropNames = new ArrayList<>(); +// dropNames.add("transationId"); +// dropNames.add("inspectionSheetId"); +// dropNames.add("batchNum"); +// queryDataParam.setDropedTagNames(dropNames); +// queryDataParam.setPageInfo(new PageInfo(1,10000)); +// +// List tables = query(queryDataParam,influxDBClient); +//// List records1 = tables.get(0).getRecords(); +//// List> lists = Utils.fixedGroup(records1, 10); +// +// for (FluxTable fluxTable : tables) { +// List records = fluxTable.getRecords(); +// for (FluxRecord fluxRecord : records) { +// Instant timms = fluxRecord.getTime(); +// System.out.println("time: "+fluxRecord.getTime() +" key:"+fluxRecord.getField()+" value: " + fluxRecord.getValueByKey("_value")+" measurement: " + fluxRecord.getMeasurement()); +// } +// } influxDBClient.close(); } diff --git a/src/main/java/com/qgs/dc/influx/config/ConfigInjector.java b/src/main/java/com/qgs/dc/influx/config/ConfigInjector.java new file mode 100644 index 0000000..53214a0 --- /dev/null +++ b/src/main/java/com/qgs/dc/influx/config/ConfigInjector.java @@ -0,0 +1,23 @@ +package com.qgs.dc.influx.config; + +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import javax.annotation.Resource; + +/** + * @Desc: "" + * @Author: caixiang + * @DATE: 2022/11/23 11:05 + */ +@Component +public class ConfigInjector { + @Resource + private InfluxConfig config; + public static InfluxConfig staticConfig; + + @PostConstruct + private void postConstruct() { + ConfigInjector.staticConfig = config; + } +} diff --git a/src/main/java/com/qgs/dc/influx/config/InfluxClient.java b/src/main/java/com/qgs/dc/influx/config/InfluxClient.java index 8c3910f..35700b3 100644 --- a/src/main/java/com/qgs/dc/influx/config/InfluxClient.java +++ b/src/main/java/com/qgs/dc/influx/config/InfluxClient.java @@ -12,6 +12,7 @@ import com.qgs.dc.influx.param.QueryDataGroupByTimeParam; import com.qgs.dc.influx.param.QueryDataParam; import com.qgs.dc.influx.param.Range; import com.qgs.dc.influx.template.Event; +import org.springframework.beans.factory.annotation.Autowired; import java.util.ArrayList; import java.util.List; @@ -21,7 +22,7 @@ public enum InfluxClient { /** * influxdb 读写客户端,,如果write比较繁忙,后续可以考虑,维护 client一个线程池。 * */ - Client("http://192.168.0.170:8086","lkBsC27QZr1W50BSPlGxpTqNNpwuUk5uz1dZZRPSPbCG5VmNDDUo8P3UkZIhGWwfJwkuz6ZGZ7Et4_KBaG3gHw==","qgs","qgs-bucket2"), + Client(), ; private String url; @@ -34,11 +35,15 @@ public enum InfluxClient { private QueryApi queryApi; - InfluxClient(String url,String token,String org,String bucket){ - this.url = url; - this.token = token; - this.org = org; - this.bucket = bucket; + @Autowired + InfluxConfig config; + + InfluxClient(){ + + this.url = ConfigInjector.staticConfig.getUrl(); + this.token = ConfigInjector.staticConfig.getToken(); + this.org = ConfigInjector.staticConfig.getOrg(); + this.bucket = ConfigInjector.staticConfig.getBucket(); this.influxDBClient = InfluxDBClientFactory.create(this.url, this.token.toCharArray(),this.org,this.bucket); this.writeApi = influxDBClient.makeWriteApi(); this.queryApi = influxDBClient.getQueryApi(); diff --git a/src/main/java/com/qgs/dc/influx/config/InfluxConfig.java b/src/main/java/com/qgs/dc/influx/config/InfluxConfig.java new file mode 100644 index 0000000..bbbca26 --- /dev/null +++ b/src/main/java/com/qgs/dc/influx/config/InfluxConfig.java @@ -0,0 +1,21 @@ +package com.qgs.dc.influx.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +/** + * @Desc: "" + * @Author: caixiang + * @DATE: 2022/11/23 10:53 + */ + +@Data +@Configuration +@ConfigurationProperties(prefix = "influx") +public class InfluxConfig { + private String url; + private String token; + private String org; + private String bucket; +} diff --git a/src/main/java/com/qgs/dc/influx/controller/InfluxController.java b/src/main/java/com/qgs/dc/influx/controller/InfluxController.java index 9ca5863..c378335 100644 --- a/src/main/java/com/qgs/dc/influx/controller/InfluxController.java +++ b/src/main/java/com/qgs/dc/influx/controller/InfluxController.java @@ -144,13 +144,13 @@ public class InfluxController { List list = new ArrayList<>(); QueryDataParam queryDataParam = new QueryDataParam(); - queryDataParam.setBucket("qgs-bucket"); - queryDataParam.setMeasurement("ASProcessCompleteEventAS"); + queryDataParam.setBucket("qgs"); + queryDataParam.setMeasurement("ForTest"); List dropNames = new ArrayList<>(); dropNames.add("transationId"); dropNames.add("inspectionSheetId"); queryDataParam.setDropedTagNames(dropNames); - queryDataParam.setTag(new Tag("argName","arg6")); + queryDataParam.setTag(new Tag("argName","argName11")); queryDataParam.setRange(new Range(DataUtils.getBeforeDate(10).toInstant(),Instant.now())); queryDataParam.setPageInfo(new PageInfo(1,10)); List query = InfluxClient.Client.query(queryDataParam); @@ -207,9 +207,10 @@ public class InfluxController { event.setTime(Instant.now()); event.setTransationId("asasd11"); event.setArgName("argName11"); + event.setBatchNum("12333"); event.setArgValue("900001"); - Point asProcessCompleteEvent = insert(event, "ASProcessCompleteEvent"); - InfluxClient.Client.insert(event,"ASProcessCompleteEvent"); +// Point asProcessCompleteEvent = insert(event, "ForTest"); + InfluxClient.Client.insert(event,"fortest"); } @PostMapping("/insertEvents") diff --git a/src/main/java/com/qgs/dc/s7/my/s7connector/api/DaveArea.java b/src/main/java/com/qgs/dc/s7/my/s7connector/api/DaveArea.java index 26889c9..a48cbab 100644 --- a/src/main/java/com/qgs/dc/s7/my/s7connector/api/DaveArea.java +++ b/src/main/java/com/qgs/dc/s7/my/s7connector/api/DaveArea.java @@ -22,7 +22,7 @@ public enum DaveArea { COUNTER200(30), // analog outputs of 200 family DB(0x84), // Peripheral I/O //DB块 DI(0x85), //DBI块 - FLAGS(0x83), //M块 + FLAGS(0x83), //M块(M块 的 areaNumber 都是0 , 只配置 byteOffset 和 bitOffset) INPUTS(0x81), //I块 T(0x1D), //T块ed C(0x1C), //C块 diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index afa57ca..a1a9a40 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -8,7 +8,7 @@ server: spring: rabbitmq: # 如果是rabbitmq+haproxy+keepalived集群 ,,那么192.168.0.176是haproxy代理的地址(严格来说是keepalived的vip) - addresses: 192.168.0.170:5672 # 新版rabbitmq 版本还未测试 + addresses: 172.16.21.191:5672 # 新版rabbitmq 版本还未测试 #addresses: 172.16.21.133:5672 username: cigs password: cigs @@ -34,19 +34,14 @@ spring: #multiplier: 2 # 间隔时间乘子,间隔时间*乘子=下一次的间隔时间,最大不能超过设置的最大间隔时间 #================重试机制 结束 -#influx: -# influxUrl: 'http://192.168.0.170:8086' -# bucket: 'qgs-bucket' -# org: 'qgs' -# token: 'lkBsC27QZr1W50BSPlGxpTqNNpwuUk5uz1dZZRPSPbCG5VmNDDUo8P3UkZIhGWwfJwkuz6ZGZ7Et4_KBaG3gHw==' -influx: - influxUrl: 'http://192.168.0.170:8086' - bucket: 'qgs-bucket' - org: 'qgs' - token: 'lkBsC27QZr1W50BSPlGxpTqNNpwuUk5uz1dZZRPSPbCG5VmNDDUo8P3UkZIhGWwfJwkuz6ZGZ7Et4_KBaG3gHw==' - # /health point #management: # health: # influxdb: # enabled: true + +influx: + url: http://172.16.21.164:8086 + token: N4yBD2iC0kDkAzlWEQ5koqJNbbd6v9PQsubEAOle1rykOIeM5zckKxUteDUsHm1LFMBnJTtfGNsRT_N54YTLcQ== + org: cigs + bucket: cigs-all diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index 04939eb..a56f22d 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -9,10 +9,17 @@ - - - - + + + + + + + + + + +