Skip to content

Commit

Permalink
feat:nacos v2 (#28)
Browse files Browse the repository at this point in the history
* feat:nacos v2

* feat:nacos v2

* fix:example push

* docs:readme
  • Loading branch information
XZ0730 authored Jan 6, 2024
1 parent 6fc2d1b commit 1c2367c
Show file tree
Hide file tree
Showing 15 changed files with 2,798 additions and 0 deletions.
19 changes: 19 additions & 0 deletions v2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# registry-nacos (*This is a community driven project*)

[中文](https://github.com/kitex-contrib/registry-nacos/blob/main/v2/README_CN.md)

Nacos as service discovery.

## 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

### More Info

Refer to [example](example) for more usage.
20 changes: 20 additions & 0 deletions v2/README_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# registry-nacos (*这是一个由社区驱动的项目*)

[English](https://github.com/kitex-contrib/registry-nacos/blob/main/v2/README.md)

使用 **nacos** 作为 **Kitex** 的注册中心

## 兼容性

- 使用 Nacos2.x 客户端

- Nacos2.x 详细信息[see](https://nacos.io/en-us/docs/v2/upgrading/2.0.0-compatibility.html)

- Go 版本不低于 1.16

- 支持Nacos 2.x

### 更多信息

更多示例请参考 [example](example)

46 changes: 46 additions & 0 deletions v2/example/basic/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2024 CloudWeGo Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"context"
"log"
"time"

"github.com/cloudwego/kitex/client"
"github.com/kitex-contrib/registry-nacos/example/hello/kitex_gen/api"
"github.com/kitex-contrib/registry-nacos/example/hello/kitex_gen/api/hello"
"github.com/kitex-contrib/registry-nacos/v2/resolver"
)

func main() {
r, err := resolver.NewDefaultNacosResolver()
if err != nil {
panic(err)
}
newClient := hello.MustNewClient(
"Hello",
client.WithResolver(r),
client.WithRPCTimeout(time.Second*3),
)
for {
resp, err := newClient.Echo(context.Background(), &api.Request{Message: "Hello"})
if err != nil {
log.Fatal(err)
}
log.Println(resp)
time.Sleep(time.Second)
}
}
54 changes: 54 additions & 0 deletions v2/example/basic/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2024 CloudWeGo Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"context"
"log"
"net"

"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/cloudwego/kitex/server"
"github.com/kitex-contrib/registry-nacos/example/hello/kitex_gen/api"
"github.com/kitex-contrib/registry-nacos/example/hello/kitex_gen/api/hello"
"github.com/kitex-contrib/registry-nacos/v2/registry"
)

type HelloImpl struct{}

func (h *HelloImpl) Echo(_ context.Context, req *api.Request) (resp *api.Response, err error) {
resp = &api.Response{
Message: req.Message,
}
return
}

func main() {
r, err := registry.NewDefaultNacosRegistry()
if err != nil {
panic(err)
}
svr := hello.NewServer(
new(HelloImpl),
server.WithRegistry(r),
server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: "Hello"}),
server.WithServiceAddr(&net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 8080}),
)
if err := svr.Run(); err != nil {
log.Println("server stopped with error:", err)
} else {
log.Println("server stopped")
}
}
74 changes: 74 additions & 0 deletions v2/example/custom-config/client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2024 CloudWeGo Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"context"
"log"
"time"

"github.com/cloudwego/kitex/client"
"github.com/kitex-contrib/registry-nacos/example/hello/kitex_gen/api"
"github.com/kitex-contrib/registry-nacos/example/hello/kitex_gen/api/hello"
"github.com/kitex-contrib/registry-nacos/v2/resolver"
"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() {
// the nacos server config
sc := []constant.ServerConfig{
*constant.NewServerConfig("127.0.0.1", 8848,
func(sc *constant.ServerConfig) {
// it is not recommended to modify GRPC-Port unless necessary,default by server port +1000
sc.GrpcPort = 9848
}),
}

// the nacos client config
cc := constant.ClientConfig{
NamespaceId: "public",
TimeoutMs: 5000,
NotLoadCacheAtStart: true,
LogDir: "/tmp/nacos/log",
CacheDir: "/tmp/nacos/cache",
LogLevel: "info",
// more ...
}

cli, err := clients.NewNamingClient(
vo.NacosClientParam{
ClientConfig: &cc,
ServerConfigs: sc,
},
)
if err != nil {
panic(err)
}
newClient := hello.MustNewClient(
"Hello",
client.WithResolver(resolver.NewNacosResolver(cli)),
client.WithRPCTimeout(time.Second*3),
)
for {
resp, err := newClient.Echo(context.Background(), &api.Request{Message: "Hello"})
if err != nil {
log.Fatal(err)
}
log.Println(resp)
time.Sleep(time.Second)
}
}
81 changes: 81 additions & 0 deletions v2/example/custom-config/server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2024 CloudWeGo Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"context"
"log"

"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/cloudwego/kitex/server"
"github.com/kitex-contrib/registry-nacos/example/hello/kitex_gen/api"
"github.com/kitex-contrib/registry-nacos/example/hello/kitex_gen/api/hello"
"github.com/kitex-contrib/registry-nacos/v2/registry"
"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"
)

type HelloImpl struct{}

func (h *HelloImpl) Echo(_ context.Context, req *api.Request) (resp *api.Response, err error) {
resp = &api.Response{
Message: req.Message,
}
return
}

func main() {
// the nacos server config
sc := []constant.ServerConfig{
*constant.NewServerConfig("127.0.0.1", 8848,
func(sc *constant.ServerConfig) {
// it is not recommended to modify GRPC-Port unless necessary,default by server port +1000
sc.GrpcPort = 9848
}),
}

// the nacos client config
cc := constant.ClientConfig{
NamespaceId: "public",
TimeoutMs: 5000,
NotLoadCacheAtStart: true,
LogDir: "/tmp/nacos/log",
CacheDir: "/tmp/nacos/cache",
LogLevel: "info",
// more ...
}

cli, err := clients.NewNamingClient(
vo.NacosClientParam{
ClientConfig: &cc,
ServerConfigs: sc,
},
)
if err != nil {
panic(err)
}

svr := hello.NewServer(
new(HelloImpl),
server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: "Hello"}),
server.WithRegistry(registry.NewNacosRegistry(cli)),
)
if err := svr.Run(); err != nil {
log.Println("server stopped with error:", err)
} else {
log.Println("server stopped")
}
}
25 changes: 25 additions & 0 deletions v2/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module github.com/kitex-contrib/registry-nacos/v2

go 1.16

require (
github.com/aliyun/alibaba-cloud-sdk-go v1.61.1800 // indirect
github.com/cloudwego/kitex v0.8.0
github.com/golang/protobuf v1.5.3 // indirect
github.com/kitex-contrib/registry-nacos v0.1.1
github.com/kr/pretty v0.3.0 // indirect
github.com/nacos-group/nacos-sdk-go/v2 v2.2.3
github.com/prometheus/client_model v0.3.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/stretchr/testify v1.8.3
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/genproto v0.0.0-20230223222841-637eb2293923 // indirect
google.golang.org/grpc v1.55.0-dev // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
)

replace github.com/apache/thrift => github.com/apache/thrift v0.13.0
Loading

0 comments on commit 1c2367c

Please sign in to comment.