文章详情

短信预约信息系统项目管理师 报名、考试、查分时间动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

Spring Boot Sample 024之spring-boot-data-influxdb

2018-10-02 09:01

关注


	Spring Boot Sample 024之spring-boot-data-influxdb
[数据库教程]

一、环境

二、目的

spring boot 通过整合influxdb

gitHub地址: https://github.com/ouyushan/ouyushan-spring-boot-samples

三、步骤

3.1、点击File -> New Project -> Spring Initializer,点击next 技术图片

3.2、在对应地方修改自己的项目信息

技术图片

3.3、选择Web依赖,选中Spring Web、Spring Boot Actuator。可以选择Spring Boot版本,本次默认为2.3.0,点击Next

技术图片

3.4、项目结构

技术图片

四、添加文件

技术图片

pom.xml文件



    4.0.0
    
        org.ouyushan
        ouyushan-spring-boot-samples
        0.0.1-SNAPSHOT
    
    org.ouyushan
    spring-boot-data-influxdb
    0.0.1-SNAPSHOT
    spring-boot-data-influxdb
    InfluxDB project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            io.micrometer
            micrometer-registry-influx
            runtime
        

        
            org.influxdb
            influxdb-java
            2.19
        

        
            org.projectlombok
            lombok
            1.18.12
            true
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        

        
            org.hibernate.validator
            hibernate-validator
            6.1.5.Final
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


application.properties文件

spring:
  influx:
    url: http://localhost:8086
    user: root
    password: ouyushan
    database: test
    retention_policy: default
    retention_policy_time: 30d


management:
  metrics:
    export:
      influx:
        enabled: true
        db: ouyushan
        uri: http://localhost:8086
        user: root
        password: ouyushan
        connect-timeout: 1s
        read-timeout: 10s
        auto-create-db: true
        step: 1m
        consistency: one
        compressed: true
        batch-size: 10000

InfluxDBProperties.java

package org.ouyushan.springboot.data.influxdb.config;

import lombok.Data;
import org.hibernate.validator.constraints.URL;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.validation.constraints.NotBlank;


@Configuration
@ConfigurationProperties(prefix = "spring.influx")
@Data
public class InfluxDBProperties {
    @URL
    private String url;
    @NotBlank
    private String user;
    @NotBlank
    private String password;
    @NotBlank
    private String database;
    @NotBlank
    private String retentionPolicy;
    @NotBlank
    private String retentionPolicyTime;

}

InfluxDBConfig.java

package org.ouyushan.springboot.data.influxdb.config;

import lombok.extern.slf4j.Slf4j;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



@Configuration
@Slf4j
public class InfluxDBConfig {

    @Autowired
    private InfluxDBProperties influxDBProperties;


    @Bean
    public InfluxDBConnect getInfluxDBConnect() {
        InfluxDBConnect influxDB = new InfluxDBConnect(influxDBProperties.getUser(), influxDBProperties.getPassword(),
                influxDBProperties.getUrl(), influxDBProperties.getDatabase(), influxDBProperties.getRetentionPolicy(),
                influxDBProperties.getRetentionPolicyTime());

        influxDB.influxDbBuild();

        influxDB.createRetentionPolicy();
        log.info("init influxdb::[{}]", influxDBProperties);
        return influxDB;
    }

}

InfluxDBConnect.java

package org.ouyushan.springboot.data.influxdb.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.influxdb.InfluxDB;
import org.influxdb.InfluxDBFactory;
import org.influxdb.dto.Point;
import org.influxdb.dto.Query;
import org.influxdb.dto.QueryResult;

import java.util.Map;
import java.util.concurrent.TimeUnit;


@Data
@Slf4j
public class InfluxDBConnect {
    
    private String user;
    private String password;
    private String url;
    public String database;
    
    private String retentionPolicy;
    
    private String retentionPolicyTime;

    
    private InfluxDB influxDB;

    public InfluxDBConnect(String user, String password, String url, String database, String retentionPolicy, String retentionPolicyTime) {
        this.user = user;
        this.password = password;
        this.url = url;
        this.database = database;
        // autogen默认的数据保存策略
        this.retentionPolicy = retentionPolicy == null || "".equals(retentionPolicy) ? "autogen" : retentionPolicy;
        this.retentionPolicyTime = retentionPolicyTime == null || "".equals(retentionPolicy) ? "30d" : retentionPolicyTime;
        this.influxDB = influxDbBuild();
    }

    
    public InfluxDB influxDbBuild() {
        if (influxDB == null) {
            influxDB = InfluxDBFactory.connect(url, user, password);
            influxDB.query(new Query("CREATE DATABASE " + database));
            influxDB.setDatabase(database);
        }
        return influxDB;
    }

    
    public void createRetentionPolicy() {
        String command = String.format("CREATE RETENTION POLICY "%s" ON "%s" DURATION %s REPLICATION %s DEFAULT",
                retentionPolicy, database, retentionPolicyTime, 1);
        this.query(command);
    }

    
    public QueryResult query(String command) {
        return influxDB.query(new Query(command, database));
    }

    
    public void insert(String measurement, Map tags, Map fields) {
        Point.Builder builder = Point.measurement(measurement);
        // 纳秒时会出现异常信息:partial write: points beyond retention policy dropped=1
        // builder.time(System.nanoTime(), TimeUnit.NANOSECONDS);
        builder.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
        builder.tag(tags);
        builder.fields(fields);

        log.info("influxDB insert data:[{}]", builder.build().toString());
        influxDB.write(database, "", builder.build());
    }
}

五、测试

SpringBootDataInfluxdbApplicationTests.java

package org.ouyushan.springboot.data.influxdb;

import org.influxdb.dto.QueryResult;
import org.junit.jupiter.api.Test;
import org.ouyushan.spring.boot.data.influxdb.config.InfluxDBConnect;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest
class SpringBootDataInfluxdbApplicationTests {

    @Resource
    InfluxDBConnect influxDBConnect;

    @Test
    public void testInsert() {
        Map tagsMap = new HashMap<>();
        Map fieldsMap = new HashMap<>();
        System.out.println("influxDB start time :" + System.currentTimeMillis());
        int i = 0;
        for (; ; ) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            tagsMap.put("user_id", String.valueOf(i % 10));
            tagsMap.put("url", "http://www.baidu.com");
            tagsMap.put("service_method", "testInsert" + (i % 5));
            fieldsMap.put("count", i % 5);
            influxDBConnect.insert("usage", tagsMap, fieldsMap);
            i++;
        }
    }

    @Test
    public void testQuery() {
        QueryResult queryResult = influxDBConnect.query("select * from usage");
        queryResult.getResults().stream().forEach(result -> System.out.println(result.getSeries()));
    }


}

Spring Boot Sample 024之spring-boot-data-influxdb

原文地址:https://www.cnblogs.com/ouyushan/p/13974710.html

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯