1 前言
歡迎訪問南瓜慢說 www.pkslow.com獲取更多精彩文章熄浓!
Kubernetes
有專門的ConfigMap
和Secret
來管理配置,但它也有一些局限性省撑,所以還是希望通過Spring Cloud Config
來管理赌蔑。在Kubernetes
上面的微服務(wù)系統(tǒng)會有所不同,我們來探索一下如何整合Spring Cloud Kubernetes
來做配置管理竟秫。
整體方案與《使用Spring Cloud Config統(tǒng)一管理配置娃惯,別再到處放配置文件了》差不多,只是引入Spring Cloud Kubernetes
來使用Kubernetes
的服務(wù)發(fā)現(xiàn)肥败,而不使用Eureka
等趾浅。
2 服務(wù)端
引入依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes</artifactId>
</dependency>
服務(wù)端啟動類如下:
package com.pkslow.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerK8s {
public static void main(String[] args) {
SpringApplication.run(ConfigServerK8s.class, args);
}
}
服務(wù)端的application.properties
配置如下:
server.port=8888
spring.application.name=config-server-k8s
spring.cloud.config.server.git.uri=https://github.com/pkslow/pkslow-config
spring.cloud.config.server.git.username=admin@pkslow.com
spring.cloud.config.server.git.password=***
spring.cloud.config.server.git.default-label=master
spring.cloud.config.server.git.search-paths=demo
這里的應(yīng)用名字為config-server-k8s
,后續(xù)部署到k8s
也是用這個名字馒稍。
k8s
的資源定義文件如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: config-server-k8s-deployment
spec:
selector:
matchLabels:
app: config-server-k8s
replicas: 1
template:
metadata:
labels:
app: config-server-k8s
spec:
containers:
- name: config-server-k8s
image: pkslow/config-server-k8s:1.0-SNAPSHOT
ports:
- containerPort: 8888
---
apiVersion: v1
kind: Service
metadata:
labels:
app: config-server-k8s
name: config-server-k8s
spec:
ports:
- port: 8888
name: config-server-k8s
protocol: TCP
targetPort: 8888
selector:
app: config-server-k8s
type: ClusterIP
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: config-server-k8s
annotations:
kubernetes.io/ingress.class: nginx
spec:
rules:
- http:
paths:
- path: /
backend:
serviceName: config-server-k8s
servicePort: 8888
host: config-server-k8s.localhost
保持Service
名字統(tǒng)一為config-server-k8s
皿哨。
3 客戶端
引入依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-kubernetes</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
spring-cloud-starter-kubernetes
為服務(wù)發(fā)現(xiàn);
spring-cloud-starter-config
作為配置客戶端纽谒;
spring-boot-starter-actuator
提供EndPoint來刷新配置证膨。
啟動類為:
package com.pkslow.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientK8s {
public static void main(String[] args) {
SpringApplication.run(ConfigClientK8s.class, args);
}
}
配置文件bootstrap.properties
如下:
server.port=8080
# 服務(wù)名
spring.application.name=config-client-k8s
# 讀取配置時的profile
spring.profiles.active=dev
# 讀取配置時的代碼分支
spring.cloud.config.label=release
# 開放刷新接口
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
# 通過服務(wù)名找到配置服務(wù)器
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=config-server-k8s
展示配置結(jié)果的Web
服務(wù):
package com.pkslow.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RefreshScope
@RestController
public class PkslowController {
@Value("${pkslow.age:0}")
private Integer age;
@Value("${pkslow.email:null}")
private String email;
@Value("${pkslow.webSite:null}")
private String webSite;
@GetMapping("/pkslow")
public Map<String, String> getConfig() {
Map<String, String> map = new HashMap<>();
map.put("age", age.toString());
map.put("email", email);
map.put("webSite", webSite);
return map;
}
}
客戶端的k8s
文件:
apiVersion: apps/v1
kind: Deployment
metadata:
name: config-client-k8s-deployment
spec:
selector:
matchLabels:
app: config-client-k8s
replicas: 1
template:
metadata:
labels:
app: config-client-k8s
spec:
containers:
- name: config-client-k8s
image: pkslow/config-client-k8s:1.0-SNAPSHOT
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
labels:
app: config-client-k8s
name: config-client-k8s
spec:
ports:
- port: 8080
name: config-client-k8s
protocol: TCP
targetPort: 8080
selector:
app: config-client-k8s
type: ClusterIP
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: config-client-k8s
annotations:
kubernetes.io/ingress.class: nginx
spec:
rules:
- http:
paths:
- path: /
backend:
serviceName: config-client-k8s
servicePort: 8080
host: config-client-k8s.localhost
注意Service
名字為config-client-k8s
。
4 部署與測試
總結(jié)一下鼓黔,服務(wù)端主要做了兩件事:
(1)提供配置服務(wù)央勒,從Github
中讀取配置;
(2)把自己注冊到Kubernetes
中去澳化,以讓客戶端發(fā)現(xiàn)并讀取配置崔步。
客戶端主要做了件事:
(1)作為配置客戶端,從服務(wù)端讀配置缎谷;
(2)把自己注冊到Kubernetes
中去刷晋,讓服務(wù)端可以訪問;
(3)提供刷新配置的功能給外界調(diào)用慎陵。
根據(jù)客戶端的名字眼虱、配置的label
和profile
,客戶端便會讀取release
分支的配置文件config-client-k8s-dev.properties
的內(nèi)部席纽。
訪問http://config-client-k8s.localhost/pkslow
結(jié)果如下:
如果修改了配置信息捏悬,客戶端不能及時生效,需要通過發(fā)送POST
請求到http://config-client-k8s.localhost/actuator/refresh
润梯,這點(diǎn)不再贅述过牙。
5 服務(wù)端統(tǒng)一刷新
如果改了大量配置甥厦,或者基礎(chǔ)配置,想讓所有客戶端生效怎么辦寇钉?總不能一個個去刷新刀疙?而且在客戶端有多個Pod
需要LoadBalance
的情況下,無法確保每個Java
應(yīng)用都能刷新到扫倡。
所以讓服務(wù)去讀取所有相關(guān)的客戶端谦秧,并刷新。實(shí)現(xiàn)很簡單撵溃,直接新增一個RefreshController
就可以了:
package com.pkslow.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
public class RefreshController {
@Autowired
private DiscoveryClient discoveryClient;
private RestTemplate rest = new RestTemplate();
@GetMapping("/refresh")
public Map<String, String> refresh() {
Map<String, String> result = new HashMap<>();
List<String> services = discoveryClient.getServices();
result.put("Basic Info", "Total services in k8s:" + services.size());
services.stream()
.filter(s -> (!"config-server-k8s".equals(s)) && s.startsWith("config-client"))
.forEach(service -> {
List<ServiceInstance> instances = discoveryClient.getInstances(service);
instances.forEach(instance -> {
String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/actuator/refresh";
try {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(null, headers);
ResponseEntity<String> response = rest.postForEntity(url, entity, String.class);
result.put(service + " " + url, response.getStatusCode().getReasonPhrase());
} catch (Exception e) {
result.put(service + " " + url, e.getMessage());
}
});
});
return result;
}
}
注意上面的過濾邏輯疚鲤,因?yàn)椴皇撬?code>Service都可以、都需要refresh缘挑,具體邏輯看業(yè)務(wù)集歇。
請求http://config-server-k8s.localhost/refresh
結(jié)果如下(把客戶端的replicas
設(shè)置成了3
):
{
"config-client-k8s http://10.1.0.126:8080/actuator/refresh": "OK",
"config-client-k8s http://10.1.0.125:8080/actuator/refresh": "OK",
"Basic Info": "Total services in k8s:7",
"config-client-k8s http://10.1.0.122:8080/actuator/refresh": "OK"
}
注:可能會遇到權(quán)限不足的問題,創(chuàng)建一個對應(yīng)的Kubernetes ServiceAccount
即可语淘,不清楚可以參考:把Spring Cloud Data Flow部署在Kubernetes上诲宇,再跑個任務(wù)試試
客戶端其實(shí)不一定需要引入
DiscoveryService
,如果不通過Server
的ServiceId
來尋找地址惶翻,而是直接配置服務(wù)端地址spring.cloud.config.uri
焕窝。但服務(wù)端是需要的,因?yàn)橐@取客戶端的信息來實(shí)現(xiàn)統(tǒng)一reload
维贺。
6 總結(jié)
配置管理其實(shí)是一門大學(xué)問它掂,把Spring Cloud Config
放在Kubernetes
上用只是其中一種場景。
關(guān)于配置的一些文章:
Spring Cloud Config在Spring Cloud Task中的應(yīng)用溯泣,比Web應(yīng)用更簡單
Spring Cloud Config整合Spring Cloud Kubernetes虐秋,在k8s上管理配置
使用Spring Cloud Config統(tǒng)一管理配置,別再到處放配置文件了
Java怎么從這四個位置讀取配置文件Properties(普通文件系統(tǒng)-classpath-jar-URL)
注解@ConfigurationProperties讓配置整齊而簡單
Springboot整合Jasypt客给,讓配置信息安全最優(yōu)雅方便的方式
歡迎關(guān)注微信公眾號<南瓜慢說>,將持續(xù)為你更新...
多讀書肢簿,多分享靶剑;多寫作,多整理池充。