当前位置:网站首页>Introduction to Jupiter frame entrance

Introduction to Jupiter frame entrance

2020-12-07 19:43:14 xfstart07

Jupiter Introduction to frame entrance

summary

Let's introduce jupiter The application portal of microservice framework , The reason why I wrote this article is to have a deep understanding of jupiter The operation mechanism of the framework .

Data structure introduction

So let's see jupiter Of Application object ,

// Application is the framework's instance, it contains the servers, workers, client and configuration settings.
// Create an instance of Application, by using &Application{}
// Jupiter  The entrance structure of the frame 
type Application struct {
    cycle        *xcycle.Cycle  //  Some functions manage the life cycle of the run 
    smu          *sync.RWMutex
    initOnce     sync.Once
    startupOnce  sync.Once
    stopOnce     sync.Once
    servers      []server.Server    //  Various services ,http, grpc  etc. 
    workers      []worker.Worker  //  Mission 
    jobs         map[string]job.Runner  //  Mission 
    logger       *xlog.Logger //  Log object 
    registerer   registry.Registry  //  Service registration interface object 
    hooks        map[uint32]*xdefer.DeferStack
    configParser conf.Unmarshaller //  System configuration object 
    disableMap   map[Disable]bool
    HideBanner   bool
}

application Objects can be divided into the following components :

  • cycle、smu、initOnce、startupOnce、stopOnce Help fields on the operating mechanism .
  • servers、workers、jobs These three fields are used to store various services , As long as the service implements the interface , The framework can start these services according to the same process .
  • registerer Is the actual object of service discovery and registration policy , It can be etcd、consul, As long as the implementation Registry Interface .
  • hooks Each hook function saved in the framework runtime
  • configParser This field holds the configuration information , The source of configuration information can be a file 、 Configuration center address, etc .
  • logger Log object ,xlog Bag is zap A secondary modification of the logstore .

Application process

In this section, we will introduce the startup process of the framework , The flow chart is as follows :

image.png

Application The object is Jupiter Application portal of framework ,

  • SetRegistry() Is used for injection service discovery and registration , for example registry In bag etcdv3 modular .
  • Startup() Method is the initialization method of each service .
  • Serve()、Schedule()、Job() It's the injection method of various services .
  • RegisterHooks() It's a way to register hooks . For example, do some behavior before and after startup , You can do it in this way .
  • Run() The method is to start the various services of the application and some services defined by the framework , For example, monitoring, etc .
  • Stop() Is to receive the system signal after some exit operation .

Methods to introduce

Here are some of the main methods in the startup process .

Startup() Method
//Startup  The initialization method before application startup 
func (app *Application) Startup(fns ...func() error) error {
    app.initialize() //  initialization  app  Each field of 
    //  Execute the initialization method defined by the framework 
    if err := app.startup(); err != nil {
        return err
    }
    //  Perform custom initialization methods 
    return xgo.SerialUntilError(fns...)()
}

Startup() The method is equivalent to Application The initialization . Initialization is divided into two parts , One part is the initialization sequence of the Framework Definition , Including parsing, starting and transferring parameters 、 Load the configuration 、 Set up link tracking 、 Traffic monitoring service 、 Service governance, etc . The other part is the initialization of each service defined by the framework user , This part of the function is usually Serve()、Job()、Schedule() Call function of several methods .

RegisterHooks() Method
//RegisterHooks register a stage Hook
func (app *Application) RegisterHooks(k uint32, fns ...func() error) error {
    hooks, ok := app.hooks[k]
    if ok {
        hooks.Push(fns...)
        return nil
    }
    return fmt.Errorf("hook stage not found")
}

This method mainly implements hook methods of some frameworks , For example, print some logs before and after startup, etc .

Run() Method
// Run run application
//  Apply the startup method 
func (app *Application) Run(servers ...server.Server) error {
    app.smu.Lock()
    app.servers = append(app.servers, servers...)
    app.smu.Unlock()

    app.waitSignals() //start signal listen task in goroutine
    defer app.clean()

    // todo jobs not graceful
    app.startJobs()

    // start servers and govern server
    app.cycle.Run(app.startServers)
    // start workers
    app.cycle.Run(app.startWorkers)

    //blocking and wait quit
    if err := <-app.cycle.Wait(); err != nil {
        app.logger.Error("jupiter shutdown with error", xlog.FieldMod(ecode.ModApp), xlog.FieldErr(err))
        return err
    }
    app.logger.Info("shutdown jupiter, bye!", xlog.FieldMod(ecode.ModApp))
    return nil
}

This method is to apply the startup method .Run() Methods can be passed into various implementations server.Server Object of the interface .waitSignals() Will start a goroutine To monitor the signal of the system . Then it will start the scheduled tasks in turn 、 Services (grpc,http,govern)、 Task queue .

In this method ,grpc、http、govern Services register information for each service in the registry . For service discovery .

Stop() Method

Stop() Method will exit the program , Call the closing method of each service , To turn off and disconnect Services .

版权声明
本文为[xfstart07]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201207194238829a.html