项目的src\main\resources路径下不要创建application.yml文件,只创建名为bootstrap.yml的文件,内容如下:
spring: application: name: springcloudk8sconfigdemo cloud: kubernetes: config: sources: - name: ${spring.application.name} namespace: default可见新增了配置项spring.cloud.kubernetes.config.source.name和spring.cloud.kubernetes.config.source.namespace,用于配置信息来源于kubernetes的哪个namespace下的哪个configmap;
增加一个配置类DummyConfig.java,注解ConfigurationProperties的prefix="greeting"表示该类用到的配置项都是名为"greeting"的配置项的子内容 :
package com.bolingcavalry.springcloudk8sconfigdemo; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; @Configuration @ConfigurationProperties(prefix = "greeting") public class DummyConfig { private String message = "This is a dummy message"; public String getMessage() { return this.message; } public void setMessage(String message) { this.message = message; } }