快速开始

让我们从将 HTTP 请求转换为 Dubbo2 请求的案例来快速展示 Pixiu 的能力。

用例

Pixiu 将 Client 的 HTTP 请求转换为 Dubbo2 请求,然后转发给背后的 Dubbo Server,然后将 Dubbo Server 的响应转换为 HTTP 响应,最后返回给 Client。

架构图

Architecture

案例

案例路径请查看 /samples/dubbogo/simple/resolve

Dubbo Server 实现和启动

Dubbo Server 提供用户增删改查的相关接口,其具体的代码实现见案例路径下的 server

Dubbo Server 的配置如下所示,注册了 Dubbo2 协议的 interface com.dubbogo.pixiu.UserService

  1. dubbo:
  2. registries:
  3. zk:
  4. protocol: zookeeper
  5. timeout: 3s
  6. address: 127.0.0.1:2181
  7. protocols:
  8. dubbo:
  9. name: dubbo
  10. port: 20000
  11. provider:
  12. registry-ids: zk
  13. services:
  14. UserProvider:
  15. group: test
  16. version: 1.0.0
  17. cluster: test_dubbo
  18. serialization: hessian2
  19. interface: com.dubbogo.pixiu.UserService

Pixiu 配置和启动

为了用例的场景,Pixiu 需要启动对应的 HTTP Listener 进行 HTTP 请求的监听,所以就会使用到 httpconnectionmanager。 然后因为要将 HTTP 请求转换为 Dubbo请求,所以需要使用 dgp.filter.http.dubboproxy,这里我们将其auto_resolve 设置为true,表示开启 HTTP to Dubbo 默认转换协议(具体定义请看附录)。

Pixiu 的具体配置如下所示

  1. static_resources:
  2. listeners:
  3. - name: "net/http"
  4. protocol_type: "HTTP"
  5. address:
  6. socket_address:
  7. address: "0.0.0.0"
  8. port: 8883
  9. filter_chains:
  10. filters:
  11. - name: dgp.filter.httpconnectionmanager
  12. config:
  13. route_config:
  14. routes:
  15. - match:
  16. prefix: "*"
  17. http_filters:
  18. - name: dgp.filter.http.dubboproxy
  19. config:
  20. dubboProxyConfig:
  21. auto_resolve: true
  22. registries:
  23. "zookeeper":
  24. protocol: "zookeeper"
  25. timeout: "3s"
  26. address: "127.0.0.1:2181"
  27. username: ""
  28. password: ""

Client 实现

Client 就是简单的 HTTP Client 实现,但是需要按照前文提及的 HTTP to Dubbo 默认转换协议在 HTTP 请求的 Path 和 Header 中填入对应的数据,具体如下所示。

  1. url := "http://localhost:8883/UserService/com.dubbogo.pixiu.UserService/GetUserByName"
  2. data := "{\"types\":\"string\",\"values\":\"tc\"}"
  3. client := &http.Client{Timeout: 5 * time.Second}
  4. req, err := http.NewRequest("POST", url, strings.NewReader(data))
  5. req.Header.Set("x-dubbo-http1.1-dubbo-version", "1.0.0")
  6. req.Header.Set("x-dubbo-service-protocol", "dubbo")
  7. req.Header.Set("x-dubbo-service-version", "1.0.0")
  8. req.Header.Set("x-dubbo-service-group", "test")
  9. assert.NoError(t, err)
  10. req.Header.Add("Content-Type", "application/json")
  11. resp, err := client.Do(req)

案例启动

项目提供了快速启动脚本,需要本地先安装有 Go 语言开发环境。

  1. # cd 到案例总目录
  2. cd samples/dubbogo/simple/
  3. # 进行环境准备,启动 zk 和准备对应配置文件
  4. ./start.sh prepare resolve
  5. # 启动 dubbo server
  6. ./start.sh startServer resolve
  7. # 启动 pixiu
  8. ./start.sh startPixiu resolve
  9. # 启动 Client 测试用例
  10. ./start.sh startTest resolve
  11. # 或者使用 curl
  12. curl -X POST 'http://localhost:8883/UserService/com.dubbogo.pixiu.UserService/GetUserByName' -d '{"types":"string","values":"tc"}' -H 'Content-Type: application/json' -H 'x-dubbo-http1.1-dubbo-version':'1.0.0' -H 'x-dubbo-service-protocol':"dubbo" -H 'x-dubbo-service-version':'1.0.0' -H 'x-dubbo-service-group':'test'
  13. # 返回值 {"age":15,"code":1,"iD":"0001","name":"tc","time":"2021-08-01T18:08:41+08:00"}

docker示例

  1. docker pull phial3/dubbo-go-pixiu:latest
  2. docker run --name pixiuname -p 8883:8883 \
  3. -v /yourpath/conf.yaml:/etc/pixiu/conf.yaml \
  4. -v /yourpath/log.yml:/etc/pixiu/log.yml \
  5. apache/dubbo-go-pixiu:latest
  6. # http请求调用dubbo服务转换,首先启动provider,这里使用zookeeper作为注册中心
  7. cd samples/dubbogo/simple/resolve/server
  8. # 添加需要的环境变量,指定provider的配置文件位置
  9. export DUBBO_GO_CONFIG_PATH="../profiles/dev/server.yml"
  10. export APP_LOG_CONF_FILE="../profiles/dev/log.yml"
  11. # 启动provider
  12. go run server.go user.go
  13. # 进入到test目录下,启动test示例
  14. cd samples/dubbogo/simple/resolve/test
  15. go test pixiu_test.go

最后修改 December 16, 2022: Fix check (#1736) (97972c1)