Vee Dee
Vee Dee

Reputation: 143

Config Client is not working in Spring boot

Config Client is not working in Spring boot

I was trying out config server client in Spring boot and came across this weird issue.

I have been able to successfully spin up the config server up and running but while trying to start the config client/consumer, it seems like the client isn't fetching any information from the config server.

Output while running client:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.1)

2020-12-24 02:01:28.717  INFO 3000 --- [           main] c.r.service.profile.ProfileApplication   : Starting ProfileApplication using Java 1.8.0_121 on DESKTOP-RID7KR with PID 3000 (E:\Spring\rent-a-car\profile\target\classes started by Dawg in E:\Spring\rent-a-car\profile)
2020-12-24 02:01:28.727  INFO 3000 --- [           main] c.r.service.profile.ProfileApplication   : No active profile set, falling back to default profiles: default
2020-12-24 02:01:30.161  INFO 3000 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-12-24 02:01:30.263  INFO 3000 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 82 ms. Found 1 JPA repository interfaces.
2020-12-24 02:01:30.642  INFO 3000 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=99ce795d-c5e8-3f6e-87d9-3efbe096b86a
2020-12-24 02:01:32.012  INFO 3000 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-12-24 02:01:32.027  INFO 3000 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-12-24 02:01:32.027  INFO 3000 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
2020-12-24 02:01:32.287  INFO 3000 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-12-24 02:01:32.287  INFO 3000 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3430 ms
2020-12-24 02:01:32.431  WARN 3000 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cloud.autoconfigure.RefreshAutoConfiguration$JpaInvokerConfiguration': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
2020-12-24 02:01:32.444  INFO 3000 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-12-24 02:01:32.478  INFO 3000 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-12-24 02:01:32.513 ERROR 3000 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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


Process finished with exit code 1

bootstrap.yml (config-client)

spring:
  application:
    name: profile
  profiles:
    active: prod
  cloud:
    config:
      name: profile
      uri: http://localhost:8181

Have not included any application.yml file for config-client.

My config server is up on port 8181 and i am able to fetch the config from github successfully,

Sample success response:

{
   "name":"profile",
   "profiles":[
      "prod"
   ],
   "label":null,
   "version":"24275f56bf516d847f171c9fc419ddef141bd39b",
   "state":null,
   "propertySources":[
      {
         "name":"https://github.com/vishu221b/rentaca-config-store.git/file:C:\\Users\\Vishal\\AppData\\Local\\Temp\\config-repo-2331899650646506\\service-config\\profile-service\\profile-prod.yml",
         "source":{
            "server.port":8081
         }
      },
      {
         "name":"https://github.com/vishu221b/rentaca-config-store.git/file:C:\\Users\\Vishal\\AppData\\Local\\Temp\\config-repo-2331899650646506\\application.yml",
         "source":{
            "spring.datasource.url":"jdbc:postgresql://127.0.0.1:5432/rent-a-car?createDatabaseIfNotExist=true",
            "spring.datasource.username":"cofix",
            "spring.datasource.password":"C0fiX",
            "spring.datasource.driver-class-name":"org.postgresql.Driver",
            "spring.jpa.hibernate.naming.physical-strategy":"org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl",
            "spring.jpa.properties.hibernate.dialect":"org.hibernate.dialect.PostgreSQLDialect"
         }
      }
   ]
}

I know there must be some key/value errors in the files above, i'll manage that but only given that my config-client starts fetching config from my config-server running successfully. There is no security in my config-server so i won't need username, password.

pom.xml (config-client)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.reantacar.service</groupId>
    <artifactId>profile</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>profile</name>
    <description>Profile service for Rent A Car application.</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2020.0.0</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

<!--        &lt;!&ndash;The jar needs to be imported to make the bootstrap.yml configuration file take effect &ndash;&gt;-->
<!--        <dependency>-->
<!--            <groupId>org.springframework.cloud</groupId>-->
<!--            <artifactId>spring-cloud-context</artifactId>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.rentacar.commons</groupId>
            <artifactId>commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>

PS: I have tried debugging this a lot over other stackoverflow questions and official docs on internet, but nothing seems to be fixing my issue. Initially i thought that my bootstrap.yml file must not be loading for some reasons, but now i am feeling that the config isn't working for my client at all because in the startup log it is mentioned that configuration from specific config-server-name/link:port was being tried to be fetched. Also, please ignore the errors in my grammar :)

Upvotes: 2

Views: 10167

Answers (3)

Fazal Haroon
Fazal Haroon

Reputation: 1163

To resolve this problem in Spring Cloud Config

If you are using a 2.4.0 or greater version of spring boot and If you are using the 2020.0.0 or greater version of spring-cloud.

you need to add this dependency to the pom.xml (spring-cloud-config-client)

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

Example of my pom.xml (spring-cloud-config-client)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.8</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>pay-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>pay-service</name>
    <description>Pay project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
        <spring-cloud.version>2021.0.5</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Try like this, I hope it will work

Sometimes, it happen because you added the 'config client of spring cloud config' dependency, it required configuration in application.properties, add the configuration of your config server so you see the example below

src/main/resources/application.properties

server.port=8100
spring.config.import=optional:configserver:http://localhost:8888

Note: SNAPSHOT, M1, M2, M3, and M4 releases typically WORK IN PROGRESS. The Spring team is still working on them, Recommend NOT using them.

Upvotes: 2

ayman.mostafa
ayman.mostafa

Reputation: 619

However, this was working before, but now and as mentioned above you have to your client project

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

spring-boot ver=2.4.3

spring-cloud ver=2020.0.1

Also, you can set the below property in your bootstrap. properties to make the client project fails to startup if fails to connect to the config server:

spring.cloud.config.fail-fast=true

Upvotes: 1

Piotr
Piotr

Reputation: 570

2020.0.0 Spring Cloud Config does not support bootstrap file.

For a workaround use bootstrap.{yml|properties} and add a dependency on spring-cloud-starter-bootstrap to restore the old behavior.

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

More information:
https://github.com/spring-cloud/spring-cloud-release/wiki/Spring-Cloud-2020.0-Release-Notes#breaking-changes

Upvotes: 12

Related Questions