From 5c3906cfe785088649cd4b7b46b4d441df303954 Mon Sep 17 00:00:00 2001 From: lbbniu Date: Sat, 18 Mar 2023 11:30:06 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=B0=83=E6=95=B4=E6=B3=A8=E5=86=8C?= =?UTF-8?q?=E4=B8=AD=E5=BF=83=E6=B3=A8=E5=85=A5=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/PolarisServer/client/client.go | 4 +-- examples/PolarisServer/main.go | 5 +--- tars/application.go | 23 +++++++------- tars/config.go | 40 +++++++++++++++++++++++-- tars/endpointmanager.go | 4 +-- tars/httpserver.go | 14 ++++----- tars/registry.go | 40 ++++++++++--------------- 7 files changed, 77 insertions(+), 53 deletions(-) diff --git a/examples/PolarisServer/client/client.go b/examples/PolarisServer/client/client.go index 52353184..7dfdd72c 100644 --- a/examples/PolarisServer/client/client.go +++ b/examples/PolarisServer/client/client.go @@ -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) diff --git a/examples/PolarisServer/main.go b/examples/PolarisServer/main.go index 1e1f6439..8ec6ea0b 100644 --- a/examples/PolarisServer/main.go +++ b/examples/PolarisServer/main.go @@ -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() @@ -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")))) } diff --git a/tars/application.go b/tars/application.go index 7569b138..c06aefcd 100755 --- a/tars/application.go +++ b/tars/application.go @@ -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 { @@ -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) { @@ -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) diff --git a/tars/config.go b/tars/config.go index 39dba6ce..1f67e3f6 100755 --- a/tars/config.go +++ b/tars/config.go @@ -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 @@ -144,7 +181,7 @@ func newServerConfig() *serverConfig { } func newClientConfig() *clientConfig { - conf := &clientConfig{ + return &clientConfig{ Stat: Stat, Property: Property, ModuleName: ModuleName, @@ -161,5 +198,4 @@ func newClientConfig() *clientConfig { ReqDefaultTimeout: ReqDefaultTimeout, ObjQueueMax: ObjQueueMax, } - return conf } diff --git a/tars/endpointmanager.go b/tars/endpointmanager.go index ac65d0b1..07a732ca 100755 --- a/tars/endpointmanager.go +++ b/tars/endpointmanager.go @@ -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) } diff --git a/tars/httpserver.go b/tars/httpserver.go index bf431cfe..b1bdc757 100644 --- a/tars/httpserver.go +++ b/tars/httpserver.go @@ -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] diff --git a/tars/registry.go b/tars/registry.go index 19b7469f..8dc21e7c 100644 --- a/tars/registry.go +++ b/tars/registry.go @@ -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 := ®istry.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 := ®istry.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) } }