-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from zstone12/main
feat: add zookeeper register
- Loading branch information
Showing
14 changed files
with
905 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,25 @@ | ||
Copyright (c) 2013, Samuel Stauffer <samuel@descolada.com> | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above copyright | ||
notice, this list of conditions and the following disclaimer in the | ||
documentation and/or other materials provided with the distribution. | ||
* Neither the name of the author nor the | ||
names of its contributors may be used to endorse or promote products | ||
derived from this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY | ||
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains 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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2012-2020 Mat Ryer, Tyler Bunnell and contributors. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains 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 @@ | ||
ZOOKEEPRER_VERSION ?= 3.7.0 | ||
|
||
prepare: | ||
docker pull zookeeper:$(ZOOKEEPRER_VERSION) | ||
docker run --name zookeeper -p 2181:2181 -d zookeeper:$(ZOOKEEPRER_VERSION) |
This file contains 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,111 @@ | ||
# zookeeper (*This is a community driven project*) | ||
|
||
Zookeeper as service discovery for Hertz. | ||
|
||
## How to use? | ||
|
||
### Server | ||
|
||
**[example/standard/server/main.go](example/standard/server/main.go)** | ||
|
||
```go | ||
import ( | ||
"context" | ||
"time" | ||
|
||
"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/zookeeper" | ||
) | ||
|
||
func main() { | ||
addr := "127.0.0.1:8888" | ||
r, err := zookeeper.NewZookeeperRegistry([]string{"127.0.0.1:2181"}, 40*time.Second) | ||
if err != nil { | ||
panic(err) | ||
} | ||
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.GET("/ping", func(c context.Context, ctx *app.RequestContext) { | ||
ctx.JSON(consts.StatusOK, utils.H{"ping": "pong2"}) | ||
}) | ||
h.Spin() | ||
} | ||
|
||
``` | ||
|
||
### Client | ||
|
||
**[example/standard/client/main.go](example/standard/client/main.go)** | ||
|
||
```go | ||
import ( | ||
"context" | ||
"time" | ||
|
||
"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/zookeeper" | ||
) | ||
|
||
func main() { | ||
cli, err := client.NewClient() | ||
if err != nil { | ||
panic(err) | ||
} | ||
r, err :=zookeeper.NewZookeeperResolver([]string{"127.0.0.1:2181"}, 40*time.Second) | ||
if err != nil { | ||
panic(err) | ||
} | ||
cli.Use(sd.Discovery(r)) | ||
// ... | ||
} | ||
``` | ||
## How to run example? | ||
|
||
### Run docker | ||
|
||
```shell | ||
make prepare | ||
``` | ||
|
||
### Run server | ||
|
||
```go | ||
go run ./example/standard/server/main.go | ||
``` | ||
|
||
### Run client | ||
|
||
```go | ||
go run ./example/standard/client/main.go | ||
``` | ||
|
||
```go | ||
2022/08/21 23:31:59.391243 main.go:44: [Info] code=200,body={"ping":"pong2"} | ||
2022/08/21 23:31:59.391493 main.go:44: [Info] code=200,body={"ping":"pong2"} | ||
2022/08/21 23:31:59.391603 main.go:44: [Info] code=200,body={"ping":"pong2"} | ||
2022/08/21 23:31:59.391714 main.go:44: [Info] code=200,body={"ping":"pong2"} | ||
2022/08/21 23:31:59.391816 main.go:44: [Info] code=200,body={"ping":"pong2"} | ||
2022/08/21 23:31:59.391913 main.go:44: [Info] code=200,body={"ping":"pong2"} | ||
2022/08/21 23:31:59.392039 main.go:44: [Info] code=200,body={"ping":"pong2"} | ||
2022/08/21 23:31:59.392144 main.go:44: [Info] code=200,body={"ping":"pong2"} | ||
2022/08/21 23:31:59.392249 main.go:44: [Info] code=200,body={"ping":"pong2"} | ||
2022/08/21 23:31:59.392379 main.go:44: [Info] code=200,body={"ping":"pong2"} | ||
``` | ||
|
||
## Compatibility | ||
Compatible with server (3.4.0 - 3.7.0), If you want to use older server version, please modify the version in `Makefile` to test. | ||
|
||
[zookeeper server version list]( https://zookeeper.apache.org/documentation.html) |
This file contains 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,45 @@ | ||
// Copyright 2021 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" | ||
"time" | ||
|
||
"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/zookeeper" | ||
) | ||
|
||
func main() { | ||
cli, err := client.NewClient() | ||
if err != nil { | ||
panic(err) | ||
} | ||
r, err := zookeeper.NewZookeeperResolverWithAuth([]string{"127.0.0.1:2181"}, 40*time.Second, "hertzuser", "hertzpass") | ||
if err != nil { | ||
panic(err) | ||
} | ||
cli.Use(sd.Discovery(r)) | ||
for i := 0; i < 10; i++ { | ||
status, body, err := cli.Get(context.Background(), nil, "http://hertz.test.demo/ping", config.WithSD(true)) | ||
if err != nil { | ||
hlog.Fatal(err) | ||
} | ||
hlog.Infof("code=%d,body=%s", status, string(body)) | ||
} | ||
} |
This file contains 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,47 @@ | ||
// Copyright 2021 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" | ||
"time" | ||
|
||
"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/zookeeper" | ||
) | ||
|
||
func main() { | ||
addr := "127.0.0.1:8888" | ||
r, err := zookeeper.NewZookeeperRegistryWithAuth([]string{"127.0.0.1:2181"}, 20*time.Second, "hertzuser", "hertzpass") | ||
if err != nil { | ||
panic(err) | ||
} | ||
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.GET("/ping", func(c context.Context, ctx *app.RequestContext) { | ||
ctx.JSON(consts.StatusOK, utils.H{"ping": "pong2"}) | ||
}) | ||
h.Spin() | ||
} |
This file contains 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,45 @@ | ||
// Copyright 2021 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" | ||
"time" | ||
|
||
"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/zookeeper" | ||
) | ||
|
||
func main() { | ||
cli, err := client.NewClient() | ||
if err != nil { | ||
panic(err) | ||
} | ||
r, err := zookeeper.NewZookeeperResolver([]string{"127.0.0.1:2181"}, 40*time.Second) | ||
if err != nil { | ||
panic(err) | ||
} | ||
cli.Use(sd.Discovery(r)) | ||
for i := 0; i < 10; i++ { | ||
status, body, err := cli.Get(context.Background(), nil, "http://hertz.test.demo/ping", config.WithSD(true)) | ||
if err != nil { | ||
hlog.Fatal(err) | ||
} | ||
hlog.Infof("code=%d,body=%s", status, string(body)) | ||
} | ||
} |
This file contains 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,47 @@ | ||
// Copyright 2021 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" | ||
"time" | ||
|
||
"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/zookeeper" | ||
) | ||
|
||
func main() { | ||
addr := "127.0.0.1:8888" | ||
r, err := zookeeper.NewZookeeperRegistry([]string{"127.0.0.1:2181"}, 40*time.Second) | ||
if err != nil { | ||
panic(err) | ||
} | ||
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.GET("/ping", func(c context.Context, ctx *app.RequestContext) { | ||
ctx.JSON(consts.StatusOK, utils.H{"ping": "pong2"}) | ||
}) | ||
h.Spin() | ||
} |
This file contains 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,12 @@ | ||
module github.com/hertz-contrib/registry/zookeeper | ||
|
||
go 1.16 | ||
|
||
require ( | ||
github.com/cloudwego/hertz v0.2.2-0.20220809100505-e428c8fd938b | ||
github.com/go-zookeeper/zk v1.0.3 | ||
github.com/golang/protobuf v1.5.2 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect | ||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect | ||
) |
Oops, something went wrong.