influxdb的安装等操作在我的文章。
首先得装influxdb,其次操作如下。
application.properties
spring.datasource.test1.jdbc-url=jdbc:mysql://localhost:3306/spms-prod?useSSL=false&serverTimezone=GMT%2B8&characterEncoding=UTF-8 spring.datasource.test1.driver-class-name=com.mysql.jdbc.Driver spring.datasource.test1.username=root spring.datasource.test1.password=root spring.datasource.test2.jdbc-url=jdbc:oracle:thin:@localhost:1521:orcl spring.datasource.test2.driver-class-name=oracle.jdbc.driver.OracleDriver spring.datasource.test2.username=SYSTEM spring.datasource.test2.password=root #spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.use-lock=true server.port=8089 mybatis.mapper-locations= classpath*:mapper/*/*.xml mybatis.type-aliases-package=com.hainei.dao.* pagehelper.helper-dialect=mysql pagehelper.reasonable=true pagehelper.support-methods-arguments=true pagehelper.params=count=countSql #??????sql???????????????????? #mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl #????????????????? #jasypt.encryptor.password=zheTang spring.jackson.time-zone= GMT+8 #influxDB influxDBUserName=admin influxDBPassword= hainei1205 influxDBOpenurl=http://192.168.1.145:8086 influxDBDatabase= newopc opcIp: 192.168.100.231 opcUserName: Administrator opcPassWord: Abc.123+ opcCode: D66FD91F-927D-47df-B074-EB2CD3F85C18
influxdb中的模块
schedule
package com.hainei.influxdb.schedule; import com.hainei.influxdb.utils.InfluxDBConnection; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; /** * Created with IntelliJ IDEA. * User: lzx * Date: 2019/9/26 * Time: 17:41 * Description: No Description */ @Component @PropertySource({"classpath:application.properties"}) public class PointSchedule { @Value("${influxDBUserName}") private String influxDBUserName ; @Value("${influxDBPassword}") private String influxDBPassword ; @Value("${influxDBOpenurl}") private String influxDBOpenurl ; @Value("${influxDBDatabase}") private String influxDBDatabase ; //@Scheduled(cron = "*/5 * * * * *") public void simulate(String tag,String pointId,String value,String status,Long times){ // InfluxDBConnection influxDBConnection = new InfluxDBConnection("admin", "admin2", "http://10.100.10.127:8086", "opc", null); InfluxDBConnection influxDBConnection = new InfluxDBConnection(influxDBUserName, influxDBPassword, influxDBOpenurl, influxDBDatabase, null); Map<String, String> tags = new HashMap<String, String>(); tags.put("tag1", tag); Map<String, Object> fields = new HashMap<String, Object>(); fields.put("pointId", pointId); fields.put("pointValue", value); fields.put("status", status); fields.put("times", times); influxDBConnection.insert("opc", tags, fields, System.currentTimeMillis(), TimeUnit.MILLISECONDS); } public static void main(String[] args){ new Thread() { @Override public void run() { while (true) { System.out.println(2); for(int i = 0;i<100;i++){ double v = (1 + Math.random() * (10 - 1 + 1)); InfluxDBConnection influxDBConnection = new InfluxDBConnection("admin", "admin2", "http://192.168.1.145:8086", "newopc", null); Map<String, String> tags = new HashMap<String, String>(); tags.put("tag1", "TIA_70"+i); Map<String, Object> fields = new HashMap<String, Object>(); fields.put("pointId", "TIA_70"+i); fields.put("pointValue", ""+v); fields.put("status", "good"); fields.put("times", new Date().getTime()); influxDBConnection.insert("opc", tags, fields, System.currentTimeMillis(), TimeUnit.MILLISECONDS); } try { Thread.sleep(10000); //设置暂停的时间,5000=5秒 } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); } }
service
package com.hainei.influxdb.schedule; import com.hainei.influxdb.utils.InfluxDBConnection; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; /** * Created with IntelliJ IDEA. * User: lzx * Date: 2019/9/26 * Time: 17:41 * Description: No Description */ @Component @PropertySource({"classpath:application.properties"}) public class PointSchedule { @Value("${influxDBUserName}") private String influxDBUserName ; @Value("${influxDBPassword}") private String influxDBPassword ; @Value("${influxDBOpenurl}") private String influxDBOpenurl ; @Value("${influxDBDatabase}") private String influxDBDatabase ; //@Scheduled(cron = "*/5 * * * * *") public void simulate(String tag,String pointId,String value,String status,Long times){ // InfluxDBConnection influxDBConnection = new InfluxDBConnection("admin", "admin2", "http://10.100.10.127:8086", "opc", null); InfluxDBConnection influxDBConnection = new InfluxDBConnection(influxDBUserName, influxDBPassword, influxDBOpenurl, influxDBDatabase, null); Map<String, String> tags = new HashMap<String, String>(); tags.put("tag1", tag); Map<String, Object> fields = new HashMap<String, Object>(); fields.put("pointId", pointId); fields.put("pointValue", value); fields.put("status", status); fields.put("times", times); influxDBConnection.insert("opc", tags, fields, System.currentTimeMillis(), TimeUnit.MILLISECONDS); } public static void main(String[] args){ new Thread() { @Override public void run() { while (true) { System.out.println(2); for(int i = 0;i<100;i++){ double v = (1 + Math.random() * (10 - 1 + 1)); InfluxDBConnection influxDBConnection = new InfluxDBConnection("admin", "admin2", "http://192.168.1.145:8086", "newopc", null); Map<String, String> tags = new HashMap<String, String>(); tags.put("tag1", "TIA_70"+i); Map<String, Object> fields = new HashMap<String, Object>(); fields.put("pointId", "TIA_70"+i); fields.put("pointValue", ""+v); fields.put("status", "good"); fields.put("times", new Date().getTime()); influxDBConnection.insert("opc", tags, fields, System.currentTimeMillis(), TimeUnit.MILLISECONDS); } try { Thread.sleep(10000); //设置暂停的时间,5000=5秒 } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); } }
utils
package com.hainei.influxdb.utils; import okhttp3.OkHttpClient; import org.influxdb.InfluxDB; import org.influxdb.InfluxDBFactory; import org.influxdb.dto.*; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; /** * FileName: InfluxDBConnection * Author: Administrator * Date: 2019/7/30 9:20 * Description: influxDb工具类 */ public class InfluxDBConnection { // 用户名 private String username; // 密码 private String password; // 连接地址 private String openurl; // 数据库 private String database; // 保留策略 private String retentionPolicy; private InfluxDB influxDB; public InfluxDBConnection(String username, String password, String openurl, String database, String retentionPolicy) { this.username = username; this.password = password; this.openurl = openurl; this.database = database; this.retentionPolicy = retentionPolicy == null || retentionPolicy.equals("") ? "autogen" : retentionPolicy; influxDbBuild(); } /** * 创建数据库 * * @param dbName */ @SuppressWarnings("deprecation") public void createDB(String dbName) { influxDB.createDatabase(dbName); } /** * 删除数据库 * * @param dbName */ @SuppressWarnings("deprecation") public void deleteDB(String dbName) { influxDB.deleteDatabase(dbName); } /** * 测试连接是否正常 * * @return true 正常 */ public boolean ping() { boolean isConnected = false; Pong pong; try { pong = influxDB.ping(); if (pong != null) { isConnected = true; } } catch (Exception e) { e.printStackTrace(); } return isConnected; } /** * 连接时序数据库 ,若不存在则创建 * * @return */ public InfluxDB influxDbBuild() { if (influxDB == null) { influxDB = InfluxDBFactory.connect(openurl, username, password); } try { // if (!influxDB.databaseExists(database)) { // influxDB.createDatabase(database); // } } catch (Exception e) { // 该数据库可能设置动态代理,不支持创建数据库 // e.printStackTrace(); } finally { influxDB.setRetentionPolicy(retentionPolicy); } influxDB.setLogLevel(InfluxDB.LogLevel.NONE); return influxDB; } /** * 创建自定义保留策略 * * @param policyName * 策略名 * @param duration * 保存天数 * @param replication * 保存副本数量 * @param isDefault * 是否设为默认保留策略 */ public void createRetentionPolicy(String policyName, String duration, int replication, Boolean isDefault) { String sql = String.format("CREATE RETENTION POLICY "%s" ON "%s" DURATION %s REPLICATION %s ", policyName, database, duration, replication); if (isDefault) { sql = sql + " DEFAULT"; } this.query(sql); } /** * 创建默认的保留策略 * 策略名 * @param :default,保存天数:30天,保存副本数量:1 * 设为默认保留策略 */ public void createDefaultRetentionPolicy() { String command = String.format("CREATE RETENTION POLICY "%s" ON "%s" DURATION %s REPLICATION %s DEFAULT", "default", database, "30d", 1); this.query(command); } /** * 查询 * * @param command * 查询语句 * @return */ public QueryResult query(String command) { return influxDB.query(new Query(command, database)); } /** * 插入 * * @param measurement * 表 * @param tags * 标签 * @param fields * 字段 */ public void insert(String measurement, Map<String, String> tags, Map<String, Object> fields, long time, TimeUnit timeUnit) { Point.Builder builder = Point.measurement(measurement); builder.tag(tags); builder.fields(fields); if (0 != time) { builder.time(time, timeUnit); } influxDB.write(database, retentionPolicy, builder.build()); } /** * 批量写入测点 * * @param batchPoints */ public void batchInsert(BatchPoints batchPoints) { influxDB.write(batchPoints); // influxDB.enableGzip(); // influxDB.enableBatch(2000,100,TimeUnit.MILLISECONDS); // influxDB.disableGzip(); // influxDB.disableBatch(); } /** * 批量写入数据 * * @param database * 数据库 * @param retentionPolicy * 保存策略 * @param consistency * 一致性 * @param records * 要保存的数据(调用BatchPoints.lineProtocol()可得到一条record) */ public void batchInsert(final String database, final String retentionPolicy, final InfluxDB.ConsistencyLevel consistency, final List<String> records) { influxDB.write(database, retentionPolicy, consistency, records); } /** * 删除 * * @param command * 删除语句 * @return 返回错误信息 */ public String deleteMeasurementData(String command) { QueryResult result = influxDB.query(new Query(command, database)); return result.getError(); } /** * 关闭数据库 */ public void close() { influxDB.close(); } /** * 构建Point * * @param measurement * @param time * @param fields * @return */ public Point pointBuilder(String measurement, long time, Map<String, String> tags, Map<String, Object> fields) { Point point = Point.measurement(measurement).time(time, TimeUnit.MILLISECONDS).tag(tags).fields(fields).build(); return point; } }
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.hainei</groupId> <artifactId>getoracle</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>getoracle</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <java.encoding>UTF-8</java.encoding> <junit-version>4.11</junit-version> <jar-no-version-redefined-version>2.0.0.RELEASE</jar-no-version-redefined-version> <pom-version>3.10-FINAL</pom-version> <spring-version>4.0.7.RELEASE</spring-version> <servlet-version>2.5</servlet-version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <jar.scope>compile</jar.scope> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- jackson --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <!--引入pageHelper分页插件 --> <!--mybatis分页插件--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.6</version> </dependency> <!--引入第三方数据源--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.43</version> <scope>runtime</scope> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc7</artifactId> <version>12.2.0.1</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>1.2.4</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Jackson --> <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-guava</artifactId> <version>2.5.3</version> </dependency> <!-- tools --> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.2</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.10</version> </dependency> <!--<dependency>--> <!--<groupId>org.quartz-scheduler</groupId>--> <!--<artifactId>quartz</artifactId>--> <!--<version>1.8.4</version>--> <!--</dependency>--> <!--多数据源--> <dependency> <groupId>org.influxdb</groupId> <artifactId>influxdb-java</artifactId> <version>2.15</version> </dependency> <dependency> <groupId>org.opentsdb.opc</groupId> <artifactId>lib</artifactId> <version>1.3.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.jinterop.dcom</groupId> <artifactId>common</artifactId> <version>2.0.4</version> </dependency> <!--<!–kie api 构建kie虚拟文件系统,关联decisiontable和drl文件,很关键 –>--> <!--<dependency>--> <!--<groupId>org.kie</groupId>--> <!--<artifactId>kie-api</artifactId>--> <!--</dependency>--> <!--<!– 规则引擎核心包,里面包含了RETE引擎和LEAPS 引擎–>--> <!--<dependency>--> <!--<groupId>org.drools</groupId>--> <!--<artifactId>drools-core</artifactId>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.drools</groupId>--> <!--<artifactId>drools-compiler</artifactId>--> <!--</dependency>--> <!--<!– 决策表依赖–>--> <!--<dependency>--> <!--<groupId>org.drools</groupId>--> <!--<artifactId>drools-decisiontables</artifactId>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.drools</groupId>--> <!--<artifactId>drools-templates</artifactId>--> <!--</dependency>--> <!--导出poi的需要的工具类--> <!--<dependency>--> <!--<groupId>org.apache.poi</groupId>--> <!--<artifactId>poi</artifactId>--> <!--<version>3.0-FINAL</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.apache.poi</groupId>--> <!--<artifactId>poi-ooxml</artifactId>--> <!--<version>4.0.1</version>--> <!--</dependency>--> <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>1.16</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.5.RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> </dependency> <!--activiti--> <!--<dependency>--> <!--<groupId>org.activiti</groupId>--> <!--<artifactId>activiti-spring-boot-starter-basic</artifactId>--> <!--<version>5.22.0</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.activiti</groupId>--> <!--<artifactId>activiti-modeler</artifactId>--> <!--<version>5.22.0</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.activiti</groupId>--> <!--<artifactId>activiti-diagram-rest</artifactId>--> <!--<version>5.22.0</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.aspectj</groupId>--> <!--<artifactId>aspectjweaver</artifactId>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>com.alibaba</groupId>--> <!--<artifactId>druid-spring-boot-starter</artifactId>--> <!--<version>${druid.version}</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.quartz-scheduler</groupId>--> <!--<artifactId>quartz</artifactId>--> <!--<version>${quartz.version}</version>--> <!--<exclusions>--> <!--<exclusion>--> <!--<groupId>com.mchange</groupId>--> <!--<artifactId>c3p0</artifactId>--> <!--</exclusion>--> <!--</exclusions>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>commons-fileupload</groupId>--> <!--<artifactId>commons-fileupload</artifactId>--> <!--<version>${commons.fileupload.version}</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>commons-io</groupId>--> <!--<artifactId>commons-io</artifactId>--> <!--<version>${commons.io.version}</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>io.springfox</groupId>--> <!--<artifactId>springfox-swagger2</artifactId>--> <!--<version>${swagger.version}</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>io.springfox</groupId>--> <!--<artifactId>springfox-swagger-ui</artifactId>--> <!--<version>${swagger.version}</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.springframework.boot</groupId>--> <!--<artifactId>spring-boot-starter-websocket</artifactId>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>commons-beanutils</groupId>--> <!--<artifactId>commons-beanutils</artifactId>--> <!--<version>${commons.beanutils.version}</version>--> <!--</dependency>--> <!--<!– https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 –>--> <!--<dependency>--> <!--<groupId>org.apache.commons</groupId>--> <!--<artifactId>commons-collections4</artifactId>--> <!--<version>${commons.collections4.version}</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>com.aspose</groupId>--> <!--<artifactId>cells</artifactId>--> <!--<version>${aspose.cells.version}</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>com.aspose</groupId>--> <!--<artifactId>words</artifactId>--> <!--<version>${aspose.words.version}</version>--> <!--</dependency>--> <!--图片压缩,缩放--> <!--<dependency>--> <!--<groupId>net.coobird</groupId>--> <!--<artifactId>thumbnailator</artifactId>--> <!--<version>${net.coobird.thumbnailator.version}</version>--> <!--</dependency>--> <!--<!–restTemplate访问https–>--> <!--<dependency>--> <!--<groupId>org.apache.httpcomponents</groupId>--> <!--<artifactId>httpclient</artifactId>--> <!--<version>${org.apache.httpcomponents.version}</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.apache.ant</groupId>--> <!--<artifactId>ant</artifactId>--> <!--<version>${org.apache.ant.version}</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.apache.ant</groupId>--> <!--<artifactId>ant</artifactId>--> <!--<version>1.8.4</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>com.baomidou</groupId>--> <!--<artifactId>mybatisplus-spring-boot-starter</artifactId>--> <!--<version>1.0.4</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>com.baomidou</groupId>--> <!--<artifactId>mybatis-plus</artifactId>--> <!--<version>2.0.7</version>--> <!--</dependency>--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> <version>1.4.0.RELEASE</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${servlet-version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-core</artifactId> <version>5.7.0</version> </dependency> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> <scope>compile</scope> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.1</version> <scope>compile</scope> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> <scope>compile</scope> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> <scope>${jar.scope}</scope> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> <scope>${jar.scope}</scope> </dependency> <dependency> <groupId>net.sf.ezmorph</groupId> <artifactId>ezmorph</artifactId> <version>1.0.6</version> <scope>${jar.scope}</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>