Skip to content

Commit

Permalink
debug: 解决远程配置首次读取为空异常
Browse files Browse the repository at this point in the history
Signed-off-by: MEX7 <[email protected]>
  • Loading branch information
kl7sn committed Dec 18, 2020
1 parent 5583e04 commit 9a009a3
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 5 deletions.
12 changes: 12 additions & 0 deletions example/remoteconfig/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
remote = "read:key"
[jupiter.server.http]
port = 9090
[jupiter.server.governor]
port = 19090
[jupiter.logger.default]
debug = true
enableConsole = true
name = "default.json"
dir = "."
async = false
level = "info"
7 changes: 7 additions & 0 deletions example/remoteconfig/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{"lv":"info","ts":1608293220,"msg":"set global tracer","mod":"trace"}
{"lv":"info","ts":1608293221,"msg":"yaseeDataSource","change":"juno_configuration_version = \"d769051f23a2919baf58ba8c5371af79\"\nremote = \"xxxx\"\n[jupiter.server.http]\nport = 20105\n\n[jupiter.server.grpc]\nport = 20102\n\n[jupiter.etcdv3.default]\nendpoints = [\n \"etcd-naming1.dz11.com:2379\",\n \"etcd-naming2.dz11.com:2379\",\n \"etcd-naming3.dz11.com:2379\"\n]\nsecure = false\n\n[jupiter.registry.bj01]\nconfigKey = \"jupiter.etcdv3.default\"\ntimeout = \"2s\"\n\n[jupiter.registry.bj02]\nconfigKey = \"jupiter.etcdv3.default\"\ntimeout = \"1s\"\n\n[jupiter.logger.default]\ndebug = false \nenableConsole = false \nname = \"default.json\"\ndir = \".\" \nasync = false \nlevel = \"info\" \n\n[jupiter.mysql.clean]\nconnMaxLifetime = \"300s\"\ndebug = true\ndsn = \"vod_web:hgKnhcwdxHPtny4Ad7bO@tcp(10.1.51.19:3306)/gcr_manage?charset=utf8&parseTime=True&loc=Local&readTimeout=1s&timeout=1s&writeTimeout=1s\"\nlevel = \"panic\"\nmaxIdleConns = 50\nmaxOpenConns = 100\ndisableTrace = true\ndisableMetric = true\n\n[jupiter.mysql.source]\nconnMaxLifetime = \"300s\"\ndebug = true\ndsn = \"bbs_read:bbs_read567lSCNgErdNUGVayni@tcp(10.1.105.122:3306)/test?charset=utf8&parseTime=True&loc=Local\"\nlevel = \"panic\"\nmaxIdleConns = 50\nmaxOpenConns = 100\ndisableTrace = true\ndisableMetric = true\n\n[jupiter.trace.jaeger]\nenableRPCMetrics = false\n\n[jupiter.trace.jaeger.sampler]\ntype = \"const\"\nparam = 0.001\n\n[resty]\n\n[resty.adv]\naddr = \"http://10.1.97.72:59067\"\n\n[resty.yami]\naddr = \"http://wsd-ocr-admin-yami-go.live.unp.oyw\"\ntoken = \"123456\"\n\n[resty.nsfw]\naddr = \"http://pornidentify.dz11.com\"\n\n[resty.bigData]\naddr = \"http://shield.ocean-bp.com.live.ocean.dz11.com\"\naccessKey = \"douyu\"\n\n[source]\npage = 1\n\n[core]\nsyncInterval = 60\n\n[review]\n\n[review.yami]\nenable = false\n\n[review.nsfw]\nenable = false\n\n[review.adv]\nenable = false\n\n[review.sensitiveWords]\nenable = false\n"}
{"lv":"info","ts":1608293221,"msg":"update level","level":"info","name":"default.json"}
{"lv":"info","ts":1608293222,"msg":"yaseeDataSource","change":"juno_configuration_version = \"d769051f23a2919baf58ba8c5371af79\"\nremote = \"xxxx\"\n[jupiter.server.http]\nport = 20105\n\n[jupiter.server.grpc]\nport = 20102\n\n[jupiter.etcdv3.default]\nendpoints = [\n \"etcd-naming1.dz11.com:2379\",\n \"etcd-naming2.dz11.com:2379\",\n \"etcd-naming3.dz11.com:2379\"\n]\nsecure = false\n\n[jupiter.registry.bj01]\nconfigKey = \"jupiter.etcdv3.default\"\ntimeout = \"2s\"\n\n[jupiter.registry.bj02]\nconfigKey = \"jupiter.etcdv3.default\"\ntimeout = \"1s\"\n\n[jupiter.logger.default]\ndebug = false \nenableConsole = false \nname = \"default.json\"\ndir = \".\" \nasync = false \nlevel = \"info\" \n\n[jupiter.mysql.clean]\nconnMaxLifetime = \"300s\"\ndebug = true\ndsn = \"vod_web:hgKnhcwdxHPtny4Ad7bO@tcp(10.1.51.19:3306)/gcr_manage?charset=utf8&parseTime=True&loc=Local&readTimeout=1s&timeout=1s&writeTimeout=1s\"\nlevel = \"panic\"\nmaxIdleConns = 50\nmaxOpenConns = 100\ndisableTrace = true\ndisableMetric = true\n\n[jupiter.mysql.source]\nconnMaxLifetime = \"300s\"\ndebug = true\ndsn = \"bbs_read:bbs_read567lSCNgErdNUGVayni@tcp(10.1.105.122:3306)/test?charset=utf8&parseTime=True&loc=Local\"\nlevel = \"panic\"\nmaxIdleConns = 50\nmaxOpenConns = 100\ndisableTrace = true\ndisableMetric = true\n\n[jupiter.trace.jaeger]\nenableRPCMetrics = false\n\n[jupiter.trace.jaeger.sampler]\ntype = \"const\"\nparam = 0.001\n\n[resty]\n\n[resty.adv]\naddr = \"http://10.1.97.72:59067\"\n\n[resty.yami]\naddr = \"http://wsd-ocr-admin-yami-go.live.unp.oyw\"\ntoken = \"123456\"\n\n[resty.nsfw]\naddr = \"http://pornidentify.dz11.com\"\n\n[resty.bigData]\naddr = \"http://shield.ocean-bp.com.live.ocean.dz11.com\"\naccessKey = \"douyu\"\n\n[source]\npage = 1\n\n[core]\nsyncInterval = 60\n\n[review]\n\n[review.yami]\nenable = false\n\n[review.nsfw]\nenable = false\n\n[review.adv]\nenable = false\n\n[review.sensitiveWords]\nenable = false\n"}
{"lv":"info","ts":1608293222,"msg":"update level","level":"info","name":"default.json"}
{"lv":"info","ts":1608293223,"msg":"yaseeDataSource","change":"juno_configuration_version = \"d769051f23a2919baf58ba8c5371af79\"\nremote = \"xxxx\"\n[jupiter.server.http]\nport = 20105\n\n[jupiter.server.grpc]\nport = 20102\n\n[jupiter.etcdv3.default]\nendpoints = [\n \"etcd-naming1.dz11.com:2379\",\n \"etcd-naming2.dz11.com:2379\",\n \"etcd-naming3.dz11.com:2379\"\n]\nsecure = false\n\n[jupiter.registry.bj01]\nconfigKey = \"jupiter.etcdv3.default\"\ntimeout = \"2s\"\n\n[jupiter.registry.bj02]\nconfigKey = \"jupiter.etcdv3.default\"\ntimeout = \"1s\"\n\n[jupiter.logger.default]\ndebug = false \nenableConsole = false \nname = \"default.json\"\ndir = \".\" \nasync = false \nlevel = \"info\" \n\n[jupiter.mysql.clean]\nconnMaxLifetime = \"300s\"\ndebug = true\ndsn = \"vod_web:hgKnhcwdxHPtny4Ad7bO@tcp(10.1.51.19:3306)/gcr_manage?charset=utf8&parseTime=True&loc=Local&readTimeout=1s&timeout=1s&writeTimeout=1s\"\nlevel = \"panic\"\nmaxIdleConns = 50\nmaxOpenConns = 100\ndisableTrace = true\ndisableMetric = true\n\n[jupiter.mysql.source]\nconnMaxLifetime = \"300s\"\ndebug = true\ndsn = \"bbs_read:bbs_read567lSCNgErdNUGVayni@tcp(10.1.105.122:3306)/test?charset=utf8&parseTime=True&loc=Local\"\nlevel = \"panic\"\nmaxIdleConns = 50\nmaxOpenConns = 100\ndisableTrace = true\ndisableMetric = true\n\n[jupiter.trace.jaeger]\nenableRPCMetrics = false\n\n[jupiter.trace.jaeger.sampler]\ntype = \"const\"\nparam = 0.001\n\n[resty]\n\n[resty.adv]\naddr = \"http://10.1.97.72:59067\"\n\n[resty.yami]\naddr = \"http://wsd-ocr-admin-yami-go.live.unp.oyw\"\ntoken = \"123456\"\n\n[resty.nsfw]\naddr = \"http://pornidentify.dz11.com\"\n\n[resty.bigData]\naddr = \"http://shield.ocean-bp.com.live.ocean.dz11.com\"\naccessKey = \"douyu\"\n\n[source]\npage = 1\n\n[core]\nsyncInterval = 60\n\n[review]\n\n[review.yami]\nenable = false\n\n[review.nsfw]\nenable = false\n\n[review.adv]\nenable = false\n\n[review.sensitiveWords]\nenable = false\n"}
{"lv":"info","ts":1608293223,"msg":"update level","level":"info","name":"default.json"}
61 changes: 61 additions & 0 deletions example/remoteconfig/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2020 Douyu
//
// 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 (
"fmt"
"time"

"github.com/douyu/jupiter"
"github.com/douyu/jupiter/pkg/conf"
"github.com/douyu/jupiter/pkg/server/xecho"
"github.com/douyu/jupiter/pkg/xlog"
"github.com/labstack/echo/v4"
)

func main() {
eng := NewEngine()
if err := eng.Run(); err != nil {
xlog.Error(err.Error())
}
}

type Engine struct {
jupiter.Application
}

func NewEngine() *Engine {
eng := &Engine{}
if err := eng.Startup(
eng.serveHTTP,
); err != nil {
xlog.Panic("startup", xlog.Any("err", err))
}
for {
fmt.Println("read remote content:", conf.GetString("remote"))
time.Sleep(time.Second)
}
return eng
}

// HTTP地址
func (eng *Engine) serveHTTP() error {
server := xecho.StdConfig("http").Build()
server.GET("/hello", func(ctx echo.Context) error {

return ctx.JSON(200, "Gopher Wuhan")
})
return eng.Serve(server)
}
2 changes: 2 additions & 0 deletions jupiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,9 @@ func (app *Application) loadConfig() error {
}

var configAddr = flag.String("config")

fmt.Printf("configAddr => %v\n", configAddr)

provider, err := manager.NewDataSource(configAddr)
if err != manager.ErrConfigAddr {
if err != nil {
Expand Down
15 changes: 10 additions & 5 deletions pkg/datasource/http/yasee.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func (y *yaseeDataSource) ReadConfig() ([]byte, error) {
// 检查watch 如果watch为真,走长轮询逻辑
switch y.enableWatch {
case true:
if y.data == "" {
content, err := y.getConfigInner(y.addr, y.enableWatch)
return []byte(content), err
}
return []byte(y.data), nil
default:
content, err := y.getConfigInner(y.addr, y.enableWatch)
Expand All @@ -94,24 +98,25 @@ func (y *yaseeDataSource) watch() {
resp, err := y.client.R().SetQueryParam("watch", strconv.FormatBool(y.enableWatch)).Get(y.addr)
// client get err
if err != nil {
time.Sleep(time.Second * 1)
xlog.Error("yaseeDataSource", xlog.String("listenConfig curl err", err.Error()))
time.Sleep(time.Second * 1)
continue
}
if resp.StatusCode() != 200 {
time.Sleep(time.Second * 1)
xlog.Error("yaseeDataSource", xlog.String("listenConfig status err", resp.Status()))
time.Sleep(time.Second * 1)
continue
}
var yaseeRes yaseeRes
if err := json.Unmarshal(resp.Body(), &yaseeRes); err != nil {
time.Sleep(time.Second * 1)
y.updateConfig(string(resp.Body()), 0)
time.Sleep(time.Second * 1)
continue
}
// default code != 200 means not change
if yaseeRes.Code != 200 {
time.Sleep(time.Second * 1)
xlog.Info("yaseeDataSource", xlog.Int64("code", int64(yaseeRes.Code)))
time.Sleep(time.Second * 1)
continue
}
y.updateConfig(yaseeRes.Data.Content, yaseeRes.Data.LastRevision)
Expand Down Expand Up @@ -156,7 +161,7 @@ func (y *yaseeDataSource) getConfigInner(addr string, enableWatch bool) (string,
}

func (y *yaseeDataSource) getConfig(addr string, enableWatch bool) (string, error) {
resp, err := y.client.SetDebug(true).R().SetQueryParam("watch", strconv.FormatBool(enableWatch)).Get(addr)
resp, err := y.client.SetDebug(false).R().SetQueryParam("watch", strconv.FormatBool(enableWatch)).Get(addr)
if err != nil {
return "", errors.New("get config err")
}
Expand Down

0 comments on commit 9a009a3

Please sign in to comment.