pixiu 0.6.0 虽然艰辛,但在社区小伙伴的努力下发布了。这个版本不止有常规社区功能,还有很多功能特性是来源于ASoC-2022 Alibaba Summer of Code,OSPP(中科院软件所[开源软件供应链点亮计划]) , GSoC(Google Summer of Code) 提案内容。
New Features In v0.6.0
nacos config
背景:pixiu 的配置,启动配置项(conf.yaml),日志配置(log.yml)均是从本地文件加载,没有接入动态配置中心的能力,而日常生产环境中,配置通过配置中心集中下发管理是更为灵活和安全的方式之一。
动态配置下发的场景还有很多扩展,接入的组件也因公司的选型有所不同,当前我们主要考虑接入 nacos 配置中心。
注:在设计上可以考虑,如何在二次开发场景下更为“标准、安全、简单”的接入其他配置中心。
Pixiu 启动时从配置中心Nacos 拉取配置 conf.yaml 数据
pixiu.conf的配置不用配置static_resources,需要配置config-center和nacos:
config-center:
type: nacos
enable: true
nacos:
server-configs:
- ip_addr: "127.0.0.1"
port: 8848
client-config:
username: nacos
password: nacos
原来我们熟悉pixiu.conf配置内容放到了nacos中:
static_resources:
listeners:
- name: "net/http"
protocol_type: "HTTP"
address:
socket_address:
address: "0.0.0.0"
port: 8881
filter_chains:
filters:
- name: dgp.filter.httpconnectionmanager
config:
route_config:
routes:
- match:
prefix: "*"
http_filters:
- name: dgp.filter.http.dubboproxy
config:
dubboProxyConfig:
auto_resolve: true
registries:
"zookeeper":
protocol: "zookeeper"
timeout: "3s"
address: "127.0.0.1:2181"
username: ""
password: ""
timeout_config:
connect_timeout: 5s
request_timeout: 5s
server_name: "test_http_dubbo"
generate_request_id: false
config:
idle_timeout: 5s
read_timeout: 5s
write_timeout: 5s
shutdown_config:
timeout: "60s"
step_timeout: "10s"
reject_policy: "immediacy"
更详细的配置参考samples: https://github.com/apache/dubbo-go-pixiu-samples/tree/main/dubbogo/simple/farconfnacos
nacos配置支持还需要进一步完善的功能:
- 动态托管/下发用户自定义配置
- 动态下发Pixiu配置
OSPP: Traffic Distribution
流量分发提供两种策略,基于 Request Header 和服务权重的流量切分。
canary-by-header
canary-by-header是要匹配的 Request Header 的值,当 Request Header 设置此值时,它将被路由到相应的cluster(cluster中绑定canary-by-header键值)。当请求不包含这一 Request Header 或 Header 值不匹配时,请求不会被发送到Canary版本,该策略适用于蓝绿发布以及A/B测试
canary-weight
这种策略适用于金丝雀部署,权重范围 0 - 100 按百分比将请求路由到指定的服务。权重为 0 意味着该金丝雀规则不会向 Canary 服务发送任何请求;权重为 100 意味着所有请求都将被发送到 Canary。
Add Graceful Shutdown
通过graceful shutdown 能力,在进程获得系统型号:SIGINT, SIGKILL, SIGTERM信号时执行graceful shutdown过程:拒绝新的请求;等待处理中请求完成直到timeout.
static_resources:
.......
.......
shutdown_config:
timeout: "60s"
step_timeout: "10s"
reject_policy: "immediacy"
配置方式参考: https://github.com/apache/dubbo-go-pixiu-samples/tree/main/shutdown
WASM Plugin for Pixiu
Pixiu 支持 WASM 插件机制,实现http fitler。
PIxiu与WASM扩展代码交互采取 Proxy-Wasm 规范,该规范是开源社区针对「网络代理场景」设计的一套 ABI 规范,采用该规范能让PIxiu复用社区既有的WASM扩展。
WASM原则上应该支持多种语言,这里以Go语言为例,最终通过tinygo编译成wasm文件。如果要实现我们的扩展WASM程序,需要按以下模版进行编码实现:
package main
import (
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
)
func main() {
proxywasm.SetNewHttpContext(newHttpContext)
}
type myHttpContext struct {
// you must embed the default context so that you need not to re-implement all the methods by yourself
proxywasm.DefaultHttpContext
contextID uint32
}
func newHttpContext(rootContextID, contextID uint32) proxywasm.HttpContext {
return &myHttpContext{contextID: contextID}
}
// override
func (ctx *myHttpContext) OnHttpRequestHeaders(numHeaders int, endOfStream bool) types.Action {
return types.ActionContinue
}
deploy pixiu as dubbo service ingress gateway in k8s istio
当dubbo部署istio + k8s环境中,pixiu可以作为dubbo provider的ingress gateway,接收dubbo/triple/http的流量,并将请求转发到dubbo provider中。
samples: https://github.com/apache/dubbo-go-pixiu-samples/tree/main/xds/dubbo-go-istio
ASoC 2022: Pixiu Metrics Implementation
Pixiu Metrics提供四个prometheus运行指标:
var reqCnt = &Metric{
ID: "reqCnt",
Name: "requests_total",
Description: "How many HTTP requests processed, partitioned by status code and HTTP method.",
Type: "counter_vec",
Args: []string{"code", "method", "host", "url"},
}
var reqDur = &Metric{
ID: "reqDur",
Name: "request_duration_seconds",
Description: "The HTTP request latencies in seconds.",
Args: []string{"code", "method", "url"},
Type: "histogram_vec",
Buckets: reqDurBuckets,
}
var resSz = &Metric{
ID: "resSz",
Name: "response_size_bytes",
Description: "The HTTP response sizes in bytes.",
Args: []string{"code", "method", "url"},
Type: "histogram_vec",
Buckets: resSzBuckets,
}
var reqSz = &Metric{
ID: "reqSz",
Name: "request_size_bytes",
Description: "The HTTP request sizes in bytes.",
Args: []string{"code", "method", "url"},
Type: "histogram_vec",
Buckets: reqSzBuckets,
}
var standardMetrics = []*Metric{
reqCnt,
reqDur,
resSz,
reqSz,
}
config配置http_filters: dgp.filter.http.prometheusmetric:
static_resources:
listeners:
- name: "net/http"
protocol_type: "HTTP"
address:
socket_address:
address: "0.0.0.0"
port: 8888
filter_chains:
filters:
- name: dgp.filter.httpconnectionmanager
config:
route_config:
routes:
- match:
prefix: /health
route:
cluster: user
cluster_not_found_response_code: 505
- match:
prefix: /user
route:
cluster: user
cluster_not_found_response_code: 505
- match:
prefix: /prefix
route:
cluster: user
cluster_not_found_response_code: 505
http_filters:
- name: dgp.filter.http.prometheusmetric
metric_collect_rules:
metric_path: "/metrics"
push_gateway_url: "http://127.0.0.1:9091"
counter_push: true
push_interval_threshold: 3
push_job_name: "prometheus"
- name: dgp.filter.http.httpproxy
config:
idle_timeout: 5s
read_timeout: 5s
write_timeout: 5s
clusters:
- name: "user"
lb_policy: "lb"
endpoints:
- id: 1
socket_address:
address: 127.0.0.1
port: 1314
health_checks:
- protocol: "tcp"
timeout: 1s
interval: 2s
healthy_threshold: 4
unhealthy_threshold: 4
shutdown_config:
timeout: "60s"
step_timeout: "10s"
reject_policy: "immediacy"
sample参考: https://github.com/apache/dubbo-go-pixiu-samples/tree/main/dubbogo/simple/prometheus
feat:consistent hashing
为cluster 提供基于一致性hash的请求负载算法;
clusters:
- name: "test_dubbo"
lb_policy: "ConsistentHashing"
registries:
"zookeeper":
timeout: "3s"
address: "127.0.0.1:2181"
username: ""
password: ""
Enhancement in v0.6.0
Remove "Types" on Http to dubbo proxy
在通过http调用dubbo服务的场景中,v0.6.0以前版本需要再请求中包含"types": ...., 来描述dubbo parameters的结构描述,示例 :{"types":"string"....
func TestPost1(t *testing.T) {
url := "http://localhost:8883/UserService/com.dubbogo.pixiu.UserService/GetUserByName"
data := "{"types":"string","values":"tc"}"
client := &http.Client{Timeout: 5 * time.Second}
req, err := http.NewRequest("POST", url, strings.NewReader(data))
req.Header.Set("x-dubbo-http1.1-dubbo-version", "1.0.0")
req.Header.Set("x-dubbo-service-protocol", "dubbo")
req.Header.Set("x-dubbo-service-version", "1.0.0")
req.Header.Set("x-dubbo-service-group", "test")
assert.NoError(t, err)
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
assert.NoError(t, err)
assert.NotNil(t, resp)
assert.Equal(t, 200, resp.StatusCode)
s, _ := ioutil.ReadAll(resp.Body)
assert.True(t, strings.Contains(string(s), "0001"))
}
改进之后data中不需要types数据。
sample 代码: https://github.com/apache/dubbo-go-pixiu-samples/blob/main/dubbogo/simple/direct/test/pixiu_test.go
ASoC 2002: Optimization of Pixiu timeout feature
dgp.filter.http.dubboproxy 支持超时配置:
..........
.........
- name: dgp.filter.http.dubboproxy
config:
dubboProxyConfig:
registries:
"zookeeper":
protocol: "zookeeper"
timeout: "3s"
address: "127.0.0.1:2181"
username: ""
password: ""
timeout_config:
connect_timeout: 5s
request_timeout: 5s
欢迎钉钉扫码加入 dubbogo 社区钉钉群【钉钉群号 23331795】进行交流。