Spring Cloud Kubernetes-Spring引导未读取配置映射

Spring Cloud Kubernetes-Spring引导未读取配置映射,spring,spring-boot,kubernetes,configmap,spring-cloud-kubernetes,Spring,Spring Boot,Kubernetes,Configmap,Spring Cloud Kubernetes,我有一个SpringBootWeb应用程序,它只打印在Kubernetes的ConfigMap中传递的属性 这是我的主要课程: @SpringBootApplication @EnableDiscoveryClient @RestController public class DemoApplication { private MyConfig config; private DiscoveryClient discoveryClient; @Autowired

我有一个SpringBootWeb应用程序,它只打印在Kubernetes的ConfigMap中传递的属性

这是我的主要课程:

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class DemoApplication {

    private MyConfig config;
    private DiscoveryClient discoveryClient;

    @Autowired
    public DemoApplication(MyConfig config, DiscoveryClient discoveryClient) {
        this.config = config;
        this.discoveryClient = discoveryClient;
    }

    @RequestMapping("/")
    public String info() {
        return config.getMessage();
    }

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

    @RequestMapping("/services")
    public String services() {
        StringBuilder b = new StringBuilder();
        discoveryClient.getServices().forEach((s) -> b.append(s).append(" , "));
        return b.toString();
    }

}
MyConfig
类是:

@Configuration
@ConfigurationProperties(prefix = "bean")
public class MyConfig {

    private String message = "a message that can be changed live";

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
基本上,通过调用根资源,我总能得到:

可以实时更改的消息

调用/services,我实际上得到了Kubernetes服务的列表

我正在创建ConfigMap,内容是
kubectl create-f ConfigMap demo.yml

apiVersion: v1
kind: ConfigMap
metadata:
  name: demo
data:
    bean.message: This is an info from k8
使用
kubecetl create-f deploy demo.yml进行部署,内容如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo
  labels:
    app: demo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: demo
  template:
    metadata:
      labels:
        app: demo
    spec:
      # this service account was created according to
      # https://kubernetes.io/docs/reference/access-authn-authz/rbac/#service-account-permissions
      # point 5 - Grant super-user access to all service accounts cluster-wide (strongly discouraged)
      serviceAccountName: i18n-spring-k8
      containers:
      - name: demo
        image: aribeiro/sck-demo
        imagePullPolicy: Never
        env:
        - name: JAVA_OPTS
          value:
        ports:
        - containerPort: 8080
      volumes:
      - name: demo
        configMap:
          name: demo
问题是,当访问根资源时,我总是得到默认的硬编码值,而从来没有得到Kubernetes的ConfigMap中定义的值

示例项目还包含yaml文件和Docker文件,可在上获得

还检查了启动调试日志,我没有看到任何错误或不应该工作的线索。

不完整。它缺少包含此依赖项的说明,无法从ConfigMap加载应用程序属性:

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

org.springframework.cloud
spring云启动器kubernetes配置
您很接近:

1) 以稍微不同的方式定义ConfigMap,使其包含属性文件。例如:

apiVersion: v1
kind: ConfigMap
metadata:
  name: demo
data:
  demo.properties: |
    bean.message: This is an info from k8
2) 将ConfigMap作为卷装载:

...
spec:
  containers:
  - name: demo
    ...
    volumeMounts:
    - name: config
      mountPath: /demo/config
  volumes:
  - name: config
    configMap:
      name: demo
因此,ConfigMap中定义的
demo.properties
文件将“出现”在运行容器内的
/demo/config
目录中

3) 将
@PropertySource
注释添加到
MyConfig
类:

@Configuration
@PropertySource("file:/demo/config/demo.properties")
@ConfigurationProperties(prefix = "bean")
public class MyConfig {
  ...
}

谢谢你,但最后我不会使用SpringCloudKubernetes了——没有它我也能用。另外,如果ConfigMap被更改,我想利用config重载。我现在知道你想使用Spring Cloud Kubernetes来读取ConfigMap。我更新了我的答案,这就是问题所在。文件中确实没有提到这一点。现在我在重新加载配置时遇到了问题,但我将为此提出一个不同的问题。我认为这是一个不同的主题。部署中是否需要卷?谢谢@apisim!但对于这种方法,我不需要Spring Cloud Kubernetes。另外,恕我直言,一旦配置发生更改,传递配置文件路径不足以重新加载配置。