准备服务
该示例由如下四个微服务组成:
- productpage-v1:  主服务,展示书的简介,并调用details和reviews服务获取书的详细信息和书评信息。
- details-v1: 提供书的详细信息。
- reviews: 提供书评信息,并调用ratings服务获取书评等级。
- ratings-v1: 提供书评等级信息。
reviews书评服务有三个版本:
- v1不调用ratings服务。
- v2调用ratings服务,并将每个等级显示为1到5个黑色星标。
- v3调用ratings服务,并将每个等级显示为1到5个红色星标。
目标
同一个服务共存多个版本,默认是轮训调用每个版本,所以会轮训调用reviews的三个版本。
- 配置路由使流量全部调用reviews-v1版本服务
- 配置基于HTTP请求头路由流量
配置流量路由到指定版本
- destination-rule-all.yaml
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 
 | apiVersion: networking.istio.io/v1alpha3kind: DestinationRule
 metadata:
 name: productpage
 spec:
 host: productpage
 subsets:
 - name: v1
 labels:
 version: v1
 ---
 apiVersion: networking.istio.io/v1alpha3
 kind: DestinationRule
 metadata:
 name: reviews
 spec:
 host: reviews
 subsets:
 - name: v1
 labels:
 version: v1
 - name: v2
 labels:
 version: v2
 - name: v3
 labels:
 version: v3
 ---
 apiVersion: networking.istio.io/v1alpha3
 kind: DestinationRule
 metadata:
 name: ratings
 spec:
 host: ratings
 subsets:
 - name: v1
 labels:
 version: v1
 - name: v2
 labels:
 version: v2
 - name: v2-mysql
 labels:
 version: v2-mysql
 - name: v2-mysql-vm
 labels:
 version: v2-mysql-vm
 ---
 apiVersion: networking.istio.io/v1alpha3
 kind: DestinationRule
 metadata:
 name: details
 spec:
 host: details
 subsets:
 - name: v1
 labels:
 version: v1
 - name: v2
 labels:
 version: v2
 ---
 
 | 
- virtual-service-all-v1.yaml
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 
 | apiVersion: networking.istio.io/v1alpha3kind: VirtualService
 metadata:
 name: productpage
 spec:
 hosts:
 - productpage
 http:
 - route:
 - destination:
 host: productpage
 subset: v1
 ---
 apiVersion: networking.istio.io/v1alpha3
 kind: VirtualService
 metadata:
 name: reviews
 spec:
 hosts:
 - reviews
 http:
 - route:
 - destination:
 host: reviews
 subset: v1
 ---
 apiVersion: networking.istio.io/v1alpha3
 kind: VirtualService
 metadata:
 name: ratings
 spec:
 hosts:
 - ratings
 http:
 - route:
 - destination:
 host: ratings
 subset: v1
 ---
 apiVersion: networking.istio.io/v1alpha3
 kind: VirtualService
 metadata:
 name: details
 spec:
 hosts:
 - details
 http:
 - route:
 - destination:
 host: details
 subset: v1
 ---
 
 | 
访问: http://bookinfo.example.com/productpage
请注意,无论您刷新多少次,页面的评论部分均不会显示星级。这是因为您将Istio配置为将评论服务的所有流量路由到版本reviews:v1,并且该服务的该版本无法访问星级评分服务。
配置基于HTTP请求头路由流量
- virtual-service-reviews-test-v2.yaml
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 
 | apiVersion: networking.istio.io/v1alpha3kind: VirtualService
 metadata:
 name: reviews
 spec:
 hosts:
 - reviews
 http:
 - match:
 - headers:
 end-user:
 exact: jason
 route:
 - destination:
 host: reviews
 subset: v2
 - route:
 - destination:
 host: reviews
 subset: v1
 
 | 
该配置会覆盖之前配置
访问: http://bookinfo.example.com/productpage
右上角用jason用户名登录,密码任意,然后请求会调用review-v2服务,该服务会调用ratings-v1服务,显示出星图标。
因为页面引用了googleapis下的jquery,由于网络原因,不能获取到,导致js脚本失效
参考资料