首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从spring云服务器获取数据库属性

从spring云服务器获取数据库属性
EN

Stack Overflow用户
提问于 2019-12-04 15:22:05
回答 1查看 1K关注 0票数 1

我可以使用@value从spring云服务器获取并存储mysql配置属性,但不知道如何使用这些值连接到数据库。

server.properties

代码语言:javascript
复制
   spring-application-name=spring-cloud-config-server
   server.port=8888
   spring.profiles.active=native
   spring.cloud.config.server.svn.uri=url

project-master.properties

代码语言:javascript
复制
server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/project_wdm
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=password

客户端属性

代码语言:javascript
复制
spring.application.name = project-master
spring.cloud.config.uri = http://localhost:8888

错误

代码语言:javascript
复制
***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).`
EN

回答 1

Stack Overflow用户

发布于 2020-07-28 17:44:55

正如@M.deinum所说,如果您还想从云配置服务器获取与数据库相关的属性,则配置服务器详细信息需要位于名为bootstrap.properties的文件中,而不是application.properties。这样做的原因是,这个文件是在启动阶段的早期加载的,当spring注意到spring.cloud.config.*属性时,它会实例化客户端并在尝试创建数据库连接之前从服务器获取属性。

此外,请确保在pom.xml中具有云配置客户端依赖项

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
    <version>2.2.3.RELEASE</version>
</dependency>

对于debug,如果您将org.springframework.core.env.PropertySourcesPropertyResolver的日志级别设置为debug,您将在日志中看到以下类型的启动条目:

代码语言:javascript
复制
Found key 'some.prop' in PropertySource 'bootstrapProperties-app_name-app_profile' with value of type someType
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59170827

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档