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

SpringCloud集成AlloyDB的示例代碼

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

1.AlloyDB是什么?

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

AlloyDB 的工作原理

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

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

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

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

cloud.google.com

3.代碼工程

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

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

1. 添加 Maven 依賴

在項(xiàng)目的 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 實(shí)例的連接信息:

# 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-idyour-region、your-cluster-id、your-instance-id、your-username、your-passwordyour-database-name 替換為實(shí)際值。

3. 編寫實(shí)啟動(dòng)類

創(chuàng)建啟動(dòng)類:

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

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

4. 編寫控制器

創(chuàng)建一個(gè)控制器來測試數(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ùn)行應(yīng)用程序

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

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

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

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

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

4.測試

應(yīng)用程序啟動(dò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)文章

  • Java 執(zhí)行CMD命令或執(zhí)行BAT批處理方式

    Java 執(zhí)行CMD命令或執(zhí)行BAT批處理方式

    這篇文章主要介紹了Java 執(zhí)行CMD命令或執(zhí)行BAT批處理方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java高并發(fā)測試框架JCStress詳解

    Java高并發(fā)測試框架JCStress詳解

    這篇文章主要介紹了Java高并發(fā)測試框架JCStress,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • SpringBoot項(xiàng)目打成war和jar的區(qū)別說明

    SpringBoot項(xiàng)目打成war和jar的區(qū)別說明

    這篇文章主要介紹了SpringBoot項(xiàng)目打成war和jar的區(qū)別說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • MyBatis中的表關(guān)聯(lián)查詢實(shí)現(xiàn)示例

    MyBatis中的表關(guān)聯(lián)查詢實(shí)現(xiàn)示例

    這篇文章主要介紹了MyBatis中的表關(guān)聯(lián)查詢實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Java二叉樹的四種遍歷方式詳解

    Java二叉樹的四種遍歷方式詳解

    這篇文章主要介紹了Java二叉樹的四種遍歷,二叉樹的遍歷可以分為前序、中序、后序、層次遍歷,需要的朋友可以參考下
    2021-11-11
  • 在Java中操作Zookeeper的示例代碼詳解

    在Java中操作Zookeeper的示例代碼詳解

    這篇文章主要介紹了在Java中操作Zookeeper的示例代碼詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • SpringBoot如何讀取配置文件中的數(shù)據(jù)到map和list

    SpringBoot如何讀取配置文件中的數(shù)據(jù)到map和list

    這篇文章主要介紹了SpringBoot如何讀取配置文件中的數(shù)據(jù)到map和list,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • java項(xiàng)目中讀取jdbc.properties文件操作

    java項(xiàng)目中讀取jdbc.properties文件操作

    這篇文章主要介紹了java項(xiàng)目中讀取jdbc.properties文件操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 如何開啟控制臺(tái)輸出mybatis執(zhí)行的sql日志問題

    如何開啟控制臺(tái)輸出mybatis執(zhí)行的sql日志問題

    這篇文章主要介紹了如何開啟控制臺(tái)輸出mybatis執(zhí)行的sql日志問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • SpringBoot jackson 精度處理問題解決

    SpringBoot jackson 精度處理問題解決

    由于JavaScript處理的最大數(shù)值限制,較大的雪花ID在JS中容易溢出,為解決此問題,可在SpringMVC或SpringBoot中使用@RequestBody注解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-10-10

最新評論

凤山市| 临澧县| 土默特左旗| 会同县| 长兴县| 齐齐哈尔市| 佛坪县| 巴楚县| 罗平县| 金塔县| 喜德县| 于都县| 东港市| 红原县| 韩城市| 友谊县| 儋州市| 延寿县| 寻乌县| 和田市| 和林格尔县| 桂东县| 宝山区| 孝义市| 芒康县| 红原县| 磴口县| 抚宁县| 灌云县| 资中县| 上思县| 巴彦淖尔市| 岑巩县| 茂名市| 开封县| 安多县| 图片| 庐江县| 扎赉特旗| 双柏县| 贵阳市|