欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringCloud集成AlloyDB的示例代碼

 更新時間:2025年01月10日 08:27:31   作者:HBLOG  
AlloyDB?是?Google?Cloud?提供的一種高度可擴展、強性能的關(guān)系型數(shù)據(jù)庫服務(wù),它兼容?PostgreSQL,并提供了更快的查詢性能和更高的可用性,本文給大家介紹了如何使用SpringCloud集成AlloyDB,需要的朋友可以參考下

1.AlloyDB是什么?

AlloyDB 是 Google Cloud 提供的一種高度可擴展、強性能的關(guān)系型數(shù)據(jù)庫服務(wù),它兼容 PostgreSQL,并提供了更快的查詢性能和更高的可用性。AlloyDB 主要適用于需要處理復(fù)雜查詢、高吞吐量和對數(shù)據(jù)庫性能要求嚴(yán)格的應(yīng)用場景。

AlloyDB 的工作原理

AlloyDB 是建立在 Google Cloud 的分布式架構(gòu)上的,具有以下特點:

  • 高性能:通過在內(nèi)存中緩存熱數(shù)據(jù)和優(yōu)化查詢執(zhí)行,AlloyDB 提供比傳統(tǒng) PostgreSQL 快四倍的性能。
  • 高可用性:支持跨區(qū)域的高可用部署,具備內(nèi)置的自動故障轉(zhuǎn)移功能。
  • 兼容性:完全兼容 PostgreSQL,使得現(xiàn)有 PostgreSQL 應(yīng)用可以無縫遷移。
  • 可管理性:簡化了運維工作,通過 Google Cloud 平臺的工具進(jìn)行統(tǒng)一管理。

2.搭建測試環(huán)境

參照這個文檔在google cloud上創(chuàng)建數(shù)據(jù)庫

cloud.google.com

3.代碼工程

Spring Cloud 提供了 spring-cloud-gcp-starter-alloydb 模塊,用于簡化與 AlloyDB 的集成。通過此模塊,您可以輕松配置并連接到 AlloyDB 實例。

以下是一個完整的示例,演示如何使用 Spring Boot 應(yīng)用程序連接到 AlloyDB。

1. 添加 Maven 依賴

在項目的 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">
    <parent>
        <artifactId>spring-cloud-gcp</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-cloud-gcp-alloydb-sample</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.google.cloud</groupId>
                <artifactId>spring-cloud-gcp-dependencies</artifactId>
                <version>5.9.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>spring-cloud-gcp-starter-alloydb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Test-related dependencies. -->
        <dependency>
            <groupId>org.awaitility</groupId>
            <artifactId>awaitility</artifactId>
            <version>4.2.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.17.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

2. 配置 AlloyDB 數(shù)據(jù)源

在 application.properties 或 application.yml 文件中配置 AlloyDB 實例的連接信息:

# Set to the Postgres user you want to connect to; 'postgres' is the default user.
spring.datasource.username=postgres
spring.datasource.password=123456
# spring.cloud.gcp.project-id=

spring.cloud.gcp.alloydb.database-name=postgres

# This value is formatted in the form: projects/PROJECT_ID/locations/REGION_ID/clusters/CLUSTER_ID/instances/INSTANCE_ID
spring.cloud.gcp.alloydb.instance-connection-uri=projects/PROJECT_ID/locations/REGION_ID/clusters/CLUSTER_ID/instances/INSTANCE_ID

# The IP type options are: PRIVATE (default), PUBLIC, PSC.
#spring.cloud.gcp.alloydb.ip-type=PUBLIC
# spring.cloud.gcp.alloydb.target-principal=【your-service-account-email】
# spring.cloud.gcp.alloydb.delegates=[delegates]
# spring.cloud.gcp.alloydb.admin-service-endpoint=[admin-service-endpoint]
#spring.cloud.gcp.alloydb.quota-project=feisty-truth-447013-m7
# spring.cloud.gcp.alloydb.enable-iam-auth=true
# spring.cloud.gcp.alloydb.named-connector=[named-connector]
#spring.cloud.gcp.alloydb.credentials.location=file:///Users/liuhaihua/Downloads/feisty-truth-447013-m7-db149f9a2f86.json

# So app starts despite "table already exists" errors.
spring.sql.init.continue-on-error=true
# Enforces database initialization
spring.sql.init.mode=always

# Set the logging level
logging.level.root=DEBUG

your-project-id、your-region、your-cluster-id、your-instance-idyour-username、your-passwordyour-database-name 替換為實際值。

3. 編寫實啟動類

創(chuàng)建啟動類:

/** Sample application. */
@SpringBootApplication
public class AlloyDbApplication {

  public static void main(String[] args) {
    SpringApplication.run(AlloyDbApplication.class, args);
  }
}

4. 編寫控制器

創(chuàng)建一個控制器來測試數(shù)據(jù)庫連接:

/*
 * Copyright 2024 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.et;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/** Web app controller for sample app. */
@RestController
public class WebController {

  private final JdbcTemplate jdbcTemplate;

  public WebController(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
  }

  @GetMapping("/getTuples")
  public List<String> getTuples() {
    return this.jdbcTemplate.queryForList("SELECT * FROM users").stream()
        .map(m -> m.values().toString())
        .collect(Collectors.toList());
  }
}

5. 運行應(yīng)用程序

確保您的 Google Cloud 項目已啟用 AlloyDB API,并配置了必要的權(quán)限。

  • 在本地運行應(yīng)用程序時,確保設(shè)置了 Google Cloud 的服務(wù)賬號憑據(jù):

    export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-service-account-key.json

  • 部署到 Google Cloud 時,建議使用 Compute Engine 或 Kubernetes Engine,其內(nèi)置身份驗證會自動獲取憑據(jù)。

以上只是一些關(guān)鍵代碼。

4.測試

應(yīng)用程序啟動后,在瀏覽器中導(dǎo)航到 http://localhost:8080/getTuples,或使用 Cloud Shell 中的 Web Preview 按鈕在端口 8080 上預(yù)覽應(yīng)用程序。這將打印用戶表的內(nèi)容。

以上就是SpringCloud集成AlloyDB的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于SpringCloud集成AlloyDB的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • SpringBoot中的自動注入方式

    SpringBoot中的自動注入方式

    這篇文章主要介紹了SpringBoot中的自動注入方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Java日期時間類及計算詳解

    Java日期時間類及計算詳解

    這篇文章主要介紹了Java日期時間類及計算詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下,希望對你的學(xué)習(xí)有所幫助
    2022-07-07
  • Java使用Sharding-JDBC分庫分表進(jìn)行操作

    Java使用Sharding-JDBC分庫分表進(jìn)行操作

    Sharding-JDBC 是無侵入式的 MySQL 分庫分表操作工具,本文主要介紹了Java使用Sharding-JDBC分庫分表進(jìn)行操作,感興趣的可以了解一下
    2021-08-08
  • 詳解Java中的內(nèi)存屏障

    詳解Java中的內(nèi)存屏障

    這篇文章主要介紹了Java中的內(nèi)存屏障的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-05-05
  • Java Cmd運行Jar出現(xiàn)亂碼的解決方案

    Java Cmd運行Jar出現(xiàn)亂碼的解決方案

    這篇文章主要介紹了Java Cmd運行Jar出現(xiàn)亂碼的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java日期轉(zhuǎn)換注解配置date?format時間失效

    Java日期轉(zhuǎn)換注解配置date?format時間失效

    這篇文章主要為大家介紹了Java日期轉(zhuǎn)換注解配置date?format時間失效,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • Java源碼解析之ClassLoader

    Java源碼解析之ClassLoader

    在看系統(tǒng)啟動的流程中看到了ClassLoader使用,重新溫故下ClassLoader流程和原理,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • Java C++ leetcode執(zhí)行一次字符串交換能否使兩個字符串相等

    Java C++ leetcode執(zhí)行一次字符串交換能否使兩個字符串相等

    這篇文章主要為大家介紹了Java C++ leetcode1790執(zhí)行一次字符串交換能否使兩個字符串相等,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • Java遞歸實現(xiàn)評論多級回復(fù)功能

    Java遞歸實現(xiàn)評論多級回復(fù)功能

    這篇文章主要介紹了Java遞歸實現(xiàn)評論多級回復(fù)功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • Java基于BIO實現(xiàn)文件上傳功能

    Java基于BIO實現(xiàn)文件上傳功能

    這篇文章主要為大家詳細(xì)介紹了Java基于BIO實現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11

最新評論