Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Springboot integrates Spring Cloud Kubernetes to read ConfigMap

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how Springboot integrates Spring Cloud Kubernetes to read ConfigMap. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1 preface

The usage of Spring Cloud Config was introduced earlier, but for Kubernetes applications, you may need to read the configuration of ConfigMap. Let's see how Springboot can easily read ConfigMap and Secret.

2 integrate Spring Cloud Kubenetes

Spring Cloud Kubernetes provides the association between Spring Cloud applications and Kubernetes services, and we can write our own Java programs to get the features of Kubernetes, but Spring does it for us.

2.1 Project Code

Introduce dependencies:

Org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-kubernetes-config

All you need is Springboot Web and Spring Cloud Kubernetes Config. It's simple.

Springboot startup class:

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

Prepare an EndPoint to show the configuration information you read:

@ 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; @ GetMapping ("/ pkslow") public Map getConfig () {Map map = new HashMap () Map.put ("age", age.toString ()); map.put ("email", email); map.put ("webSite", webSite); map.put ("password", password); return map;}}

The default is empty, password is read from Secret, others are read from ConfigMap.

The configuration files applied are as follows:

Server: port: 8080spring: application: name: spring-cloud-kubernetes-configmap cloud: kubernetes: config: name: spring-cloud-kubernetes-configmap

The spring.cloud.kubernetes.config.name here is the key point, and we will use it to find ConfigMap later.

Encrypted password:

$echo-n "pkslow-pass" | base64 cGtzbG93LXBhc3M=

Create a Kubernetes Secret:

Kind: SecretapiVersion: v1metadata: name: spring-cloud-kubernetes-secret namespace: defaultdata: pkslow.password: cGtzbG93LXBhc3M=type: Opaque

The ConfigMap is as follows:

Kind: ConfigMapapiVersion: v1metadata: name: spring-cloud-kubernetes-configmap namespace: default labels: app: scdf-serverdata: application.yaml: |-pkslow: age: 19 email: admin@pkslow.com webSite: www.pkslow.com

It is important to note that the name here is the same as the previous configuration, both spring-cloud-kubernetes-configmap.

Then complete the Dockerfile and K8s deployment files. Note that the value of Secret is mapped to the environment variable:

Env:-name: PKSLOW_PASSWORD valueFrom: secretKeyRef: name: spring-cloud-kubernetes-secret key: pkslow.password2.2 start and test

Upon startup, the application will go to Kubernetes to find the corresponding ConfigMap and Secret:

. _ _ / / _ _ _ (() _ _ _ |'_ _ _ | |\ / _ _ _ | | | (_ _ | |) )'| _ |. _ _ | _ | | _ | _ | | _\ _ _ | | / = | _ | = | _ _ / = / _ /:: Spring Boot:: (v2.2.5.RELEASE) 2020-08-25 00 INFO 13 INFO 17.374-[main] b.c.PropertySourceBootstrapConfiguration: Located property source: CompositePropertySource {name='composite-configmap' | PropertySources= [ConfigMapPropertySource {name='configmap.spring-cloud-kubernetes-configmap.default'}]} 2020-08-25 00 13 INFO 17.376 INFO 7-[main] b.c.PropertySourceBootstrapConfiguration: Located property source: CompositePropertySource {name='composite-secrets', propertySources= []}

Visit spring-cloud-kubernetes-configmap.localhost/pkslow to read the configuration correctly. The contents of ConfigMap and Secret are obtained:

3 automatic refresh configuration 3.1 principle introduction and code change

We need to modify the configuration and make it effective while Web is running, and there are several modes. Modify the configuration file as follows:

Server: port: 8080spring: application: name: spring-cloud-kubernetes-configmap cloud: kubernetes: config: name: spring-cloud-kubernetes-configmap namespace: default secrets: name: spring-cloud-kubernetes-secret namespace: default enabled: true reload: enabled: true monitoring-config-maps: true monitoring-secrets: true strategy: restart_context Mode: eventmanagement: endpoint: restart: enabled: true endpoints: web: exposure: include: restart

(1) refresh feature needs to be enabled for spring.cloud.kubernetes.reload.enabled=true

(2) load policy strategy:

Refresh: works only for specific configurations, with comments @ ConfigurationProperties or @ RefreshScope.

Restart_context: the entire Spring Context will be rebooted gracefully and all configurations in it will be reloaded.

You need to open actuator endpoint, so configure management.endpoint. And increase dependence:

Org.springframework.boot spring-boot-actuator org.springframework.boot spring-boot-actuator-autoconfigure

Shutdown: restart the container.

(3) Mode mode

Event Event: the changes to the ConfigMap will be monitored through the K8s API, the configuration will be read and take effect.

Polling: check periodically to see if there is any change. If there is any change, it will trigger. The default is 15 seconds.

3.2 Test

Let's modify the configuration of ConfigMap and update it to K8s.

$kubectl apply-f src/main/k8s/config.yaml configmap/spring-cloud-kubernetes-configmap configured

Check and find that both age and email have been modified:

Let's check the Pod log as follows:

Springboot first detects a change in ConfigMap and then triggers a Context restart.

This is the end of the article on "how Springboot integrates Spring Cloud Kubernetes Reading ConfigMap". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report