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

lyfkaqu1  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(431)

我正在尝试根据配置文件读取来自不同属性的消息。例如,我在github中放置了3个属性文件:
测试-应用-开发属性
测试应用产品属性
test-app-stage.属性
现在,我的配置服务器在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 service
当我尝试访问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,但是我看不到任何错误消息。有人能帮我吗?

kcwpcxri

kcwpcxri1#

尝试进行以下更改
在git中,每个环境(dev,prod,…)必须有不同的url。

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

通过这些配置,您的配置服务器将使用以下逻辑为客户机提供服务
如果客户端应用程序具有名称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的应用程序属性,但需要那些将服务于客户端的属性。
在此处查看更多信息
spring云配置文档

相关问题