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
}
初始化了一堆根底设置,来看看 Transporttransport.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,两种形式
- micro.NewService() 中传入
micro.Transport(grpc.NewTransport()),
,即可应用 grpc Transport - plugins.go 中 import 退出
"github.com/micro/go-plugins/transport/grpc/v2"