Skip to content
프로젝트

소개

gRPC 호출을 유연하게 라우팅하면서 오버헤드는 최소화하는 Go 기반 리버스 프록시입니다.

시스템 및 인프라

이 수치를 측정하는 방법

측정된 스타 증가

in 24h
0
in 7 days
+1
in 15 days
+1
in 1 month
-1

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original

gRPC Proxy

Travis Build Go Report Card Go Reference Apache 2.0 License

gRPC Go Proxy server

Project Goal

Build a transparent reverse proxy for gRPC targets that will make it easy to expose gRPC services over the internet. This includes:

  • no needed knowledge of the semantics of requests exchanged in the call (independent rollouts)
  • easy, declarative definition of backends and their mappings to frontends
  • simple round-robin load balancing of inbound requests from a single connection to multiple backends

The project now exists as a proof of concept, with the key piece being the proxy package that is a generic gRPC reverse proxy handler.

Proxy Handler

The package proxy contains a generic gRPC reverse proxy handler that allows a gRPC server to not know about registered handlers or their data types. Please consult the docs, here's an exaple usage.

You can call proxy.NewProxy to create a *grpc.Server that proxies requests.

director = func(ctx context.Context, fullMethodName string) (context.Context, *grpc.ClientConn, error) {
    // Make sure we never forward internal services.
    if strings.HasPrefix(fullMethodName, "/com.example.internal.") {
        return nil, nil, status.Errorf(codes.Unimplemented, "Unknown method")
    }
    md, ok := metadata.FromIncomingContext(ctx)

    if ok {
        // Decide on which backend to dial
        if val, exists := md[":authority"]; exists && val[0] == "staging.api.example.com" {
            // Make sure we use DialContext so the dialing can be cancelled/time out together with the context.
            conn, err := grpc.DialContext(ctx, "api-service.staging.svc.local", grpc.WithCodec(proxy.Codec()))
            return ctx, conn, err
        } else if val, exists := md[":authority"]; exists && val[0] == "api.example.com" {
            conn, err := grpc.DialContext(ctx, "api-service.prod.svc.local", grpc.WithCodec(proxy.Codec()))
            return ctx, conn, err
        }
    }
    return nil, nil, status.Errorf(codes.Unimplemented, "Unknown method")
}

Then you need to register it with a grpc.Server. The server may have other handlers that will be served locally.

server := grpc.NewServer(
    grpc.CustomCodec(proxy.Codec()),
    grpc.UnknownServiceHandler(proxy.TransparentHandler(director)))
pb_test.RegisterTestServiceServer(server, &testImpl{})

Testing

To make debugging a bit simpler, there are some helpers.

testservice contains a method TestTestServiceServerImpl which performs a complete test against the reference implementation of the TestServiceServer.

In proxy_test.go, the test framework spins up a TestServiceServer that it tests the proxy against. To make debugging a bit simpler (eg. if the developer needs to step into google.golang.org/grpc methods), this TestServiceServer can be provided by a server by passing -test-backend=addr to go test. A simple, local-only implementation of TestServiceServer exists in testservice/server.

License

grpc-proxy is released under the Apache 2.0 license. See LICENSE.txt.

관련 프로젝트 살펴보기

불러오는 중…