Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring配置客户端无法从配置服务器获取值_Java_Spring_Spring Boot_Spring Cloud_Spring Cloud Config - Fatal编程技术网

Java Spring配置客户端无法从配置服务器获取值

Java Spring配置客户端无法从配置服务器获取值,java,spring,spring-boot,spring-cloud,spring-cloud-config,Java,Spring,Spring Boot,Spring Cloud,Spring Cloud Config,我正在尝试根据配置文件读取来自不同属性的消息。例如,我在github中放置了3个属性文件: test-app-dev.properties test-app-prod.properties test-app-stage.properties 现在,我的配置服务器在application.properties中有以下详细信息 spring.cloud.config.server.git.uri=https://github.com/asudheer09/local-config-server-co

我正在尝试根据配置文件读取来自不同属性的消息。例如,我在github中放置了3个属性文件:

  • test-app-dev.properties
  • test-app-prod.properties
  • test-app-stage.properties
  • 现在,我的配置服务器在application.properties中有以下详细信息

    spring.cloud.config.server.git.uri=https://github.com/asudheer09/local-config-server-configs.git server.port=8888 spring.cloud.config.server.git.default label=main spring.application.name=config服务器 server.servlet.context path=/config服务

    当我试图访问http://localhost:8888/config-service/test-app-prod.properties我可以在浏览器上看到属性文件,类似地,我也可以看到其他属性文件

    以下是我的配置客户端详细信息:

    在bootstrap.properties中:

    spring.profiles.active=dev
    spring.cloud.config.uri=http://localhost:8888/config-service
    management.security.enabled=false
    spring.application.name=test-app
    
    java文件:

    package com.example.demo;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.core.env.Environment;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @SpringBootApplication
    @RefreshScope
    public class SpringProfilesExampleApplication {
    
        @Autowired
        public void setEnv(Environment e) {
            System.out.println(e.getActiveProfiles().toString());
            System.out.println(e.getProperty("message"));
        }
    
        public static void main(String[] args) {
            SpringApplication.run(SpringProfilesExampleApplication.class, args);
        }
    
    }
    
    @RefreshScope
    @RestController
    class MessageRestController {
    
        @Value("${message:Config Server is not working. Please check...}")
        private String msg;
    
        @GetMapping("/msg")
        public String getMsg() {
            return this.msg;
        }
    }
    

    当我运行我的配置客户端时,我得到的消息值为null,但是我看不到任何错误消息。有人能帮我吗?

    尝试做以下更改

    在git中,每个环境(dev、prod等)必须有不同的URL

    通过这些配置,您的配置服务器将使用以下逻辑为客户端提供服务

    如果客户端应用程序具有名称test app和profile dev,那么它将从
    https://github.com/asudheer09/local-config-server-configs/test-app-dev.properties

    如果客户端应用程序具有名称test app和profile prod,则它将从
    https://github.com/asudheer09/local-config-server-configs/test-app-prod.properties

    在配置服务器启动时,尝试直接在配置服务器上创建System.out.println没有任何意义。您不需要config server的应用程序属性,但需要那些将在客户端上提供的属性

    在这里查看更多信息


    客户端尝试的url是
    http://localhost:8888/config-服务/测试应用程序/
    你能试试那个吗?还有
    /exactor/env
    说了什么?
    spring:
      cloud:
        config:
          server:
            git:
              repos:
                dev:
                  pattern: test-app/dev
                  uri: https://github.com/asudheer09/local-config-server-configs/test-app-dev.properties
                prod:
                  pattern: test-app/prod
                  uri: https://github.com/asudheer09/local-config-server-configs/test-app-prod.properties