go micro store

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
}

初始化了一堆基础设置,来看看Store
store.DefaultStore,
这里不要直接去看store/store.go中的
DefaultStore Store = new(noopStore)
因为在micro/defaults.go中已经初始化了,默认使用的是
store.DefaultStore = memoryStore.NewStore()

// NewStore returns a memory store
func NewStore(opts ...store.Option) store.Store {
    s := &memoryStore{
        options: store.Options{
            Database: "micro",
            Table:    "micro",
        },
        store: cache.New(cache.NoExpiration, 5*time.Minute),
    }
    for _, o := range opts {
        o(&s.options)
    }
    return s
}

type memoryStore struct {
    options store.Options

    store *cache.Cache
}
  1. 初始化了memoryStore{},store是使用的patrickmn/go-cache
  2. 设置options,返回memoryStore实例

功能比较简单,在store/memory/memory.go封装了读写删改等操作函数

其他store有cache,cockroach,file,service,实现都比较简单,有兴趣可以自行翻看

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

你可能感兴趣的:(micro,golang)