最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot 集成 InfluxDB 3.x完整教程

 更新時(shí)間:2026年01月22日 11:08:20   作者:高級(jí)盤絲洞  
本文詳細(xì)介紹了如何在SpringBoot項(xiàng)目中集成InfluxDB3.x,并提供了一個(gè)完整的教程,包括環(huán)境準(zhǔn)備、添加依賴、配置連接信息、創(chuàng)建客戶端配置類、數(shù)據(jù)寫入與查詢示例以及關(guān)鍵注意事項(xiàng),感興趣的朋友跟隨小編一起看看吧

Spring Boot 集成 InfluxDB 3.x 完整教程

你希望實(shí)現(xiàn) Spring Boot 與 InfluxDB 3.x 的集成,InfluxDB 3.x (基于 IOx)相比舊版本有較大架構(gòu)調(diào)整,推薦使用官方提供的 Java 客戶端進(jìn)行集成,以下是詳細(xì)步驟:

一、環(huán)境準(zhǔn)備

  1. 確保已安裝 InfluxDB 3.x(可通過 Docker 快速部署:docker run -p 8086:8086 quay.io/influxdb/influxdb:3.0.0
  2. 獲取 InfluxDB 3.x 核心配置信息:
    • 連接 URL(默認(rèn):http://localhost:8086
    • 認(rèn)證令牌(Token,在 InfluxDB 控制臺(tái)生成)
    • 數(shù)據(jù)庫名稱(Bucket,對(duì)應(yīng) InfluxDB 3.x 的存儲(chǔ)桶)
    • 組織 ID(Organization ID,InfluxDB 控制臺(tái)可查詢)

二、添加依賴

在 Spring Boot 項(xiàng)目的 pom.xml(Maven)或 build.gradle(Gradle)中添加 InfluxDB 3.x Java 客戶端依賴,推薦使用官方的 influxdb3-java

Maven 依賴

<!-- InfluxDB 3.x Java 客戶端 -->
<dependency>
    <groupId>com.influxdb</groupId>
    <artifactId>influxdb3-java</artifactId>
    <version>6.8.0</version> <!-- 推薦使用最新穩(wěn)定版 -->
</dependency>
<!-- Spring Boot 核心依賴(已存在可忽略) -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

Gradle 依賴

dependencies {
    implementation 'com.influxdb:influxdb3-java:6.8.0'
    implementation 'org.springframework.boot:spring-boot-starter'
}

三、配置 InfluxDB 3.x 連接信息

在 application.yml 或 application.properties 中配置 InfluxDB 3.x 連接參數(shù),便于統(tǒng)一管理:

application.yml 配置

spring:
  influxdb3:
    url: http://localhost:8086 # InfluxDB 3.x 連接地址
    token: your-influxdb3-auth-token # 認(rèn)證令牌
    bucket: your-bucket-name # 存儲(chǔ)桶名稱(對(duì)應(yīng)數(shù)據(jù)庫)
    org: your-organization-id # 組織ID
    timeout: 30000 # 連接超時(shí)時(shí)間(可選,默認(rèn)30秒)

application.properties 配置

properties

spring.influxdb3.url=http://localhost:8086
spring.influxdb3.token=your-influxdb3-auth-token
spring.influxdb3.bucket=your-bucket-name
spring.influxdb3.org=your-organization-id
spring.influxdb3.timeout=30000

四、創(chuàng)建 InfluxDB 3.x 客戶端配置類

通過 Spring 配置類創(chuàng)建 InfluxDBClient3 單例 Bean,實(shí)現(xiàn)客戶端的統(tǒng)一管理和依賴注入:

import com.influxdb.v3.client.InfluxDBClient3;
import com.influxdb.v3.client.InfluxDBClient3Factory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * InfluxDB 3.x 客戶端配置類
 */
@Configuration
public class InfluxDB3Config {
    @Value("${spring.influxdb3.url}")
    private String influxDbUrl;
    @Value("${spring.influxdb3.token}")
    private String influxDbToken;
    @Value("${spring.influxdb3.bucket}")
    private String influxDbBucket;
    @Value("${spring.influxdb3.org}")
    private String influxDbOrg;
    @Value("${spring.influxdb3.timeout:30000}")
    private int influxDbTimeout;
    /**
     * 創(chuàng)建 InfluxDBClient3 單例 Bean
     * @return InfluxDBClient3 客戶端實(shí)例
     */
    @Bean(destroyMethod = "close") // 容器銷毀時(shí)自動(dòng)關(guān)閉客戶端連接
    public InfluxDBClient3 influxDBClient3() {
        return InfluxDBClient3Factory.create(
                influxDbUrl,
                influxDbToken.toCharArray(),
                influxDbOrg,
                influxDbBucket
        );
    }
}

五、數(shù)據(jù)寫入與查詢示例

1. 數(shù)據(jù)寫入(支持 Line Protocol 格式)

InfluxDB 3.x 推薦使用 Line Protocol 格式寫入數(shù)據(jù),以下是 Service 層示例:

import com.influxdb.v3.client.InfluxDBClient3;
import org.springframework.stereotype.Service;
import java.time.Instant;
/**
 * InfluxDB 3.x 數(shù)據(jù)操作服務(wù)
 */
@Service
public class InfluxDB3Service {
    private final InfluxDBClient3 influxDBClient3;
    // 構(gòu)造函數(shù)注入客戶端
    public InfluxDB3Service(InfluxDBClient3 influxDBClient3) {
        this.influxDBClient3 = influxDBClient3;
    }
    /**
     * 寫入監(jiān)控?cái)?shù)據(jù)(Line Protocol 格式)
     * @param measurement 測(cè)量值名稱(表名)
     * @param deviceId 設(shè)備ID(標(biāo)簽)
     * @param temperature 溫度(字段)
     * @param humidity 濕度(字段)
     */
    public void writeMonitorData(String measurement, String deviceId, double temperature, double humidity) {
        // 構(gòu)建 Line Protocol 字符串:measurement tagKey=tagValue fieldKey=fieldValue timestamp
        String lineProtocol = String.format(
                "%s,device_id=%s temperature=%.2f,humidity=%.2f %d",
                measurement,
                deviceId,
                temperature,
                humidity,
                Instant.now().toEpochMilli() * 1_000_000 // 時(shí)間戳(納秒級(jí))
        );
        // 寫入數(shù)據(jù)
        influxDBClient3.writeRecord(lineProtocol);
        System.out.println("數(shù)據(jù)寫入成功:" + lineProtocol);
    }
}

2. 數(shù)據(jù)查詢(支持 SQL 或 InfluxQL)

InfluxDB 3.x 支持 SQL 和 InfluxQL 兩種查詢語法,以下是 SQL 查詢示例:

import com.influxdb.v3.client.QueryResult;
import com.influxdb.v3.client.exceptions.InfluxException;
import java.util.List;
import java.util.Map;
/**
 * 擴(kuò)展查詢方法
 */
public class InfluxDB3Service {
    // 省略已有代碼...
    /**
     * 查詢指定設(shè)備的最新監(jiān)控?cái)?shù)據(jù)
     * @param deviceId 設(shè)備ID
     * @return 查詢結(jié)果列表
     */
    public List<Map<String, Object>> queryLatestMonitorData(String deviceId) {
        // SQL 查詢語句(InfluxDB 3.x 兼容 SQL 語法)
        String sql = String.format(
                "SELECT * FROM monitor_data WHERE device_id = '%s' ORDER BY time DESC LIMIT 10",
                deviceId
        );
        try {
            // 執(zhí)行查詢
            QueryResult queryResult = influxDBClient3.query(sql);
            // 轉(zhuǎn)換為 Map 列表(便于后續(xù)處理)
            return queryResult.getRecords();
        } catch (InfluxException e) {
            System.err.println("查詢失?。? + e.getMessage());
            throw new RuntimeException("InfluxDB 查詢異常", e);
        }
    }
}

3. 控制器調(diào)用示例

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/influxdb3")
public class InfluxDB3Controller {
    private final InfluxDB3Service influxDB3Service;
    public InfluxDB3Controller(InfluxDB3Service influxDB3Service) {
        this.influxDB3Service = influxDB3Service;
    }
    /**
     * 寫入數(shù)據(jù)接口
     */
    @PostMapping("/write")
    public String writeData(
            @RequestParam String deviceId,
            @RequestParam double temperature,
            @RequestParam double humidity
    ) {
        influxDB3Service.writeMonitorData("monitor_data", deviceId, temperature, humidity);
        return "數(shù)據(jù)寫入成功";
    }
    /**
     * 查詢數(shù)據(jù)接口
     */
    @GetMapping("/query")
    public List<Map<String, Object>> queryData(@RequestParam String deviceId) {
        return influxDB3Service.queryLatestMonitorData(deviceId);
    }
}

六、關(guān)鍵注意事項(xiàng)

  1. Token 安全性:不要硬編碼 Token,推薦通過配置文件、環(huán)境變量或配置中心管理,生產(chǎn)環(huán)境需加密存儲(chǔ)。
  2. 連接關(guān)閉:配置 @Bean(destroyMethod = "close") 確保容器銷毀時(shí)關(guān)閉客戶端,避免連接泄露。
  3. 時(shí)間戳格式:InfluxDB 3.x 推薦使用納秒級(jí)時(shí)間戳,Instant.now().toEpochMilli() * 1_000_000 可將毫秒級(jí)轉(zhuǎn)換為納秒級(jí)。
  4. 異常處理:實(shí)際項(xiàng)目中需添加更完善的異常捕獲和重試機(jī)制,避免單次網(wǎng)絡(luò)波動(dòng)導(dǎo)致數(shù)據(jù)寫入失敗。
  5. 依賴版本:定期更新 influxdb3-java 版本,獲取最新功能和 bug 修復(fù),最新版本可在 Maven Central 查詢。

總結(jié)

  1. Spring Boot 集成 InfluxDB 3.x 的核心是引入 influxdb3-java 依賴,并配置 InfluxDBClient3 客戶端 Bean。
  2. 連接參數(shù)(URL、Token、Bucket、Org)需在配置文件中統(tǒng)一配置,便于環(huán)境切換。
  3. 數(shù)據(jù)寫入優(yōu)先使用 Line Protocol 格式,數(shù)據(jù)查詢支持 SQL 和 InfluxQL 兩種語法。
  4. 通過 @Bean(destroyMethod = "close") 可自動(dòng)管理客戶端連接的生命周期,避免資源泄露。

到此這篇關(guān)于Spring Boot 集成 InfluxDB 3.x完整教程的文章就介紹到這了,更多相關(guān)Spring Boot 集成 InfluxDB 3.x內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Mybatis Mapper XML文件-插入,更新,刪除詳解(insert, update and delete)

    Mybatis Mapper XML文件-插入,更新,刪除詳解(insert, updat

    這篇文章主要介紹了MyBatis的Mapper XML文件中用于插入、更新和刪除數(shù)據(jù)的語句,包括這些語句的屬性和子元素的使用方法
    2025-02-02
  • JDK21對(duì)虛擬線程的幾種用法實(shí)踐指南

    JDK21對(duì)虛擬線程的幾種用法實(shí)踐指南

    虛擬線程是Java中的一種輕量級(jí)線程,由JVM管理,特別適合于I/O密集型任務(wù),這篇文章主要介紹了JDK21對(duì)虛擬線程的幾種用法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-09-09
  • Java中注解與元數(shù)據(jù)示例詳解

    Java中注解與元數(shù)據(jù)示例詳解

    Java注解和元數(shù)據(jù)是編程中重要的概念,用于描述程序元素的屬性和用途,這篇文章主要介紹了Java中注解與元數(shù)據(jù)的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-01-01
  • Spring Boot整合JWT的實(shí)現(xiàn)步驟

    Spring Boot整合JWT的實(shí)現(xiàn)步驟

    本文主要介紹了Spring Boot整合JWT,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Java并發(fā)編程之柵欄(CyclicBarrier)實(shí)例介紹

    Java并發(fā)編程之柵欄(CyclicBarrier)實(shí)例介紹

    這篇文章主要介紹了Java并發(fā)編程之柵欄(CyclicBarrier)實(shí)例介紹,柵欄類似閉鎖,但是它們是有區(qū)別的,需要的朋友可以參考下
    2015-04-04
  • mybatis-plus 如何判斷參數(shù)是否為空并作為查詢條件

    mybatis-plus 如何判斷參數(shù)是否為空并作為查詢條件

    這篇文章主要介紹了mybatis-plus 如何判斷參數(shù)是否為空并作為查詢條件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Spring事務(wù)管理的使用細(xì)則淺析

    Spring事務(wù)管理的使用細(xì)則淺析

    事務(wù)的作用就是為了保證用戶的每一個(gè)操作都是可靠的,事務(wù)中的每一步操作都必須成功執(zhí)行,只要有發(fā)生異常就 回退到事務(wù)開始未進(jìn)行操作的狀態(tài)。事務(wù)管理是Spring框架中最為常用的功能之一,我們?cè)谑褂肧pring開發(fā)應(yīng)用時(shí),大部分情況下也都需要使用事務(wù)
    2023-02-02
  • 在MyBatis中開啟SQL日志的具體步驟

    在MyBatis中開啟SQL日志的具體步驟

    文章詳細(xì)介紹了在原生MyBatis和SpringBoot中開啟MyBatis日志的具體步驟,包括引入日志依賴、配置日志文件和設(shè)置日志級(jí)別,文章還提供了對(duì)比總結(jié)和常見問題解答,幫助讀者更好地理解和配置MyBatis日志,需要的朋友可以參考下
    2025-11-11
  • Java FileInputStream與FileOutputStream使用詳解

    Java FileInputStream與FileOutputStream使用詳解

    這篇文章主要介紹了Java FileInputStream與FileOutputStream使用詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • 支付寶開發(fā)平臺(tái)之第三方授權(quán)登錄與獲取用戶信息

    支付寶開發(fā)平臺(tái)之第三方授權(quán)登錄與獲取用戶信息

    本文主要介紹了第三方授權(quán)登錄與獲取用戶信息的實(shí)例方法,具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-03-03

最新評(píng)論

保康县| 西丰县| 平邑县| 福泉市| 茂名市| 天峻县| 长垣县| 铁力市| 三原县| 英超| 镇巴县| 慈利县| 肇源县| 深泽县| 沈阳市| 开化县| 长泰县| 泰安市| 渝北区| 梁河县| 开远市| 临高县| 永康市| 巨鹿县| 临湘市| 德江县| 宁化县| 甘德县| 台州市| 毕节市| 北宁市| 竹山县| 淅川县| 达尔| 嘉峪关市| 弋阳县| 荆门市| 普洱| 康定县| 板桥市| 大理市|