go micro transport

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
}

初始化了一堆基础设置,来看看Transport
transport.DefaultTransport,
在transport/transport.go中的
DefaultTransport Transport = newHTTPTransport()

func NewTransport(opts ...Option) Transport {
    return newHTTPTransport(opts...)
}

func newHTTPTransport(opts ...Option) *httpTransport {
    var options Options
    for _, o := range opts {
        o(&options)
    }
    return &httpTransport{opts: options}
}

这里就初始化并设置options,返回了一个httpTransport{}

手动指定其他的Transport,两种方式

  1. micro.NewService()中传入micro.Transport(grpc.NewTransport()),, 即可使用grpc Transport
  2. plugins.go中import加入"github.com/micro/go-plugins/transport/grpc/v2"

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)