简体   繁体   中英

If you have set spring.cloud.config.server.bootstrap=true, you need to use a composite configuration

Problem : how to get the data in the location https://github.com/TEK-Leads/Config-Properties.git

When I try to run this application, it does not showing any result, It only shows the default pass result ( see. MessageController). or ConfigClient side logs showing this type of massage.

Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/application/default": Connection refused; nested exception is java.net.ConnectException: Connection refused

When changing the config-Server side application.yml file to bootstrap.yml , when throwing the exception in below.

If you are using the git profile, you need to set a Git URI in your configuration.  If you have set spring.cloud.config.server.bootstrap=true, you need to use a composite configuration.

Config Server Part


Pom.xml

    <properties>
    <java.version>11</java.version>
    <spring-cloud.version>2020.0.3</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-config-server</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <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>

application.yml

server:
  port: 9090

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/TEK-Leads/Config-Properties.git
          clone-on-start: true
        bootstrap: true
      enabled: true
   
  application:
    name: Config-Server

management:
  security:
    enabled: false

ConfigurationServerApp.class

package com.ramgovindhare.configserverapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class ConfigServerAppApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerAppApplication.class, args);
    }
}

Config Client Part


Pom.xml

<properties>
    <java.version>11</java.version>
    <spring-cloud.version>2020.0.3</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-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </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>

application.yml

spring:
  application:
    name: client-config
    
  cloud:
    config:
      uri: http://localhost:9090
  config:
    activate:
      on-profile:
        active:prod
            
management:
  security:
    enabled:false
  

ConfigClient.class

    package com.ramgovindhare.configserverclientapp.contorller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RefreshScope
@RestController
public class MessageController {
    
    @Value("${msg:Config Server is not working. Please check..}")
    private String msg;
    
    @GetMapping("/msg")
    public String getMsg() {
        return this.msg;
    }
}

As documented here ,

If you use the bootstrap flag, the config server needs to have its name and repository URI configured in bootstrap.yml.

You did not post your bootstrap files so it is unclear if you have defined them. If you have not, that could be the source of your problem since you have set spring.cloud.config.server.bootstrap to true. In your bootstrap.properties file within the resources folder for your config server, you will need to have the following properties defined:

config server bootstrap.properties

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/TEK-Leads/Config-Properties.git
          clone-on-start: true
        bootstrap: true
      enabled: true
   
  application:
    name: Config-Server

config client bootstrap.properties

spring:
  application:
    name: client-config
    
  cloud:
    config:
      uri: http://localhost:9090
  config:
    activate:
      on-profile:
        active:prod

It also might be worth noting that bootstrap context initialization is deprecated since Spring Boot 2.4 so using bootstrapping is no longer required for this setup. More information on that can be found at https://docs.spring.io/spring-cloud-config/docs/current/reference/html/ .

Try to add the dependency in the pom.xml file:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM