|
|
@ -22,6 +22,7 @@ import (
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"os/signal"
|
|
|
|
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strconv"
|
|
|
|
"syscall"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
@ -46,8 +47,41 @@ import (
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Start rpc server.
|
|
|
|
func init() {
|
|
|
|
func Start[T any](ctx context.Context, discovery *conf.Discovery, prometheusConfig *conf.Prometheus, listenIP,
|
|
|
|
prommetrics.RegistryAll()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func getConfigRpcMaxRequestBody(value reflect.Value) *conf.MaxRequestBody {
|
|
|
|
|
|
|
|
for value.Kind() == reflect.Pointer {
|
|
|
|
|
|
|
|
value = value.Elem()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if value.Kind() == reflect.Struct {
|
|
|
|
|
|
|
|
num := value.NumField()
|
|
|
|
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
|
|
|
|
field := value.Field(i)
|
|
|
|
|
|
|
|
if !field.CanInterface() {
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for field.Kind() == reflect.Pointer {
|
|
|
|
|
|
|
|
field = field.Elem()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
switch elem := field.Interface().(type) {
|
|
|
|
|
|
|
|
case conf.Share:
|
|
|
|
|
|
|
|
return &elem.RPCMaxBodySize
|
|
|
|
|
|
|
|
case conf.MaxRequestBody:
|
|
|
|
|
|
|
|
return &elem
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if field.Kind() == reflect.Struct {
|
|
|
|
|
|
|
|
if elem := getConfigRpcMaxRequestBody(field); elem != nil {
|
|
|
|
|
|
|
|
return elem
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func Start[T any](ctx context.Context, disc *conf.Discovery, prometheusConfig *conf.Prometheus, listenIP,
|
|
|
|
registerIP string, autoSetPorts bool, rpcPorts []int, index int, rpcRegisterName string, notification *conf.Notification, config T,
|
|
|
|
registerIP string, autoSetPorts bool, rpcPorts []int, index int, rpcRegisterName string, notification *conf.Notification, config T,
|
|
|
|
watchConfigNames []string, watchServiceNames []string,
|
|
|
|
watchConfigNames []string, watchServiceNames []string,
|
|
|
|
rpcFn func(ctx context.Context, config T, client discovery.SvcDiscoveryRegistry, server *grpc.Server) error,
|
|
|
|
rpcFn func(ctx context.Context, config T, client discovery.SvcDiscoveryRegistry, server *grpc.Server) error,
|
|
|
@ -65,6 +99,25 @@ func Start[T any](ctx context.Context, discovery *conf.Discovery, prometheusConf
|
|
|
|
conf.InitNotification(notification)
|
|
|
|
conf.InitNotification(notification)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
maxRequestBody := getConfigRpcMaxRequestBody(reflect.ValueOf(config))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.ZDebug(ctx, "rpc start", "rpcMaxRequestBody", maxRequestBody, "rpcRegisterName", rpcRegisterName, "registerIP", registerIP, "listenIP", listenIP)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
options = append(options,
|
|
|
|
|
|
|
|
mw.GrpcServer(),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
var clientOptions []grpc.DialOption
|
|
|
|
|
|
|
|
if maxRequestBody != nil {
|
|
|
|
|
|
|
|
if maxRequestBody.RequestMaxBodySize > 0 {
|
|
|
|
|
|
|
|
options = append(options, grpc.MaxRecvMsgSize(maxRequestBody.RequestMaxBodySize))
|
|
|
|
|
|
|
|
clientOptions = append(clientOptions, grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(maxRequestBody.RequestMaxBodySize)))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if maxRequestBody.ResponseMaxBodySize > 0 {
|
|
|
|
|
|
|
|
options = append(options, grpc.MaxSendMsgSize(maxRequestBody.ResponseMaxBodySize))
|
|
|
|
|
|
|
|
clientOptions = append(clientOptions, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxRequestBody.ResponseMaxBodySize)))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
registerIP, err := network.GetRpcRegisterIP(registerIP)
|
|
|
|
registerIP, err := network.GetRpcRegisterIP(registerIP)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
@ -101,15 +154,29 @@ func Start[T any](ctx context.Context, discovery *conf.Discovery, prometheusConf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
defer client.Close()
|
|
|
|
defer client.Close()
|
|
|
|
client.AddOption(mw.GrpcClient(), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"LoadBalancingPolicy": "%s"}`, "round_robin")))
|
|
|
|
client.AddOption(
|
|
|
|
|
|
|
|
mw.GrpcClient(), grpc.WithTransportCredentials(insecure.NewCredentials()),
|
|
|
|
// var reg *prometheus.Registry
|
|
|
|
grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"LoadBalancingPolicy": "%s"}`, "round_robin")),
|
|
|
|
// var metric *grpcprometheus.ServerMetrics
|
|
|
|
)
|
|
|
|
if prometheusConfig.Enable {
|
|
|
|
if len(clientOptions) > 0 {
|
|
|
|
// cusMetrics := prommetrics.GetGrpcCusMetrics(rpcRegisterName, share)
|
|
|
|
client.AddOption(clientOptions...)
|
|
|
|
// reg, metric, _ = prommetrics.NewGrpcPromObj(cusMetrics)
|
|
|
|
}
|
|
|
|
// options = append(options, mw.GrpcServer(), grpc.StreamInterceptor(metric.StreamServerInterceptor()),
|
|
|
|
|
|
|
|
// grpc.UnaryInterceptor(metric.UnaryServerInterceptor()))
|
|
|
|
ctx, cancel := context.WithCancelCause(ctx)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
|
|
|
|
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
case val := <-sigs:
|
|
|
|
|
|
|
|
log.ZDebug(ctx, "recv signal", "signal", val.String())
|
|
|
|
|
|
|
|
cancel(fmt.Errorf("signal %s", val.String()))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if prometheusListenAddr != "" {
|
|
|
|
options = append(
|
|
|
|
options = append(
|
|
|
|
options, mw.GrpcServer(),
|
|
|
|
options, mw.GrpcServer(),
|
|
|
|
prommetricsUnaryInterceptor(rpcRegisterName),
|
|
|
|
prommetricsUnaryInterceptor(rpcRegisterName),
|
|
|
|