Skip to content

Commit

Permalink
feat: improve glog for polaris register (#2178)
Browse files Browse the repository at this point in the history
feat: improve glog
  • Loading branch information
houseme authored Oct 9, 2022
1 parent b1611fe commit ea396a3
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 17 deletions.
7 changes: 5 additions & 2 deletions contrib/registry/polaris/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ English | [简体中文](README_ZH.MD)

Use `PolarisMesh` as service registration, discovery management and heartbeat reporting.


## Installation

```
go get -u -v github.com/gogf/gf/contrib/registry/polaris/v2
```

suggested using `go.mod`:

```
require github.com/gogf/gf/contrib/registry/polaris/v2 latest
```


## Example

### Reference example

[server](example/registry/polaris/server/main.go)

```go
package main

Expand Down Expand Up @@ -48,6 +50,7 @@ func main() {
```

[client](example/registry/polaris/client/main.go)

```go
package main

Expand Down
10 changes: 7 additions & 3 deletions contrib/registry/polaris/README_ZH.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,30 @@

使用`PolarisMesh`作为服务注册、发现管理和心跳上报。

## 安装

## Installation
```
go get -u -v github.com/gogf/gf/contrib/registry/polaris/v2
```

suggested using `go.mod`:

```
require github.com/gogf/gf/contrib/registry/polaris/v2 latest
```

## Limitation
## Golang版本限制

```
golang version >= 1.15
```


## 示例

### 引用示例

[服务端](example/registry/polaris/server/main.go)

```go
package main

Expand Down Expand Up @@ -54,6 +57,7 @@ func main() {
```

[客户端](example/registry/polaris/client/main.go)

```go
package main

Expand Down
2 changes: 1 addition & 1 deletion contrib/registry/polaris/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.15

require (
github.com/gogf/gf/v2 v2.0.0
github.com/polarismesh/polaris-go v1.2.0-beta.0.0.20220517041223-596a6a63b00f
github.com/polarismesh/polaris-go v1.2.0-beta.3
)

replace github.com/gogf/gf/v2 => ../../../
7 changes: 4 additions & 3 deletions contrib/registry/polaris/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dlclark/regexp2 v1.7.0 h1:7lJfhqlPssTb1WQx4yvTHN0uElPEv52sbaECrAQxjAo=
github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand All @@ -82,7 +84,6 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI=
github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
Expand Down Expand Up @@ -251,8 +252,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/polarismesh/polaris-go v1.2.0-beta.0.0.20220517041223-596a6a63b00f h1:IL2vXn/LjasI79p2X0/j8DZ+XnV5wgnjNpO1PQjn9Bc=
github.com/polarismesh/polaris-go v1.2.0-beta.0.0.20220517041223-596a6a63b00f/go.mod h1:xXTl4b5ybYkwvXZA+nc1HNyLK/bsHUg08R4ewTa9axc=
github.com/polarismesh/polaris-go v1.2.0-beta.3 h1:sqN50VGign37xVFp9nrN1RoLXacsB0QfRYUtuCWMJGI=
github.com/polarismesh/polaris-go v1.2.0-beta.3/go.mod h1:HsN0ierETIujHpmnnYJ3qkwQw4QGAECuHvBZTDaw1tI=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
Expand Down
11 changes: 11 additions & 0 deletions contrib/registry/polaris/polaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
"github.com/polarismesh/polaris-go"
"github.com/polarismesh/polaris-go/pkg/config"

"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/gsvc"
"github.com/gogf/gf/v2/os/glog"
)

var (
Expand Down Expand Up @@ -60,6 +62,9 @@ type options struct {

// optional, retry count. Default value is global config
RetryCount int

// optional, logger for polaris
Logger glog.ILogger
}

// Option The option is a polaris option.
Expand Down Expand Up @@ -123,6 +128,11 @@ func WithHeartbeat(heartbeat bool) Option {
return func(o *options) { o.Heartbeat = heartbeat }
}

// WithLogger with the Logger option.
func WithLogger(logger glog.ILogger) Option {
return func(o *options) { o.Logger = logger }
}

// New create a new registry.
func New(provider polaris.ProviderAPI, consumer polaris.ConsumerAPI, opts ...Option) (r *Registry) {
op := options{
Expand All @@ -137,6 +147,7 @@ func New(provider polaris.ProviderAPI, consumer polaris.ConsumerAPI, opts ...Opt
TTL: 0,
Timeout: 0,
RetryCount: 0,
Logger: g.Log(),
}
for _, option := range opts {
option(&op)
Expand Down
7 changes: 3 additions & 4 deletions contrib/registry/polaris/polaris_registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/polarismesh/polaris-go"
"github.com/polarismesh/polaris-go/pkg/model"

"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/gsvc"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
Expand Down Expand Up @@ -134,12 +133,12 @@ func (r *Registry) doHeartBeat(ctx context.Context, instanceID string, service g
},
})
if err != nil {
g.Log().Error(ctx, err.Error())
r.opt.Logger.Error(ctx, err.Error())
continue
}
g.Log().Debug(ctx, "heartbeat success")
r.opt.Logger.Debug(ctx, "heartbeat success")
case <-r.c:
g.Log().Debug(ctx, "stop heartbeat")
r.opt.Logger.Debug(ctx, "stop heartbeat")
return
}
}
Expand Down
3 changes: 1 addition & 2 deletions example/registry/polaris/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import (
func main() {
conf := config.NewDefaultConfiguration([]string{"192.168.100.222:8091"})
conf.Consumer.LocalCache.SetPersistDir("/tmp/polaris/backup")
err := api.SetLoggersDir("/tmp/polaris/log")
if err != nil {
if err := api.SetLoggersDir("/tmp/polaris/log"); err != nil {
g.Log().Fatal(context.Background(), err)
}

Expand Down
3 changes: 1 addition & 2 deletions example/registry/polaris/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import (
func main() {
conf := config.NewDefaultConfiguration([]string{"192.168.100.222:8091"})
conf.Consumer.LocalCache.SetPersistDir("/tmp/polaris/backup")
err := api.SetLoggersDir("/tmp/polaris/log")
if err != nil {
if err := api.SetLoggersDir("/tmp/polaris/log"); err != nil {
g.Log().Fatal(context.Background(), err)
}

Expand Down

0 comments on commit ea396a3

Please sign in to comment.