相关文章推荐
豪爽的毛豆  ·  springboot集成minio ...·  9 月前    · 
茫然的煎饼  ·  ios html5 video m3u8 ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am following this link: http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_client_side_usage

I tested this again and again and not seeing Spring cloud client is loading configuration from cloud server, please help to see where is the error:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

Application:

@Configuration
@EnableAutoConfiguration
@RestController
public class ConfigclientApplication {
    @Value("${spring.cloud.config.uri}")
    String url;
    @Value("${production.host}")
    String host;
    @RequestMapping("/")
    public String home() {
        return "Host is  => " + this.host ;
    public static void main(String[] args) {
        SpringApplication.run(ConfigclientApplication.class, args);

bootstrap.properties: spring.cloud.config.uri=http://localhost:8888

  • The config server is good: http://localhost:8888/spirent/default
  • "name": "spirent", "profiles": [ "default" "label": "master", "propertySources": [ "name": "classpath:/spirent.yml", "source": { "production.host": "server1", "production.port": 9999, "production.value1": 12345, "test.host": "server2.com", "test.port": 4444, "test.value": "hello123"
  • now http://localhost:8080/ can not be started at all.

    Error creating bean with name 'configclientApplication' It seemed the auto inject of @Value can not find the production.host environment value.

    How can I read the configuration in client once loaded from config server?

    Thanks for your help.

    If you are following a tutorial then follow a tutorial. The tutorial mentions that you should use the spring-cloud-starter dependency not the spring-cloud-config-client. For the application it doesn't matter where the configuration comes from, they are just properties which you can access like any other property through the Environment or with @Value. – M. Deinum May 3, 2015 at 18:42 I changed to both client or starter, but can not find out configuration value in the config server, any idea? – user3006967 May 3, 2015 at 20:16 were you able to resolve the issue, the problem I am facing is the properties from config server are begin read from the method if it annotated with @RequestMapping but not otherwise. not sure what can be done in that case. – viveksinghggits Sep 27, 2018 at 7:42

    If you are using the 2020.0.0 version of Spring Cloud then you need to add this dependency to your maven dependencies to enable config bootstrap, which is disabled by default in 2020.0.0.

    Breaking changes in 2020.0.0

    This worked for me.

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

    For those getting here after using the default Maven Spring Cloud dependency version related to Spring Boot to 2.4.0 or 2.4.1 (e.g., 2020.0.0-M5 for Spring Boot 2.4.0), besides following RubesMN's good advice, be aware that bootstrapping is not enabled by default in such Spring Cloud dependency. According to the Spring Cloud 2020.0 Release Notes:

    Bootstrap, provided by spring-cloud-commons, is no longer enabled by default. If your project requires it, it can be re-enabled by properties or by a new starter.

  • To re-enable by properties set spring.cloud.bootstrap.enabled=true or spring.config.use-legacy-processing=true. These need to be set as an environment variable, java system property or a command line argument.
  • The other option is to include the new spring-cloud-starter-bootstrap (in your POM file).
  • I used the second option and worked just fine.

    If you are using 2020.0.0 version of spring cloud then you need to add this dependency in your maven dependencies to enable bootstrap, which is disabled by default:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
                    Machan, Thank you ban man dawas ganak kattak kawa, Me dependancy eka nattam config server ekata connect wenne  naha. Thanks ban.
    – dasun_001
                    Feb 3, 2021 at 8:48
    

    As Deinum implies, I'd ensure you have the client configured with the parent as spring-cloud-starter-parent and give it a version. Maven plugins provided by spring cloud wont work when you include in your dependencies and remember cloud is a different project than boot. Change it to:

    <parent>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-parent</artifactId>
        <version>1.0.0.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    

    Second, as a new discipline (and likely not your problem here), I'd use the new annotation on your Application instead of @Configuration and @EnableAutoConfiguration

    @SpringBootApplication
    

    Third, double check you have @EnableConfigServer on your config server

    Fourth, be sure your bootstrap.properties on your client has your spring application name specified:

    spring.application.name=spirent
    

    Finally, if you used the spring-cloud-config example project, there is a default user and security password that you have to set in your URI:

    http://user:ddf4757e-0077-42e4-b2ad-2ae04340b08c@localhost:8888
    

    Otherwise, try starting from the spring-cloud-config project located here to ensure your config server is setup correctly:

    https://github.com/spring-cloud/spring-cloud-config
                    Thank you @RubesMN, the crucial fix is adding: spring.application.name=myapp to bootstrap.properties, and then I can see - {"profiles":[],"configService:classpath:/myapp.yml":{"my.otherprop":"myotherval"}," from /env. Now, can you answer another question, how can I access the config value in config client? I tried @Value(${profiles}) and the code can not start, complaining the injection is wrong. Thanks
    – user3006967
                    May 5, 2015 at 5:38
                    You would access using a variable declaration annotated with @Value("${my.otherprop}") or via a class annotated with @Component @ConfigurationProperties(prefix="my") containing a variable called 'otherprop'.  Be sure to mark the answer as correct for others to view.
    – RubesMN
                    May 5, 2015 at 15:40
                    I also notice that @Value annotated variables do not get refreshed unless the spring component they are contained within has @RefreshScope and a call to the client's /refresh endpoint is called.  Neither @Value nor @ConfigurationProperties config values get refreshed from the config server when /env is called as is written in the documentation.
    – RubesMN
                    May 5, 2015 at 18:06
                    I think I am missing something here. I am following this code here (github.com/spring-cloud-samples/svn-config-server) and the config server is running in another host, so I changed the bootstrap.yml and pointed the URI to the new host. My @Value doesnt work and the client application does not boot, "Could not resolve placeholder".
    – Thiago
                    Jan 6, 2016 at 15:55
                    Seems within the client you do have spring.cloud.config.uri configured so you are good there.  Ensure you also have spring.cloud.config.enabled: true as well.  Although this would not stop it from starting, check your actual properties are set properly for SVN.   Finally, on client, make sure your top end class (typically called Application) has the correct annotations.  @SpringBootApplication @RefreshScope.  Hope this helps
    – RubesMN
                    Jan 6, 2016 at 20:35
    

    I am adding my findings. For me, after spending healthy 2 hours I found that,

  • Config Server should have: [spring-cloud-config-server]

    <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>2.5.3</version>
         <relativePath/>
    </parent>
    <properties>
         <java.version>1.8</java.version>
         <spring-cloud.version>2020.0.3</spring-cloud.version>
    </properties>
    <dependencies>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-config-server</artifactId>
         </dependency>
    </dependencies>
    
  • Client Services should have: [spring-cloud-starter-config]

    <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>2.5.3</version>
         <relativePath/>
    </parent>
    <properties>
         <java.version>1.8</java.version>
         <spring-cloud.version>2020.0.3</spring-cloud.version>
    </properties>
    <dependencies>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-config</artifactId>
         </dependency>
    </dependencies>
    
  • Client Services should declare: in application.properties or application.yml

    #This does not work
    #spring.cloud.config.uri=http://localhost:8880/config-server
    spring.application.name=name-by-which-properties/yml-file-is-created
    spring.config.import=optional:configserver:http://host:port/application-context
    management.endpoints.web.exposure.include=*
    

    What did not work for me:

  • Adding spring-cloud-starter-bootstrap
  • Adding spring-cloud-starter-parent
  • Hope this will help others like me with Spring Boot (2.5.3) and Spring Cloud 2020.0.3

    In case, anyone is facing this issue on PCF (with marketplace config-server) even after adding spring-cloud-starter-bootstrap for new spring boot versions, make sure following dependency is added as well -

    <dependency>
      <groupId>io.pivotal.spring.cloud</groupId>
      <artifactId>spring-cloud-services-starter-config-client</artifactId>
    </dependency>
    

    It is a problem with spring-boot versions. Spring-cloud-starter-config dependency doesn't work well with the older spring-boot version. Try changing your spring-boot version to some latest version probably above 2.4.0.

    The following gradle implementation worked for me:

    id 'org.springframework.boot' version '2.4.5'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •