Skip to content

Commit

Permalink
optimize(parameter): set the
Browse files Browse the repository at this point in the history
  • Loading branch information
whalecold committed Oct 6, 2023
1 parent d51736c commit fc990ca
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 68 deletions.
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (s *EchoImpl) Echo(ctx context.Context, req *api.Request) (resp *api.Respon

func main() {
klog.SetLevel(klog.LevelDebug)
nacosClient, err := nacos.New(nacos.Options{})
nacosClient, err := nacos.NewClient(nacos.Options{})
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -75,24 +75,30 @@ import (
"github.com/nacos-group/nacos-sdk-go/vo"
)

type configLog struct{}

func (cl *configLog) Apply(opt *utils.Options) {
fn := func(cp *vo.ConfigParam) {
klog.Infof("nacos config %v", cp)
}
opt.NacosCustomFunctions = append(opt.NacosCustomFunctions, fn)
}

func main() {
klog.SetLevel(klog.LevelDebug)

nacosClient, err := nacos.New(nacos.Options{})
nacosClient, err := nacos.NewClient(nacos.Options{})
if err != nil {
panic(err)
}

fn := func(cp *vo.ConfigParam) {
klog.Infof("nacos config %v", cp)
}

cl := &configLog{}
serviceName := "ServiceName"
clientName := "ClientName"
client, err := echo.NewClient(
serviceName,
client.WithHostPorts("0.0.0.0:8888"),
client.WithSuite(nacosclient.NewSuite(serviceName, clientName, nacosClient, fn)),
client.WithSuite(nacosclient.NewSuite(serviceName, clientName, nacosClient, cl)),
)
if err != nil {
log.Fatal(err)
Expand Down
20 changes: 13 additions & 7 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (s *EchoImpl) Echo(ctx context.Context, req *api.Request) (resp *api.Respon

func main() {
klog.SetLevel(klog.LevelDebug)
nacosClient, err := nacos.New(nacos.Options{})
nacosClient, err := nacos.NewClient(nacos.Options{})
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -75,24 +75,30 @@ import (
"github.com/nacos-group/nacos-sdk-go/vo"
)

type configLog struct{}

func (cl *configLog) Apply(opt *utils.Options) {
fn := func(cp *vo.ConfigParam) {
klog.Infof("nacos config %v", cp)
}
opt.NacosCustomFunctions = append(opt.NacosCustomFunctions, fn)
}

func main() {
klog.SetLevel(klog.LevelDebug)

nacosClient, err := nacos.New(nacos.Options{})
nacosClient, err := nacos.NewClient(nacos.Options{})
if err != nil {
panic(err)
}

fn := func(cp *vo.ConfigParam) {
klog.Infof("nacos config %v", cp)
}

cl := &configLog{}
serviceName := "ServiceName"
clientName := "ClientName"
client, err := echo.NewClient(
serviceName,
client.WithHostPorts("0.0.0.0:8888"),
client.WithSuite(nacosclient.NewSuite(serviceName, clientName, nacosClient, fn)),
client.WithSuite(nacosclient.NewSuite(serviceName, clientName, nacosClient, cl)),
)
if err != nil {
log.Fatal(err)
Expand Down
10 changes: 6 additions & 4 deletions client/circuit_breaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@ import (
)

// WithCircuitBreaker sets the circuit breaker policy from nacos configuration center.
func WithCircuitBreaker(dest, src string, nacosClient nacos.Client,
cfs ...nacos.CustomFunction,
) []client.Option {
func WithCircuitBreaker(dest, src string, nacosClient nacos.Client, opts utils.Options) []client.Option {
param, err := nacosClient.ClientConfigParam(&nacos.ConfigParamConfig{
Category: circuitBreakerConfigName,
ServerServiceName: dest,
ClientServiceName: src,
}, cfs...)
})
if err != nil {
panic(err)
}

for _, f := range opts.NacosCustomFunctions {
f(&param)
}

cbSuite := initCircuitBreaker(param, dest, src, nacosClient)

return []client.Option{
Expand Down
10 changes: 6 additions & 4 deletions client/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,20 @@ import (
)

// WithRetryPolicy sets the retry policy from nacos configuration center.
func WithRetryPolicy(dest, src string, nacosClient nacos.Client,
cfs ...nacos.CustomFunction,
) []client.Option {
func WithRetryPolicy(dest, src string, nacosClient nacos.Client, opts utils.Options) []client.Option {
param, err := nacosClient.ClientConfigParam(&nacos.ConfigParamConfig{
Category: retryConfigName,
ServerServiceName: dest,
ClientServiceName: src,
}, cfs...)
})
if err != nil {
panic(err)
}

for _, f := range opts.NacosCustomFunctions {
f(&param)
}

rc := initRetryContainer(param, dest, nacosClient)
return []client.Option{
client.WithRetryContainer(rc),
Expand Down
11 changes: 7 additions & 4 deletions client/rpc_timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,25 @@ import (
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/cloudwego/kitex/pkg/rpctimeout"
"github.com/kitex-contrib/config-nacos/nacos"
"github.com/kitex-contrib/config-nacos/utils"
"github.com/nacos-group/nacos-sdk-go/vo"
)

// WithRPCTimeout sets the RPC timeout policy from nacos configuration center.
func WithRPCTimeout(dest, src string, nacosClient nacos.Client,
cfs ...nacos.CustomFunction,
) []client.Option {
func WithRPCTimeout(dest, src string, nacosClient nacos.Client, opts utils.Options) []client.Option {
param, err := nacosClient.ClientConfigParam(&nacos.ConfigParamConfig{
Category: rpcTimeoutConfigName,
ServerServiceName: dest,
ClientServiceName: src,
}, cfs...)
})
if err != nil {
panic(err)
}

for _, f := range opts.NacosCustomFunctions {
f(&param)
}

return []client.Option{
client.WithTimeoutProvider(initRPCTimeoutContainer(param, dest, nacosClient)),
client.WithCloseCallbacks(func() error {
Expand Down
20 changes: 11 additions & 9 deletions client/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package client
import (
"github.com/cloudwego/kitex/client"
"github.com/kitex-contrib/config-nacos/nacos"
"github.com/kitex-contrib/config-nacos/utils"
)

const (
Expand All @@ -30,26 +31,27 @@ type NacosClientSuite struct {
nacosClient nacos.Client
service string
client string
fns []nacos.CustomFunction
opts utils.Options
}

// NewSuite service is the destination service name and client is the local identity.
func NewSuite(service, client string, cli nacos.Client,
cfs ...nacos.CustomFunction,
) *NacosClientSuite {
return &NacosClientSuite{
func NewSuite(service, client string, cli nacos.Client, opts ...utils.Option) *NacosClientSuite {
su := &NacosClientSuite{
service: service,
client: client,
nacosClient: cli,
fns: cfs,
}
for _, f := range opts {
f.Apply(&su.opts)
}
return su
}

// Options return a list client.Option
func (s *NacosClientSuite) Options() []client.Option {
opts := make([]client.Option, 0, 7)
opts = append(opts, WithRetryPolicy(s.service, s.client, s.nacosClient, s.fns...)...)
opts = append(opts, WithRPCTimeout(s.service, s.client, s.nacosClient, s.fns...)...)
opts = append(opts, WithCircuitBreaker(s.service, s.client, s.nacosClient, s.fns...)...)
opts = append(opts, WithRetryPolicy(s.service, s.client, s.nacosClient, s.opts)...)
opts = append(opts, WithRPCTimeout(s.service, s.client, s.nacosClient, s.opts)...)
opts = append(opts, WithCircuitBreaker(s.service, s.client, s.nacosClient, s.opts)...)
return opts
}
19 changes: 13 additions & 6 deletions example/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,34 @@ import (
"github.com/cloudwego/kitex/pkg/klog"
nacosclient "github.com/kitex-contrib/config-nacos/client"
"github.com/kitex-contrib/config-nacos/nacos"
"github.com/kitex-contrib/config-nacos/utils"
"github.com/nacos-group/nacos-sdk-go/vo"
)

type configLog struct{}

func (cl *configLog) Apply(opt *utils.Options) {
fn := func(cp *vo.ConfigParam) {
klog.Infof("nacos config %v", cp)
}
opt.NacosCustomFunctions = append(opt.NacosCustomFunctions, fn)
}

func main() {
klog.SetLevel(klog.LevelDebug)

nacosClient, err := nacos.New(nacos.Options{})
nacosClient, err := nacos.NewClient(nacos.Options{})
if err != nil {
panic(err)
}

fn := func(cp *vo.ConfigParam) {
klog.Infof("nacos config %v", cp)
}

cl := &configLog{}
serviceName := "ServiceName"
clientName := "ClientName"
client, err := echo.NewClient(
serviceName,
client.WithHostPorts("0.0.0.0:8888"),
client.WithSuite(nacosclient.NewSuite(serviceName, clientName, nacosClient, fn)),
client.WithSuite(nacosclient.NewSuite(serviceName, clientName, nacosClient, cl)),
)
if err != nil {
log.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion example/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s *EchoImpl) Echo(ctx context.Context, req *api.Request) (resp *api.Respon

func main() {
klog.SetLevel(klog.LevelDebug)
nacosClient, err := nacos.New(nacos.Options{})
nacosClient, err := nacos.NewClient(nacos.Options{})
if err != nil {
panic(err)
}
Expand Down
24 changes: 9 additions & 15 deletions nacos/nacos.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import (
// Client the wrapper of nacos client.
type Client interface {
SetParser(ConfigParser)
ClientConfigParam(cpc *ConfigParamConfig, cfs ...CustomFunction) (vo.ConfigParam, error)
ServerConfigParam(cpc *ConfigParamConfig, cfs ...CustomFunction) (vo.ConfigParam, error)
ClientConfigParam(cpc *ConfigParamConfig) (vo.ConfigParam, error)
ServerConfigParam(cpc *ConfigParamConfig) (vo.ConfigParam, error)
RegisterConfigCallback(vo.ConfigParam, func(string, ConfigParser))
DeregisterConfig(vo.ConfigParam) error
}
Expand All @@ -57,10 +57,8 @@ type Options struct {
ConfigParser ConfigParser
}

// New Create a default Nacos client
// It can create a client with default config by env variable.
// See: env.go
func New(opts Options) (Client, error) {
// NewClient Create a default Nacos client
func NewClient(opts Options) (Client, error) {
if opts.Address == "" {
opts.Address = NacosDefaultServerAddr
}
Expand Down Expand Up @@ -138,13 +136,13 @@ func (c *client) render(cpc *ConfigParamConfig, t *template.Template) (string, e
}

// ServerConfigParam render server config parameters
func (c *client) ServerConfigParam(cpc *ConfigParamConfig, cfs ...CustomFunction) (vo.ConfigParam, error) {
return c.configParam(cpc, c.serverDataIDTemplate, cfs...)
func (c *client) ServerConfigParam(cpc *ConfigParamConfig) (vo.ConfigParam, error) {
return c.configParam(cpc, c.serverDataIDTemplate)
}

// ClientConfigParam render client config parameters
func (c *client) ClientConfigParam(cpc *ConfigParamConfig, cfs ...CustomFunction) (vo.ConfigParam, error) {
return c.configParam(cpc, c.clientDataIDTemplate, cfs...)
func (c *client) ClientConfigParam(cpc *ConfigParamConfig) (vo.ConfigParam, error) {
return c.configParam(cpc, c.clientDataIDTemplate)
}

// configParam render config parameters. All the parameters can be customized with CustomFunction.
Expand All @@ -154,7 +152,7 @@ func (c *client) ClientConfigParam(cpc *ConfigParamConfig, cfs ...CustomFunction
// 3. Group: DEFAULT_GROUP by default.
// 4. ServerDataId: {{.ServerServiceName}}.{{.Category}} by default.
// ClientDataId: {{.ClientServiceName}}.{{.ServerServiceName}}.{{.Category}} by default.
func (c *client) configParam(cpc *ConfigParamConfig, t *template.Template, cfs ...CustomFunction) (vo.ConfigParam, error) {
func (c *client) configParam(cpc *ConfigParamConfig, t *template.Template) (vo.ConfigParam, error) {
param := vo.ConfigParam{
Type: vo.JSON,
Content: defaultContent,
Expand All @@ -168,10 +166,6 @@ func (c *client) configParam(cpc *ConfigParamConfig, t *template.Template, cfs .
if err != nil {
return param, err
}

for _, cf := range cfs {
cf(&param)
}
return param, nil
}

Expand Down
11 changes: 7 additions & 4 deletions server/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,23 @@ import (
"github.com/nacos-group/nacos-sdk-go/vo"

"github.com/kitex-contrib/config-nacos/nacos"
"github.com/kitex-contrib/config-nacos/utils"
)

// WithLimiter sets the limiter config from nacos configuration center.
func WithLimiter(dest string, nacosClient nacos.Client,
cfs ...nacos.CustomFunction,
) server.Option {
func WithLimiter(dest string, nacosClient nacos.Client, opts utils.Options) server.Option {
param, err := nacosClient.ServerConfigParam(&nacos.ConfigParamConfig{
Category: limiterConfigName,
ServerServiceName: dest,
}, cfs...)
})
if err != nil {
panic(err)
}

for _, f := range opts.NacosCustomFunctions {
f(&param)
}

return server.WithLimit(initLimitOptions(param, dest, nacosClient))
}

Expand Down
16 changes: 9 additions & 7 deletions server/suit.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package server
import (
"github.com/cloudwego/kitex/server"
"github.com/kitex-contrib/config-nacos/nacos"
"github.com/kitex-contrib/config-nacos/utils"
)

const (
Expand All @@ -27,23 +28,24 @@ const (
type NacosServerSuite struct {
nacosClient nacos.Client
service string
fns []nacos.CustomFunction
opts utils.Options
}

// NewSuite service is the destination service.
func NewSuite(service string, cli nacos.Client,
cfs ...nacos.CustomFunction,
) *NacosServerSuite {
return &NacosServerSuite{
func NewSuite(service string, cli nacos.Client, opts ...utils.Option) *NacosServerSuite {
su := &NacosServerSuite{
service: service,
nacosClient: cli,
fns: cfs,
}
for _, opt := range opts {
opt.Apply(&su.opts)
}
return su
}

// Options return a list client.Option
func (s *NacosServerSuite) Options() []server.Option {
opts := make([]server.Option, 0, 2)
opts = append(opts, WithLimiter(s.service, s.nacosClient, s.fns...))
opts = append(opts, WithLimiter(s.service, s.nacosClient, s.opts))
return opts
}
Loading

0 comments on commit fc990ca

Please sign in to comment.