grpc server
定时任务

grpc client拨号核心代码

library/grpc/grpcclient/client.go,使用go原生grpc库"google.golang.org/grpc"

使用k8s的servicename就可以识别,正常连通

const ServiceName = "sx-cp-user-ensure-server"

[[opentelemetry]]的包在这里引入,从而[[sls日志 阿里云]]能有gRPC的调用链数据。

package grpcclient

import (
	"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"

	"github.com/ingtube/common/config"
)

func DefaultDial(serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
	// 只能调用同namespace的服务
	return DialTarget(serviceName+":8080", config.GetPodName(), serviceName, opts...)
}

// DialTarget 直接调用target
// interceptors 如果为 nil,那么使用默认的 interceptors
// 如果需要重试,自行定义
func DialTarget(target string, callerName string, loggerName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
	preOpts := []grpc.DialOption{
		grpc.WithTransportCredentials(insecure.NewCredentials()),
		grpc.WithUserAgent(callerName),
		grpc.WithChainUnaryInterceptor(
			Recovery(),
			otelgrpc.UnaryClientInterceptor(),
			Logging(loggerName),
			Error(), // todo ierror 意义不大了
		),
		grpc.WithDefaultServiceConfig(`{"loadBalancingConfig": [{"round_robin":{}}]}`),
	}
	opts = append(preOpts, opts...)

	return grpc.Dial(target, opts...)
}

grpc拨号不同k8s的namespace的grcp server,如数据端的api server

与相同namespace不同,这里需要传入server完整的ip和port,从GetConfig().DataService.JobHostPort获取

这个图主要梳理后端go的服务启动流程

[[app_api_server]]是3个apiserver之一

[[sx_sponsor_user_server]]是40多个grpc server之一

请求n个rpc server拆解请求n个rpc server