Skip to content

Commit

Permalink
fix: 调整注册中心注入方式
Browse files Browse the repository at this point in the history
  • Loading branch information
lbbniu committed Jun 16, 2023
1 parent 238078d commit 5c3906c
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 53 deletions.
4 changes: 1 addition & 3 deletions examples/PolarisServer/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ func main() {
}
defer provider.Destroy()
// 注册中心
tars.SetRegistry(pr.New(provider, pr.WithNamespace("tars")))

comm := tars.NewCommunicator()
comm := tars.NewCommunicator(tars.WithRegistry(pr.New(provider, pr.WithNamespace("tars"))))
obj := fmt.Sprintf("TestApp.PolarisServer.HelloObj")
app := new(TestApp.HelloObj)
comm.StringToProxy(obj, app)
Expand Down
5 changes: 1 addition & 4 deletions examples/PolarisServer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ func main() {
log.Fatalf("fail to create providerAPI, err is %v", err)
}
defer provider.Destroy()
// 注册中心
tars.SetRegistry(pr.New(provider, pr.WithNamespace("tars")))

// New servant imp
imp := new(HelloObjImp)
err = imp.Init()
Expand All @@ -40,5 +37,5 @@ func main() {
app.AddServantWithContext(imp, cfg.App+"."+cfg.Server+".HelloObj")

// Run application
tars.Run()
tars.Run(tars.WithRegistry(pr.New(provider, pr.WithNamespace("tars"))))
}
23 changes: 12 additions & 11 deletions tars/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ import (
"sync/atomic"
"time"

"go.uber.org/automaxprocs/maxprocs"
"github.com/TarsCloud/TarsGo/tars/util/endpoint"
"github.com/TarsCloud/TarsGo/tars/util/ssl"

"github.com/TarsCloud/TarsGo/tars/protocol"
"github.com/TarsCloud/TarsGo/tars/protocol/res/adminf"
"github.com/TarsCloud/TarsGo/tars/transport"
"github.com/TarsCloud/TarsGo/tars/util/conf"
"github.com/TarsCloud/TarsGo/tars/util/endpoint"
"github.com/TarsCloud/TarsGo/tars/util/grace"
"github.com/TarsCloud/TarsGo/tars/util/rogger"
"github.com/TarsCloud/TarsGo/tars/util/ssl"
"github.com/TarsCloud/TarsGo/tars/util/tools"
"go.uber.org/automaxprocs/maxprocs"
)

type destroyableImp interface {
Expand Down Expand Up @@ -332,11 +332,12 @@ func (a *application) initConfig() {
}

// Run the application
func (a *application) Run() {
func (a *application) Run(opts ...Option) {
defer rogger.FlushLogger()
a.isShutdowning = 0
a.init()
<-statInited
cfg.apply(opts)

for _, env := range os.Environ() {
if strings.HasPrefix(env, grace.InheritFdPrefix) {
Expand Down Expand Up @@ -437,17 +438,17 @@ func (a *application) graceRestart() {

// redirect stdout/stderr to logger
svrCfg := a.ServerConfig()
var logfile *os.File
var logFile *os.File
if svrCfg != nil {
GetLogger("")
logpath := filepath.Join(svrCfg.LogPath, svrCfg.App, svrCfg.Server, svrCfg.App+"."+svrCfg.Server+".log")
logfile, _ = os.OpenFile(logpath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
TLOG.Debugf("redirect to %s %v", logpath, logfile)
logPath := filepath.Join(svrCfg.LogPath, svrCfg.App, svrCfg.Server, svrCfg.App+"."+svrCfg.Server+".log")
logFile, _ = os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
TLOG.Debugf("redirect to %s %v", logPath, logFile)
}
if logfile == nil {
logfile = os.Stdout
if logFile == nil {
logFile = os.Stdout
}
files := []*os.File{os.Stdin, logfile, logfile}
files := []*os.File{os.Stdin, logFile, logFile}
for key, file := range grace.GetAllListenFiles() {
fd := fmt.Sprint(file.Fd())
newFd := len(files)
Expand Down
40 changes: 38 additions & 2 deletions tars/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,47 @@ package tars
import (
"time"

"github.com/TarsCloud/TarsGo/tars/registry"

"github.com/TarsCloud/TarsGo/tars/util/endpoint"
"github.com/TarsCloud/TarsGo/tars/util/tools"
)

var (
cfg = &config{}
// ServerConfigPath is the path of server config
)

// Option applies an option value for a config.
type Option interface {
apply(*config)
}

type config struct {
svrCfg *serverConfig
cltCfg *clientConfig
registry registry.Registry
}

func (c *config) apply(opts []Option) {
for _, opt := range opts {
opt.apply(c)
}
}

type registryOption struct{ r registry.Registry }

func (o registryOption) apply(c *config) {
if o.r != nil {
c.registry = o.r
}
}

// WithRegistry returns an Option to use the Registry
func WithRegistry(r registry.Registry) Option {
return registryOption{r: r}
}

type adapterConfig struct {
Endpoint endpoint.Endpoint
Protocol string
Expand Down Expand Up @@ -144,7 +181,7 @@ func newServerConfig() *serverConfig {
}

func newClientConfig() *clientConfig {
conf := &clientConfig{
return &clientConfig{
Stat: Stat,
Property: Property,
ModuleName: ModuleName,
Expand All @@ -161,5 +198,4 @@ func newClientConfig() *clientConfig {
ReqDefaultTimeout: ReqDefaultTimeout,
ObjQueueMax: ObjQueueMax,
}
return conf
}
4 changes: 2 additions & 2 deletions tars/endpointmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,14 @@ func newTarsEndpointManager(objName string, comm *Communicator, opts ...Endpoint
TLOG.Debug("proxy mode:", objName)
e.objName = objName
e.directProxy = false
if registryInstance == nil {
if cfg.registry == nil {
obj, _ := e.comm.GetProperty("locator")
query := new(queryf.QueryF)
TLOG.Debug("string to proxy locator ", obj)
e.comm.StringToProxy(obj, query)
e.registry = tars.New(query)
} else {
e.registry = registryInstance
e.registry = cfg.registry
}
e.checkAdapter = make(chan *AdapterProxy, 1000)
}
Expand Down
14 changes: 7 additions & 7 deletions tars/httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,18 @@ func (mux *TarsHttpMux) reportHttpStat(st *httpStatInfo) {
if mux.cfg == nil || StatReport == nil {
return
}
cfg := mux.cfg
httpConf := mux.cfg
var statInfo = statf.StatMicMsgHead{}
statInfo.MasterName = "http_client"
statInfo.MasterIp = st.reqAddr

statInfo.TarsVersion = cfg.Version
statInfo.SlaveName = cfg.AppName
statInfo.SlaveIp = cfg.IP // from server
statInfo.SlavePort = cfg.Port
statInfo.TarsVersion = httpConf.Version
statInfo.SlaveName = httpConf.AppName
statInfo.SlaveIp = httpConf.IP // from server
statInfo.SlavePort = httpConf.Port
statInfo.InterfaceName = st.pattern
if cfg.SetId != "" {
setList := strings.Split(cfg.SetId, ".")
if httpConf.SetId != "" {
setList := strings.Split(httpConf.SetId, ".")
statInfo.SlaveSetName = setList[0]
statInfo.SlaveSetArea = setList[1]
statInfo.SlaveSetID = setList[2]
Expand Down
40 changes: 16 additions & 24 deletions tars/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,45 @@ import (
"github.com/TarsCloud/TarsGo/tars/registry"
)

var (
registryInstance registry.Registry
)

func SetRegistry(reg registry.Registry) {
registryInstance = reg
}

func registryAdapters(ctx context.Context) {
if registryInstance == nil {
if cfg.registry == nil {
return
}
cfg := GetServerConfig()
for _, adapter := range cfg.Adapters {
svrCfg := GetServerConfig()
for _, adapter := range svrCfg.Adapters {
servant := &registry.ServantInstance{
TarsVersion: Version,
App: cfg.App,
Server: cfg.Server,
App: svrCfg.App,
Server: svrCfg.Server,
Servant: adapter.Obj,
EnableSet: cfg.Enableset,
SetDivision: cfg.Setdivision,
EnableSet: svrCfg.Enableset,
SetDivision: svrCfg.Setdivision,
Protocol: adapter.Protocol,
Endpoint: adapter.Endpoint,
}
if err := registryInstance.Registry(ctx, servant); err != nil {
if err := cfg.registry.Registry(ctx, servant); err != nil {
TLOG.Errorf("%+v registry error: %+v", servant, err)
}
}
}

func deregisterAdapters(ctx context.Context) {
if registryInstance == nil {
if cfg.registry == nil {
return
}
cfg := GetServerConfig()
for _, adapter := range cfg.Adapters {
svrCfg := GetServerConfig()
for _, adapter := range svrCfg.Adapters {
servant := &registry.ServantInstance{
TarsVersion: Version,
App: cfg.App,
Server: cfg.Server,
App: svrCfg.App,
Server: svrCfg.Server,
Servant: adapter.Obj,
EnableSet: cfg.Enableset,
SetDivision: cfg.Setdivision,
EnableSet: svrCfg.Enableset,
SetDivision: svrCfg.Setdivision,
Protocol: adapter.Protocol,
Endpoint: adapter.Endpoint,
}
if err := registryInstance.Deregister(ctx, servant); err != nil {
if err := cfg.registry.Deregister(ctx, servant); err != nil {
TLOG.Errorf("%+v deregister error: %+v", servant, err)
}
}
Expand Down

0 comments on commit 5c3906c

Please sign in to comment.