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

feat: support service registeration and discovery with Netflix Eureka #16

Merged
merged 47 commits into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
e666fb6
feat: add eureka registry
Haswf Aug 20, 2022
85c821e
feat: add eureka resolver
Haswf Aug 23, 2022
7e71623
style: run gofumpt
Haswf Aug 23, 2022
f43e5a1
test: add test cases
Haswf Aug 28, 2022
c5677a9
feat: zookeeper init code
zstone12 Aug 23, 2022
2374cb2
chore: simplify dupliate code && no eps return nil
zstone12 Aug 23, 2022
d8e6f9e
style: import style
zstone12 Aug 23, 2022
c658aad
chore: remove utils/entity folder && use Localp
zstone12 Aug 23, 2022
203dbdf
fix: typo
zstone12 Aug 23, 2022
00d541c
chore: change basic -> standard
zstone12 Aug 23, 2022
2c2a192
refactor: use local utils to get ipv4Address
zstone12 Aug 23, 2022
e820c96
fix: add cacheKey
zstone12 Aug 23, 2022
ae552af
test: unit test assert nil
zstone12 Aug 23, 2022
708ea72
style: add license header
zstone12 Aug 23, 2022
0932c52
fix: handle host :: case
zstone12 Aug 23, 2022
b1c5d38
doc: add compatibility part
zstone12 Aug 23, 2022
94034ff
refactor: use Localp instead of local utils
zstone12 Aug 23, 2022
13c03ed
test: add assert for reslove
zstone12 Aug 23, 2022
ee143ff
test: add test with hertz
zstone12 Aug 23, 2022
0b88fdc
style: add license header && gofumpt
zstone12 Aug 23, 2022
cd9ca96
style: add newline to end of file
zstone12 Aug 23, 2022
8fff6d9
doc: optimize README.md format
zstone12 Aug 23, 2022
14d6524
fix: handle host :: case && optimize getInstances code
zstone12 Aug 24, 2022
30303b3
fix: resolver & licences typo
zstone12 Aug 24, 2022
5d19ee0
test: merge test file && remove judge len(ep)
zstone12 Aug 24, 2022
483630e
doc: first letter upper
zstone12 Aug 24, 2022
03819c8
fix: lint error
zstone12 Aug 24, 2022
94c1525
feat: optimize buildPath code
zstone12 Aug 24, 2022
90e9154
style: add comment for test func
zstone12 Aug 24, 2022
d0a4e8e
style: license folders name typo (#11)
zstone12 Aug 24, 2022
7b24eeb
fix: sleep time change to 2s (#12)
zstone12 Aug 24, 2022
574da3d
feat: add polaris registry (#9)
rogerogers Aug 25, 2022
980c889
feat: add nacos registry (#7)
Skyenought Aug 27, 2022
92f3ff3
feat: finish etcd register and resolver
qiuyuyin Aug 17, 2022
afb4042
feat: add register
qiuyuyin Aug 17, 2022
acf7d3e
doc: add README.md
Haswf Aug 28, 2022
ef71525
test: add eureka service to tests.yml
Haswf Aug 28, 2022
0050777
test: add eureka service to tests.yml
Haswf Aug 28, 2022
310b493
Merge branch 'main' into feat_eureka
Haswf Aug 28, 2022
dbc2ecc
doc: remove Makefile as it's no long required
Haswf Aug 28, 2022
4c546fa
test: fix data race
Haswf Aug 28, 2022
3dd2d90
test: add test case for local ip
Haswf Aug 28, 2022
5b5a72f
doc: capitalise title
Haswf Aug 28, 2022
7788eb1
test: fix data race
Haswf Aug 28, 2022
5598748
test: remove local ip test case
Haswf Aug 28, 2022
7d7a7fc
fix: use SplitHostPort
Haswf Aug 28, 2022
528b6a0
style: remove extra new line
Haswf Aug 28, 2022
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
4 changes: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ jobs:
- '8848:8848'
env:
MODE: standalone
eureka:
image: 'xdockerh/eureka-server:latest'
ports:
- "8761:8761"
steps:
- uses: actions/checkout@v3

Expand Down
191 changes: 191 additions & 0 deletions eureka/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
# registry-eureka (*This is a community driven project*)

registry-eureka implements Hertz registry and resolver for Netflix Eureka.

## How to use?

### Server

**[example/server/main.go](examples/server/main.go)**

```go
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/eureka"
)

func main() {
addr := "127.0.0.1:8888"
r := eureka.NewEurekaRegistry([]string{"http://127.0.0.1:8761/eureka"}, 40*time.Second)
h := server.Default(
server.WithHostPorts(addr),
server.WithRegistry(r, &registry.Info{
ServiceName: "hertz.discovery.eureka",
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/client/main.go](example/client/main.go)**

```go
package main

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/eureka"
)

func main() {
cli, err := client.NewClient()
if err != nil {
hlog.Fatal(err)
return
}
r := eureka.NewEurekaResolver([]string{"http://127.0.0.1:8761/eureka"})

cli.Use(sd.Discovery(r))
for i := 0; i < 10; i++ {
status, body, err := cli.Get(context.Background(), nil, "http://hertz.discovery.eureka/ping", config.WithSD(true))
if err != nil {
hlog.Fatal(err)
}
hlog.Infof("code=%d,body=%s", status, string(body))
}
}

```

## How to Run example?

### Start Eureka Server

```bash
docker-compose up
```

### Run Server

```go
go run ./example/server/main.go
```

### Run Client

```go
go run ./example/client/main.go
```

```go
2022/08/28 22:16:59 Getting app hertz.discovery.eureka from url http://127.0.0.1:8761/eureka/apps/hertz.discovery.eureka
2022/08/28 22:16:59 Got eureka response from url=http://127.0.0.1:8761/eureka/apps/hertz.discovery.eureka
2022/08/28 22:16:59.443078 main.go:41: [Info] code=200,body={"ping":"pong2"}
2022/08/28 22:16:59.443258 main.go:41: [Info] code=200,body={"ping":"pong2"}
2022/08/28 22:16:59.443405 main.go:41: [Info] code=200,body={"ping":"pong2"}
2022/08/28 22:16:59.443548 main.go:41: [Info] code=200,body={"ping":"pong2"}
2022/08/28 22:16:59.443697 main.go:41: [Info] code=200,body={"ping":"pong2"}
2022/08/28 22:16:59.443855 main.go:41: [Info] code=200,body={"ping":"pong2"}
2022/08/28 22:16:59.444004 main.go:41: [Info] code=200,body={"ping":"pong2"}
2022/08/28 22:16:59.444149 main.go:41: [Info] code=200,body={"ping":"pong2"}
2022/08/28 22:16:59.444289 main.go:41: [Info] code=200,body={"ping":"pong2"}
2022/08/28 22:16:59.444405 main.go:41: [Info] code=200,body={"ping":"pong2"}


```

## Configuration

This project uses [fargo](https://github.com/hudl/fargo) as eureka client. You should refer to
[fargo](https://github.com/hudl/fargo) documentation for advanced configuration.


There are multiple ways to crate a `eurekaRegistry`.
- `NewEurekaRegistry` creates a registry with a slice of eureka server addresses.
- `NewEurekaRegistryFromConfig` creates a registry with given `fargo.Config`.
- `NewEurekaRegistryFromConn` creates a registry using existing `fargo.EurekaConnection` .

The same also applies for `eurekaResolver`.
- `NewEurekaResolver` creates a resolver with a slice of eureka server addresses.
- `NewEurekaResolverFromConfig` creates a resolver with given `fargo.Config`.
- `NewEurekaResolverFromConn` creates a resolver using existing `fargo.EurekaConnection` .

### Authentication
A straight-forward approach is passing [credentials in uri](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#access_using_credentials_in_the_url) e.g. `[]string{"http://username:password@127.0.0.1:8080/eureka"`.
Alternatively, you can pass existing connection to `NewEurekaRegistryFromConn` or `NewEurekaResolverFromConn`.

### Setting Log Level

As discussed above, this project uses fargo as eureka client, which relies on [go-logging](github.com/op/go-logging) for logging.
Unfortunately, [go-logging](github.com/op/go-logging) does not provide an interface to adjust log level. The following code demonstrates how to set log level.
```go

package main

import (
"context"
"github.com/op/go-logging"
"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/eureka"
)

func main() {

logging.SetLevel(logging.WARNING, "fargo")
// set this to a higher level if you wish to check responses from eureka
logging.SetLevel(logging.WARNING, "fargo.metadata")
logging.SetLevel(logging.WARNING, "fargo.marshal")

addr := "127.0.0.1:8888"
r := eureka.NewEurekaRegistry([]string{"http://127.0.0.1:8761/eureka"}, 40*time.Second)
h := server.Default(
server.WithHostPorts(addr),
server.WithRegistry(r, &registry.Info{
ServiceName: "hertz.discovery.eureka",
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()
}


```




## Compatibility

This project is compatible with eureka server v1.
22 changes: 22 additions & 0 deletions eureka/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 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.
#

version: '3'

services:
eureka:
image: 'xdockerh/eureka-server:latest'
ports:
- 8761:8761
Loading