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ù)庫
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-id
、your-username
、your-password
和 your-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)文章
Java使用Sharding-JDBC分庫分表進(jìn)行操作
Sharding-JDBC 是無侵入式的 MySQL 分庫分表操作工具,本文主要介紹了Java使用Sharding-JDBC分庫分表進(jìn)行操作,感興趣的可以了解一下2021-08-08Java日期轉(zhuǎn)換注解配置date?format時間失效
這篇文章主要為大家介紹了Java日期轉(zhuǎn)換注解配置date?format時間失效,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Java C++ leetcode執(zhí)行一次字符串交換能否使兩個字符串相等
這篇文章主要為大家介紹了Java C++ leetcode1790執(zhí)行一次字符串交換能否使兩個字符串相等,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10