我可以使用@value从spring云服务器获取并存储mysql配置属性,但不知道如何使用这些值连接到数据库。
server.properties
spring-application-name=spring-cloud-config-server
server.port=8888
spring.profiles.active=native
spring.cloud.config.server.svn.uri=urlproject-master.properties
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客户端属性
spring.application.name = project-master
spring.cloud.config.uri = http://localhost:8888错误
***************************
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).`发布于 2020-07-28 17:44:55
正如@M.deinum所说,如果您还想从云配置服务器获取与数据库相关的属性,则配置服务器详细信息需要位于名为bootstrap.properties的文件中,而不是application.properties。这样做的原因是,这个文件是在启动阶段的早期加载的,当spring注意到spring.cloud.config.*属性时,它会实例化客户端并在尝试创建数据库连接之前从服务器获取属性。
此外,请确保在pom.xml中具有云配置客户端依赖项
<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,您将在日志中看到以下类型的启动条目:
Found key 'some.prop' in PropertySource 'bootstrapProperties-app_name-app_profile' with value of type someTypehttps://stackoverflow.com/questions/59170827
复制相似问题