如何在使用Spring Cloud GCP Starter时连接到GCP Spanner的多个数据库/实例?

huangapple go评论66阅读模式
英文:

How do I connect to multiple databases/instances in GCP Spanner using Spring Cloud GCP Starter?

问题

我目前正在构建一个连接到Spanner数据库的应用程序。该应用程序的最终目标是能够连接到多个数据库(可能还有实例),以便可以使用GraphQL实现拉取数据。我目前正在使用Spring Cloud GCP Starter和Spring Cloud GCP Starter Data Spanner Maven包来处理配置和数据映射。Spring Cloud GCP Starter要求我在application.properties文件中设置以下行:

spring.cloud.gcp.spanner.instance-id=blah
spring.cloud.gcp.spanner.database=blah
spring.cloud.gcp.project-id=blah

目前,该应用程序已经设置了每个表的模型,一个存储库(使用SpannerRepository),以及一个控制器。

问题是,当应用程序运行时,我无法找到如何更改配置以使用初始值。有没有人遇到过这个问题并找到了解决方法,还是说这是Spring Cloud GCP Starter中当前实现的限制,我应该寻找不同的解决方案?

我尝试过:

  • 尝试在网上寻找有相同问题的人,目前找不到类似的情况
  • 尝试查找如何使用/更改低级别的实现,比如自动配置创建的SpannerTemplate,但无法弄清如何更改/使用它们
  • 尝试找到在运行时更改application.properties文件并重新加载的方法,但经过一些研究,这似乎是一个糟糕的主意

非常感谢任何帮助,谢谢!

英文:

I am currently building an application that connects to a database on Spanner. The end goal of the application is to be able to connect to multiple databases (and possibly instances) so it can pull data using a GraphQL implementation. I am currently using Spring Cloud GCP Starter and Spring Cloud GCP Starter Data Spanner Maven packages to handle the configuration and data mapping. The Spring Cloud GCP Starter asks me to set up these lines in application.properties:

spring.cloud.gcp.spanner.instance-id=blah
spring.cloud.gcp.spanner.database=blah
spring.cloud.gcp.project-id=blah

Currently the application is set up to have models for each table, a repository (using SpannerRepository), and a controller.

The issue is I haven't been able to figure out how to change the configuration from the initial values when the application is run. Has anyone run into this and figured it out, or is it a limitation of my current implementation in Spring Cloud GCP Starter and I should look for different solution?

What I have tried:

  • Tried finding someone with the same issue online, nothing similar that I can find currently
  • Tried looking how to use/change the low level implementations things like SpannerTemplate that the autoconfiguration creates, but wasn't able to figure out how to change/use them
  • Tried finding a way to change application.properties and reloading during runtime, but after some research this seemed like a horrible idea

Any help would be greatly appreciated, thank you!

答案1

得分: 0

I finally found it!

Documentation

By creating a custom bean for DatabaseIdProvider, you can set the projectId, instanceId, and database to whatever you would like. Confirmed to be working the SpannerRepository implementation as well.

If anyone else finds this, here is an example of how you would create your own implementation (how you go about feeding it a new database name is up to you):

@Bean
public DatabaseIdProvider databaseIdProvider() {
    return () -> DatabaseId.of("projectId", "instanceId", "databaseName");
}

The DatabaseIdProvider interface extends Supplier<DatabaseId>. DatabaseId has a method (of) that creates the new DatabaseId that you need to connect to a different database/instance.

英文:

I finally found it!

Documentation

By creating a custom bean for DatabaseIdProvider you can set the projectId, instanceId, and database to whatever you would like. Confirmed to be working the SpannerRepository implementation as well.

If anyone else finds this, here is an example of how you would create your own implementation (how you go about feeding it a new database name is up to you):

@Bean
public DatabaseIdProvider databaseIdProvider() {
    return () -&gt; DatabaseId.of(&quot;projectId&quot;, &quot;instanceId&quot;, &quot;databaseName&quot;);
}

The DatabaseIdProvider interface extends Supplier&lt;DatabaseId&gt;. DatabaseId has a method (of) that creates the new DatabaseId that you need to connect to a different database/instance.

huangapple
  • 本文由 发表于 2023年2月16日 10:38:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467302.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定