31 lines
768 B
Go
31 lines
768 B
Go
|
|
package grpcclient
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"errors"
|
|||
|
|
|
|||
|
|
"google.golang.org/grpc"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// Config 客户端配置.
|
|||
|
|
type Config struct {
|
|||
|
|
// ServerAddrs gRPC服务器地址列表,格式: "host:port"
|
|||
|
|
// 支持多个地址,客户端将使用轮询负载均衡
|
|||
|
|
ServerAddrs []string
|
|||
|
|
// ServerAddr 单个服务器地址(向后兼容),如果设置了此字段,将忽略ServerAddrs
|
|||
|
|
ServerAddr string
|
|||
|
|
// DialOptions 额外的gRPC拨号选项
|
|||
|
|
DialOptions []grpc.DialOption
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetAddrs 获取服务器地址列表.
|
|||
|
|
func (c *Config) GetAddrs() ([]string, error) {
|
|||
|
|
switch {
|
|||
|
|
case len(c.ServerAddrs) > 0:
|
|||
|
|
return c.ServerAddrs, nil
|
|||
|
|
case c.ServerAddr != "":
|
|||
|
|
return []string{c.ServerAddr}, nil
|
|||
|
|
default:
|
|||
|
|
return nil, errors.New("at least one server address is required")
|
|||
|
|
}
|
|||
|
|
}
|