spring cloud config动态刷新_Springboot整合K8s读取ConfigMap刷新配置

本文详细介绍了如何将Spring Boot应用与Spring Cloud Kubernetes结合,实现动态读取和刷新Kubernetes的ConfigMap和Secret配置。通过配置Spring Cloud Kubernetes的相关属性,实现了配置的自动刷新功能,当ConfigMap发生变更时,应用能够感知并优雅重启,确保配置的实时更新。
摘要由CSDN通过智能技术生成

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;   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值