diff --git a/README.md b/README.md index d435f7c..98daeb2 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,9 @@ Nacos as service discovery. ## How to use? -### Server +### Basic + +#### Server ```go import ( @@ -38,10 +40,9 @@ func main() { } // ... } - ``` -### Client +#### Client ```go import ( @@ -68,9 +69,10 @@ func main() { } ``` -## Custom Nacos Client Configuration +### Custom Nacos Client Configuration + +#### Server -### Server ```go import ( // ... @@ -79,13 +81,12 @@ import ( "github.com/nacos-group/nacos-sdk-go/clients/naming_client" "github.com/nacos-group/nacos-sdk-go/common/constant" "github.com/nacos-group/nacos-sdk-go/vo" - "github.com/cloudwego/kitex/pkg/rpcinfo" // ... ) func main() { // ... sc := []constant.ServerConfig{ - *constant.NewServerConfig("127.0.0.1", 8848), + *constant.NewServerConfig("127.0.0.1", 8848), } cc := constant.ClientConfig{ @@ -96,11 +97,11 @@ func main() { CacheDir: "/tmp/nacos/cache", LogLevel: "info", Username: "your-name", - Password: "your-password" + Password: "your-password", } cli, err := clients.NewNamingClient( - vo.NacosClientParam{ + vo.NacosClientParam{ ClientConfig: &cc, ServerConfigs: sc, }, @@ -108,13 +109,11 @@ func main() { if err != nil { panic(err) } - - svr := echo.NewServer( - new(EchoImpl), - server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: "echo"}), - server.WithRegistry(r), + + svr := echo.NewServer(new(EchoImpl), + server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: "echo"}), + server.WithRegistry(registry.NewNacosRegistry(cli)), ) - if err := svr.Run(); err != nil { log.Println("server stopped with error:", err) } else { @@ -122,10 +121,10 @@ func main() { } // ... } - ``` -### Client +#### Client + ```go import ( // ... @@ -139,23 +138,25 @@ import ( func main() { // ... sc := []constant.ServerConfig{ - *constant.NewServerConfig("127.0.0.1", 8848)} + *constant.NewServerConfig("127.0.0.1", 8848), + } cc := constant.ClientConfig{ - NamespaceId: "public", - TimeoutMs: 5000, - NotLoadCacheAtStart: true, - LogDir: "/tmp/nacos/log", - CacheDir: "/tmp/nacos/cache", - LogLevel: "info", - Username: "your-name", - Password: "your-password" + NamespaceId: "public", + TimeoutMs: 5000, + NotLoadCacheAtStart: true, + LogDir: "/tmp/nacos/log", + CacheDir: "/tmp/nacos/cache", + LogLevel: "info", + Username: "your-name", + Password: "your-password", } - cli,err := clients.NewNamingClient( + cli, err := clients.NewNamingClient( vo.NacosClientParam{ ClientConfig: &cc, ServerConfigs: sc, - },) + }, + ) if err != nil { panic(err) } @@ -167,8 +168,7 @@ func main() { } ``` - -## Environment Variable +### Environment Variable | Environment Variable Name | Environment Variable Default Value | Environment Variable Introduction | | ------------------------- | ---------------------------------- | --------------------------------- | @@ -176,10 +176,12 @@ func main() { | serverPort | 8848 | nacos server port | | namespace | | the namespaceId of nacos | +### More Info +Refer to [example](example) for more usage. ## Compatibility -The server of Nacos2.0 is fully compatible with 1.X nacos-sdk-go. [see](https://nacos.io/en-us/docs/2.0.0-compatibility.html) +The server of Nacos2.0 is fully compatible with 1.X nacos-sdk-go. [see](https://nacos.io/en-us/docs/2.0.0-compatibility.html) maintained by: [baiyutang](https://github.com/baiyutang) diff --git a/README_CN.md b/README_CN.md index f79d917..b60a619 100644 --- a/README_CN.md +++ b/README_CN.md @@ -6,9 +6,9 @@ ## 这个项目应当如何使用? -### 服务端 +### 基本使用 -**registry-nacos/example/server/main.go** +#### 服务端 ```go import ( @@ -18,18 +18,21 @@ import ( "github.com/nacos-group/nacos-sdk-go/clients/naming_client" "github.com/nacos-group/nacos-sdk-go/common/constant" "github.com/nacos-group/nacos-sdk-go/vo" + "github.com/cloudwego/kitex/pkg/rpcinfo" // ... ) func main() { // ... - r, err := registry.NewDefaultNacosRegistry() if err != nil { panic(err) } - - svr := echo.NewServer(new(EchoImpl), server.WithRegistry(r)) + svr := echo.NewServer( + new(EchoImpl), + server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: "echo"}), + server.WithRegistry(r), + ) if err := svr.Run(); err != nil { log.Println("server stopped with error:", err) } else { @@ -37,14 +40,9 @@ func main() { } // ... } - ``` -### 客户端 - -**registry-nacos/example/client/main.go** - -**** +#### 客户端 ```go import ( @@ -58,13 +56,11 @@ import ( ) func main() { - // ... - + // ... r, err := resolver.NewDefaultNacosResolver() if err != nil { panic(err) } - client, err := echo.NewClient("echo", client.WithResolver(r)) if err != nil { log.Fatal(err) @@ -73,9 +69,10 @@ func main() { } ``` -## 自定义 Nacos Client 配置 +### 自定义 Nacos Client 配置 + +#### 服务端 -### 服务端 ```go import ( // ... @@ -89,7 +86,7 @@ import ( func main() { // ... sc := []constant.ServerConfig{ - *constant.NewServerConfig("127.0.0.1", 8848), + *constant.NewServerConfig("127.0.0.1", 8848), } cc := constant.ClientConfig{ @@ -100,11 +97,11 @@ func main() { CacheDir: "/tmp/nacos/cache", LogLevel: "info", Username: "your-name", - Password: "your-password" + Password: "your-password", } cli, err := clients.NewNamingClient( - vo.NacosClientParam{ + vo.NacosClientParam{ ClientConfig: &cc, ServerConfigs: sc, }, @@ -112,8 +109,11 @@ func main() { if err != nil { panic(err) } - - svr := echo.NewServer(new(EchoImpl), server.WithRegistry(registry.NewNacosRegistry(cli))) + + svr := echo.NewServer(new(EchoImpl), + server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: "echo"}), + server.WithRegistry(registry.NewNacosRegistry(cli)), + ) if err := svr.Run(); err != nil { log.Println("server stopped with error:", err) } else { @@ -121,10 +121,10 @@ func main() { } // ... } - ``` -### 客户端 +#### 客户端 + ```go import ( // ... @@ -138,25 +138,27 @@ import ( func main() { // ... sc := []constant.ServerConfig{ - *constant.NewServerConfig("127.0.0.1", 8848)} + *constant.NewServerConfig("127.0.0.1", 8848), + } cc := constant.ClientConfig{ - NamespaceId: "public", - TimeoutMs: 5000, - NotLoadCacheAtStart: true, - LogDir: "/tmp/nacos/log", - CacheDir: "/tmp/nacos/cache", - LogLevel: "info", - Username: "your-name", - Password: "your-password" + NamespaceId: "public", + TimeoutMs: 5000, + NotLoadCacheAtStart: true, + LogDir: "/tmp/nacos/log", + CacheDir: "/tmp/nacos/cache", + LogLevel: "info", + Username: "your-name", + Password: "your-password", } - cli,err := clients.NewNamingClient( + cli, err := clients.NewNamingClient( vo.NacosClientParam{ - ClientConfig: &cc, - ServerConfigs: sc, - },) + ClientConfig: &cc, + ServerConfigs: sc, + }, + ) if err != nil { - panic(err) + panic(err) } client, err := echo.NewClient("echo", client.WithResolver(resolver.NewNacosResolver(cli)) if err != nil { @@ -166,7 +168,7 @@ func main() { } ``` -## **环境变量** +### 环境变量 | 变量名 | 变量默认值 | 作用 | | ------------------------- | ---------------------------------- | --------------------------------- | @@ -174,6 +176,10 @@ func main() { | serverPort | 8848 | nacos 服务器端口 | | namespace | | nacos 中的 namespace Id | +### 更多信息 + +更多示例请参考 [example](example) + ## 兼容性 Nacos 2.0 和 1.X 版本的 nacos-sdk-go 是完全兼容的,[详情](https://nacos.io/en-us/docs/2.0.0-compatibility.html)