Skip to content

Commit eaae8a5

Browse files
author
songshiyuan 00649746
committed
[feat] support refresh the config of syncer without redeploy servicecomb-center
1 parent 0534041 commit eaae8a5

File tree

5 files changed

+58
-30
lines changed

5 files changed

+58
-30
lines changed

cmd/scserver/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ import (
2929
)
3030

3131
func main() {
32-
syncsvr.Run()
32+
go syncsvr.Run()
3333
server.Run()
3434
}

syncer/config/config.go

+15-9
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ package config
2020
import (
2121
"fmt"
2222
"path/filepath"
23+
"reflect"
24+
25+
"github.com/go-chassis/go-archaius"
2326

2427
"github.com/apache/servicecomb-service-center/pkg/log"
2528
"github.com/apache/servicecomb-service-center/pkg/util"
26-
"github.com/go-chassis/go-archaius"
2729
)
2830

2931
var config Config
@@ -44,28 +46,32 @@ type Peer struct {
4446
Mode []string `yaml:"mode"`
4547
}
4648

47-
func Init() error {
49+
func Init() (error, bool) {
4850
err := archaius.AddFile(filepath.Join(util.GetAppRoot(), "conf", "syncer.yaml"))
4951
if err != nil {
5052
log.Warn(fmt.Sprintf("can not add syncer config file source, error: %s", err))
51-
return err
53+
return err, false
5254
}
5355

54-
err = Reload()
56+
err, isRefresh := Reload()
5557
if err != nil {
5658
log.Fatal("reload syncer configs failed", err)
57-
return err
59+
return err, false
5860
}
59-
return nil
61+
return nil, isRefresh
6062
}
6163

6264
// Reload all configurations
63-
func Reload() error {
65+
func Reload() (error, bool) {
66+
oldConfig := config
6467
err := archaius.UnmarshalConfig(&config)
6568
if err != nil {
66-
return err
69+
return err, false
70+
}
71+
if !reflect.DeepEqual(oldConfig, config) {
72+
return nil, true
6773
}
68-
return nil
74+
return nil, false
6975
}
7076

7177
// GetConfig return the syncer full configurations

syncer/config/config_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ import (
2525

2626
_ "github.com/apache/servicecomb-service-center/test"
2727

28-
"github.com/apache/servicecomb-service-center/syncer/config"
2928
"github.com/stretchr/testify/assert"
29+
30+
"github.com/apache/servicecomb-service-center/syncer/config"
3031
)
3132

3233
func TestGetConfig(t *testing.T) {
3334
changeConfigPath()
34-
assert.NoError(t, config.Init())
35+
err, _ := config.Init()
36+
assert.NoError(t, err)
3537
assert.NotNil(t, config.GetConfig().Sync)
3638
}
3739

syncer/server/server.go

+33-15
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,54 @@
1818
package server
1919

2020
import (
21+
"time"
22+
23+
"github.com/go-chassis/go-chassis/v2"
24+
chassisServer "github.com/go-chassis/go-chassis/v2/core/server"
25+
2126
"github.com/apache/servicecomb-service-center/pkg/log"
2227
syncv1 "github.com/apache/servicecomb-service-center/syncer/api/v1"
2328
"github.com/apache/servicecomb-service-center/syncer/config"
2429
"github.com/apache/servicecomb-service-center/syncer/metrics"
2530
"github.com/apache/servicecomb-service-center/syncer/rpc"
2631
"github.com/apache/servicecomb-service-center/syncer/service/admin"
2732
"github.com/apache/servicecomb-service-center/syncer/service/sync"
28-
"github.com/go-chassis/go-chassis/v2"
29-
chassisServer "github.com/go-chassis/go-chassis/v2/core/server"
3033
)
3134

35+
const syncRefreshTime = 2 * time.Minute
36+
3237
// Run register chassis schema and run syncer services before chassis.Run()
3338
func Run() {
34-
if err := config.Init(); err != nil {
35-
log.Error("syncer config init failed", err)
36-
}
39+
ticker := time.NewTicker(syncRefreshTime)
40+
defer ticker.Stop()
3741

38-
if !config.GetConfig().Sync.EnableOnStart {
39-
log.Warn("syncer is disabled")
40-
return
41-
}
42+
for {
43+
select {
44+
case <-ticker.C:
45+
err, isRefresh := config.Init()
46+
if err != nil {
47+
log.Error("syncer config init failed", err)
48+
}
49+
if !isRefresh {
50+
return
51+
}
52+
53+
if !config.GetConfig().Sync.EnableOnStart {
54+
log.Warn("syncer is disabled")
55+
return
56+
}
57+
58+
chassis.RegisterSchema("grpc", rpc.NewServer(),
59+
chassisServer.WithRPCServiceDesc(&syncv1.EventService_ServiceDesc))
4260

43-
chassis.RegisterSchema("grpc", rpc.NewServer(),
44-
chassisServer.WithRPCServiceDesc(&syncv1.EventService_ServiceDesc))
61+
admin.Init()
4562

46-
admin.Init()
63+
sync.Init()
4764

48-
sync.Init()
65+
if err := metrics.Init(); err != nil {
66+
log.Error("syncer metrics init failed", err)
67+
}
4968

50-
if err := metrics.Init(); err != nil {
51-
log.Error("syncer metrics init failed", err)
69+
}
5270
}
5371
}

syncer/service/admin/health_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ import (
3030

3131
_ "github.com/apache/servicecomb-service-center/test"
3232

33+
"github.com/stretchr/testify/assert"
34+
3335
v1sync "github.com/apache/servicecomb-service-center/syncer/api/v1"
3436
"github.com/apache/servicecomb-service-center/syncer/config"
3537
syncrpc "github.com/apache/servicecomb-service-center/syncer/rpc"
3638
"github.com/apache/servicecomb-service-center/syncer/service/admin"
37-
"github.com/stretchr/testify/assert"
3839
)
3940

4041
type mockServer struct {
@@ -132,9 +133,10 @@ func checkError(resp *admin.Resp, err error) bool {
132133

133134
func TestHealthTotalTime(t *testing.T) {
134135
changeConfigPath()
135-
assert.NoError(t, config.Init())
136+
err, _ := config.Init()
137+
assert.NoError(t, err)
136138
now := time.Now()
137-
_, err := admin.Health()
139+
_, err = admin.Health()
138140
assert.NoError(t, err)
139141
healthEndTime := time.Now()
140142
if healthEndTime.Sub(now) >= time.Second*30 {

0 commit comments

Comments
 (0)