influxdb java maven,springboot整合influxdb库

1、添加依赖

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.4.3

com.tire.net

tire

0.0.1-SNAPSHOT

tire

Demo project for Spring Boot

1.8

org.springframework.boot

spring-boot-starter-actuator

org.springframework.boot

spring-boot-starter-web

org.influxdb

influxdb-java

org.projectlombok

lombok

true

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-maven-plugin

2、添加配置文件

spring:

influx:

url: *

password: admin

user: 123

database: test_data

application:

name: tire-net

server:

port: 8081

package com.tire.net.configure;

import com.tire.net.utils.InfluxDbUtils;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

/**

* @author bai

*/

@Configuration

public class InfluxDbConfig {

@Value("${spring.influx.url:''}")

private String influxDBUrl;

@Value("${spring.influx.user:''}")

private String userName;

@Value("${spring.influx.password:''}")

private String password;

@Value("${spring.influx.database:''}")

private String database;

@Bean

public InfluxDbUtils influxDbUtils() {

return new InfluxDbUtils(userName, password, influxDBUrl, database, "");

}

}

package com.tire.net.utils;

import lombok.Data;

import org.influxdb.InfluxDB;

import org.influxdb.InfluxDBFactory;

import org.influxdb.dto.Query;

/**

* @author bai

*/

@Data

public class InfluxDbUtils {

private String userName;

private String password;

private String url;

public String database;

private String retentionPolicy;

/**

* InfluxDB实例

*/

private InfluxDB influxDB;

public InfluxDbUtils(String userName, String password, String url, String database,

String retentionPolicy) {

this.userName = userName;

this.password = password;

this.url = url;

this.database = database;

this.retentionPolicy = retentionPolicy == null || "".equals(retentionPolicy) ? "autogen" : retentionPolicy;

this.influxDB = influxDbBuild();

}

/**

* 连接数据库 ,若不存在则创建

*

* @return influxDb实例

*/

private InfluxDB influxDbBuild() {

if (influxDB == null) {

influxDB = InfluxDBFactory.connect(url, userName, password);

}

try {

createDB(database);

influxDB.setDatabase(database);

} catch (Exception e) {

} finally {

influxDB.setRetentionPolicy(retentionPolicy);

}

influxDB.setLogLevel(InfluxDB.LogLevel.BASIC);

return influxDB;

}

/****

* 创建数据库

* @param database

*/

private void createDB(String database) {

influxDB.query(new Query("CREATE DATABASE " + database));

}

}

标签:springboot,database,spring,boot,influxdb,private,整合,org,String

来源: https://www.cnblogs.com/liubaihui/p/14439869.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值