diff --git a/.github/workflows/pr-check.yml b/.github/workflows/pr-check.yml index 8cb7338..cd2a099 100644 --- a/.github/workflows/pr-check.yml +++ b/.github/workflows/pr-check.yml @@ -1,10 +1,10 @@ name: Pull Request Check -on: [ pull_request ] +on: [pull_request] jobs: compliant: - runs-on: [ self-hosted, X64 ] + runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 @@ -17,13 +17,13 @@ jobs: uses: crate-ci/typos@master staticcheck: - runs-on: [ self-hosted, X64 ] + runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up Go - uses: actions/setup-go@v3 + uses: actions/setup-go@v5 with: - go-version: 1.19 + go-version: "1.20" - uses: actions/cache@v3 with: @@ -45,16 +45,17 @@ jobs: staticcheck_flags: -checks=inherit,-SA1029 lint: - runs-on: [ self-hosted, X64 ] + runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up Go - uses: actions/setup-go@v3 + uses: actions/setup-go@v5 with: - go-version: 1.19 - + go-version: "1.20" - name: Golangci Lint # https://golangci-lint.run/ - uses: golangci/golangci-lint-action@v3 + uses: golangci/golangci-lint-action@v4 with: version: latest + args: --timeout 30m -E gofumpt --go 1.20 + skip-pkg-cache: true diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 062b5df..09bc6aa 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -1,29 +1,28 @@ name: Tests -on: [ push, pull_request ] +on: [push, pull_request] jobs: unit-benchmark-test: strategy: matrix: - go: [ 1.19 ] - os: [ X64, ARM64 ] - runs-on: ${{ matrix.os }} + go: ["1.20"] + runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up Go - uses: actions/setup-go@v3 + uses: actions/setup-go@v5 with: go-version: ${{ matrix.go }} - # block scenario, comment temporarily -# - uses: actions/cache@v3 -# with: -# path: ~/go/pkg/mod -# key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} -# restore-keys: | -# ${{ runner.os }}-go- + # block scenario, comment temporarily + # - uses: actions/cache@v3 + # with: + # path: ~/go/pkg/mod + # key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + # restore-keys: | + # ${{ runner.os }}-go- - name: Unit Test run: go test -race -covermode=atomic -coverprofile=coverage.out ./... diff --git a/README.md b/README.md index f127536..de39d04 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,12 @@ -# config-consul +# config-consul + [中文](./README_CN.md) -etcd as config center for service governance. +consul as config center for service governance. + +## Install + +`go get github.com/kitex-contrib/config-consul` ## Usage @@ -14,11 +19,11 @@ etcd as config center for service governance. package main import ( - "config-consul/consul" + "github.com/kitex-contrib/config-consul/consul" "context" "log" - consulserver "config-consul/server" + consulserver "github.com/kitex-contrib/config-consul/server" "github.com/cloudwego/kitex-examples/kitex_gen/api" "github.com/cloudwego/kitex-examples/kitex_gen/api/echo" @@ -41,11 +46,11 @@ func (s *EchoImpl) Echo(ctx context.Context, req *api.Request) (resp *api.Respon func main() { klog.SetLevel(klog.LevelDebug) serviceName := "ServiceName" // your server-side service name - etcdClient, _ := consul.NewClient(consul.Options{}) + consulClient, _ := consul.NewClient(consul.Options{}) svr := echo.NewServer( new(EchoImpl), server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: serviceName}), - server.WithSuite(consulserver.NewSuite(serviceName, etcdClient)), + server.WithSuite(consulserver.NewSuite(serviceName, consulClient)), ) if err := svr.Run(); err != nil { log.Println("server stopped with error:", err) @@ -56,6 +61,7 @@ func main() { ``` + #### Client ```go @@ -63,13 +69,13 @@ func main() { package main import ( - "config-consul/consul" - "config-consul/utils" + "github.com/kitex-contrib/config-consul/consul" + "github.com/kitex-contrib/config-consul/utils" "context" "log" "time" - consulclient "config-consul/client" + consulclient "github.com/kitex-contrib/config-consul/client" "github.com/cloudwego/kitex-examples/kitex_gen/api" "github.com/cloudwego/kitex-examples/kitex_gen/api/echo" @@ -118,11 +124,13 @@ func main() { } ``` + ### Consul Configuration #### CustomFunction -Provide the mechanism to custom the etcd parameter `Key`. +Provide the mechanism to custom the consul parameter `Key`. + ```go type Key struct { Type ConfigType @@ -131,17 +139,16 @@ Path string } ``` - #### Options Variable | Variable Name | Default Value | -|------------------|-------------------------------------------------------------| +| ---------------- | ----------------------------------------------------------- | | Addr | 127.0.0.1:8500 | | Prefix | /KitexConfig | | ServerPathFormat | {{.ServerServiceName}}/{{.Category}} | | ClientPathFormat | {{.ClientServiceName}}/{{.ServerServiceName}}/{{.Category}} | | DataCenter | dc1 | -| Timeout | 5 * time.Second | +| Timeout | 5 \* time.Second | | NamespaceId | | | Token | | | Partition | | @@ -149,17 +156,19 @@ Path string | ConfigParser | defaultConfigParser | #### Governance Policy + > The configPath and configPrefix in the following example use default values, the service name is `ServiceName` and the client name is `ClientName`. ##### Rate Limit Category=limit + > Currently, current limiting only supports the server side, so ClientServiceName is empty. [JSON Schema](https://github.com/cloudwego/kitex/blob/develop/pkg/limiter/item_limiter.go#L33) | Variable | Introduction | -|------------------|------------------------------------| -| connection_limit | Maximum concurrent connections | -| qps_limit | Maximum request number every 100ms | +| ---------------- | ---------------------------------- | +| connection_limit | Maximum concurrent connections | +| qps_limit | Maximum request number every 100ms | Example: @@ -179,12 +188,13 @@ Note: - connection_limit and qps_limit can be configured independently, e.g. connection_limit = 100, qps_limit = 0 ##### Retry Policy Category=retry + [JSON Schema](https://github.com/cloudwego/kitex/blob/develop/pkg/retry/policy.go#L63) | Variable | Introduction | -|-------------------------------|------------------------------------------------| -| type | 0: failure_policy 1: backup_policy | -| failure_policy.backoff_policy | Can only be set one of `fixed` `none` `random` | +| ----------------------------- | ---------------------------------------------- | +| type | 0: failure_policy 1: backup_policy | +| failure_policy.backoff_policy | Can only be set one of `fixed` `none` `random` | Example: @@ -192,43 +202,44 @@ Example: ```json { - "*": { - "enable": true, - "type": 0, - "failure_policy": { - "stop_policy": { - "max_retry_times": 3, - "max_duration_ms": 2000, - "cb_policy": { - "error_rate": 0.3 - } - }, - "backoff_policy": { - "backoff_type": "fixed", - "cfg_items": { - "fix_ms": 50 - } - }, - "retry_same_node": false + "*": { + "enable": true, + "type": 0, + "failure_policy": { + "stop_policy": { + "max_retry_times": 3, + "max_duration_ms": 2000, + "cb_policy": { + "error_rate": 0.3 + } + }, + "backoff_policy": { + "backoff_type": "fixed", + "cfg_items": { + "fix_ms": 50 } - }, - "echo": { - "enable": true, - "type": 1, - "backup_policy": { - "retry_delay_ms": 100, - "retry_same_node": false, - "stop_policy": { - "max_retry_times": 2, - "max_duration_ms": 300, - "cb_policy": { - "error_rate": 0.2 - } - } + }, + "retry_same_node": false + } + }, + "echo": { + "enable": true, + "type": 1, + "backup_policy": { + "retry_delay_ms": 100, + "retry_same_node": false, + "stop_policy": { + "max_retry_times": 2, + "max_duration_ms": 300, + "cb_policy": { + "error_rate": 0.2 } + } } + } } ``` + Note: retry.Container has built-in support for specifying the default configuration using the `*` wildcard (see the [getRetryer](https://github.com/cloudwego/kitex/blob/v0.5.1/pkg/retry/retryer.go#L240) method for details). ##### RPC Timeout Category=rpc_timeout @@ -251,6 +262,7 @@ Example: } } ``` + Note: The circuit breaker implementation of kitex does not currently support changing the global default configuration (see [initServiceCB](https://github.com/cloudwego/kitex/blob/v0.5.1/pkg/circuitbreak/cbsuite.go#L195) for details). ##### Circuit Break: Category=circuit_break @@ -258,8 +270,8 @@ Note: The circuit breaker implementation of kitex does not currently support cha [JSON Schema](https://github.com/cloudwego/kitex/blob/develop/pkg/circuitbreak/item_circuit_breaker.go#L30) | Variable | Introduction | -|------------|-----------------------------------| -| min_sample | Minimum statistical sample number | +| ---------- | --------------------------------- | +| min_sample | Minimum statistical sample number | Example: @@ -271,18 +283,18 @@ The echo method uses the following configuration (0.3, 100) and other methods us { "echo": { "enable": true, - "err_rate": 0.3, - "min_sample": 100 + "err_rate": 0.3, + "min_sample": 100 } } ``` + ### More Info Refer to [example](https://github.com/kitex-contrib/config-consul/tree/main/example) for more usage. ## Compatibility -the version of Go must >=1.19 +the version of Go must >=1.20 maintained by: [hiahia12](https://github.com/hiahia12) - diff --git a/README_CN.md b/README_CN.md index 43dc470..01b2bb5 100644 --- a/README_CN.md +++ b/README_CN.md @@ -1,9 +1,13 @@ -# config-consul +# config-consul [English](./README.md) 使用 **consul** 作为 **Kitex** 的服务治理配置中心 +## 安装 + +`go get github.com/kitex-contrib/config-consul` + ## 用法 ### 基本使用 @@ -15,11 +19,11 @@ package main import ( - "config-consul/consul" + "github.com/kitex-contrib/config-consul/consul" "context" "log" - consulserver "config-consul/server" + consulserver "github.com/kitex-contrib/config-consul/server" "github.com/cloudwego/kitex-examples/kitex_gen/api" "github.com/cloudwego/kitex-examples/kitex_gen/api/echo" @@ -42,11 +46,11 @@ func (s *EchoImpl) Echo(ctx context.Context, req *api.Request) (resp *api.Respon func main() { klog.SetLevel(klog.LevelDebug) serviceName := "ServiceName" // your server-side service name - etcdClient, _ := consul.NewClient(consul.Options{}) + consulClient, _ := consul.NewClient(consul.Options{}) svr := echo.NewServer( new(EchoImpl), server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: serviceName}), - server.WithSuite(consulserver.NewSuite(serviceName, etcdClient)), + server.WithSuite(consulserver.NewSuite(serviceName, consulClient)), ) if err := svr.Run(); err != nil { log.Println("server stopped with error:", err) @@ -65,13 +69,13 @@ func main() { package main import ( - "config-consul/consul" - "config-consul/utils" + "github.com/kitex-contrib/config-consul/consul" + "github.com/kitex-contrib/config-consul/utils" "context" "log" "time" - consulclient "config-consul/client" + consulclient "github.com/kitex-contrib/config-consul/client" "github.com/cloudwego/kitex-examples/kitex_gen/api" "github.com/cloudwego/kitex-examples/kitex_gen/api/echo" @@ -126,6 +130,7 @@ func main() { #### CustomFunction 允许用户自定义 consul 的参数来自定义参数 `Key`. + ```go type Key struct { Type ConfigType @@ -143,26 +148,27 @@ Path string | ServerPathFormat | {{.ServerServiceName}}/{{.Category}} | | ClientPathFormat | {{.ClientServiceName}}/{{.ServerServiceName}}/{{.Category}} | | DataCenter | dc1 | -| Timeout | 5 * time.Second | +| Timeout | 5 \* time.Second | | NamespaceId | | | Token | | | Partition | | | LoggerConfig | NULL | | ConfigParser | defaultConfigParser | - #### 治理策略 + 下面例子中的 configPath 以及 configPrefix 均使用默认值,服务名称为 ServiceName,客户端名称为 ClientName ##### 限流 Category=limit + > 限流目前只支持服务端,所以 ClientServiceName 为空。 [JSON Schema](https://github.com/cloudwego/kitex/blob/develop/pkg/limiter/item_limiter.go#L33) -| 字段 | 说明 | -|------------------|------------------| -| connection_limit | 最大并发数量 | -| qps_limit | 每 100ms 内的最大请求数量 | +| 字段 | 说明 | +| ---------------- | ------------------------- | +| connection_limit | 最大并发数量 | +| qps_limit | 每 100ms 内的最大请求数量 | 例子: @@ -170,10 +176,11 @@ Path string ```json { - "connection_limit": 100, - "qps_limit": 2000 + "connection_limit": 100, + "qps_limit": 2000 } ``` + 注: - 限流配置的粒度是 Server 全局,不分 client、method @@ -184,10 +191,10 @@ Path string [JSON Schema](https://github.com/cloudwego/kitex/blob/develop/pkg/retry/policy.go#L63) -| 参数 | 说明 | -|-------------------------------|------------------------------------| -| type | 0: failure_policy 1: backup_policy | -| failure_policy.backoff_policy | 可以设置的策略: `fixed` `none` `random` | +| 参数 | 说明 | +| ----------------------------- | ---------------------------------------- | +| type | 0: failure_policy 1: backup_policy | +| failure_policy.backoff_policy | 可以设置的策略: `fixed` `none` `random` | 例子: @@ -195,44 +202,45 @@ Path string ```json { - "*": { - "enable": true, - "type": 0, - "failure_policy": { - "stop_policy": { - "max_retry_times": 3, - "max_duration_ms": 2000, - "cb_policy": { - "error_rate": 0.3 - } - }, - "backoff_policy": { - "backoff_type": "fixed", - "cfg_items": { - "fix_ms": 50 - } - }, - "retry_same_node": false + "*": { + "enable": true, + "type": 0, + "failure_policy": { + "stop_policy": { + "max_retry_times": 3, + "max_duration_ms": 2000, + "cb_policy": { + "error_rate": 0.3 + } + }, + "backoff_policy": { + "backoff_type": "fixed", + "cfg_items": { + "fix_ms": 50 } - }, - "echo": { - "enable": true, - "type": 1, - "backup_policy": { - "retry_delay_ms": 100, - "retry_same_node": false, - "stop_policy": { - "max_retry_times": 2, - "max_duration_ms": 300, - "cb_policy": { - "error_rate": 0.2 - } - } + }, + "retry_same_node": false + } + }, + "echo": { + "enable": true, + "type": 1, + "backup_policy": { + "retry_delay_ms": 100, + "retry_same_node": false, + "stop_policy": { + "max_retry_times": 2, + "max_duration_ms": 300, + "cb_policy": { + "error_rate": 0.2 } + } } + } } ``` -注:retry.Container 内置支持用 * 通配符指定默认配置(详见 [getRetryer](https://github.com/cloudwego/kitex/blob/v0.5.1/pkg/retry/retryer.go#L240) 方法) + +注:retry.Container 内置支持用 \* 通配符指定默认配置(详见 [getRetryer](https://github.com/cloudwego/kitex/blob/v0.5.1/pkg/retry/retryer.go#L240) 方法) ##### 超时 Category=rpc_timeout @@ -245,7 +253,7 @@ Path string ```json { "*": { - "conn_timeout_ms": 100, + "conn_timeout_ms": 100, "rpc_timeout_ms": 3000 }, "echo": { @@ -254,15 +262,16 @@ Path string } } ``` + 注:kitex 的熔断实现目前不支持修改全局默认配置(详见 [initServiceCB](https://github.com/cloudwego/kitex/blob/v0.5.1/pkg/circuitbreak/cbsuite.go#L195)) ##### 熔断: Category=circuit_break [JSON Schema](https://github.com/cloudwego/kitex/blob/develop/pkg/circuitbreak/item_circuit_breaker.go#L30) -| 参数 | 说明 | -|------------|----------| -| min_sample | 最小的统计样本数 | +| 参数 | 说明 | +| ---------- | ---------------- | +| min_sample | 最小的统计样本数 | 例子: @@ -279,12 +288,13 @@ echo 方法使用下面的配置(0.3、100),其他方法使用全局默认 } } ``` + ### 更多信息 更多示例请参考 [example](https://github.com/kitex-contrib/config-consul/tree/main/example) ## Compatibility -Go 的版本必须 >= 1.19 -主要贡献者: [hiahia12](https://github.com/hiahia12) +Go 的版本必须 >= 1.20 +主要贡献者: [hiahia12](https://github.com/hiahia12) diff --git a/client/circult_breaker.go b/client/circult_breaker.go index c9769e5..9683a92 100644 --- a/client/circult_breaker.go +++ b/client/circult_breaker.go @@ -15,10 +15,11 @@ package client import ( - "config-consul/consul" - "config-consul/utils" "strings" + "github.com/kitex-contrib/config-consul/consul" + "github.com/kitex-contrib/config-consul/utils" + "github.com/cloudwego/kitex/client" "github.com/cloudwego/kitex/pkg/circuitbreak" "github.com/cloudwego/kitex/pkg/klog" diff --git a/client/retry.go b/client/retry.go index fb022fe..e451dd3 100644 --- a/client/retry.go +++ b/client/retry.go @@ -15,8 +15,8 @@ package client import ( - "config-consul/consul" - "config-consul/utils" + "github.com/kitex-contrib/config-consul/consul" + "github.com/kitex-contrib/config-consul/utils" "github.com/cloudwego/kitex/client" "github.com/cloudwego/kitex/pkg/klog" diff --git a/client/rpc_timeout.go b/client/rpc_timeout.go index 3320e76..92aeecc 100644 --- a/client/rpc_timeout.go +++ b/client/rpc_timeout.go @@ -15,8 +15,8 @@ package client import ( - "config-consul/consul" - "config-consul/utils" + "github.com/kitex-contrib/config-consul/consul" + "github.com/kitex-contrib/config-consul/utils" "github.com/cloudwego/kitex/client" "github.com/cloudwego/kitex/pkg/klog" diff --git a/client/suite.go b/client/suite.go index 7a25951..b9b549a 100644 --- a/client/suite.go +++ b/client/suite.go @@ -15,8 +15,8 @@ package client import ( - "config-consul/consul" - "config-consul/utils" + "github.com/kitex-contrib/config-consul/consul" + "github.com/kitex-contrib/config-consul/utils" "github.com/cloudwego/kitex/client" ) diff --git a/example/client/main.go b/example/client/main.go index 54371bd..3a114db 100644 --- a/example/client/main.go +++ b/example/client/main.go @@ -15,13 +15,13 @@ package main import ( - "config-consul/consul" - "config-consul/utils" "context" "log" "time" - consulclient "config-consul/client" + consulclient "github.com/kitex-contrib/config-consul/client" + "github.com/kitex-contrib/config-consul/consul" + "github.com/kitex-contrib/config-consul/utils" "github.com/cloudwego/kitex-examples/kitex_gen/api" "github.com/cloudwego/kitex-examples/kitex_gen/api/echo" diff --git a/example/server/main.go b/example/server/main.go index f04255e..93900e2 100644 --- a/example/server/main.go +++ b/example/server/main.go @@ -15,11 +15,11 @@ package main import ( - "config-consul/consul" "context" "log" - consulserver "config-consul/server" + "github.com/kitex-contrib/config-consul/consul" + consulserver "github.com/kitex-contrib/config-consul/server" "github.com/cloudwego/kitex-examples/kitex_gen/api" "github.com/cloudwego/kitex-examples/kitex_gen/api/echo" @@ -42,11 +42,11 @@ func (s *EchoImpl) Echo(ctx context.Context, req *api.Request) (resp *api.Respon func main() { klog.SetLevel(klog.LevelDebug) serviceName := "ServiceName" // your server-side service name - etcdClient, _ := consul.NewClient(consul.Options{}) + consulClient, _ := consul.NewClient(consul.Options{}) svr := echo.NewServer( new(EchoImpl), server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: serviceName}), - server.WithSuite(consulserver.NewSuite(serviceName, etcdClient)), + server.WithSuite(consulserver.NewSuite(serviceName, consulClient)), ) if err := svr.Run(); err != nil { log.Println("server stopped with error:", err) diff --git a/go.mod b/go.mod index b64927f..7f845e9 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ -module config-consul +module github.com/kitex-contrib/config-consul -go 1.19 +go 1.20 require ( github.com/cloudwego/kitex v0.8.0 @@ -38,7 +38,6 @@ require ( github.com/hashicorp/go-rootcerts v1.0.2 // indirect github.com/hashicorp/go-version v1.6.0 // indirect github.com/hashicorp/golang-lru v0.5.4 // indirect - github.com/hashicorp/hcl v1.0.0 // indirect github.com/hashicorp/serf v0.10.1 // indirect github.com/iancoleman/strcase v0.2.0 // indirect github.com/jhump/protoreflect v1.8.2 // indirect diff --git a/go.sum b/go.sum index 240f5bd..c3ec6e2 100644 --- a/go.sum +++ b/go.sum @@ -206,8 +206,6 @@ github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09 github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= -github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= -github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= github.com/hashicorp/mdns v1.0.4/go.mod h1:mtBihi+LeNXGtG8L9dX59gAEa12BDtBQSp4v/YAJqrc= github.com/hashicorp/memberlist v0.5.0 h1:EtYPN8DpAURiapus508I4n9CzHs2W+8NZGbmmR/prTM= diff --git a/server/limiter.go b/server/limiter.go index 2218cc3..465a1d7 100644 --- a/server/limiter.go +++ b/server/limiter.go @@ -15,10 +15,11 @@ package server import ( - "config-consul/consul" - "config-consul/utils" "sync/atomic" + "github.com/kitex-contrib/config-consul/consul" + "github.com/kitex-contrib/config-consul/utils" + "github.com/cloudwego/kitex/pkg/klog" "github.com/cloudwego/kitex/pkg/limit" "github.com/cloudwego/kitex/pkg/limiter" diff --git a/server/suit.go b/server/suit.go index fc0ae5d..d3e6a1a 100644 --- a/server/suit.go +++ b/server/suit.go @@ -15,8 +15,8 @@ package server import ( - "config-consul/consul" - "config-consul/utils" + "github.com/kitex-contrib/config-consul/consul" + "github.com/kitex-contrib/config-consul/utils" "github.com/cloudwego/kitex/server" ) diff --git a/utils/options.go b/utils/options.go index 0407865..e50d710 100644 --- a/utils/options.go +++ b/utils/options.go @@ -14,7 +14,7 @@ package utils -import "config-consul/consul" +import "github.com/kitex-contrib/config-consul/consul" // Option is used to custom Options. type Option interface {