rpcx

package
v1.0.15 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 23, 2022 License: MIT Imports: 26 Imported by: 0

README

1.

go get -u google.golang.org/grpc go get -u google.golang.org/protobuf

2.

go install google.golang.org/protobuf/cmd/protoc-gen-go@latest go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

3.

protoc --go_out=. ./rpcx/proto/pub_worker.proto protoc --go-grpc_out=. ./rpcx/proto/pub_worker.proto

4. 生成TLS证书

生成ca私钥

openssl genrsa -out ca.key 4096

自签名生成ca.crt 证书文件

如果在 Windows 使用 Git Bash 出现错误

name is expected to be in the format /type0=value0/type1=value1/type2=... where characters may be escaped by . This name is not in that format: ...

则需要在命令前加上

MSYS_NO_PATHCONV=1

例如 MSYS_NO_PATHCONV=1 openssl ...

MSYS_NO_PATHCONV=1 openssl req -new -x509 -days 3650 -key ca.key -out ca.crt -subj "/CN=localhost"

生成server/client key file

openssl genrsa -out server.key 2048 openssl genrsa -out client.key 2048

生成server/client csr file

openssl req -new -key server.key -out server.csr -config TLS.md -extensions SAN openssl req -new -key client.key -out client.csr -config TLS.md -extensions SAN

生成server/client crt file

Generates server.crt which is the certChainFile for the server

openssl x509 -req -days 3650 -in server.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out server.crt -extfile TLS.md -extensions SAN openssl x509 -req -days 3650 -in client.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out client.crt -extfile TLS.md -extensions SAN

Generates server.pem which is the privateKeyFile for the Server

openssl pkcs8 -topk8 -nocrypt -in server.key -out server.pem

独立server.key进行SAN自签

openssl genrsa -out server.key 2048 openssl req -new -key server.key -out server.csr -config TLS.md -extensions SAN openssl x509 -req -days 3650 -in server.csr -set_serial 01 -signkey server.key -out server.crt -extfile TLS.md -extensions SAN

Documentation

Index

Constants

View Source
const (
	DefaultConsulHost = "consulx.com:8500"
)

Variables

This section is empty.

Functions

func GetAuthorizeTLS

func GetAuthorizeTLS() (*gorsa.RsaObj, error)

func GetGRPCJwtConfig

func GetGRPCJwtConfig() (*jwt.JwtConfig, error)

func InitDefaultConsul added in v1.0.13

func InitDefaultConsul()

func NewClientConn

func NewClientConn(object GRPC) (pool.Conn, error)

func RunClient

func RunClient(appId ...string)

RunClient Important: ensure that the service starts only once JWT Token expires in 1 hour The remaining 1200s will be automatically renewed and detected every 15s

func RunServer

func RunServer(consulDs string, authenticate bool, objects ...*GRPC)

Types

type AppConfig

type AppConfig struct {
	AppId    string
	AppKey   string
	Status   int64
	LastTime int64
}

func GetGRPCAppConfig

func GetGRPCAppConfig(appid string) (AppConfig, error)

type AuthObject

type AuthObject struct {
	AppId     string
	Nonce     string
	Time      int64
	Signature string
}

type ClientConnPool

type ClientConnPool struct {
	// contains filtered or unexported fields
}

type ConsulConfig

type ConsulConfig struct {
	DsName       string // 数据源名
	Host         string // consul host
	CheckPort    int    // 健康监测端口
	RpcPort      int    // RPC调用端口
	Protocol     string // RPC协议, tcp
	Timeout      string // 请求超时时间, 3s
	Interval     string // 健康监测时间, 5s
	DestroyAfter string // 销毁服务时间, 600s
	CheckPath    string // 健康检测path /xxx/check
	LogPath      string // 日志输出路径
	SlowQuery    int64  // 0.不开启筛选 >0开启筛选查询 毫秒
	SlowLogPath  string // 慢查询写入地址
}

Consulx配置参数

type ConsulManager

type ConsulManager struct {
	Host    string
	Token   string
	Consulx *consulapi.Client
	Config  *ConsulConfig
}

func NewConsul

func NewConsul(ds ...string) (*ConsulManager, error)

func (*ConsulManager) CheckService

func (self *ConsulManager) CheckService(services []*consulapi.AgentService, srvName, addr string) bool

func (*ConsulManager) Client

func (self *ConsulManager) Client(ds ...string) (*ConsulManager, error)

func (*ConsulManager) GetAesJsonValue added in v1.0.8

func (self *ConsulManager) GetAesJsonValue(key, encKey string, result interface{}) error

通过Consul中心获取指定加密JSON配置数据

func (*ConsulManager) GetAllService

func (self *ConsulManager) GetAllService(service string) ([]*consulapi.AgentService, error)

根据服务名获取可用列表

func (*ConsulManager) GetCacheService

func (self *ConsulManager) GetCacheService(service, tag string, cacheSecond int) ([]*consulapi.ServiceEntry, error)

func (*ConsulManager) GetHealthService

func (self *ConsulManager) GetHealthService(service, tag string) ([]*consulapi.ServiceEntry, error)

func (*ConsulManager) GetJsonValue

func (self *ConsulManager) GetJsonValue(key string, result interface{}) error

通过Consul中心获取指定JSON配置数据

func (*ConsulManager) GetSlowLog

func (self *ConsulManager) GetSlowLog() *zap.Logger

func (*ConsulManager) GetTextValue

func (self *ConsulManager) GetTextValue(key string) ([]byte, error)

func (*ConsulManager) HealthCheck

func (self *ConsulManager) HealthCheck(w http.ResponseWriter, r *http.Request)

接口服务健康检查

func (*ConsulManager) InitConfig

func (self *ConsulManager) InitConfig(input ...ConsulConfig) (*ConsulManager, error)

func (*ConsulManager) RemoveService

func (self *ConsulManager) RemoveService(serviceIDs ...string)

type GRPC

type GRPC struct {
	Ds      string                    // consul数据源ds
	Tags    []string                  // 服务标签名称
	Address string                    // 服务地址,为空时自动填充内网IP
	RpcPort int                       // 服务地址端口
	Service string                    // 服务名称
	Cache   int                       // 服务缓存时间/秒
	Timeout int                       // context timeout/毫秒
	AddRPC  func(server *grpc.Server) // grpc注册proto服务
}

type GRPCManager

type GRPCManager struct {
	// contains filtered or unexported fields
}

func (*GRPCManager) ClientInterceptor

func (self *GRPCManager) ClientInterceptor(ctx context.Context, method string, req, reply interface{}, conn *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) (err error)

func (*GRPCManager) CreateAppConfigCall

func (self *GRPCManager) CreateAppConfigCall(fun func(appId string) (AppConfig, error))

func (*GRPCManager) CreateAuthorizeTLS

func (self *GRPCManager) CreateAuthorizeTLS(keyPath string)

CreateAuthorizeTLS If server TLS is used, the certificate server.key is used by default Otherwise, the method needs to be explicitly called to set the certificate

func (*GRPCManager) CreateClientTLS

func (self *GRPCManager) CreateClientTLS(tlsConfig TlsConfig)

func (*GRPCManager) CreateJwtConfig

func (self *GRPCManager) CreateJwtConfig(tokenKey string, tokenExp ...int64)

func (*GRPCManager) CreateRateLimiterCall

func (self *GRPCManager) CreateRateLimiterCall(fun func(method string) (rate.Option, error))

func (*GRPCManager) CreateSelectionCall

func (self *GRPCManager) CreateSelectionCall(fun func([]*consulapi.ServiceEntry, GRPC) *consulapi.ServiceEntry)

func (*GRPCManager) CreateServerTLS

func (self *GRPCManager) CreateServerTLS(tlsConfig TlsConfig)

func (*GRPCManager) ServerInterceptor

func (self *GRPCManager) ServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error)

type TlsConfig

type TlsConfig struct {
	UseTLS    bool
	UseMTLS   bool
	CACrtFile string
	CAKeyFile string
	KeyFile   string
	CrtFile   string
	HostName  string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL