Home > Software engineering >  Spring cloud-config client giving error Failed to configure a DataSource: 'url' attribute
Spring cloud-config client giving error Failed to configure a DataSource: 'url' attribute

Time:11-30

  1. Spring cloud-config server started with application.properties :
server.port:8888
spring.application.name=test-config-server
spring.cloud.config.server.git.uri=https://gitlab.com/pearsontechnology/gpt/sms/sms-micro-services/config-server.git
spring.cloud.config.server.git.default-label=develop

#Private repo. access credentials
spring.cloud.config.server.git.username=xxx
spring.cloud.config.server.git.password=xxxx

spring.cloud.config.server.git.clone-on-start=true
spring.cloud.config.profile=dev

On starting the config-client, Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

My version of spring boot, spring-cloud and dependencies are as follows from pom.xml :

<artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.2</version>
<java.version>17</java.version>
<spring-cloud.version>2021.0.3</spring-cloud.version>

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <scope>runtime</scope>
        </dependency>

application.prop of config-client :

spring.application.name=systems-lookup-service
spring.cloud.config.profile=dev
spring.config.import=optional:configserver:
server.port=8081

Properties related to Datasource like url etc. need to be taken from systems-lookup-service-dev.properties hosted on Git.

custom.url=jdbc:oracle:thin:@localhost:1998/smscert
custom.username=smscert
custom.password=go#salt
custom.driverClassName=

And the DAO class in config-client accessing the db :

public class XXDaoImpl implements XXDao {

    private JdbcTemplate jdbcTemplate;

    @Autowired(required=false)
    private DataSourceConfig config;

    @Autowired
    public SystemDaoImpl(JdbcTemplate jdbcTemplateIn){
        final DataSource dataSource = DataSourceBuilder.create()
                                                        .driverClassName(config.getDriverClassName())
                                                        .url(config.getUrl())
                                                        .username(config.getUsername())
                                                        .password(config.getPassword())
                                                        .build();
        this.jdbcTemplate = new JdbcTemplate(dataSource);

    }
    
    ...............
}


@Component
@ConfigurationProperties("custom")
public class DataSourceConfig {

    private String url;

    private String username;

    private String password;

    //@Value("${greeting.message}")
    private String driverClassName;
    ....
}

CodePudding user response:

I believe you follow the first boot-strapping for your central cloud registration to do so you need following artifact with in your client service pom file.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

Add the following properties to you client service property file application.prop

spring.application.name=systems-lookup-service
spring.cloud.config.uri=http://localhost:"cloud-config-port"
spring.profiles.active=dev
spring.config.import=optional:configserver:

With in main class on central cloud config add the annotation @EnableConfigServer and within it pom file add following artifact

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

append following properties to your central cloud config property file

spring.application.name=configuration-server
server.port=8780
management.endpoints.web.exposure.include=*
spring.cloud.config.server.git.uri=file:absoluthe-path
spring.cloud.config.server.git.clone-on-start=true
spring.cloud.config.allowOverride=true

Finally add your client service properties in your gitrepo with naming servicename-profile convention.

Extra point

You may consider to use spring cloud boss, for hot reloading the configs and not restarting services to handshake again, find out more in here.

CodePudding user response:

Disabled auto-configuration of the data source by annotating client main with @SpringBootApplication(exclude={DataSourceAutoConfiguration.class})

  • Related