package
0.0.5
Repository: https://github.com/lhdhtrc/proxy-go.git
Documentation: pkg.go.dev

# README

Grpc Proxy Grpc

Simple implementation grpc agent grpc, used on the gateway

How to use it?

package main

import (
	"context"
	grpcProxy "github.com/lhdhtrc/microservice-go/proxy/grpc"
	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/encoding"
	"google.golang.org/grpc/metadata"
	"google.golang.org/grpc/status"
	"net"
)

func main() {
	director := func(ctx context.Context, fullMethodName string) (context.Context, *grpc.ClientConn, error) {
		// 服务发现的服务列表
		endPoint := make(map[string][]string)

		// 根据fullMethodName获取可用节点
		nodes, ok := endPoint[fullMethodName]

		var cc *grpc.ClientConn
		err := status.Errorf(codes.Unimplemented, "Unknown method")

		if ok && len(nodes) != 0 {
			md, _ := metadata.FromIncomingContext(ctx)
			ctx = metadata.NewOutgoingContext(ctx, md.Copy())

			cc, err = grpc.DialContext(ctx, nodes[0])
		}

		return ctx, cc, err
	}

	listen, _ := net.Listen("tcp", 8080)
	server := grpc.NewServer(grpc.UnknownServiceHandler(grpcProxy.TransparentHandler(director)))
	_ = server.Serve(listen)
}

Finally

  • If you feel good, click on star.
  • If you have a good suggestion, please ask the issue.

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
RegisterService 注册代理服务.
TransparentHandler 提供一个透明代理的方式,将未知的服务请求转发到后端服务,返回处理程序可以作为grpc.UnknownServiceHandler使用.

# Type aliases

No description provided by the author