Skip to content

Commit 00c48b9

Browse files
committed
Cloudify with Spring Cloud Kubernetes
1 parent 29a999d commit 00c48b9

File tree

37 files changed

+3143
-694
lines changed

37 files changed

+3143
-694
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
TAG=1.0
1+
TAG=1.0_k8s

README.md

Lines changed: 79 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,92 @@
11
[![](https://img.shields.io/badge/release-v1.0-lightgray.svg)](https://github.com/ReionChan/programmatic-microservice-arch/) [![](https://img.shields.io/badge/license-Apache--2.0-orange.svg)](https://github.com/ReionChan/programmatic-microservice-arch/blob/main/LICENSE) [![](https://img.shields.io/badge/Author-ReionChan-cyan.svg)](https://reionchan.github.io/)
22

3-
# 基于『Spring Cloud』编程式、非云原生的骨架
3+
# 基于『Spring Cloud Kubernetes』云原生的骨架
44

5-
> **子曰:欣聞關注,樂見散佈!**&emsp;&emsp; <a href="https://github.com/ReionChan/programmatic-microservice-arch/stargazers"><img src="https://img.shields.io/github/stars/ReionChan/programmatic-microservice-arch?style=social&label=Star" title="关注" alt="关注" height="18" /></a>&emsp;<a href="https://github.com/ReionChan/programmatic-microservice-arch/network/members"><img src="https://img.shields.io/github/stars/ReionChan/programmatic-microservice-arch?style=social&label=Fork" title="关注" alt="关注" height="18" /></a>
6-
7-
## 为什么强调非原生?
8-
9-
&emsp;&emsp;这是个容器化、云端化的后微服务时代。
10-
11-
&emsp;&emsp;若从分布式微服务技术与解决方案演进角度来看,**后微服务时代**主张 “***向应用代码隐藏分布式架构复杂度、让分布式架构得以成为一种可普遍推广的普适架构风格***[^1] 的技术流派,可称为 “仙术” 派。
12-
13-
&emsp;&emsp;而采用**足够灵活的编程式**微服务解决方案,是软硬件能够提供**透明化的不可变基础设施**出现之前的过渡方案,这个时期也被称作**微服务时代**,而采用编程式提供微服务的技术流派,不妨称作 “体术” 派。之所以在 “体术” 派的项目中着重强调其非 “仙术” 派,是为了增强对比性来观察体会架构迭代中的原由。
5+
> **子曰:欣聞關注,樂見散佈!**&emsp;&emsp; <a href="https://github.com/ReionChan/programmatic-microservice-arch/stargazers"><img src="https://img.shields.io/github/stars/ReionChan/kubernetes-microservice-arch?style=social&label=Star" title="关注" alt="关注" height="18" /></a>&emsp;<a href="https://github.com/ReionChan/programmatic-microservice-arch/network/members"><img src="https://img.shields.io/github/stars/ReionChan/kubernetes-microservice-arch?style=social&label=Fork" title="关注" alt="关注" height="18" /></a>
146
157
## 项目架构
168

17-
![](https://raw.githubusercontent.com/ReionChan/PhotoRepo/master/arch/programmatic-microservice-arch%20Private.png)
9+
![](https://raw.githubusercontent.com/ReionChan/PhotoRepo/master/arch/kubernetes-microservice-arch.png)
1810

1911
## 项目运行
2012

21-
### dev 模式
13+
### 直接 Kubernetes 部署
2214

15+
> 🔔 容器内 minikube kubernetes 环境,执行以下命令获得转发端口:
16+
> `minikube service -n arch-namespace arch-gateway --url`
2317
```sh
24-
# 容器启动必要依赖中间件: Nacos、Jaeger、OpenTelemetry Collector
25-
docker compose up -d --build
26-
27-
# 然后在 IDE 运行模块,推荐顺序:
28-
# 认证授权中心
29-
arch-iam
30-
# 用户模块
31-
arch-users
32-
# 测试应用
33-
arch-app
34-
# API 网关
35-
arch-gateway
18+
# 应用 all in one 部署资源描述
19+
kubectl apply -f https://raw.githubusercontent.com/ReionChan/kubernetes-microservice-arch/main/arch-k8s-all-in-one.yaml
3620
```
3721

38-
### test 模式
22+
23+
### dev 模式
3924

4025
```sh
41-
# 采用 test 配置 Docker Compose 方式启动所有模块
42-
docker compose --profile test up -d --build
26+
# 方式一:采用 skaffold 部署到本地 Docker 容器内的 minikube 环境
27+
# 根据最后输出的本地转发端口进行接口访问
28+
skaffold dev -t 1.0_k8s --port-forward
4329
```
4430

31+
32+
## Web API 端点
33+
34+
* 应用内部零信任网络端点认证端点 *OAuth2 Client - credentials 模式*(包含:后台服务、前台 Web 端服务、前端 App 端)
35+
36+
```sh
37+
# 示例演示 WEB 前端认证获得访问令牌
38+
POST http://localhost:9000/arch-iam/oauth2/token
39+
Content-Type: application/x-www-form-urlencoded
40+
Authorization: Basic YXJjaC13ZWI6c2VjcmV0d2Vi
41+
42+
grant_type=client_credentials&scope=WEB
43+
```
44+
45+
* 应用自身用户登录端点 *OAuth2 Client - password 模式* (即:己方或一方用户登录)
46+
47+
> 🔔 系统初始化的用户账号及密码参考 `arch-user` 模块资源文件夹下面的 `data.sql`
48+
49+
```sh
50+
# 示例演示用户 wukong 使用 WEB 端登录获取访问、刷新令牌
51+
POST http://localhost:9000/arch-iam/oauth2/token
52+
Content-Type: application/x-www-form-urlencoded
53+
Authorization: Basic YXJjaC13ZWI6c2VjcmV0d2Vi
54+
55+
grant_type=password&scope=WEB&username=wukong&password=wukong
56+
```
57+
58+
* 应用自身用户访问令牌刷新端点
59+
60+
```sh
61+
# 示例演示用户 wukong 使用 WEB 端刷新令牌
62+
POST http://localhost:9000/arch-iam/oauth2/token
63+
Content-Type: application/x-www-form-urlencoded
64+
Authorization: Basic YXJjaC13ZWI6c2VjcmV0d2Vi
65+
66+
grant_type=refresh_token&scope=WEB&refresh_token=kGrXegF9RW2zqwvMl_NvAc47YtIsVMy_eSV-P7MgmKPwPmS8Ov1mF0qLe7Z2L-FBmfMmGooQlkLHqdl0vn7QM_BRT88D5mL73W-7bEn6bByprP1uIyxS3gmo7sC2OJWk
67+
```
68+
69+
* 登录用户访问受限资源测试端点
70+
71+
```sh
72+
# 示例演示用户 wukong 使用登录令牌认证方式访问 arch-app 下的受限资源 /ping
73+
GET http://localhost:9000/arch-app/ping
74+
Authorization: Bearer eyJraWQiOiI2ZTQxNTE4NS05YWU3LTRkZjgtYjU5MS0zZTU5NWZhYzgwNTIiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJ3dWtvbmciLCJhdWQiOiJhcmNoLXdlYiIsIm5iZiI6MTcxODA5OTkzOCwic2NvcGUiOlsiV0VCIl0sInJvbGVzIjpbIlVTRVIiXSwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo5MDkwIiwiZXhwIjoxNzE4MTAwMjM4LCJpYXQiOjE3MTgwOTk5MzgsImp0aSI6ImQ5NGVkNzMwLTA2MjItNGM1OS05YzYyLTljMmJjMzlhNmNjZSJ9.SUrLC7Jy3azs6apyaZ3s6rZdQCX2WvZPtgPcEPTXpq2gBQYgXaj-fhn_iU59fvAuHWitfwTOl7dnlnTArSubAsXtDQjYrCLMViItXYbJFan683sZPkaxnUYVZlMNjQTcsvkH9YR13p2ZHf_YNN4dgnvS2Meup41L9uJLvfcfMAuRanZFzsoCUlGSkeGJyaHME5VeaVt-U8fDLsv9xAnWwDoXN4wCYf5CEBPm8zw5QPcc0Wg4CM7o8RaxdFFXuXjC7O8XgXMm48zj3j2GzVnrf6rZrl_zXri7aFm99RS_-FZcoIrS2NbCH27QUKtgwANV-mmeTwG04eDhcOS1mhHGew
75+
76+
```
77+
4578
## 使用技术栈
4679

4780
* 服务注册与发现
48-
* Alibaba Nacos
81+
* Spring Cloud Kubernetes fabric8
4982
* 负载均衡
50-
* Spring Cloud Loadbalancer
83+
* Spring Cloud Kubernetes fabric8 loadbalancer
5184
* 服务容错
52-
* Spring Cloud Circuitbreak
53-
* Resilience4J
85+
* Spring Cloud Circuitbreak ***[编程式]***
86+
* Resilience4J ***[编程式]***
87+
* 服务网关
88+
* Spring Cloud Gateway ***[编程式]***
89+
5490
* RPC
5591
* Spring Cloud OpenFeign
5692
* 认证授权
@@ -65,17 +101,17 @@ docker compose --profile test up -d --build
65101
* 追踪数据观测,包括不限于:Jaeger、Zipkin、Tempo
66102
* 日志数据观测,包括不限于:ELK、Loki
67103

68-
## 云原生基础设施可替代
69-
70-
| | 基于 Spring Cloud 编程式 | 基于 K8S 云原生基础设施 |
71-
| -------- | ------------------------------------------------------------ | ----------------------- |
72-
| 弹性伸缩 | —— | Autoscaling |
73-
| 服务发现 | Spring Cloud Alibaba Nacos / Netflix Eureka | KubeDNS / CoreDNS |
74-
| 配置中心 | Spring Cloud Config Alibaba Nacos / Azure App Configuratioin | ConfigMap / Secret |
75-
| 服务网关 | Spring Cloud Gateway | Ingress Controller |
76-
| 负载均衡 | Spring Cloud Loadbalancer | Load Balancer |
77-
| 服务安全 | Spring Security OAuth2 | RBAC API |
78-
| 监控追踪 | Micrometer Tracing | Metrics API / Dashboard |
79-
| 熔断降级 | Spring Cloud Circuit Breaker with Resilience4J / Spring Retry | Istio Envoy |
104+
## 编程式 → 云原生 进程
105+
106+
| | 基于 Spring Cloud 编程式 | 基于 Spring Cloud Kubernetes 基础设施 | 进展 |
107+
| -------- | ------------------------------------------------------------ | ------------------------------------- | ---- |
108+
| 弹性伸缩 | —— | Autoscaling ||
109+
| 服务发现 | Spring Cloud Alibaba Nacos / Netflix Eureka | KubeDNS / CoreDNS ||
110+
| 配置中心 | Spring Cloud Config Alibaba Nacos / Azure App Configuratioin | ConfigMap / Secret ||
111+
| 服务网关 | Spring Cloud Gateway | Ingress Controller | 🔜 |
112+
| 负载均衡 | Spring Cloud Loadbalancer | Load Balancer ||
113+
| 服务安全 | Spring Security OAuth2 | RBAC API | 🔜 |
114+
| 监控追踪 | Micrometer Tracing | Metrics API / Dashboard | 🔜 |
115+
| 熔断降级 | Spring Cloud Circuit Breaker with Resilience4J / Spring Retry | Istio Envoy | 🔜 |
80116

81117
[^1]: [凤凰架构 - 从微服务到云原生](https://icyfenix.cn/immutable-infrastructure/msa-to-cn.html)

arch-business/arch-app/src/main/java/io/github/reionchan/AppBootstrap.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
6+
import org.springframework.scheduling.annotation.EnableScheduling;
57
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
68

79
/**
@@ -10,6 +12,8 @@
1012
**/
1113
@SpringBootApplication
1214
@EnableMethodSecurity
15+
@EnableScheduling
16+
@EnableDiscoveryClient
1317
public class AppBootstrap {
1418
public static void main(String[] args) {
1519
SpringApplication.run(AppBootstrap.class, args);
Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,20 @@
1-
server:
2-
port: 8080
3-
41
spring:
52
application:
63
name: arch-app
74

85
profiles:
96
default: dev
107

11-
---
12-
# 开发环境配置
13-
spring:
148
config:
15-
activate:
16-
# 仅在 dev 环境激活本配置
17-
on-profile: dev
18-
# 仅启用本地配置
19-
import:
20-
- classpath:application_common_dev.yml
21-
- classpath:application_dev.yml
9+
import: [ "kubernetes:" ]
2210

23-
---
24-
# 测试环境
25-
spring:
26-
config:
27-
activate:
28-
on-profile: test
29-
import:
30-
- nacos:${spring.application.name:arch-app}?refresh=true&group=DEFAULT_GROUP
31-
32-
---
33-
# 生产环境
34-
spring:
35-
config:
36-
activate:
37-
on-profile: prod
38-
import:
39-
- nacos:${spring.application.name:arch-app}?refresh=true&group=DEFAULT_GROUP
11+
cloud:
12+
kubernetes:
13+
reload:
14+
enabled: true
15+
monitoring-secrets: true
16+
strategy: shutdown
17+
config:
18+
namespace: arch-namespace
19+
name: ${spring.application.name}
20+
fail-fast: true

arch-business/arch-app/src/main/resources/application_dev.yml

Lines changed: 0 additions & 13 deletions
This file was deleted.

arch-business/arch-users/src/main/java/io/github/reionchan/UserBootstrap.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
56
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
7+
import org.springframework.scheduling.annotation.EnableScheduling;
68
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
79

810
/**
@@ -12,6 +14,8 @@
1214
@SpringBootApplication
1315
@EnableJpaRepositories
1416
@EnableMethodSecurity
17+
@EnableScheduling
18+
@EnableDiscoveryClient
1519
public class UserBootstrap {
1620
public static void main(String[] args) {
1721
SpringApplication.run(UserBootstrap.class, args);

arch-business/arch-users/src/main/resources/application.yml

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,16 @@ spring:
88
profiles:
99
default: dev
1010

11-
---
12-
# 开发环境配置
13-
spring:
14-
config:
15-
activate:
16-
# 仅在 dev 环境激活本配置
17-
on-profile: dev
18-
# 仅启用本地配置
19-
import:
20-
- classpath:application_common_dev.yml
21-
- classpath:application_dev.yml
22-
23-
---
24-
# 测试环境
25-
spring:
2611
config:
27-
activate:
28-
on-profile: test
29-
import:
30-
- nacos:${spring.application.name:arch-users}?refresh=true&group=DEFAULT_GROUP
12+
import: [ "kubernetes:" ]
3113

32-
---
33-
# 生产环境
34-
spring:
35-
config:
36-
activate:
37-
on-profile: prod
38-
import:
39-
- nacos:${spring.application.name:arch-users}?refresh=true&group=DEFAULT_GROUP
14+
cloud:
15+
kubernetes:
16+
reload:
17+
enabled: true
18+
monitoring-secrets: true
19+
strategy: shutdown
20+
config:
21+
namespace: arch-namespace
22+
name: ${spring.application.name}
23+
fail-fast: true

arch-business/arch-users/src/main/resources/application_dev.yml

Lines changed: 0 additions & 51 deletions
This file was deleted.

arch-commons/arch-core/src/main/java/io/github/reionchan/config/OAuth2ClientResourceSecurityConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws
5151
.accessDeniedHandler(GlobalExceptionHandler::handleAuthException)
5252
.authenticationEntryPoint(GlobalExceptionHandler::handleAuthException))
5353
.authorizeHttpRequests(httpReq ->
54-
httpReq.requestMatchers(HttpMethod.GET, "/actuator/health", "/actuator/health/**",
54+
httpReq.requestMatchers(HttpMethod.GET, "/actuator/health", "/actuator/health/**", "/actuator/refresh",
5555
"/v3/api-docs/**", "/swagger-ui/**", "/swagger-ui.html").permitAll()
5656
.anyRequest().authenticated());
5757
return http.build();

0 commit comments

Comments
 (0)