Skip to content

Commit

Permalink
feat: 新功能,配置文件发生更改时,利用k8s滚动更新特性来实现热更新
Browse files Browse the repository at this point in the history
  • Loading branch information
Liusiyuan-git committed Feb 6, 2023
1 parent 4f10b2b commit e356708
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 128 deletions.
4 changes: 0 additions & 4 deletions app/achievement/service/cmd/achievement/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"flag"
"fmt"
nc "github.com/go-kratos/kratos/contrib/config/nacos/v2"
"github.com/go-kratos/kratos/contrib/log/tencent/v2"
"github.com/go-kratos/kratos/contrib/registry/nacos/v2"
Expand Down Expand Up @@ -147,16 +146,13 @@ func configNew() {
}

if err = servieConfig.Watch("config", func(s string, value config.Value) {
fmt.Println("config watch")
kubeClient, err := kube.NewKubeClient()
if err != nil {
fmt.Println(err)
log.Error(err)
return
}
err = kubeClient.Update("matrix", "achievement")
if err != nil {
fmt.Println(err)
log.Error(err)
return
}
Expand Down
38 changes: 14 additions & 24 deletions app/bff/interface/cmd/bff/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
"github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/nacos-group/nacos-sdk-go/vo"
"github.com/the-zion/matrix-core/app/bff/interface/internal/conf"
"github.com/the-zion/matrix-core/pkg/kube"
"github.com/the-zion/matrix-core/pkg/trace"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
"gopkg.in/yaml.v3"
"os"
"runtime"
"strconv"
"strings"
)
Expand Down Expand Up @@ -50,7 +50,6 @@ var (
sc []constant.ServerConfig
cc *constant.ClientConfig
cleanup func()
restart = true
Name = "matrix.bff.interface"
id, _ = os.Hostname()
)
Expand Down Expand Up @@ -147,14 +146,15 @@ func configNew() {
}

if err = servieConfig.Watch("config", func(s string, value config.Value) {
if err = servieConfig.Scan(&bootstrap); err != nil {
kubeClient, err := kube.NewKubeClient()
if err != nil {
log.Error(err)
return
}
restart = true
if err = app.Stop(); err != nil {
err = kubeClient.Update("matrix", "bff")
if err != nil {
log.Error(err)
restart = false
return
}
}); err != nil {
panic(err)
Expand Down Expand Up @@ -213,15 +213,6 @@ func appInit() {
}
}

func appRefresh() {
traceInit()
loggerInit()
clientNew()
appInit()
runtime.GC()
restart = false
}

func appRun() {
if err := app.Run(); err != nil {
panic(err)
Expand All @@ -234,22 +225,21 @@ func appClean() {
if tencentLogger != nil {
tencentLogger.Close()
}
servieConfig.Close()
}

func matrixRun() {
for {
if !restart {
break
}
appRefresh()
appRun()
appClean()
}
servieConfig.Close()
appInit()
appRun()
appClean()
}

func main() {
flag.Parse()
nacosInit()
traceInit()
loggerInit()
clientNew()
appInit()
matrixRun()
}
37 changes: 13 additions & 24 deletions app/comment/service/cmd/comment/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
"github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/nacos-group/nacos-sdk-go/vo"
"github.com/the-zion/matrix-core/app/comment/service/internal/conf"
"github.com/the-zion/matrix-core/pkg/kube"
"github.com/the-zion/matrix-core/pkg/trace"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
"gopkg.in/yaml.v3"
"os"
"runtime"
"strconv"
"strings"
)
Expand Down Expand Up @@ -50,7 +50,6 @@ var (
sc []constant.ServerConfig
cc *constant.ClientConfig
cleanup func()
restart = true
Name = "matrix.comment.service"
id, _ = os.Hostname()
)
Expand Down Expand Up @@ -147,14 +146,15 @@ func configNew() {
}

if err = servieConfig.Watch("config", func(s string, value config.Value) {
if err = servieConfig.Scan(&bootstrap); err != nil {
kubeClient, err := kube.NewKubeClient()
if err != nil {
log.Error(err)
return
}
restart = true
if err = app.Stop(); err != nil {
err = kubeClient.Update("matrix", "comment")
if err != nil {
log.Error(err)
restart = false
return
}
}); err != nil {
panic(err)
Expand Down Expand Up @@ -213,15 +213,6 @@ func appInit() {
}
}

func appRefresh() {
traceInit()
loggerInit()
clientNew()
appInit()
runtime.GC()
restart = false
}

func appRun() {
if err := app.Run(); err != nil {
panic(err)
Expand All @@ -234,22 +225,20 @@ func appClean() {
if tencentLogger != nil {
tencentLogger.Close()
}
servieConfig.Close()
}

func matrixRun() {
for {
if !restart {
break
}
appRefresh()
appRun()
appClean()
}
servieConfig.Close()
appInit()
appRun()
appClean()
}

func main() {
flag.Parse()
nacosInit()
traceInit()
loggerInit()
clientNew()
matrixRun()
}
37 changes: 13 additions & 24 deletions app/creation/service/cmd/creation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
"github.com/nacos-group/nacos-sdk-go/common/constant"
"github.com/nacos-group/nacos-sdk-go/vo"
"github.com/the-zion/matrix-core/app/creation/service/internal/conf"
"github.com/the-zion/matrix-core/pkg/kube"
"github.com/the-zion/matrix-core/pkg/trace"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
"gopkg.in/yaml.v3"
"os"
"runtime"
"strconv"
"strings"
)
Expand Down Expand Up @@ -50,7 +50,6 @@ var (
sc []constant.ServerConfig
cc *constant.ClientConfig
cleanup func()
restart = true
Name = "matrix.creation.service"
id, _ = os.Hostname()
)
Expand Down Expand Up @@ -147,14 +146,15 @@ func configNew() {
}

if err = servieConfig.Watch("config", func(s string, value config.Value) {
if err = servieConfig.Scan(&bootstrap); err != nil {
kubeClient, err := kube.NewKubeClient()
if err != nil {
log.Error(err)
return
}
restart = true
if err = app.Stop(); err != nil {
err = kubeClient.Update("matrix", "creation")
if err != nil {
log.Error(err)
restart = false
return
}
}); err != nil {
panic(err)
Expand Down Expand Up @@ -213,15 +213,6 @@ func appInit() {
}
}

func appRefresh() {
traceInit()
loggerInit()
clientNew()
appInit()
runtime.GC()
restart = false
}

func appRun() {
if err := app.Run(); err != nil {
panic(err)
Expand All @@ -234,22 +225,20 @@ func appClean() {
if tencentLogger != nil {
tencentLogger.Close()
}
servieConfig.Close()
}

func matrixRun() {
for {
if !restart {
break
}
appRefresh()
appRun()
appClean()
}
servieConfig.Close()
appInit()
appRun()
appClean()
}

func main() {
flag.Parse()
nacosInit()
traceInit()
loggerInit()
clientNew()
matrixRun()
}
37 changes: 13 additions & 24 deletions app/message/service/cmd/message/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import (
"github.com/nacos-group/nacos-sdk-go/vo"
"github.com/the-zion/matrix-core/app/message/service/internal/conf"
"github.com/the-zion/matrix-core/app/message/service/internal/server"
"github.com/the-zion/matrix-core/pkg/kube"
"github.com/the-zion/matrix-core/pkg/trace"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
"gopkg.in/yaml.v3"
"os"
"runtime"
"strconv"
"strings"
)
Expand Down Expand Up @@ -51,7 +51,6 @@ var (
sc []constant.ServerConfig
cc *constant.ClientConfig
cleanup func()
restart = true
Name = "matrix.message.service"
id, _ = os.Hostname()
)
Expand Down Expand Up @@ -149,14 +148,15 @@ func configNew() {
}

if err = servieConfig.Watch("config", func(s string, value config.Value) {
if err = servieConfig.Scan(&bootstrap); err != nil {
kubeClient, err := kube.NewKubeClient()
if err != nil {
log.Error(err)
return
}
restart = true
if err = app.Stop(); err != nil {
err = kubeClient.Update("matrix", "message")
if err != nil {
log.Error(err)
restart = false
return
}
}); err != nil {
panic(err)
Expand Down Expand Up @@ -215,15 +215,6 @@ func appInit() {
}
}

func appRefresh() {
traceInit()
loggerInit()
clientNew()
appInit()
runtime.GC()
restart = false
}

func appRun() {
if err := app.Run(); err != nil {
panic(err)
Expand All @@ -236,22 +227,20 @@ func appClean() {
if tencentLogger != nil {
tencentLogger.Close()
}
servieConfig.Close()
}

func matrixRun() {
for {
if !restart {
break
}
appRefresh()
appRun()
appClean()
}
servieConfig.Close()
appInit()
appRun()
appClean()
}

func main() {
flag.Parse()
nacosInit()
traceInit()
loggerInit()
clientNew()
matrixRun()
}
Loading

0 comments on commit e356708

Please sign in to comment.