Spring Cloud Config整合Spring Cloud Kubernetes,在k8s上管理配置
阿新 • • 發佈:2020-08-25
# 1 前言
> 歡迎訪問[南瓜慢說 www.pkslow.com](https://www.pkslow.com/)獲取更多精彩文章!
`Kubernetes`有專門的`ConfigMap`和`Secret`來管理配置,但它也有一些侷限性,所以還是希望通過`Spring Cloud Config`來管理。在`Kubernetes`上面的微服務系統會有所不同,我們來探索一下如何整合`Spring Cloud Kubernetes`來做配置管理。
整體方案與《[使用Spring Cloud Config統一管理配置,別再到處放配置檔案了](https://www.pkslow.com/archives/spring-cloud-config)》差不多,只是引入`Spring Cloud Kubernetes`來使用`Kubernetes`的服務發現,而不使用`Eureka`等。
# 2 服務端
引入依賴:
```xml
```
服務端啟動類如下:
```java
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);
}
}
```
服務端的`application.properties`配置如下:
```properties
server.port=8888
spring.application.name=config-server-k8s
spring.cloud.config.server.git.uri=https://github.com/pkslow/pkslow-config
[email protected]
spring.cloud.config.server.git.password=***
spring.cloud.config.server.git.default-label=master
spring.cloud.config.server.git.search-paths=demo
```
這裡的應用名字為`config-server-k8s`,後續部署到`k8s`也是用這個名字。
`k8s`的資源定義檔案如下:
```yaml
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`名字統一為`config-server-k8s`。
# 3 客戶端
引入依賴:
```xml
```
`spring-cloud-starter-kubernetes`為服務發現;
`spring-cloud-starter-config`作為配置客戶端;
`spring-boot-starter-actuator`提供EndPoint來重新整理配置。
啟動類為:
```java
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`如下:
```properties
server.port=8080
# 服務名
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
# 通過服務名找到配置伺服器
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=config-server-k8s
```
展示配置結果的`Web`服務:
```java
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")
pu