Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: complete the extension #2

Merged
merged 27 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8d6c235
fix:example module and panic error
XZ0730 Oct 4, 2023
165d5d3
fix:retryContainer init and deal with deleted changes
XZ0730 Oct 4, 2023
208fa5b
style: Delete TODO.md
XZ0730 Oct 4, 2023
d6877c0
fix:option extension and compatibility of new suite param
XZ0730 Oct 5, 2023
1adcb08
Merge branch 'dev' of https://github.com/XZ0730/config-apollo into dev
XZ0730 Oct 5, 2023
f2494fb
fix:code lint
XZ0730 Oct 5, 2023
f7f607d
fix:option extend and README file
XZ0730 Oct 9, 2023
8421e7c
fix: go.mod dependencies bug
XZ0730 Oct 9, 2023
c968d59
fix:README|delete some useless param|close listen
XZ0730 Oct 15, 2023
c2244f3
style:ci-lint
XZ0730 Oct 15, 2023
a923e17
style:README
XZ0730 Oct 15, 2023
6e68032
fix:multi-connection of listener and previous bugs
XZ0730 Nov 24, 2023
93e11b9
fix:update license
XZ0730 Nov 24, 2023
924440d
chore: uopdate go.mod
li-jin-gou Nov 24, 2023
080fd6b
fix:sync commit
XZ0730 Nov 24, 2023
13368e2
fix:nil map problem caused by temporary variables
XZ0730 Nov 25, 2023
fa299bd
fix|style: readme file and some specification
XZ0730 Nov 25, 2023
3544758
chore: rename license
li-jin-gou Nov 25, 2023
5b976f4
fix:deregister with uniqueID
XZ0730 Nov 25, 2023
70ae229
Merge branch 'dev' of https://github.com/XZ0730/config-apollo into dev
XZ0730 Nov 25, 2023
6b589bc
Merge branch 'dev' of https://github.com/XZ0730/config-apollo into dev
XZ0730 Nov 25, 2023
567749a
chore: add stack
li-jin-gou Nov 25, 2023
130a782
Merge branch 'dev' of https://github.com/XZ0730/config-apollo into dev
XZ0730 Nov 26, 2023
b3f0bac
feat:apollo_test file
XZ0730 Nov 26, 2023
8928455
fix:license supplement
XZ0730 Nov 26, 2023
442b66d
fix:data race between goroutine in test
XZ0730 Nov 26, 2023
4951b38
chore: modify .gitignore
li-jin-gou Nov 26, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
290 changes: 289 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,289 @@
# .github
# config-apollo (*This is a community driven project*)

[中文](https://github.com/kitex-contrib/config-apollo/blob/main/README_CN.md)
XZ0730 marked this conversation as resolved.
Show resolved Hide resolved

Apollo as config centre.

## How to use?

### Basic

#### Server

```go
package main

import (
"context"
"log"
"net"

"github.com/cloudwego/kitex-examples/kitex_gen/api"
li-jin-gou marked this conversation as resolved.
Show resolved Hide resolved
"github.com/cloudwego/kitex-examples/kitex_gen/api/echo"
"github.com/cloudwego/kitex/pkg/klog"
li-jin-gou marked this conversation as resolved.
Show resolved Hide resolved
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/cloudwego/kitex/server"
"github.com/kitex-contrib/config-apollo/apollo"
apolloserver "github.com/kitex-contrib/config-apollo/server"
"github.com/kitex-contrib/config-apollo/utils"
)

type configLog struct{}

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

var _ api.Echo = &EchoImpl{}

// EchoImpl implements the last service interface defined in the IDL.
type EchoImpl struct{}

// Echo implements the Echo interface.
func (s *EchoImpl) Echo(ctx context.Context, req *api.Request) (resp *api.Response, err error) {
klog.Info("echo called")
return &api.Response{Message: req.Message}, nil
}

func main() {
klog.SetLevel(klog.LevelDebug)
apolloClient, err := apollo.NewClient(apollo.Options{})
if err != nil {
panic(err)
}
serviceName := "ServiceName"
XZ0730 marked this conversation as resolved.
Show resolved Hide resolved
addr, err := net.ResolveTCPAddr("tcp", "localhost:8899")
if err != nil {
panic(err)
}

cl := &configLog{}

svr := echo.NewServer(
new(EchoImpl),
server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: serviceName}),
server.WithSuite(apolloserver.NewSuite(serviceName, apolloClient, cl)),
server.WithServiceAddr(addr),
)
if err := svr.Run(); err != nil {
log.Println("server stopped with error:", err)
} else {
log.Println("server stopped")
}
}
```

#### Client

```go
package main

import (
"context"
"log"
"time"

"github.com/cloudwego/kitex-examples/kitex_gen/api"
li-jin-gou marked this conversation as resolved.
Show resolved Hide resolved
"github.com/cloudwego/kitex-examples/kitex_gen/api/echo"
"github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/pkg/klog"
"github.com/kitex-contrib/config-apollo/apollo"
apolloclient "github.com/kitex-contrib/config-apollo/client"
"github.com/kitex-contrib/config-apollo/utils"
)

type configLog struct{}

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

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

apolloClient, err := apollo.NewClient(apollo.Options{})
if err != nil {
panic(err)
}
cl := &configLog{}

serviceName := "ServiceName"
clientName := "ClientName"
client, err := echo.NewClient(
serviceName,
client.WithHostPorts("0.0.0.0:8899"),
client.WithSuite(apolloclient.NewSuite(serviceName, clientName, apolloClient, cl)),
)
if err != nil {
log.Fatal(err)
}
for {
req := &api.Request{Message: "my request"}
resp, err := client.Echo(context.Background(), req)
if err != nil {
klog.Errorf("take request error: %v", err)
} else {
klog.Infof("receive response %v", resp)
}
time.Sleep(time.Second * 10)
}
}
```

### Apollo Configuration

Initialize the client according to the parameters in Options. After establishing the link, the suite will subscribe to the corresponding configuration based on 'AppId', 'NameSpace', 'ServiceName', or 'ClientName' and dynamically update its own policy. Please refer to the following environment variables for specific parameters.
XZ0730 marked this conversation as resolved.
Show resolved Hide resolved

The configured format supports' json 'by default, and can be customized using the function [SetParser] for format parsing. In' NewSuite ', the field function in the instance that implements the' Option 'interface is used to modify the format of the subscription function.
####

#### CustomFunction

Allow users to use instances of custom implementation Option interfaces to customize Apollo parameters

#### Options Variable

| 参数 | 变量默认值 | 作用 |
| :-------------- | :-------------------------------------------: | ------------------------------------------------------------ |
| ConfigServerURL | 127.0.0.1:8080 | apollo config service address |
| nameSpaceID | {{.Category}} | WithSuite specifies the rule method for post rendering |
XZ0730 marked this conversation as resolved.
Show resolved Hide resolved
| AppID | KitexApp | appid of apollo |
XZ0730 marked this conversation as resolved.
Show resolved Hide resolved
| ClientKeyFormat | {{.ClientServiceName}}.{{.ServerServiceName}} | Using the go [template](https://pkg.go.dev/text/template) syntax to render and generate the corresponding ID, using two metadata: `ClientServiceName` and `ServiceName` |
| ServerKeyFormat | {{.ServerServiceName}}.{{.Category}} | Using the go [template](https://pkg.go.dev/text/template) Syntax rendering generates corresponding IDs, using 'ServiceName' as a single metadata |
| cluster | default | Using fixed values or dynamic rendering, the usage is the same as KeyFormat |
XZ0730 marked this conversation as resolved.
Show resolved Hide resolved

#### Governance Policy

> The namespace in the following example uses fixed policy values, with default values for APPID and Cluster. 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 |
Example:
```
namespace: `limit`
key: `ServiceName`

{
"connection_limit": 100,
"qps_limit": 2000
}
```

Note:

- The granularity of the current limit configuration is server global, regardless of client or method.
- Not configured or value is 0 means not enabled.
- 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` |

Example:
```
namespace: `retry`
key: `ClientName.ServiceName`
{
"*": {
"enable": true,
"type": 0,
"failure_policy": {
"stop_policy": {
"max_retry_times": 3,
"max_duration_ms": 2000,
"cb_policy": {
"error_rate": 0.5
XZ0730 marked this conversation as resolved.
Show resolved Hide resolved
}
},
"backoff_policy": {
"backoff_type": "fixed",
"cfg_items": {
"fix_ms": 50
}
},
"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

[JSON Schema](https://github.com/cloudwego/kitex/blob/develop/pkg/rpctimeout/item_rpc_timeout.go#L42)

Example:
```
namespace: `rpc_timeout`
key: `ClientName.ServiceName`
{
"*": {
"conn_timeout_ms": 100,
"rpc_timeout_ms": 3000
},
"echo": {
"conn_timeout_ms": 50,
"rpc_timeout_ms": 1000
}
}
```
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

[JSON Schema](https://github.com/cloudwego/kitex/blob/develop/pkg/circuitbreak/item_circuit_breaker.go#L30)

|Variable|Introduction|
|----|----|
|min_sample| Minimum statistical sample number|
Example:
```
The echo method uses the following configuration (0.3, 100) and other methods use the global default configuration (0.5, 200)
namespace: `circuit_break`
key: `ClientName.ServiceName`
{
"echo": {
"enable": true,
"err_rate": 0.3,
"min_sample": 100
}
}
```
### More Info

Refer to [example](https://github.com/kitex-contrib/examples/config/apollo) for more usage.

maintained by: [xz](https://github.com/XZ0730)

Loading
Loading