Skip to content

Commit

Permalink
Merge pull request #8 from kitex-contrib/chore/parameter
Browse files Browse the repository at this point in the history
optimize(parameter): optimize the compatibility of initialize function
  • Loading branch information
felix021 authored Oct 17, 2023
2 parents d51736c + 9ad4966 commit fca5c33
Show file tree
Hide file tree
Showing 16 changed files with 1,740 additions and 1,179 deletions.
22 changes: 14 additions & 8 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 All @@ -111,7 +117,7 @@ func main() {

### Nacos Configuration

The client is initialized according to the parameters of `Options` and connects to the nacos server. After the connection is established, the suite subscribes the appropriate configuration based on `Group`, `ServerDataIDFormat` and `ClientDataIDFormat` to updates its own policy dynamically. See the environment variables below for specific parameters.
The client is initialized according to the parameters of `Options` and connects to the nacos server. After the connection is established, the suite subscribes the appropriate configuration based on `Group`, `ServerDataIDFormat` and `ClientDataIDFormat` to updates its own policy dynamically. See the `Options` variables below for specific parameters.

The configuration format supports `json` and `yaml`. You can use the [SetParser](https://github.com/kitex-contrib/config-nacos/blob/eb006978517678dd75a81513142d3faed6a66f8d/nacos/nacos.go#L68) function to customise the format parsing method, and the `CustomFunction` function to customise the format of the subscription function during `NewSuite`.
####
Expand Down
22 changes: 14 additions & 8 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 All @@ -110,7 +116,7 @@ func main() {
```
### Nacos 配置

根据 Options 的参数初始化 client,建立链接之后 suite 会根据 `Group` 以及 `ServerDataIDFormat` 或者 `ClientDataIDFormat` 订阅对应的配置并动态更新自身策略,具体参数参考下面环境变量
根据 Options 的参数初始化 client,建立链接之后 suite 会根据 `Group` 以及 `ServerDataIDFormat` 或者 `ClientDataIDFormat` 订阅对应的配置并动态更新自身策略,具体参数参考下面 `Options` 变量

配置的格式默认支持 `json``yaml`,可以使用函数 [SetParser](https://github.com/kitex-contrib/config-nacos/blob/eb006978517678dd75a81513142d3faed6a66f8d/nacos/nacos.go#L68) 进行自定义格式解析方式,并在 `NewSuite` 的时候使用 `CustomFunction` 函数修改订阅函数的格式。

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
}
18 changes: 13 additions & 5 deletions example/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,35 @@ 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
66 changes: 66 additions & 0 deletions example/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
module example

go 1.19

require (
github.com/cloudwego/kitex v0.7.2
github.com/cloudwego/kitex-examples v0.2.0
github.com/kitex-contrib/config-nacos v0.1.1
)

require (
github.com/aliyun/alibaba-cloud-sdk-go v1.61.18 // indirect
github.com/apache/thrift v0.13.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/bytedance/gopkg v0.0.0-20230728082804-614d0af6619b // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/choleraehyq/pid v0.0.17 // indirect
github.com/cloudwego/configmanager v0.2.0 // indirect
github.com/cloudwego/dynamicgo v0.1.3 // indirect
github.com/cloudwego/fastpb v0.0.4 // indirect
github.com/cloudwego/frugal v0.1.8 // indirect
github.com/cloudwego/localsession v0.0.2 // indirect
github.com/cloudwego/netpoll v0.5.0 // indirect
github.com/cloudwego/thriftgo v0.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/go-errors/errors v1.0.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/pprof v0.0.0-20220608213341-c488b8fa1db3 // indirect
github.com/iancoleman/strcase v0.2.0 // indirect
github.com/jhump/protoreflect v1.8.2 // indirect
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/gls v0.0.0-20220109145502-612d0167dce5 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nacos-group/nacos-sdk-go v1.1.4 // indirect
github.com/oleiade/lane v1.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/tidwall/gjson v1.9.3 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.15.0 // indirect
golang.org/x/arch v0.2.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
google.golang.org/genproto v0.0.0-20230216225411-c8e22ba71e44 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/ini.v1 v1.42.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace github.com/kitex-contrib/config-nacos => ../
Loading

0 comments on commit fca5c33

Please sign in to comment.