Java Spring配置客户端无法从配置服务器获取值
我正在尝试根据配置文件读取来自不同属性的消息。例如,我在github中放置了3个属性文件: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
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