2020-09-18 11:41:52 +08:00
|
|
|
package zrpc
|
2020-07-26 17:09:05 +08:00
|
|
|
|
|
|
|
import (
|
2023-03-03 23:40:17 +08:00
|
|
|
"time"
|
|
|
|
|
2022-01-04 15:51:32 +08:00
|
|
|
"github.com/zeromicro/go-zero/core/discov"
|
|
|
|
"github.com/zeromicro/go-zero/core/service"
|
|
|
|
"github.com/zeromicro/go-zero/core/stores/redis"
|
2023-01-08 19:34:05 +08:00
|
|
|
"github.com/zeromicro/go-zero/zrpc/internal"
|
2022-01-04 15:51:32 +08:00
|
|
|
"github.com/zeromicro/go-zero/zrpc/resolver"
|
2020-07-26 17:09:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2023-01-08 19:34:05 +08:00
|
|
|
// ClientMiddlewaresConf defines whether to use client middlewares.
|
|
|
|
ClientMiddlewaresConf = internal.ClientMiddlewaresConf
|
|
|
|
// ServerMiddlewaresConf defines whether to use server middlewares.
|
|
|
|
ServerMiddlewaresConf = internal.ServerMiddlewaresConf
|
2023-04-08 22:45:05 +08:00
|
|
|
// StatConf defines the stat config.
|
|
|
|
StatConf = internal.StatConf
|
2023-10-26 08:55:26 +08:00
|
|
|
// MethodTimeoutConf defines specified timeout for gRPC method.
|
|
|
|
MethodTimeoutConf = internal.MethodTimeoutConf
|
2023-01-08 19:34:05 +08:00
|
|
|
|
2023-03-03 23:40:17 +08:00
|
|
|
// A RpcClientConf is a rpc client config.
|
|
|
|
RpcClientConf struct {
|
|
|
|
Etcd discov.EtcdConf `json:",optional,inherit"`
|
|
|
|
Endpoints []string `json:",optional"`
|
|
|
|
Target string `json:",optional"`
|
|
|
|
App string `json:",optional"`
|
|
|
|
Token string `json:",optional"`
|
|
|
|
NonBlock bool `json:",optional"`
|
|
|
|
Timeout int64 `json:",default=2000"`
|
2023-04-21 11:15:05 +08:00
|
|
|
KeepaliveTime time.Duration `json:",optional"`
|
2023-03-03 23:40:17 +08:00
|
|
|
Middlewares ClientMiddlewaresConf
|
|
|
|
}
|
|
|
|
|
2021-03-01 23:52:44 +08:00
|
|
|
// A RpcServerConf is a rpc server config.
|
2020-07-26 17:09:05 +08:00
|
|
|
RpcServerConf struct {
|
|
|
|
service.ServiceConf
|
|
|
|
ListenOn string
|
2022-11-08 15:27:48 +08:00
|
|
|
Etcd discov.EtcdConf `json:",optional,inherit"`
|
2020-07-26 17:09:05 +08:00
|
|
|
Auth bool `json:",optional"`
|
|
|
|
Redis redis.RedisKeyConf `json:",optional"`
|
|
|
|
StrictControl bool `json:",optional"`
|
2021-02-19 10:44:39 +08:00
|
|
|
// setting 0 means no timeout
|
2020-07-26 17:09:05 +08:00
|
|
|
Timeout int64 `json:",default=2000"`
|
|
|
|
CpuThreshold int64 `json:",default=900,range=[0:1000]"`
|
2022-08-23 13:44:21 +08:00
|
|
|
// grpc health check switch
|
2023-01-08 19:34:05 +08:00
|
|
|
Health bool `json:",default=true"`
|
|
|
|
Middlewares ServerMiddlewaresConf
|
2023-10-25 21:01:57 +08:00
|
|
|
// setting specified timeout for gRPC method
|
2023-10-26 08:55:26 +08:00
|
|
|
MethodTimeouts []MethodTimeoutConf `json:",optional"`
|
2020-07-26 17:09:05 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-03-01 23:52:44 +08:00
|
|
|
// NewDirectClientConf returns a RpcClientConf.
|
2020-08-18 18:36:44 +08:00
|
|
|
func NewDirectClientConf(endpoints []string, app, token string) RpcClientConf {
|
2020-07-26 17:09:05 +08:00
|
|
|
return RpcClientConf{
|
2020-08-18 18:36:44 +08:00
|
|
|
Endpoints: endpoints,
|
|
|
|
App: app,
|
|
|
|
Token: token,
|
2020-07-26 17:09:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 23:52:44 +08:00
|
|
|
// NewEtcdClientConf returns a RpcClientConf.
|
2020-07-26 17:09:05 +08:00
|
|
|
func NewEtcdClientConf(hosts []string, key, app, token string) RpcClientConf {
|
|
|
|
return RpcClientConf{
|
|
|
|
Etcd: discov.EtcdConf{
|
|
|
|
Hosts: hosts,
|
|
|
|
Key: key,
|
|
|
|
},
|
|
|
|
App: app,
|
|
|
|
Token: token,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 23:52:44 +08:00
|
|
|
// HasEtcd checks if there is etcd settings in config.
|
2020-07-26 17:09:05 +08:00
|
|
|
func (sc RpcServerConf) HasEtcd() bool {
|
|
|
|
return len(sc.Etcd.Hosts) > 0 && len(sc.Etcd.Key) > 0
|
|
|
|
}
|
|
|
|
|
2021-03-01 23:52:44 +08:00
|
|
|
// Validate validates the config.
|
2020-07-26 17:09:05 +08:00
|
|
|
func (sc RpcServerConf) Validate() error {
|
2021-04-18 22:49:03 +08:00
|
|
|
if !sc.Auth {
|
|
|
|
return nil
|
2020-07-26 17:09:05 +08:00
|
|
|
}
|
|
|
|
|
2021-04-18 22:49:03 +08:00
|
|
|
return sc.Redis.Validate()
|
2020-07-26 17:09:05 +08:00
|
|
|
}
|
|
|
|
|
2021-12-01 20:22:15 +08:00
|
|
|
// BuildTarget builds the rpc target from the given config.
|
|
|
|
func (cc RpcClientConf) BuildTarget() (string, error) {
|
|
|
|
if len(cc.Endpoints) > 0 {
|
|
|
|
return resolver.BuildDirectTarget(cc.Endpoints), nil
|
|
|
|
} else if len(cc.Target) > 0 {
|
|
|
|
return cc.Target, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cc.Etcd.Validate(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cc.Etcd.HasAccount() {
|
|
|
|
discov.RegisterAccount(cc.Etcd.Hosts, cc.Etcd.User, cc.Etcd.Pass)
|
|
|
|
}
|
2022-01-02 20:23:50 +08:00
|
|
|
if cc.Etcd.HasTLS() {
|
|
|
|
if err := discov.RegisterTLS(cc.Etcd.Hosts, cc.Etcd.CertFile, cc.Etcd.CertKeyFile,
|
|
|
|
cc.Etcd.CACertFile, cc.Etcd.InsecureSkipVerify); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
2021-12-01 20:22:15 +08:00
|
|
|
|
|
|
|
return resolver.BuildDiscovTarget(cc.Etcd.Hosts, cc.Etcd.Key), nil
|
|
|
|
}
|
|
|
|
|
2021-03-01 23:52:44 +08:00
|
|
|
// HasCredential checks if there is a credential in config.
|
2020-07-26 17:09:05 +08:00
|
|
|
func (cc RpcClientConf) HasCredential() bool {
|
|
|
|
return len(cc.App) > 0 && len(cc.Token) > 0
|
|
|
|
}
|