# 路由的作用
路由配置是 CAT 集群流量负载均衡最重要的一环。路由配置提供了默认机器列表、多机房划分、地域划分等基本功能,路由选择的优先级为:同机房 > 同地域 > 默认机器 > 备用机器。
对于流量较高的机器,用户可以扩展相应的策略,通过路由配置将部分流量指定到低负载的机器。
# 配置入口
http:///localhost:8080/s/config?op=routerConfigUpdate (opens new window)
# 配置示例说明
通过一个实际生产环境的场景来说明路由配置的最佳实践:
- CAT 集群信息
地域 | 机房分组 | 机器列表 |
---|---|---|
上海机房 ShangHai | sh_group1 | 10.8.101.1 |
10.8.101.2 | ||
10.8.101.3 | ||
10.8.101.4 | ||
10.8.101.5 | ||
sh_group1 | 10.9.101.1 | |
10.9.101.2 | ||
10.9.101.3 | ||
10.9.101.4 | ||
10.9.101.5 | ||
北京机房 Beijing | bg_group1 | 10.2.170.1 |
10.2.170.2 | ||
10.2.170.3 | ||
10.2.170.4 | ||
10.2.170.5 | ||
bg_group2 | 10.3.170.1 | |
10.3.170.2 | ||
10.3.170.3 | ||
10.3.170.4 | ||
10.3.170.5 |
- 路由配置示例
<?xml version="1.0" encoding="utf-8"?>
<router-config backup-server="10.8.101.1" backup-server-port="2280" backup-server-http-port="8080">
<!-- sh_group1 -->
<default-server id="10.8.101.1" weight="1.0" port="2280" enable="true" http-port="8080"/>
<default-server id="10.8.101.2" weight="1.0" port="2280" enable="true" http-port="8080"/>
<default-server id="10.8.101.3" weight="1.0" port="2280" enable="true" http-port="8080"/>
<default-server id="10.8.101.4" weight="1.0" port="2280" enable="true" http-port="8080"/>
<default-server id="10.8.101.5" weight="1.0" port="2280" enable="true" http-port="8080"/>
<!-- sh_group2 -->
<default-server id="10.9.101.1" weight="1.0" port="2280" enable="true" http-port="8080"/>
<default-server id="10.9.101.2" weight="1.0" port="2280" enable="true" http-port="8080"/>
<default-server id="10.9.101.3" weight="1.0" port="2280" enable="true" http-port="8080"/>
<default-server id="10.9.101.4" weight="1.0" port="2280" enable="true" http-port="8080"/>
<default-server id="10.9.101.5" weight="1.0" port="2280" enable="true" http-port="8080"/>
<!-- bj_group1 -->
<default-server id="10.2.170.1" weight="1.0" port="2280" enable="true" http-port="8080"/>
<default-server id="10.2.170.2" weight="1.0" port="2280" enable="true" http-port="8080"/>
<default-server id="10.2.170.3" weight="1.0" port="2280" enable="true" http-port="8080"/>
<default-server id="10.2.170.4" weight="1.0" port="2280" enable="true" http-port="8080"/>
<default-server id="10.2.170.5" weight="1.0" port="2280" enable="true" http-port="8080"/>
<!-- bj_group2 -->
<default-server id="10.3.170.1" weight="1.0" port="2280" enable="true" http-port="8080"/>
<default-server id="10.3.170.2" weight="1.0" port="2280" enable="true" http-port="8080"/>
<default-server id="10.3.170.3" weight="1.0" port="2280" enable="true" http-port="8080"/>
<default-server id="10.3.170.4" weight="1.0" port="2280" enable="true" http-port="8080"/>
<default-server id="10.3.170.5" weight="1.0" port="2280" enable="true" http-port="8080"/>
<network-policy id="shanghai" title="default" block="false" server-group="shanghai">
<network id="10.8.101.0/24"/>
<network id="10.8.102.0/24"/>
<network id="10.9.101.0/24"/>
<network id="10.9.102.0/24"/>
</network-policy>
<network-policy id="beijing" title="default" block="false" server-group="beijing">
<network id="10.2.170.0/24"/>
<network id="10.2.171.0/24"/>
<network id="10.3.170.1/24"/>
<network id="10.3.171.1/24"/>
</network-policy>
<network-policy id="sh_group1" title="default" block="false" server-group="sh_group1">
<network id="10.8.101.0/24"/>
<network id="10.8.102.0/24"/>
</network-policy>
<network-policy id="sh_group2" title="default" block="false" server-group="sh_group1">
<network id="10.9.101.0/24"/>
<network id="10.9.102.0/24"/>
</network-policy>
<network-policy id="bj_group1" title="default" block="false" server-group="bj_group1">
<network id="10.2.170.0/24"/>
<network id="10.2.171.0/24"/>
</network-policy>
<network-policy id="bj_group2" title="default" block="false" server-group="bj_group1">
<network id="10.3.170.0/24"/>
<network id="10.3.171.0/24"/>
</network-policy>
<network-policy id="default" title="default" block="false" server-group="default">
<network id="10.8.101.0/24"/>
<network id="10.3.170.0/24"/>
</network-policy>
<server-group id="default" title="默认机器">
<group-server id="10.8.101.1"/>
<group-server id="10.8.101.2"/>
<group-server id="10.3.170.1"/>
<group-server id="10.3.170.2"/>
</server-group>
<server-group id="sh_group1" title="上海机房一组">
<group-server id="10.8.101.1"/>
<group-server id="10.8.101.2"/>
<group-server id="10.8.101.3"/>
<group-server id="10.8.101.4"/>
<group-server id="10.8.101.5"/>
</server-group>
<server-group id="sh_group2" title="上海机房二组">
<group-server id="10.9.101.1"/>
<group-server id="10.9.101.2"/>
<group-server id="10.9.101.3"/>
<group-server id="10.9.101.4"/>
<group-server id="10.9.101.5"/>
</server-group>
<server-group id="bj_group1" title="北京机房一组">
<group-server id="10.2.170.1"/>
<group-server id="10.2.170.2"/>
<group-server id="10.2.170.3"/>
<group-server id="10.2.170.4"/>
<group-server id="10.2.170.5"/>
</server-group>
<server-group id="bj_group2" title="北京机房二组">
<group-server id="10.3.170.1"/>
<group-server id="10.3.170.2"/>
<group-server id="10.3.170.3"/>
<group-server id="10.3.170.4"/>
<group-server id="10.3.170.5"/>
</server-group>
<server-group id="shanghai" title="上海机器">
<group-server id="10.8.101.1"/>
<group-server id="10.8.101.2"/>
<group-server id="10.8.101.3"/>
<group-server id="10.8.101.4"/>
<group-server id="10.8.101.5"/>
<group-server id="10.9.101.1"/>
<group-server id="10.9.101.2"/>
<group-server id="10.9.101.3"/>
<group-server id="10.9.101.4"/>
<group-server id="10.9.101.5"/>
</server-group>
<server-group id="beijing" title="北京机器">
<group-server id="10.2.170.1"/>
<group-server id="10.2.170.2"/>
<group-server id="10.2.170.3"/>
<group-server id="10.2.170.4"/>
<group-server id="10.2.170.5"/>
<group-server id="10.3.170.1"/>
<group-server id="10.3.170.2"/>
<group-server id="10.3.170.3"/>
<group-server id="10.3.170.4"/>
<group-server id="10.3.170.5"/>
</server-group>
<domain id="service-1">
<group id="default">
<server id="10.8.101.1" port="2280" weight="1.0"/>
<server id="10.8.101.2" port="2280" weight="1.0"/>
<server id="10.8.101.3" port="2280" weight="1.0"/>
</group>
<group id="shanghai">
<server id="10.8.101.1" port="2280" weight="1.0"/>
<server id="10.8.101.2" port="2280" weight="1.0"/>
<server id="10.8.101.3" port="2280" weight="1.0"/>
</group>
<group id="beijing">
<server id="10.2.170.1" port="2280" weight="1.0"/>
<server id="10.2.170.3" port="2280" weight="1.0"/>
<server id="10.2.170.5" port="2280" weight="1.0"/>
</group>
</domain>
<domain id="service-2">
<group id="default">
<server id="10.8.101.1" port="2280" weight="1.0"/>
<server id="10.8.101.3" port="2280" weight="1.0"/>
<server id="10.8.101.5" port="2280" weight="1.0"/>
</group>
<group id="sh_group1">
<server id="10.8.101.1" port="2280" weight="1.0"/>
<server id="10.8.101.3" port="2280" weight="1.0"/>
<server id="10.8.101.5" port="2280" weight="1.0"/>
</group>
<group id="shanghai">
<server id="10.8.101.1" port="2280" weight="1.0"/>
<server id="10.8.101.2" port="2280" weight="1.0"/>
<server id="10.8.101.3" port="2280" weight="1.0"/>
</group>
<group id="bj_group1">
<server id="10.2.170.1" port="2280" weight="1.0"/>
<server id="10.2.170.3" port="2280" weight="1.0"/>
<server id="10.2.170.5" port="2280" weight="1.0"/>
</group>
<group id="beijing">
<server id="10.2.170.1" port="2280" weight="1.0"/>
<server id="10.2.170.2" port="2280" weight="1.0"/>
<server id="10.2.170.3" port="2280" weight="1.0"/>
</group>
</domain>
</router-config>
1
2
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
2
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
配置说明:
default-server : CAT 集群机器列表
* enable : true 表示机器处于在线状态,主动对外提供服务;false 表示不对外主动提供服务,可用于定向测试、灰度测试以及备用应急处理。
* weight : 机器权重比例。在定期构建路由报表时,会根据权重以及机器丢失数据量进行路由调整,具体参考 RouterConfigAdjustor。
network-policy : 网络划分,与公司内部网段划分有关。
* network : 具体的网段划分。 例如 <network id="10.8.101.0/24"/> 对应在 <server-group id="sh_group1" title="上海机房一组"/> 中寻找最佳路由。
server-group : CAT 集群按地域、机房等策略划分,根据不同场景需求按需配置。
# 应用定向路由 & 动态调整
使用路由配置中 domain 配置项可以针对具体应用指定路由,常用于流量均衡调整、灰度测试等。
上述路由配置示例中应用 service-1 配置如下:
<domain id="service-1">
<group id="default">
<server id="10.8.101.1" port="2280" weight="1.0"/>
<server id="10.8.101.2" port="2280" weight="1.0"/>
<server id="10.8.101.3" port="2280" weight="1.0"/>
</group>
<group id="shanghai">
<server id="10.8.101.1" port="2280" weight="1.0"/>
<server id="10.8.101.2" port="2280" weight="1.0"/>
<server id="10.8.101.3" port="2280" weight="1.0"/>
</group>
<group id="beijing">
<server id="10.2.170.1" port="2280" weight="1.0"/>
<server id="10.2.170.3" port="2280" weight="1.0"/>
<server id="10.2.170.5" port="2280" weight="1.0"/>
</group>
</domain>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
如果应用 service-1 集群中某个 IP 属于 beijing 的 server-group ,那么将优先使用 beijing 分组所提供的路由列表。