deep Kumar
deep Kumar

Reputation: 45

Fetch database properties from spring cloud server

I am able to fetch and store the mysql configuration properties from spring cloud server using @value but don't know how to use those values to connect to database.

server.properties

   spring-application-name=spring-cloud-config-server
   server.port=8888
   spring.profiles.active=native
   spring.cloud.config.server.svn.uri=url

project-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

Client-properties

spring.application.name = project-master
spring.cloud.config.uri = http://localhost:8888

Error

***************************
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).`

Upvotes: 1

Views: 1815

Answers (1)

Adrian B.
Adrian B.

Reputation: 1570

Like @M.deinum said, if you also want to get the database related properties from the cloud config server, the config server details need to be in a file called bootstrap.properties and not application.properties. The reason for this is that this file is loaded earlier in the startup phase and when spring notices the spring.cloud.config.* props, it instantiates the client and gets the properties from the server before attempting to create the database connection.

Also, make sure to have the cloud config client dependency in your pom.xml:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
    <version>2.2.3.RELEASE</version>
</dependency>

for debug, if you set the log level for org.springframework.core.env.PropertySourcesPropertyResolver to debug, you will see in the log at startup entries of the type:

Found key 'some.prop' in PropertySource 'bootstrapProperties-app_name-app_profile' with value of type someType

Upvotes: 1

Related Questions