micro.newService()中newOptions

func newOptions(opts ...Option) Options {
    opt := Options{
        Auth:      auth.DefaultAuth,
        Broker:    broker.DefaultBroker,
        Cmd:       cmd.DefaultCmd,
        Config:    config.DefaultConfig,
        Client:    client.DefaultClient,
        Server:    server.DefaultServer,
        Store:     store.DefaultStore,
        Registry:  registry.DefaultRegistry,
        Router:    router.DefaultRouter,
        Runtime:   runtime.DefaultRuntime,
        Transport: transport.DefaultTransport,
        Context:   context.Background(),
        Signal:    true,
    }
    
    for _, o := range opts {
        o(&opt)
    }
    
    return opt
}

初始化了一堆基础设置,先来看看cmd cmd.DefaultCmd,
在config/cmd/cmd.go DefaultCmd = newCmd()

type Cmd interface {
    // The cli app within this cmd
    App() *cli.App
    // Adds options, parses flags and initialise
    // exits on error
    Init(opts ...Option) error
    // Options set within this command
    Options() Options
}

type cmd struct {
    opts Options
    app  *cli.App
}

type Option func(o *Options)

func newCmd(opts ...Option) Cmd {
    options := Options{
        Auth:      &auth.DefaultAuth,
        Broker:    &broker.DefaultBroker,
        Client:    &client.DefaultClient,
        Registry:  &registry.DefaultRegistry,
        Server:    &server.DefaultServer,
        Selector:  &selector.DefaultSelector,
        Transport: &transport.DefaultTransport,
        Router:    &router.DefaultRouter,
        Runtime:   &runtime.DefaultRuntime,
        Store:     &store.DefaultStore,
        Tracer:    &trace.DefaultTracer,
        Profile:   &profile.DefaultProfile,
        Config:    &config.DefaultConfig,

        Brokers:    DefaultBrokers,
        Clients:    DefaultClients,
        Registries: DefaultRegistries,
        Selectors:  DefaultSelectors,
        Servers:    DefaultServers,
        Transports: DefaultTransports,
        Routers:    DefaultRouters,
        Runtimes:   DefaultRuntimes,
        Stores:     DefaultStores,
        Tracers:    DefaultTracers,
        Auths:      DefaultAuths,
        Profiles:   DefaultProfiles,
        Configs:    DefaultConfigs,
    }

    for _, o := range opts {
        o(&options)
    }

    if len(options.Description) == 0 {
        options.Description = "a go-micro service"
    }

    cmd := new(cmd)
    cmd.opts = options
    cmd.app = cli.NewApp()
    cmd.app.Name = cmd.opts.Name
    cmd.app.Version = cmd.opts.Version
    cmd.app.Usage = cmd.opts.Description
    cmd.app.Before = cmd.Before
    cmd.app.Flags = DefaultFlags
    cmd.app.Action = func(c *cli.Context) error {
        return nil
    }

    if len(options.Version) == 0 {
        cmd.app.HideVersion = true
    }

    return cmd
}

这里的cmd结构体中

  1. opts就是options.go中定义的各种属性,基本上micro中每个模块的option都在options.go中,然后是一堆设置属性的函数(函数选项模式)
  2. app是cli.app实例,这个cli是fork的一个开源cli库urfave/cli , 文档
  3. newCmd()也初始化了很多Options,接受参数赋值o(&options)
  4. 初始化cmd,设置一些基础值,包括DefaultFlags,这些可以在命令行帮助文本出现,另外还有cmd.app.Before = cmd.Before
func (c *cmd) Before(ctx *cli.Context) error
{
    //...太长省略
    //这里下面第三步注释
    authFn := func() auth.Auth { return *c.opts.Auth }
    cacheFn := func() *client.Cache { return (*c.opts.Client).Options().Cache }
    microClient := wrapper.CacheClient(cacheFn, grpc.NewClient())
    microClient = wrapper.AuthClient(authFn, microClient)
    //...太长省略
}

这里做了很多事情

  1. 设置clientOpts []client.Option,从命令行取以下参数放入数组:client_retries,client_request_timeout,client_pool_size,client_pool_ttl,
  2. 设置serverOpts []server.Option,从命令行取以下参数放入数组:server_metadata(数组),server_name,server_version,server_id,server_address,server_advertise,register_ttl,register_interval,
  3. 设置microClient,先定义了authFn,cacheFn用于初始化client.client,相当于在client上wrapper包装auth和cache,默认的cache是memory
  4. 设置authOpts,auth的options,从命令行取以下参数放入数组:auth_address,auth_id,auth_public_key,auth_private_key,service_namespace,auth_provider,auth_provider_client_id,auth_provider_endpoint,auth_provider_redirect,auth_provider_scope
  5. 设置auth,如果命令行参数auth给了指定的auth,就初始化指定的,没有就使用默认的noop(没有验证)
  6. 生成服务serverID
  7. 设置broker options:brokerOpts,命令行参数broker_address
  8. 设置registry options:registryOpts,命令行参数registry_address。设置服务注册registry,命令行参数registry,命令行有指定就初始化指定的服务注册中心,不然就用默认的,目前默认是mdns(单机有效)
  9. 设置selector,命令行参数selector,命令行有指定就初始化指定的selector(目前2.x版本是临时状态,v3有较大变化)
  10. 设置路由router,命令行参数service_namespace,router_address,router,命令行有指定就初始化指定的
  11. 设置store options,storeOpts命令行参数store_address,store_database,store_table,store,默认memory,基于patrickmn/go-cache
  12. 设置runtime options,runtimeOpts,命令行参数runtime_source,runtime,命令行有指定就初始化指定的
  13. 设置tracer,profile,命令行参数tracer,profile
  14. 设置broker,命令行参数broker,这里如果有自定义命令行参数,会同步添加到serverOpts,clientOpts
  15. 设置transport options,命令行参数transport_address,transport这里如果有自定义命令行参数,会同步添加到serverOpts,clientOpts
  16. 设置config源,命令行参数config,service_namespace,如果config设为service会有特殊设置
  17. 设置client,命令行参数client
  18. 设置server,命令行参数client

终于设置完了。。。
总结,micro 微服务启动过程中,通过cmd设置好了许多组件,cmd.Options()即可获取

go micro 分析系列文章
go micro server 启动分析
go micro client
go micro broker
go micro cmd
go micro config
go micro store
go micro registry
go micro router
go micro runtime
go micro transport
go micro web
go micro registry 插件consul
go micro plugin
go micro jwt 网关鉴权
go micro 链路追踪
go micro 熔断与限流
go micro wrapper 中间件
go micro metrics 接入Prometheus、Grafana


舞林
1.5k 声望38 粉丝

漫步风雨中


« 上一篇
go micro broker
下一篇 »
go micro config