add custom endpoints configuration for package grpcx (#2625)

This commit is contained in:
xxxwang1983
2023-07-05 09:54:57 +08:00
committed by GitHub
parent c90e9311e3
commit 30cf3dbbe6
3 changed files with 44 additions and 3 deletions

View File

@ -262,10 +262,14 @@ func (s *GrpcServer) GetListenedPort() int {
func (s *GrpcServer) calculateListenedEndpoints(ctx context.Context) gsvc.Endpoints {
var (
configAddr = s.config.Address
endpoints = make(gsvc.Endpoints, 0)
configAddr = s.config.Address
endpoints = make(gsvc.Endpoints, 0)
configAddrs = s.config.Endpoints
)
for _, address := range gstr.SplitAndTrim(configAddr, ",") {
if len(configAddrs) == 0 {
configAddrs = gstr.SplitAndTrim(configAddr, ",")
}
for _, address := range configAddrs {
var (
addrArray = gstr.Split(address, ":")
listenedIps []string

View File

@ -28,6 +28,7 @@ type GrpcServerConfig struct {
ErrorLogPattern string // (optional) ErrorLogPattern specifies the error log file pattern like: error-{Ymd}.log
AccessLogEnabled bool // (optional) AccessLogEnabled enables access logging content to file.
AccessLogPattern string // (optional) AccessLogPattern specifies the error log file pattern like: access-{Ymd}.log
Endpoints []string // (optional) Address for server register if null use Address value.
Options []grpc.ServerOption // (optional) GRPC Server options.
}

View File

@ -0,0 +1,36 @@
package grpcx
import (
"testing"
"github.com/gogf/gf/v2/test/gtest"
)
func Test_Grpcx_Grpc_Server_Config(t *testing.T) {
cfg := Server.NewConfig()
addr := "10.0.0.29:80"
cfg.Endpoints = []string{
addr,
}
// cfg set one endpoint
gtest.C(t, func(t *gtest.T) {
s := Server.New(cfg)
s.doServiceRegister()
for _, svc := range s.services {
t.Assert(svc.GetEndpoints().String(), addr)
}
})
// cfg set more endpoints
addr = "10.0.0.29:80,10.0.0.29:81"
cfg.Endpoints = []string{
"10.0.0.29:80",
"10.0.0.29:81",
}
gtest.C(t, func(t *gtest.T) {
s := Server.New(cfg)
s.doServiceRegister()
for _, svc := range s.services {
t.Assert(svc.GetEndpoints().String(), addr)
}
})
}