ce7aa18477
CI / changes (push) Successful in 12s
CI / ad (push) Successful in 1s
CI / admin (push) Successful in 0s
CI / bff (push) Failing after 25s
CI / chore (push) Successful in 1s
CI / express (push) Successful in 0s
CI / product (push) Successful in 27s
CI / sale (push) Successful in 0s
CI / user (push) Successful in 0s
CI / wecom (push) Successful in 25s
43 lines
1.2 KiB
Go
43 lines
1.2 KiB
Go
package dialer
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"lone-services/bff/internal/response"
|
|
"lone-services/pkg/discovery"
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/zeromicro/go-zero/zrpc"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
)
|
|
|
|
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.WithUnaryInterceptor(response.UserClientInterceptor)),
|
|
zrpc.WithDialOption(grpc.WithDefaultCallOptions(grpc.WaitForReady(true))),
|
|
zrpc.WithDialOption(grpc.WithConnectParams(grpc.ConnectParams{
|
|
MinConnectTimeout: 2 * time.Second,
|
|
})),
|
|
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)))
|
|
})),
|
|
)
|
|
}
|