docs: Polish user document. (#3089)

docs: Polish user document
pull/3096/head
Steve Rao 2 years ago committed by GitHub
parent 4378537a38
commit 244bb93776
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,14 +2,14 @@
image::pic/governance-module.png[]
Microservices Governance是Spring Cloud Alibaba最新推出的微服务治理能力提供了多种类型的微服务治理能力包括标签路由服务鉴权等。并且对接了多种控制面比如IstioOpenSergo让用户无需改造Spring Cloud应用也能实时感知到上述治理控制面下发的治理规则并将此规则应用到Spring Cloud应用上从而完成对Spring Cloud应用的治理。
Spring Cloud Alibaba Governance是Spring Cloud Alibaba最新推出的微服务治理能力提供了多种类型的微服务治理能力包括标签路由服务鉴权等。并且对接了多种控制面比如 https://istio.io/[Istio]http://opensergo.io/[OpenSergo]让用户仅通过添加特定的Adapter模块就能实时感知到上述治理控制面下发的治理规则并将此规则作用到应用上从而完成对应用的治理。
=== 配置转换
image::pic/resource-transform.png[]
Microservices Governance的配置转换模块会将不同控制面下发的配置进行统一的转换目前支持将来自IstioOpenSergo等控制面下发的配置统一转换为Spring Cloud Alibaba统一抽象出的数据结构以供后续使用
Microservices Governance的规则转换模块会将不同控制面下发的规则进行统一的转换目前支持将来自IstioOpenSergo等控制面下发的规则统一转换为Spring Cloud Alibaba统一抽象出的数据结构以供后续使用
如果在项目中使用Istio来实现规则转换需要使用如下starter依赖
如果在项目中使用Istio来实现规则转换首先注意需要搭建一个Kubernetes集群并且在其中部署Istio具体参考 https://istio.io/latest/zh/docs/setup/install[Istio安装]。然后在需要接收到来自Istio规则的应用一般为服务消费者中添加如下starter依赖
[source,xml,indent=0]
----
<dependency>
@ -17,9 +17,9 @@ Microservices Governance的配置转换模块会将不同控制面下发的配
<artifactId>spring-cloud-starter-xds-adapter</artifactId>
</dependency>
----
OpenSergo请参考https://github.com/alibaba/spring-cloud-alibaba/tree/2.2.x/spring-cloud-alibaba-examples/governance-example/label-routing-example[Spring Cloud Alibaba Routing Examples]
OpenSergo请参考 https://github.com/alibaba/spring-cloud-alibaba/tree/2.2.x/spring-cloud-alibaba-examples/governance-example/label-routing-example[Spring Cloud Alibaba Routing Examples]
之后在application.yml配置文件中配置如下配置
之后在application.yml配置文件中设置如下配置内容:
[source,yaml,indent=0]
----
@ -75,9 +75,9 @@ spring:
==== 使用路由
在引入配置转换模块后,就能获取到相应的治理规则来对应用赋予相应的治理能力。标签路由模块可以实现对应用根据请求头,请求参数等标签来路由到不同的服务
在引入配置转换模块后,就能获取到相应的治理规则来对应用赋予相应的治理能力。标签路由模块可以实现对应用根据请求头,请求参数等标签来路由到不同的服务
1.如果在项目中使用Spring Cloud Alibaba 标签路由,需要使用添加如下starter
1.如果在项目中使用Spring Cloud Alibaba 标签路由需要添加如下starter(一般添加在服务消费者应用上)
[source,xml,indent=0]
----
<dependency>
@ -92,13 +92,13 @@ spring:
spring.cloud.governance.routing.rule=RandomRule
----
在引入Istio配置转换模块的前提下标签路由模块支持对以下几种请求的元信息做路由
在引入Istio配置转换模块的前提下标签路由模块支持对以下几种请求的元信息做路由
* 请求路径
* 请求头
* 请求参数
使用Istio下发对应的 `DestinationRule` 以及 `VirtualService` ,即可配置对应的标签路由规则,具体的配置方法请参考以下文档与示例
使用Istio下发对应的 `DestinationRule` 以及 `VirtualService` ,即可配置对应的标签路由规则,具体的配置方法请参考以下文档与示例
* https://istio.io/latest/zh/docs/reference/config/networking/virtual-service/#VirtualService[Istio VirtualService]
* https://istio.io/latest/zh/docs/concepts/traffic-management/#destination-rules[Istio Destination Rule]
@ -107,7 +107,7 @@ spring.cloud.governance.routing.rule=RandomRule
=== 服务鉴权
image::pic/auth-process.png[]
在引入配置转换模块后我们就能获取到相应的治理规则来对Spring Cloud应用赋予相应的治理能力。服务鉴权模块给Spring Cloud应用提供多种鉴权方式如IP黑白名单JWT鉴权等
在引入规则转换Adapter后就能获取到相应的治理规则来对应用赋予相应的治理能力。服务鉴权模块给应用提供多种鉴权方式如IP黑白名单JWT鉴权等
如果使用Spring Cloud Alibaba服务鉴权功能需要使用添加如下依赖
[source,xml,indent=0]

@ -2,15 +2,15 @@
image::pic/governance-module.png[]
Microservices Governance module is a micro-service governance sub-module launched by Spring Cloud Alibaba, which provides various types of microservices governance capabilities, including label routing, service authentication, etc. Moreover, it supports various control planes, such as Istio and OpenSergo, so that users can get the governance rules in real time without modifying Spring Cloud applications, and apply these rules to applications to govern the Spring Cloud application.
Spring Cloud Alibaba Governance is a Microservices governance capabilities provided by Spring Cloud Alibaba, which provides various types of microservices governance capabilities, including label routing, service authentication, etc. Moreover, it supports various control planes, such as https://istio.io/[Istio] and http://opensergo.io/[OpenSergo], so that users can get the governance rules in real time just adding relevant dependency, and apply these rules to applications to govern the Spring Cloud Alibaba application.
=== Resource-Transform
=== Resource Transform
image::pic/resource-transform.png[]
The resource-transform module of Microservices Governance will uniformly transform the configurations published by different control planes, like Istio and OperSergo, into the unified abstract data structure of Spring Cloud Alibaba for subsequent use.
The resource-transform adapter of Microservices Governance will uniformly transform the rules published by different control planes, like Istio and OperSergo, into the unified abstract data structure of Spring Cloud Alibaba for subsequent use.
If you use Istio in your project to transform the configuration, you need to use a following starter:
If you use Istio in your project to transform the rule, first of all, note that you need to build a Kubernetes cluster and deploy Istio in it. For details, refer to https://istio.io/latest/zh/docs/setup/install[Istio installation], you need to add a following starter dependency application(Generally added to service consumer application):
[source,xml,indent=0]
----
<dependency>
@ -74,10 +74,10 @@ Load balance Component: Ribbon
In the future, more components such as RestTemplate, Spring Cloud LoadBalancer and so on will be supported.
==== Using
With the introduction of the configuration transformation module, we can obtain the governance rules to give the Spring Cloud application some governance capabilities. Label routing module can route the Spring Cloud application according to the request header, request parameters and other tags to route to different services.
==== Using Routing capacity
With the introduction of the transformation adapter of rule, we can obtain the governance rules to give the Spring Cloud application some governance capabilities. Label routing module can route the Spring Cloud application according to the request header, request parameters and other tags to route to different services.
If you use Microservices Governance Label Routing in your project, You need to use a following starter:
If you use Microservices Governance Label Routing in your project, You need to use a following starter dependency(Generally added to service consumer application):
[source,xml,indent=0]
----
<dependency>
@ -92,7 +92,7 @@ If no configuration,use default ribbon load balance algorithm ZoneAvoidanceRule.
spring.cloud.governance.routing.rule=RandomRule
----
With the introduction of Istio Resource Transform module, the label routing module supports routing of the following types of request meta-information:
With the introduction of Istio Resource Transform adapter, the label routing module supports routing of the following types of request meta-information:
* Request Path
* Request Header
@ -107,7 +107,7 @@ We use Istio to publish corresponding `DestinationRule` and `VirtualService` to
=== Authentication
image::pic/auth-process.png[]
With the introduction of the configuration transformation module, we can obtain the governance rules to give the Spring Cloud application some governance capabilities. The Authentication module provides various authentication modes for Spring Cloud applications, such as IP blacklist and whitelist and JWT authentication.
With the introduction of the transformation adapter of rule, we can obtain the governance rules to give the Spring Cloud application some governance capabilities. The Authentication module provides various authentication modes for Spring Cloud applications, such as IP blacklist and whitelist and JWT authentication.
Add following Microservices Authentication starter:

@ -16,8 +16,8 @@
## 示例
### 如何接入
在启动示例进行演示之前,我们先了解一下 Spring Cloud 应用如何接入Istio并提供鉴权功能。 注意 本章节只是为了便于理解接入方式,本示例代码中已经完成接入工作,您无需再进行修改。
1. 修改`pom.xml`文件引入Istio资源转换以及Spring Cloud Alibaba鉴权模块:
在启动示例进行演示之前先了解一下应用如何接入Istio并提供鉴权功能。 注意 本章节只是为了便于理解接入方式,本示例代码中已经完成接入工作,您无需再进行修改。
1. 修改`pom.xml`文件引入Istio规则Adapter以及Spring Cloud Alibaba鉴权模块:
```xml
<dependency>
@ -61,7 +61,7 @@ spring:
|连接Istio<br>15012端口时使用的JWT token| spring.cloud.istio.config.istiod-token|应用所在pod的`/var/run/secrets/tokens/istio-token`文件的内容|
|是否打印xDS相关日志| spring.cloud.istio.config.log-xds|true|
### 运行应用
需要将应用运行在K8s环境中并给运行的应用将K8s的一些元信息注入以下环境变量中:
注意应用运行在K8s环境中在非默认命名空间下的应用需要接收Istiod下发的规则需要将运行的应用K8s的元信息注入以下环境变量中具体操作方式可参考 [Kubernetes文档](https://kubernetes.io/zh-cn/docs/tasks/inject-data-application/environment-variable-expose-pod-information)
|环境变量名|K8s pod metadata name|
|--|--|
|POD_NAME|metadata.name|
@ -71,8 +71,8 @@ spring:
### 效果演示
下面给出几个简单的鉴权规则配置的示例:
#### IP黑白名单
我们在使用如下命令通过Istio下发一条鉴权规则至demo应用这条规则的限制了访问该应用的来源IP:
```
使用如下命令通过Istio下发一条鉴权规则至demo应用这条规则的限制了访问该应用的来源IP:
```YAML
kubectl apply -f - << EOF
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
@ -105,7 +105,7 @@ received request from ${from_ip}, local addr is ${local_ip}, local host is ${loc
```
说明通过了SCA的鉴权将会返回此请求的一些元信息。
在此之后,我们删除这条IP黑白名单的鉴权规则:
在此之后删除这条IP黑白名单的鉴权规则:
```shell
kubectl delete AuthorizationPolicy from-ip-allow -n ${namespace_name}
```
@ -115,8 +115,8 @@ received request from ${from_ip}, local addr is ${local_ip}, local host is ${loc
```
#### 请求头认证
我们在使用如下命令通过Istio下发一条鉴权规则至demo应用这条规则的限制了访问该应用的请求header:
```
使用如下命令通过Istio下发一条鉴权规则至demo应用这条规则的限制了访问该应用的请求header:
```YAML
kubectl apply -f - << EOF
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
@ -151,7 +151,7 @@ curl --location --request GET '${demo_ip}/auth'
```
Auth failed, please check the request and auth rule
```
在此之后,我们删除这条请求头认证的规则:
在此之后,删除这条请求头认证的规则:
```shell
kubectl delete AuthorizationPolicy http-headers-allow -n ${namespace_name}
```
@ -161,8 +161,8 @@ received request from ${from_ip}, local addr is ${local_ip}, local host is ${loc
```
#### JWT认证
我们使用如下命令通过Istio下发一条鉴权规则至demo应用这条规则限制了访问该应用需要携带的JWT token value:
```
使用如下命令通过Istio下发一条鉴权规则至demo应用这条规则限制了访问该应用需要携带的JWT token value:
```YAML
kubectl apply -f - <<EOF
apiVersion: security.istio.io/v1beta1
kind: RequestAuthentication
@ -196,7 +196,7 @@ curl --location --request GET '${demo_ip}/auth' \
```
Auth failed, please check the request and auth rule
```
在此之后,我们删除这条JWT认证的规则:
在此之后删除这条JWT认证的规则:
```shell
kubectl delete RequestAuthentication jwt-jwks-uri -n ${namespace_name}
```

@ -11,8 +11,8 @@ Please refer to [tools](https://kubernetes.io/zh-cn/docs/tasks/tools/) chapter o
Please refer to [install](https://istio.io/latest/zh/docs/setup/install/) chapter of Istio document.
## Introduction to Istio authentication rules
- [overview](https://istio.io/latest/zh/docs/concepts/security/#authorization)
- [detail](https://istio.io/latest/zh/docs/reference/config/security/)
- [Istio Authorization Overview](https://istio.io/latest/zh/docs/concepts/security/#authorization)
- [Istio Security Detail](https://istio.io/latest/zh/docs/reference/config/security/)
## Demo
### Connect to Istio
@ -24,7 +24,6 @@ Before launching the example for demonstration, let's look at how a Spring Cloud
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-governance-auth</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-xds-adapter</artifactId>
@ -57,13 +56,13 @@ Here's an explanation of each field:
|Whether to connect to Istio to obtain authentication configuration| spring.cloud.istio.config.enabled|true|
|Host of Istiod| spring.cloud.istio.config.host|127.0.0.1|
|Port of Istiod| spring.cloud.istio.config.port|15012|15010 port does not need TLSbut 15012 does
|Thread pool size for SCA to pull the config| spring.cloud.istio.config.polling-pool-size|10|
|Time interval for SCA to pull the config| spring.cloud.istio.config.polling-time|30|The unit is second|
|JWT token for SCA to connect to 15012 port| spring.cloud.istio.config.istiod-token|Content of file `/var/run/secrets/tokens/istio-token` in the pod of application|
|Thread pool size for application to pull the config| spring.cloud.istio.config.polling-pool-size|10|
|Time interval for application to pull the config| spring.cloud.istio.config.polling-time|30|The unit is second|
|JWT token for application to connect to 15012 port| spring.cloud.istio.config.istiod-token|Content of file `/var/run/secrets/tokens/istio-token` in the pod of application|
|Whether to print logs about xDS| spring.cloud.istio.config.log-xds|true|
### Run the application
You need to run the application in the K8s environment and inject some meta information about K8s into the following environment variables for the running application:
Note that the application runs in the K8s environment, and the application in the non-default namespace needs to receive the rules issued by Istiod, and needs to inject the meta information of the running application Kubernetes into the following environment variables. For the specific operation method, please refer to [Kubernetes documentation](https://kubernetes.io/zh-cn/docs/tasks/inject-data-application/environment-variable-expose-pod-information):
|Environment variable name|K8s pod metadata name|
|--|--|
|POD_NAME|metadata.name|
@ -74,7 +73,7 @@ You need to run the application in the K8s environment and inject some meta info
The following are some simple examples of authentication rule configurations:
#### IP Blocks
The following command is used to deliver an authentication rule to the demo application through Istio. This rule restricts the source IP addresses that can access the application:
```
```YAML
kubectl apply -f - << EOF
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
@ -105,7 +104,7 @@ If the source IP of the request is not '127.0.0.1', then the application returns
```
received request from ${from_ip}, local addr is ${local_ip}, local host is ${local_host}, request path is/auth
```
It indicates that the request has been authenticated by SCA and some meta data of the request will be returned.
It indicates that the request has been authenticated by application and some meta data of the request will be returned.
After that, we delete the authentication rule for the IP Blocks:
```shell
@ -118,7 +117,7 @@ received request from ${from_ip}, local addr is ${local_ip}, local host is ${loc
#### Request Header Authentication
We use the following command to deliver an authentication rule to the demo application through Istio. This rule restricts the request header for accessing the application:
```
```YAML
kubectl apply -f - << EOF
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
@ -165,7 +164,7 @@ received request from ${from_ip}, local addr is ${local_ip}, local host is ${loc
#### JWT Authentication
We use the following command to deliver an authentication rule to the demo application through Istio. This rule restricts the JWT token value that must be carried to access the application:
```
```YAML
kubectl apply -f - <<EOF
apiVersion: security.istio.io/v1beta1
kind: RequestAuthentication
@ -181,7 +180,7 @@ spec:
jwksUri: https://raw.githubusercontent.com/istio/istio/release-1.5/security/tools/jwt/samples/jwks.json
EOF
```
An Http request with a correct JWT token is then sent to verify that the rule is valid:
A Http request with a correct JWT token is then sent to verify that the rule is valid:
```
curl --location --request GET '${demo_ip}/auth' \
--header 'Authorization: Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6IkRIRmJwb0lVcXJZOHQyenBBMnFYZkNtcjVWTzVaRXI0UnpIVV8tZW52dlEiLCJ0eXAiOiJKV1QifQ.eyJleHAiOjQ2ODU5ODk3MDAsImZvbyI6ImJhciIsImlhdCI6MTUzMjM4OTcwMCwiaXNzIjoidGVzdGluZ0BzZWN1cmUuaXN0aW8uaW8iLCJzdWIiOiJ0ZXN0aW5nQHNlY3VyZS5pc3Rpby5pbyJ9.CfNnxWP2tcnR9q0vxyxweaF3ovQYHYZl82hAUsn21bwQd9zP7c-LS9qd_vpdLG4Tn1A15NxfCjp5f7QNBUo-KC9PJqYpgGbaXhaGx7bEdFWjcwv3nZzvc7M__ZpaCERdwU7igUmJqYGBYQ51vr2njU9ZimyKkfDe3axcyiBZde7G6dabliUosJvvKOPcKIWPccCgefSj_GNfwIip3-SsFdlR7BtbVUcqR-yv-XOxJ3Uc1MI0tz3uMiiZcyPV7sNCU4KRnemRIMHVOfuvHsU60_GhGbiSFzgPTAa9WTltbnarTbxudb_YEOx12JiwYToeX0DCPb43W1tzIBxgm8NxUg'
@ -190,7 +189,7 @@ Since this request carries a correct JWT token, it will return:
```
received request from ${from_ip}, local addr is ${local_ip}, local host is ${local_host}, request path is/auth
```
An Http request with a invalid JWT token is then sent to verify that the rule is valid:
A Http request with an invalid JWT token is then sent to verify that the rule is valid:
```
curl --location --request GET '${demo_ip}/auth' \
--header 'Authorization: Bearer invalid token'

@ -1,8 +1,8 @@
# routing example
# Routing Example
## 项目说明
本项目演示如何使用 spring cloud ailbaba governance routing 模块完成标签路由功能。
本项目演示如何使用 Spring Cloud Alibaba Governance Routing 模块完成标签路由功能。
## 模块结构
@ -22,7 +22,7 @@
### 如何接入
**注意 本章节只是为了便于您理解接入方式,本示例代码中已经完成接入工作,您无需再进行修改。**
1. 首先,修改需要进行路由服务的`pom.xml` 文件,引入 spring cloud ailbaba governance labelrouting依赖。
1. 首先,修改需要进行路由服务的`pom.xml` 文件,引入 `spring-cloud-starter-alibaba-governance-routing` 依赖。
```xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
@ -82,7 +82,7 @@ public void getDataFromControlPlaneTest() {
}
```
代码对应的规则如下:
若同时满足请求参数中含有tag=gray请求头中含有id且值小于10uri为/router-test则流量全部路由到v2版本中若有一条不满足则流量路由到v1版本中。
若同时满足请求参数中含有`tag=gray`请求头中含有id且值小于10uri为`/router-test`则流量全部路由到v2版本中若有一条不满足则流量路由到v1版本中。
规则也支持动态修改,测试动态修改的规则如下:
```java
@ -127,7 +127,7 @@ public void getDataFromControlPlaneTest() {
}
```
代码对应的规则如下:
若同时满足请求参数中含有tag=gray请求头中含有id且值小于10uri为/router-test则50%流量路由到v2版本中剩下的流量路由到v1版本中若有一条不满足则流量路由到v1版本中。
若同时满足请求参数中含有`tag=gray`请求头中含有id且值小于10uri为`/router-test`则50%流量路由到v2版本中剩下的流量路由到v1版本中若有一条不满足则流量路由到v1版本中。
##### 演示步骤
1. 访问 http://localhost:18083/add 将路由规则由控制面接口推入路由规则仓库中。
@ -178,7 +178,7 @@ public void getDataFromControlPlaneTest() {
</dependency>
```
2. 在`src/main/resources/application.yml`配置文件中配置Istio控制面的相关信息:
```
```YAML
server:
port: 18084
spring:
@ -218,8 +218,8 @@ spring:
启动三个模块的启动类分别为IstioConsumerApplication两个ProviderApplication将其注入到Nacos注册中心中。
### 下发配置
我们通过Istio控制面下发标签路由规则首先下发DestinationRule规则:
```
通过Istio控制面下发标签路由规则首先下发DestinationRule规则:
```YAML
kubectl apply -f - << EOF
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
@ -237,8 +237,8 @@ spec:
EOF
```
此规则将后端服务拆分为两个版本label为v1的pod被分到v1版本label为v2的pod被分到v2版本
之后,我们下发VirtualService规则:
```
之后下发VirtualService规则:
```YAML
kubectl apply -f - << EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
@ -264,9 +264,9 @@ spec:
subset: v1
EOF
```
这条VirtualService指定了一条最简单的标签路由规则将请求头tag为gray请求路径为/istio-label-routing的HTTP请求路由到v2版本其余的流量都路由到v1版本:
这条VirtualService指定了一条最简单的标签路由规则将请求头tag为gray请求路径为`/istio-label-routing`的HTTP请求路由到v2版本其余的流量都路由到v1版本:
### 效果演示
我们发送一条不带请求头的HTTP请求至IstioConsumerApplication:
发送一条不带请求头的HTTP请求至IstioConsumerApplication:
```
curl --location --request GET '127.0.0.1:18084/istio-label-routing'
```
@ -274,7 +274,7 @@ curl --location --request GET '127.0.0.1:18084/istio-label-routing'
```
Route in 30.221.132.228: 18081,version is v1.
```
之后我们发送一条请求头tag为gray且请求路径为/istio-label-routing的HTTP请求:
之后发送一条请求头tag为gray且请求路径为`/istio-label-routing`的HTTP请求:
```
curl --location --request GET '127.0.0.1:18084/istio-label-routing' --header 'tag: gray'
```
@ -282,7 +282,7 @@ curl --location --request GET '127.0.0.1:18084/istio-label-routing' --header 'ta
```
Route in 30.221.132.228: 18082,version is v2.
```
最后我们删除这条标签路由规则:
最后删除这条标签路由规则:
```shell
kubectl delete VirtualService sca-virtual-service
kubectl delete DestinationRule my-destination-rule
@ -292,7 +292,7 @@ kubectl delete DestinationRule my-destination-rule
## 集成OpenSergo
**注意 本章节只是为了便于您理解接入方式,本示例代码中已经完成接入工作,您无需再进行修改。**
1. 首先,修改`pom.xml` 文件,引入`spring-cloud-starter-alibaba-governance-routing`依赖。同时引入Spring Cloud Alibaba的`spring-cloud-starter-opensergo-adapter`模块
```
```XML
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-governance-routing</artifactId>
@ -314,7 +314,7 @@ spring.cloud.opensergo.endpoint=127.0.0.1:10246
[启动 OpenSergo 控制面](https://opensergo.io/zh-cn/docs/quick-start/opensergo-control-plane/) ,并通过 OpenSergo 控制面下发流量路由规则
```
```YAML
kubectl apply -f - << EOF
apiVersion: traffic.opensergo.io/v1alpha1
kind: TrafficRouter
@ -347,7 +347,7 @@ EOF
这条TrafficRouter指定了一条最简单的流量路由规则将请求头tag为v2的HTTP请求路由到v2版本其余的流量都路由到v1版本。
如果v2版本没有对应的节点则将流量fallback至v1版本。
### 效果演示
我们发送一条不带请求头的HTTP请求至IstioConsumerApplication:
发送一条不带请求头的HTTP请求至IstioConsumerApplication:
```
curl --location --request GET '127.0.0.1:18083/router-test'
```
@ -355,7 +355,7 @@ curl --location --request GET '127.0.0.1:18083/router-test'
```
Route in 30.221.132.228: 18081,version is v1.
```
之后我们发送一条请求头tag为gray的HTTP请求
之后发送一条请求头tag为gray的HTTP请求
```
curl --location --request GET '127.0.0.1:18083/router-test' --header 'tag: v2'
```
@ -363,7 +363,7 @@ curl --location --request GET '127.0.0.1:18083/router-test' --header 'tag: v2'
```
Route in 30.221.132.228: 18082,version is v2.
```
最后我们删除这条标签路由规则:
最后删除这条标签路由规则:
```shell
kubectl delete VirtualService sca-virtual-service
kubectl delete DestinationRule my-destination-rule
@ -373,15 +373,15 @@ kubectl delete DestinationRule my-destination-rule
## 集成OpenSergo
**注意 本章节只是为了便于您理解接入方式,本示例代码中已经完成接入工作,您无需再进行修改。**
1. 首先修改pom.xml 文件,引入`spring-cloud-starter-alibaba-governance-routing`依赖。同时引入Spring Cloud Alibaba的`spring-cloud-starter-opensergo-adapter`模块
```
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-governance-routing</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-opensergo-adapter</artifactId>
</dependency>
```XML
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-governance-routing</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-opensergo-adapter</artifactId>
</dependency>
```
2. 在application.properties配置文件中配置OpenSergo控制面的相关信息
```
@ -395,7 +395,7 @@ spring.cloud.opensergo.endpoint=127.0.0.1:10246
[启动 OpenSergo 控制面](https://opensergo.io/zh-cn/docs/quick-start/opensergo-control-plane/) ,并通过 OpenSergo 控制面下发流量路由规则
```
```YAML
kubectl apply -f - << EOF
apiVersion: traffic.opensergo.io/v1alpha1
kind: TrafficRouter
@ -428,7 +428,7 @@ EOF
这条TrafficRouter指定了一条最简单的流量路由规则将请求头tag为v2的HTTP请求路由到v2版本其余的流量都路由到v1版本。
如果v2版本没有对应的节点则将流量fallback至v1版本。
### 效果演示
我们发送一条不带请求头的HTTP请求至OpenSergoConsumerApplication
发送一条不带请求头的HTTP请求至OpenSergoConsumerApplication
```
curl --location --request GET '127.0.0.1:18083/router-test'
```
@ -436,7 +436,7 @@ curl --location --request GET '127.0.0.1:18083/router-test'
```
Route in 30.221.132.228: 18081,version is v1.
```
之后我们发送一条请求头tag为gray的HTTP请求
之后发送一条请求头tag为gray的HTTP请求
```
curl --location --request GET '127.0.0.1:18083/router-test' --header 'tag: v2'
```
@ -444,7 +444,7 @@ curl --location --request GET '127.0.0.1:18083/router-test' --header 'tag: v2'
```
Route in 30.221.132.228: 18082,version is v2.
```
我们停止v2版本的ProviderApplication后继续发送一条请求头tag为gray的HTTP请求
停止v2版本的ProviderApplication后继续发送一条请求头tag为gray的HTTP请求
```
curl --location --request GET '127.0.0.1:18083/router-test' --header 'tag: v2'
```

@ -1,10 +1,10 @@
# routing example
# Routing Example
## Project Description
This project demonstrates how to use the spring cloud ailbaba governance routing module to complete the label routing function.
This project demonstrates how to use the Spring Cloud Alibaba Governance Routing module to complete the routing capacity.
## module structure
## Module structure
This module includes a consumer instance and a provider cluster, which contains two instances.
@ -23,7 +23,7 @@ In the future, more components such as RestTemplate, Spring Cloud LoadBalancer a
### How to access
**Note that this section is only for your convenience in understanding the access method. The access work has been completed in this sample code, and you do not need to modify it.**
1. First, modify the pom XML file, which introduces the spring cloud ailbaba governance labelrouting dependency.
1. First, modify the pom XML file, which introduces the Spring Cloud Alibaba governance routing dependency.
```xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
@ -84,7 +84,7 @@ public void getDataFromControlPlaneTest() {
}
```
The rules corresponding to the code are as follows:
If the request parameter contains tag=gray and the request header contains id and the value is greater than 10, uri is /router-test at the same time, the traffic is routed to the v2 version. If one of the request parameters does not meet the requirement, the traffic is routed to the v1 version.
If the request parameter contains tag=gray and the request header contains id and the value is greater than 10, uri is `/router-test` at the same time, the traffic is routed to the v2 version. If one of the request parameters does not meet the requirement, the traffic is routed to the v1 version.
Rules also support dynamic modification. The rules for testing dynamic modification are as follows:
```java
@ -129,9 +129,9 @@ public void getDataFromControlPlaneTest() {
}
```
The rules corresponding to the code are as follows:
If the request parameter contains tag=gray, and the request header contains id and the value is greater than 10,uri is /router-test, 50% of the traffic is routed to the v2 version, and the rest is routed to the v1 version. If one of the traffic does not meet the requirements, the traffic is routed to the v1 version.
If the request parameter contains tag=gray, and the request header contains id and the value is greater than 10, URL is `/router-test`, 50% of the traffic is routed to the v2 version, and the rest is routed to the v1 version. If one of the traffic does not meet the requirements, the traffic is routed to the v1 version.
##### demonstrate Steps
##### demonstration Steps
1. visit http://localhost:18083/add Push the routing rules from the control surface interface to the routing rule warehouse
visit http://localhost:18083/router -The test does not meet the routing rules. When the test is routed to the v1 version, the v1 version instance prints and returns the following results:
```
@ -165,8 +165,8 @@ Please refer to [tools](https://kubernetes.io/zh-cn/docs/tasks/tools/) chapter o
### Enable Istio on K8s
Please refer to [install](https://istio.io/latest/zh/docs/setup/install/) chapter of Istio document.
### Introduction to Istio traffic control rules
- [overview](https://istio.io/latest/zh/docs/concepts/security/#authorization)
- [detail](https://istio.io/latest/zh/docs/reference/config/security/)
- [Istio Authorization Overview](https://istio.io/latest/zh/docs/concepts/security/#authorization)
- [Istio Security Detail](https://istio.io/latest/zh/docs/reference/config/security/)
1. First, modify the pom.xml file to introduce the `spring-cloud-starter-alibaba-governance-routing` and `spring-cloud-starter-xds-adapter` dependency
```xml
<dependency>
@ -179,7 +179,7 @@ Please refer to [install](https://istio.io/latest/zh/docs/setup/install/) chapte
</dependency>
```
2. Configure application.yml for Istio control plane:
```
```YAML
server:
port: 18084
spring:
@ -220,7 +220,7 @@ Start IstioConsumerApplication and two ProviderApplications, and inject it into
### Publish Configuration
We publish the label routing rules through the Istio control plane. We publish a DestinationRule rule first:
```
```YAML
kubectl apply -f - << EOF
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
@ -239,7 +239,7 @@ EOF
```
This rule splits the back-end service into two versions. Pod with label v1 is assigned to v1, and pod with label v2 is assigned to v2
After that, we publish the VirtualService rule:
```
```YAML
kubectl apply -f - << EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
@ -265,8 +265,8 @@ spec:
subset: v1
EOF
```
This VirtualService specifies the simplest label routing rule. HTTP requests with a gray header and /istio-label-routing path are routed to v2, and the rest of the traffic is routed to v1:
### Demonstrate effect
This VirtualService specifies the simplest label routing rule. HTTP requests with a gray header and `/istio-label-routing` path are routed to v2, and the rest of the traffic is routed to v1:
### Demonstration effect
We send an HTTP request without a request header to IstioConsumerApplication:
```
curl --location --request GET '127.0.0.1:18084/istio-label-routing'
@ -275,7 +275,7 @@ Since the request header is not gray, the request will be routed to version v1 w
```
Route in 30.221.132.228: 18081,version is v1.
```
We then send an HTTP request with a gray tag in its header and the request path is /istio-label-routing:
We then send an HTTP request with a gray tag in its header and the request path is `/istio-label-routing`:
```
curl --location --request GET '127.0.0.1:18084/istio-label-routing' --header 'tag: gray'
```
@ -283,27 +283,28 @@ The request is routed to version v2 because the routing rule is matched by the r
```
Route in 30.221.132.228: 18082,version is v2.
```
Finally, we delete this label routing rule::
Finally, we delete this label routing rule:
```shell
kubectl delete VirtualService sca-virtual-service
kubectl delete DestinationRule my-destination-rule
```
After the rule is deleted, the routing policy is not determined by whether the request header is carried or not, but completely depends on the implementation of the loadbalancer。
After the rule is deleted, the routing policy is not determined by whether the request header is carried or not, but completely depends on the implementation of the loadbalancer.
## Integrating OpenSergo
**Note that this section is only for your convenience in understanding the access method. The access work has been completed in this sample code, and you do not need to modify it.**
### Configure
1. First, modify the pom.xml file to introduce the `spring-cloud-starter-alibaba-governance-routing` and `spring-cloud-starter-opensergo-adapter` dependency
```
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-governance-routing</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-opensergo-adapter</artifactId>
</dependency>
### Configuration
1. First, modify the `pom.xml` file to introduce the `spring-cloud-starter-alibaba-governance-routing` and `spring-cloud-starter-opensergo-adapter` dependency
```xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-governance-routing</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-opensergo-adapter</artifactId>
</dependency>
```
2. Configure application.yml for OpenSergo control plane
2. Configure `application.yml` for OpenSergo control plane
```
# The endpoint of OpenSergo ControlPlane
spring.cloud.opensergo.endpoint=127.0.0.1:10246
@ -312,7 +313,7 @@ spring.cloud.opensergo.endpoint=127.0.0.1:10246
Start OpenSergoConsumerApplication and two ProviderApplications, and inject it into the Nacos registry center.
### Publish Configuration
[First start OpenSergo control plan](https://opensergo.io/docs/quick-start/opensergo-control-plane/) , Then we publish the label routing rules through the OpenSergo control plane. We publish a TrafficRouter rule.
```
```YAML
kubectl apply -f - << EOF
apiVersion: traffic.opensergo.io/v1alpha1
kind: TrafficRouter
@ -353,7 +354,7 @@ Since the request header is not gray, the request will be routed to version v1 w
```
Route in 30.221.132.228: 18081,version is v1.
```
We then send an HTTP request with a gray tag in its header and the request path is /istio-label-routing
We then send an HTTP request with a gray tag in its header and the request path is `/istio-label-routing`:
```
curl --location --request GET '127.0.0.1:18083/router-test' --header 'tag: gray'
```

Loading…
Cancel
Save