forked from bfenetworks/bfe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e5f8b2d
commit 5dcc39f
Showing
2 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
# 分流转发 | ||
|
||
## 场景说明 | ||
|
||
* 假设我们有一个http server对外提供服务,并且有2个服务实例;1个负责处理静态文件请求,另外1个负责处理动态请求 | ||
* 域名:example.org | ||
* 以/static开头的请求都转发至静态文件服务实例;地址:10.0.0.1:8001 | ||
* 其他的请求都转发至动态服务实例;地址:10.0.0.1:8002 | ||
|
||
在[样例配置](../../../conf/)上稍做修改,就可以实现上述转发功能 | ||
|
||
* 首先,在[bfe.conf](../../../conf/bfe.conf)上配置转发功能使用的配置文件路径 | ||
|
||
``` | ||
hostRuleConf = server_data_conf/host_rule.data #域名规则配置文件 | ||
routeRuleConf = server_data_conf/route_rule.data #分流规则配置文件 | ||
clusterConf = server_data_conf/cluster_conf.data #集群配置文件 | ||
clusterTableConf = cluster_conf/cluster_table.data #集群实例列表配置文件 | ||
gslbConf = cluster_conf/gslb.data #子集群负载均衡配置文件 | ||
``` | ||
|
||
* 配置域名规则([server_data_conf/host_rule.data](../../../conf/server_data_conf/host_rule.data)) | ||
* 将example.org域名关联到产品线example_product | ||
|
||
``` | ||
{ | ||
"Version": "init version", | ||
"DefaultProduct": null, | ||
"Hosts": { | ||
"exampleTag":[ | ||
"example.org" // 域名example.org=>域名标签exampleTag | ||
] | ||
}, | ||
"HostTags": { | ||
"example_product":[ | ||
"exampleTag" // 域名标签exampleTag=>产品线名称example_product | ||
] | ||
} | ||
} | ||
``` | ||
|
||
* 配置集群的基础信息([server_data_conf/cluster_conf.data](../../../conf/server_data_conf/cluster_conf.data)) | ||
* 配置集群cluster_demo_static和cluster_demo_dynamic健康检查的参数,其他均使用默认值 | ||
|
||
``` | ||
{ | ||
"Version": "init version", | ||
"Config": { | ||
"cluster_demo_static": { // 集群cluster_demo_static的配置 | ||
"CheckConf": { // 健康检查配置 | ||
"Schem": "http", | ||
"Uri": "/health_check", | ||
"Host": "example.org", | ||
"StatusCode": 200 | ||
} | ||
}, | ||
"cluster_demo_dynamic": { // 集群cluster_demo_dynamic的配置 | ||
"CheckConf": { // 健康检查配置 | ||
"Schem": "http", | ||
"Uri": "/health_check", | ||
"Host": "example.org", | ||
"StatusCode": 200 | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
* 配置集群下挂载的实例信息([cluster_conf/cluster_table.data](../../../conf/cluster_conf/cluster_table.data)) | ||
* 在cluster_demo_static和cluster_demo_dynamic集群下面分别创建子集群demo_static.all和demo_dynamic.all | ||
* 将静态文件服务实例10.0.0.1:8001挂载到demo_static.all子集群,动态服务实例10.0.0.1:8002挂载到demo_dynamic.all子集群 | ||
|
||
``` | ||
{ | ||
"Version": "init version", | ||
"Config": { | ||
"cluster_demo_static": { // 集群 => 子集群 => 实例列表 | ||
"demo_static.all": [{ // 子集群demo_static.all | ||
"Addr": "10.0.0.1", // 实例地址:10.0.0.1 | ||
"Name": "static.A", // 实例名:static.A | ||
"Port": 8001, // 实例端口:8001 | ||
"Weight": 1 // 实例权重:1 | ||
}] | ||
}, | ||
"cluster_demo_dynamic": { | ||
"demo_dynamic.all": [{ | ||
"Addr": "10.0.0.1", | ||
"Name": "dynamic.A", | ||
"Port": 8002, | ||
"Weight": 1 | ||
}] | ||
} | ||
} | ||
} | ||
``` | ||
|
||
* 配置子集群内负载均衡([cluster_conf/gslb.data](../../../conf/cluster_conf/gslb.data)) | ||
* cluster_demo_static集群的流量全部转发到demo_static.all子集群 | ||
* cluster_demo_dynamic集群的全部流量全部转发到demo_dynamic.all子集群 | ||
|
||
``` | ||
{ | ||
"Hostname": "", | ||
"Ts": "0", | ||
"Clusters": { | ||
"cluster_demo_static": { // 集群 => 子集群权重 | ||
"GSLB_BLACKHOLE": 0, // 黑洞的分流权重为0,表示不丢弃流量 | ||
"demo_static.all": 100 // 权重为100,表示全部分流到demo_static.all | ||
}, | ||
"cluster_demo_dynamic": { | ||
"GSLB_BLACKHOLE": 0, | ||
"demo_dynamic.all": 100 | ||
} | ||
} | ||
} | ||
``` | ||
|
||
* 配置分流规则([server_data_conf/route_rule.data](../../../conf/server_data_conf/route_rule.data)) | ||
* 将/static开头的流量转发到cluster_demo_static集群 | ||
* 其余流量转发到cluster_demo_dynamic集群 | ||
|
||
``` | ||
{ | ||
"Version": "init version", | ||
"ProductRule": { | ||
"example_product": [ // 产品线 => 分流规则 | ||
{ | ||
// 以/static开头的path分流到cluster_demo_static集群 | ||
"Cond": "req_path_prefix_in(\"/static\", false)", | ||
"ClusterName": "cluster_demo_static" | ||
}, | ||
{ | ||
// 其他流量分流到cluster_demo_dynamic集群 | ||
"Cond": "default_t()", | ||
"ClusterName": "cluster_demo_dynamic" | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
|
||
现在,用curl验证下是否可以转发成功 | ||
|
||
curl -H "host: example.org" "http://127.1:8080/static/test.html" 将请求转发至10.0.0.1:8001 | ||
|
||
curl -H "host: example.org" "http://127.1:8080/api/test" 将请求转发至10.0.0.1:8002 |