Springboot 使用 Influx

740 阅读2分钟

这是我参与8月更文挑战的第19天,活动详情查看:8月更文挑战

docker 安装 Influx

下载config.yml

docker run --rm influxdb:2.0.7 influxd print-config > config.yml

选好一个固定的位置 安装config.yml文件

例如 /xxxx/xxx/xxx

安装Influx

docker run -d -p 8086:8086 --name Docker_InfluxDB2 --restart always --volume PWD/docker/influxdb2/data/:/var/lib/influxdb2volumePWD/docker/influxdb2/data/:/var/lib/influxdb2 --volume PWD/docker/influxdb2/config.yml: /xxxx/xxx/xxx/config.yml influxdb:2.0.7

浏览器输入 localhost:8086 跳出Influx平台即为成功

成功之后,会要求你输入4个选项名称

账户名,密码,组织名,存储桶

存储桶可以选择保留时间。

Data -》 Tokens 下有个 token 复制下来

自此平台配置完毕。

springboot 链接 Influx

创建一个 class 用于做 influx的配置类 Constants

public class Constants{
   public final static String CONFIG_URL = "http://localhost:8086";
    public final static String CONFIG_TOKEN = "复制的token";
    public final static String CONFIG_BUCKET = "存储桶名";
    public final static String CONFIG_ORGANIZATION = "组织名";
}
@Data    
public class  Message<T> implements Serializable {
        //时间戳, id,等公共属性
        XXX
        XXX
        XXX
        //传输内容
        private T content;
    } 
@Data
public class TimeSeries implements Serializable {
​
    private Map<String, String> tags;
​
    private Map<String, Object> fields;
}

存储服务

这里要科普几个基础知识

Influx中有几个基本概念

  • measurement:相当于普通数据库中表的概念
  • Point: 定义将写入数据库的值,相当于普通数据库中行的概念
  • Tag: 数据源本身的属性 (时序数据库中不变的部分)
  • Fields: 采集的数据指标,随着时间推移,每个数据源的fields值都会不断增加。
  • Influx并不需要先建表

存储服务

   public void message(Message message) {
        if (length(message.getContent()) == 0) {
            log.error("Content is Empty!");
            return;
        }
        String measurement = "表名";
        TimeSeries timeSeries = timeSeriesConverter.convert(message);
        storage(measurement, timeSeries);
    }

处理 timeSeries

    public TimeSeries convert(Message message) {
        Map<String, Object> content = (Map<String, Object>) message.getContent();
        Map<String, String> tags = new HashMap<>(3);
        //将唯一值 放入 tags
        tags.put("设备模型",content.get("设备模型").toString());
        tags.put("设备厂商",content.get("设备厂商").toString());
        tags.put("设备唯一ID",content.get("设备唯一ID").toString());
        //去掉传入message中的唯一值
        content.remove("设备模型");
        content.remove("设备厂商");
        content.remove("设备唯一ID");
        //去掉除唯一值外的  可变值中无意义的部分
        content.remove("时间戳");
        content.remove("时间");
        TimeSeries timeSeries = new TimeSeries(tags, content);
        return timeSeries;
    }

存储服务

    public class InfluxRepository {
​
        private final InfluxDBClient influxTemplate;
    
        public InfluxRepository(InfluxDBClient influxTemplate) {
            this.influxTemplate = influxTemplate;
        }
    
        public void storage(String measurement, TimeSeries timeSeries){
            Point point = Point.measurement(measurement)
                    .addTags(timeSeries.getTags())
                    .addFields(timeSeries.getFields())
                    .time(Instant.now(), WritePrecision.NS);
            try (WriteApi writeApi = influxTemplate.getWriteApi()) {
                writeApi.writePoint(InfluxConstants.CONFIG_BUCKET, InfluxConstants.CONFIG_ORGANIZATION, point);
                log.info("Storage Success: {}", point);
            } catch (Exception e) {
                log.error("Storage Exception: {}", e.getMessage());
            }
        }
    
    }

\