-
Notifications
You must be signed in to change notification settings - Fork 33
feat: add nacos v2 #100
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
Merged
Merged
feat: add nacos v2 #100
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2e3e2be
feat: add nacos v2
Skyenought 15102c9
feat: add nacos v2
Skyenought 9084df4
feat: add nacos v2
Skyenought 902e9fd
feat: add nacos v2
Skyenought e82549a
feat: add nacos v2
Skyenought 21fbed0
feat: add nacos v2
Skyenought 517e264
feat: add nacos v2
Skyenought 056bcec
feat: add nacos v2
Skyenought c377c69
feat: add nacos v2
Skyenought File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
NACOS_VERSION ?= 2.0.3 | ||
|
||
prepare: | ||
docker pull nacos/nacos-server:$(NACOS_VERSION) | ||
docker run --name nacos-quick -e MODE=standalone -p 8848:8848 -p 9848:9848 -d nacos/nacos-server:$(NACOS_VERSION) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,244 @@ | ||
# nacos (*This is a community driven project*) | ||
|
||
[中文](README_CN.md) | ||
|
||
Nacos as service discovery for Hertz. | ||
|
||
## How to use? | ||
|
||
### Server | ||
|
||
**[example/standard/server/main.go](examples/standard/server/main.go)** | ||
|
||
```go | ||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/cloudwego/hertz/pkg/app" | ||
"github.com/cloudwego/hertz/pkg/app/server" | ||
"github.com/cloudwego/hertz/pkg/app/server/registry" | ||
"github.com/cloudwego/hertz/pkg/common/utils" | ||
"github.com/cloudwego/hertz/pkg/protocol/consts" | ||
"github.com/hertz-contrib/registry/nacos/v2" | ||
) | ||
|
||
func main() { | ||
addr := "127.0.0.1:8888" | ||
r, err := nacos.NewDefaultNacosRegistry() | ||
if err != nil { | ||
log.Fatal(err) | ||
return | ||
} | ||
h := server.Default( | ||
server.WithHostPorts(addr), | ||
server.WithRegistry(r, ®istry.Info{ | ||
ServiceName: "hertz.test.demo", | ||
8000 Addr: utils.NewNetAddr("tcp", addr), | ||
Weight: 10, | ||
Tags: nil, | ||
}), | ||
) | ||
h.GET("/ping", func(c context.Context, ctx *app.RequestContext) { | ||
ctx.JSON(consts.StatusOK, utils.H{"ping": "pong"}) | ||
}) | ||
h.Spin() | ||
} | ||
``` | ||
|
||
### Client | ||
|
||
**[example/standard/client/main.go](examples/standard/client/main.go)** | ||
|
||
```go | ||
import ( | ||
"context" | ||
"log" | ||
|
||
"github.com/cloudwego/hertz/pkg/app/client" | ||
"github.com/cloudwego/hertz/pkg/app/middlewares/client/sd" | ||
"github.com/cloudwego/hertz/pkg/common/config" | ||
"github.com/cloudwego/hertz/pkg/common/hlog" | ||
"github.com/hertz-contrib/registry/nacos/v2" | ||
) | ||
|
||
func main() { | ||
client, err := client.NewClient() | ||
if err != nil { | ||
panic(err) | ||
} | ||
r, err := nacos.NewDefaultNacosResolver() | ||
if err != nil { | ||
log.Fatal(err) | ||
return | ||
} | ||
client.Use(sd.Discovery(r)) | ||
// ... | ||
} | ||
|
||
``` | ||
|
||
## How to run example? | ||
|
||
### run docker | ||
|
||
- make prepare | ||
|
||
```bash | ||
make prepare | ||
``` | ||
|
||
### run server | ||
|
||
```go | ||
go run ./examples/standard/server/main.go | ||
``` | ||
|
||
### run client | ||
|
||
```go | ||
go run ./examples/standard/client/main.go | ||
``` | ||
|
||
```go | ||
2022/07/26 13:52:47.310617 main.go:46: [Info] code = 200, body ={"ping":"pong"} | ||
2022/07/26 13:52:47.311019 main.go:46: [Info] code = 200, body ={"ping":"pong"} | ||
2022/07/26 13:52:47.311186 main.go:46: [Info] code = 200, body ={"ping":"pong"} | ||
2022/07/26 13:52:47.311318 main.go:46: [Info] code = 200, body ={"ping":"pong"} | ||
2022/07/26 13:52:47.311445 main.go:46: [Info] code = 200, body ={"ping":"pong"} | ||
2022/07/26 13:52:47.311585 main.go:46: [Info] code = 200, body ={"ping":"pong"} | ||
2022/07/26 13:52:47.311728 main.go:46: [Info] code = 200, body ={"ping":"pong"} | ||
2022/07/26 13:52:47.311858 main.go:46: [Info] code = 200, body ={"ping":"pong"} | ||
2022/07/26 13:52:47.311977 main.go:46: [Info] code = 200, body ={"ping":"pong"} | ||
2022/07/26 13:52:47.312107 main.go:46: [Info] code = 200, body ={"ping":"pong"} | ||
``` | ||
|
||
## Custom Nacos Client Configuration | ||
|
||
### Server | ||
|
||
**[example/custom_config/server/main.go](examples/custom_config/server/main.go)** | ||
|
||
```go | ||
import ( | ||
"context" | ||
"github.com/cloudwego/hertz/pkg/app" | ||
"github.com/cloudwego/hertz/pkg/app/server" | ||
"github.com/cloudwego/hertz/pkg/app/server/registry" | ||
"github.com/cloudwego/hertz/pkg/common/utils" | ||
"github.com/cloudwego/hertz/pkg/protocol/consts" | ||
"github.com/hertz-contrib/registry/nacos/v2" | ||
"github.com/nacos-group/nacos-sdk-go/v2/clients" | ||
"github.com/nacos-group/nacos-sdk-go/v2/common/constant" | ||
"github.com/nacos-group/nacos-sdk-go/v2/vo" | ||
) | ||
|
||
func main() { | ||
sc := []constant.ServerConfig{ | ||
*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", | ||
} | ||
|
||
cli, err := clients.NewNamingClient( | ||
vo.NacosClientParam{ | ||
ClientConfig: &cc, | ||
ServerConfigs: sc, | ||
}, | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
addr := "127.0.0.1:8888" | ||
r := nacos.NewNacosRegistry(cli) | ||
h := server.Default( | ||
server.WithHostPorts(addr), | ||
server.WithRegistry(r, ®istry.Info{ | ||
ServiceName: "hertz.test.demo", | ||
Addr: utils.NewNetAddr("tcp", addr), | ||
Weight: 10, | ||
Tags: nil, | ||
}), | ||
) | ||
// ... | ||
h.Spin() | ||
} | ||
|
||
``` | ||
|
||
### Client | ||
|
||
**[example/custom_config/client/main.go](examples/custom_config/client/main.go)** | ||
|
||
```go | ||
import ( | ||
"context" | ||
"github.com/cloudwego/hertz/pkg/app/client" | ||
"github.com/cloudwego/hertz/pkg/app/middlewares/client/sd" | ||
"github.com/cloudwego/hertz/pkg/common/config" | ||
"github.com/cloudwego/hertz/pkg/common/hlog" | ||
"github.com/hertz-contrib/registry/nacos/v2" | ||
"github.com/nacos-group/nacos-sdk-go/v2/clients" | ||
"github.com/nacos-group/nacos-sdk-go/v2/common/constant" | ||
"github.com/nacos-group/nacos-sdk-go/v2/vo" | ||
) | ||
|
||
func main() { | ||
cli, err := client.NewClient() | ||
if err != nil { | ||
panic(err) | ||
} | ||
sc := []constant.ServerConfig{ | ||
*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", | ||
} | ||
|
||
nacosCli, err := clients.NewNamingClient( | ||
vo.NacosClientParam{ | ||
ClientConfig: &cc, | ||
ServerConfigs: sc, | ||
} | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
r := nacos.NewNacosResolver(nacosCli) | ||
cli.Use(sd.Discovery(r)) | ||
// ... | ||
} | ||
|
||
``` | ||
|
||
## Environment Variable | ||
|
||
| Environment Variable Name | Environment Variable Default Value | Environment Variable Introduction | | ||
| ------------------------- | ---------------------------------- | --------------------------------- | | ||
| serverAddr | 127.0.0.1 | nacos server address | | ||
| serverPort | 8848 | nacos server port | | ||
| namespace | | the namespaceId of nacos | | ||
|
||
|
||
## Compatibility | ||
|
||
- This package use Nacos2.x client. | ||
|
||
- Nacos2.x detail [see](https://nacos.io/en-us/docs/v2/upgrading/2.0.0-compatibility.html) | ||
|
||
- Supported Go version over 1.16 | ||
|
||
- Supported Nacos version over 2.x |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.