Grpc NetWorkFilter 介绍

Grpc NetWorkFilter 介绍

使用 grpc 调用服务提供程序

下面的文档符合代码

定义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: 8881
  9. filter_chains:
  10. filters:
  11. - name: dgp.filter.httpconnectionmanager
  12. config:
  13. route_config:
  14. routes:
  15. - match:
  16. prefix: "/api/v1"
  17. route:
  18. cluster: "test-grpc"
  19. cluster_not_found_response_code: 505
  20. http_filters:
  21. - name: dgp.filter.http.grpcproxy
  22. config:
  23. path: /mnt/d/WorkSpace/GoLandProjects/dubbo-go-pixiu/samples/http/grpc/proto
  24. - name: dgp.filter.http.response
  25. config:
  26. server_name: "test-http-grpc"
  27. generate_request_id: false
  28. config:
  29. idle_timeout: 5s
  30. read_timeout: 5s
  31. write_timeout: 5s
  32. clusters:
  33. - name: "test-grpc"
  34. lb_policy: "RoundRobin"
  35. endpoints:
  36. - socket_address:
  37. address: 127.0.0.1
  38. port: 50001
  39. protocol_type: "GRPC"
  40. timeout_config:
  41. connect_timeout: "5s"
  42. request_timeout: "10s"
  43. shutdown_config:
  44. timeout: "60s"
  45. step_timeout: "10s"
  46. reject_policy: "immediacy"

Grpc 服务器在“集群”中定义

目前 http 请求仅支持 json body 解析参数

准备服务器

generate pb files under with command:

  1. protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative hello_grpc.proto

启动 Server

Run

  1. go run server.go

启动 Pixiu

  1. ./dubbo-go-pixiu gateway start --config {CURRENT_PATH}/samples/http/grpc/pixiu/conf.yaml

使用 curl 进行测试

使用以下命令运行命令 curl:

  1. curl http://127.0.0.1:8881/api/v1/provider.UserProvider/GetUser
  1. curl http://127.0.0.1:8881/api/v1/provider.UserProvider/GetUser -X POST -d '{"userId":1}'

如果响应正文是 json,则Content-Type的标头将设置为application/json。如果它只是一个纯文本,则Content-Type的标题是text/plain

最后修改 September 13, 2024: Refactor website structure (#2860) (1a4b998f54b)