1 前言
之前介绍了Spring Cloud Config的用法,但对于Kubernetes应用,可能会需要读取ConfigMap的配置,我们看看Springboot是如何方便地读取ConfigMap和Secret。
2 整合Spring Cloud Kubenetes
Spring Cloud Kubernetes提供了Spring Cloud应用与Kubernetes服务关联,我们也可以自己写Java程序来获取Kubernetes的特性,但Spring又为我们做了。
2.1 项目代码
引入依赖:
org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-kubernetes-config
只需要Springboot Web和Spring Cloud Kubernetes Config即可,很简单。
Springboot启动类:
@SpringBootApplicationpublic class ConfigMapMain { public static void main(String[] args) { SpringApplication.run(ConfigMapMain.class, args); }}
准备一个EndPoint来展示所读到的配置信息:
@RestControllerpublic class PkslowController { @Value("${pkslow.age:0}") private Integer age; @Value("${pkslow.email:null}") private String email; @Value("${pkslow.webSite:null}") private String webSite; @Value("${pkslow.password:null}") private String password;