路由配置

路由的作用

路由配置是 CAT 集群流量负载均衡最重要的一环。路由配置提供了默认机器列表、多机房划分、地域划分等基本功能,路由选择的优先级为:同机房 > 同地域 > 默认机器 > 备用机器。

对于流量较高的机器,用户可以扩展相应的策略,通过路由配置将部分流量指定到低负载的机器。

配置入口

http:///localhost:8080/s/config?op=routerConfigUpdate

配置示例说明

通过一个实际生产环境的场景来说明路由配置的最佳实践:

  1. CAT 集群信息

路由配置 - 图1

  1. 路由配置示例
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <router-config backup-server="10.8.101.1" backup-server-port="2280" backup-server-http-port="8080">
  3. <!-- sh_group1 -->
  4. <default-server id="10.8.101.1" weight="1.0" port="2280" enable="true" http-port="8080"/>
  5. <default-server id="10.8.101.2" weight="1.0" port="2280" enable="true" http-port="8080"/>
  6. <default-server id="10.8.101.3" weight="1.0" port="2280" enable="true" http-port="8080"/>
  7. <default-server id="10.8.101.4" weight="1.0" port="2280" enable="true" http-port="8080"/>
  8. <default-server id="10.8.101.5" weight="1.0" port="2280" enable="true" http-port="8080"/>
  9. <!-- sh_group2 -->
  10. <default-server id="10.9.101.1" weight="1.0" port="2280" enable="true" http-port="8080"/>
  11. <default-server id="10.9.101.2" weight="1.0" port="2280" enable="true" http-port="8080"/>
  12. <default-server id="10.9.101.3" weight="1.0" port="2280" enable="true" http-port="8080"/>
  13. <default-server id="10.9.101.4" weight="1.0" port="2280" enable="true" http-port="8080"/>
  14. <default-server id="10.9.101.5" weight="1.0" port="2280" enable="true" http-port="8080"/>
  15. <!-- bj_group1 -->
  16. <default-server id="10.2.170.1" weight="1.0" port="2280" enable="true" http-port="8080"/>
  17. <default-server id="10.2.170.2" weight="1.0" port="2280" enable="true" http-port="8080"/>
  18. <default-server id="10.2.170.3" weight="1.0" port="2280" enable="true" http-port="8080"/>
  19. <default-server id="10.2.170.4" weight="1.0" port="2280" enable="true" http-port="8080"/>
  20. <default-server id="10.2.170.5" weight="1.0" port="2280" enable="true" http-port="8080"/>
  21. <!-- bj_group2 -->
  22. <default-server id="10.3.170.1" weight="1.0" port="2280" enable="true" http-port="8080"/>
  23. <default-server id="10.3.170.2" weight="1.0" port="2280" enable="true" http-port="8080"/>
  24. <default-server id="10.3.170.3" weight="1.0" port="2280" enable="true" http-port="8080"/>
  25. <default-server id="10.3.170.4" weight="1.0" port="2280" enable="true" http-port="8080"/>
  26. <default-server id="10.3.170.5" weight="1.0" port="2280" enable="true" http-port="8080"/>
  27. <network-policy id="shanghai" title="default" block="false" server-group="shanghai">
  28. <network id="10.8.101.0/24"/>
  29. <network id="10.8.102.0/24"/>
  30. <network id="10.9.101.0/24"/>
  31. <network id="10.9.102.0/24"/>
  32. </network-policy>
  33. <network-policy id="beijing" title="default" block="false" server-group="beijing">
  34. <network id="10.2.170.0/24"/>
  35. <network id="10.2.171.0/24"/>
  36. <network id="10.3.170.1/24"/>
  37. <network id="10.3.171.1/24"/>
  38. </network-policy>
  39. <network-policy id="sh_group1" title="default" block="false" server-group="sh_group1">
  40. <network id="10.8.101.0/24"/>
  41. <network id="10.8.102.0/24"/>
  42. </network-policy>
  43. <network-policy id="sh_group2" title="default" block="false" server-group="sh_group1">
  44. <network id="10.9.101.0/24"/>
  45. <network id="10.9.102.0/24"/>
  46. </network-policy>
  47. <network-policy id="bj_group1" title="default" block="false" server-group="bj_group1">
  48. <network id="10.2.170.0/24"/>
  49. <network id="10.2.171.0/24"/>
  50. </network-policy>
  51. <network-policy id="bj_group2" title="default" block="false" server-group="bj_group1">
  52. <network id="10.3.170.0/24"/>
  53. <network id="10.3.171.0/24"/>
  54. </network-policy>
  55. <network-policy id="default" title="default" block="false" server-group="default">
  56. <network id="10.8.101.0/24"/>
  57. <network id="10.3.170.0/24"/>
  58. </network-policy>
  59. <server-group id="default" title="默认机器">
  60. <group-server id="10.8.101.1"/>
  61. <group-server id="10.8.101.2"/>
  62. <group-server id="10.3.170.1"/>
  63. <group-server id="10.3.170.2"/>
  64. </server-group>
  65. <server-group id="sh_group1" title="上海机房一组">
  66. <group-server id="10.8.101.1"/>
  67. <group-server id="10.8.101.2"/>
  68. <group-server id="10.8.101.3"/>
  69. <group-server id="10.8.101.4"/>
  70. <group-server id="10.8.101.5"/>
  71. </server-group>
  72. <server-group id="sh_group2" title="上海机房二组">
  73. <group-server id="10.9.101.1"/>
  74. <group-server id="10.9.101.2"/>
  75. <group-server id="10.9.101.3"/>
  76. <group-server id="10.9.101.4"/>
  77. <group-server id="10.9.101.5"/>
  78. </server-group>
  79. <server-group id="bj_group1" title="北京机房一组">
  80. <group-server id="10.2.170.1"/>
  81. <group-server id="10.2.170.2"/>
  82. <group-server id="10.2.170.3"/>
  83. <group-server id="10.2.170.4"/>
  84. <group-server id="10.2.170.5"/>
  85. </server-group>
  86. <server-group id="bj_group2" title="北京机房二组">
  87. <group-server id="10.3.170.1"/>
  88. <group-server id="10.3.170.2"/>
  89. <group-server id="10.3.170.3"/>
  90. <group-server id="10.3.170.4"/>
  91. <group-server id="10.3.170.5"/>
  92. </server-group>
  93. <server-group id="shanghai" title="上海机器">
  94. <group-server id="10.8.101.1"/>
  95. <group-server id="10.8.101.2"/>
  96. <group-server id="10.8.101.3"/>
  97. <group-server id="10.8.101.4"/>
  98. <group-server id="10.8.101.5"/>
  99. <group-server id="10.9.101.1"/>
  100. <group-server id="10.9.101.2"/>
  101. <group-server id="10.9.101.3"/>
  102. <group-server id="10.9.101.4"/>
  103. <group-server id="10.9.101.5"/>
  104. </server-group>
  105. <server-group id="beijing" title="北京机器">
  106. <group-server id="10.2.170.1"/>
  107. <group-server id="10.2.170.2"/>
  108. <group-server id="10.2.170.3"/>
  109. <group-server id="10.2.170.4"/>
  110. <group-server id="10.2.170.5"/>
  111. <group-server id="10.3.170.1"/>
  112. <group-server id="10.3.170.2"/>
  113. <group-server id="10.3.170.3"/>
  114. <group-server id="10.3.170.4"/>
  115. <group-server id="10.3.170.5"/>
  116. </server-group>
  117. <domain id="service-1">
  118. <group id="default">
  119. <server id="10.8.101.1" port="2280" weight="1.0"/>
  120. <server id="10.8.101.2" port="2280" weight="1.0"/>
  121. <server id="10.8.101.3" port="2280" weight="1.0"/>
  122. </group>
  123. <group id="shanghai">
  124. <server id="10.8.101.1" port="2280" weight="1.0"/>
  125. <server id="10.8.101.2" port="2280" weight="1.0"/>
  126. <server id="10.8.101.3" port="2280" weight="1.0"/>
  127. </group>
  128. <group id="beijing">
  129. <server id="10.2.170.1" port="2280" weight="1.0"/>
  130. <server id="10.2.170.3" port="2280" weight="1.0"/>
  131. <server id="10.2.170.5" port="2280" weight="1.0"/>
  132. </group>
  133. </domain>
  134. <domain id="service-2">
  135. <group id="default">
  136. <server id="10.8.101.1" port="2280" weight="1.0"/>
  137. <server id="10.8.101.3" port="2280" weight="1.0"/>
  138. <server id="10.8.101.5" port="2280" weight="1.0"/>
  139. </group>
  140. <group id="sh_group1">
  141. <server id="10.8.101.1" port="2280" weight="1.0"/>
  142. <server id="10.8.101.3" port="2280" weight="1.0"/>
  143. <server id="10.8.101.5" port="2280" weight="1.0"/>
  144. </group>
  145. <group id="shanghai">
  146. <server id="10.8.101.1" port="2280" weight="1.0"/>
  147. <server id="10.8.101.2" port="2280" weight="1.0"/>
  148. <server id="10.8.101.3" port="2280" weight="1.0"/>
  149. </group>
  150. <group id="bj_group1">
  151. <server id="10.2.170.1" port="2280" weight="1.0"/>
  152. <server id="10.2.170.3" port="2280" weight="1.0"/>
  153. <server id="10.2.170.5" port="2280" weight="1.0"/>
  154. </group>
  155. <group id="beijing">
  156. <server id="10.2.170.1" port="2280" weight="1.0"/>
  157. <server id="10.2.170.2" port="2280" weight="1.0"/>
  158. <server id="10.2.170.3" port="2280" weight="1.0"/>
  159. </group>
  160. </domain>
  161. </router-config>

配置说明:

default-server : CAT 集群机器列表

  1. * enable : true 表示机器处于在线状态,主动对外提供服务;false 表示不对外主动提供服务,可用于定向测试、灰度测试以及备用应急处理。
  2. * weight : 机器权重比例。在定期构建路由报表时,会根据权重以及机器丢失数据量进行路由调整,具体参考 RouterConfigAdjustor

network-policy : 网络划分,与公司内部网段划分有关。

  1. * network : 具体的网段划分。 例如 <network id="10.8.101.0/24"/> 对应在 <server-group id="sh_group1" title="上海机房一组"/> 中寻找最佳路由。

server-group : CAT 集群按地域、机房等策略划分,根据不同场景需求按需配置。

应用定向路由 & 动态调整

使用路由配置中 domain 配置项可以针对具体应用指定路由,常用于流量均衡调整、灰度测试等。

上述路由配置示例中应用 service-1 配置如下:

  1. <domain id="service-1">
  2. <group id="default">
  3. <server id="10.8.101.1" port="2280" weight="1.0"/>
  4. <server id="10.8.101.2" port="2280" weight="1.0"/>
  5. <server id="10.8.101.3" port="2280" weight="1.0"/>
  6. </group>
  7. <group id="shanghai">
  8. <server id="10.8.101.1" port="2280" weight="1.0"/>
  9. <server id="10.8.101.2" port="2280" weight="1.0"/>
  10. <server id="10.8.101.3" port="2280" weight="1.0"/>
  11. </group>
  12. <group id="beijing">
  13. <server id="10.2.170.1" port="2280" weight="1.0"/>
  14. <server id="10.2.170.3" port="2280" weight="1.0"/>
  15. <server id="10.2.170.5" port="2280" weight="1.0"/>
  16. </group>
  17. </domain>

如果应用 service-1 集群中某个 IP 属于 beijing 的 server-group ,那么将优先使用 beijing 分组所提供的路由列表。