Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gRPC 转 Rest 的几种实现 #1

Open
jxlwqq opened this issue Dec 29, 2021 · 3 comments
Open

gRPC 转 Rest 的几种实现 #1

jxlwqq opened this issue Dec 29, 2021 · 3 comments

Comments

@jxlwqq
Copy link
Owner

jxlwqq commented Dec 29, 2021

@jxlwqq jxlwqq changed the title gPRC 转换 Rest 的几种实现 gPRC 转 Rest 的几种实现 Dec 29, 2021
@jxlwqq jxlwqq changed the title gPRC 转 Rest 的几种实现 gRPC 转 Rest 的几种实现 Dec 29, 2021
@jxlwqq
Copy link
Owner Author

jxlwqq commented Dec 29, 2021

Envoy Filter gRPC-JSON Transcoder Demo:

apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: myapp-grpc-transcoder
spec:
  workloadSelector:
    labels:
      app: myapp # match your deployment
  configPatches:
  - applyTo: HTTP_FILTER
    match:
      context: SIDECAR_INBOUND
      listener:
        filterChain:
          filter:
            name: envoy.filters.network.http_connection_manager
            subFilter:
              name: envoy.filters.http.router
        portNumber: 50051 # application port
    patch:
      operation: INSERT_BEFORE
      value:
        name: envoy.filters.http.grpc_json_transcoder
        typed_config:
          "@type": type.googleapis.com/envoy.extensions.filters.http.grpc_json_transcoder.v3.GrpcJsonTranscoder
          services:
          - mycompany.myapi.v1.MyService # your gRPC service
          print_options:
            add_whitespace: true
            always_print_enums_as_ints: false
            always_print_primitive_fields: true
            preserve_proto_field_names: false
          convert_grpc_status: true
          proto_descriptor_bin: # base64 encoded proto descriptor

参考:

istio/istio#7652

https://www.tetrate.io/blog/grpc-transcoding-with-istio/

需要设置 proto_descriptor_bin,跟业务逻辑有一定的耦合,如果 proto 文件发生变更,EnvoyFilter 也要跟着更新。

性能方面存疑,没有具体测试过。

@jxlwqq
Copy link
Owner Author

jxlwqq commented Jan 6, 2022

本 demo 使用 gRPC Gateway,部分代码如下:

// Start HTTP server
logger.Infof("HTTP Listening on port %s", conf.Blog.Server.HTTP.Port)
httpMux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
if err = v1.RegisterBlogServiceHandlerFromEndpoint(context.Background(), httpMux, conf.Blog.Server.GRPC.Port, opts); err != nil {
logger.Fatal(err)
}
httpServer := &http.Server{
Addr: conf.Blog.Server.HTTP.Port,
Handler: httpMux,
}
go func() {
if err = httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Fatal(err)
}
}()

@jxlwqq
Copy link
Owner Author

jxlwqq commented Jan 10, 2022

Kong 插件在 Kubernetes 集群的使用如下:

apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
  name: <grpc-gateway-example>
config: 
  proto: path/to/hello.proto
plugin: grpc-gateway
---
apiVersion: networking/v1beta1
kind: Ingress
metadata:
  name: {ROUTE}
  annotations:
    kubernetes.io/ingress.class: kong
    konghq.com/plugins: <grpc-gateway-example>
spec:
  rules:
  - host: examplehostname.com
    http:
      paths:
      - path: /bar
        backend:
          serviceName: echo
          servicePort: 80

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant