38 lines
965 B
Go
38 lines
965 B
Go
package dialer
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
|
|
"pkg.local/discovery"
|
|
|
|
"github.com/zeromicro/go-zero/zrpc"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
)
|
|
|
|
// Nacos builds a zrpc client whose dials resolve via discovery.Pick.
|
|
func Nacos(conf zrpc.RpcClientConf) zrpc.Client {
|
|
serviceName := conf.Target
|
|
if serviceName == "" {
|
|
panic("gateway dialer: Grpc.Target (nacos serviceName) is required")
|
|
}
|
|
|
|
cliConf := conf
|
|
cliConf.Target = "passthrough:///nacos/" + serviceName
|
|
cliConf.Endpoints = nil
|
|
|
|
return zrpc.MustNewClient(cliConf,
|
|
zrpc.WithDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
|
|
zrpc.WithDialOption(grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) {
|
|
inst, err := discovery.Pick(serviceName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var d net.Dialer
|
|
return d.DialContext(ctx, "tcp", net.JoinHostPort(inst.IP, fmt.Sprintf("%d", inst.Port)))
|
|
})),
|
|
)
|
|
}
|