go micro v2版本中,consul不在默认反对,官网举荐应用etcd,具体起因官网blog有讲https://medium.com/microhq/de…
consul被放到go-plugins, 由社区保护github.com/micro/go-plugins/registry/consul
要应用consul,须要从go-plugins引入
import (
"fmt"
"os"
"context"
"github.com/micro/cli/v2"
proto "github.com/micro/examples/service/proto"
"github.com/micro/go-micro/v2"
"github.com/micro/go-micro/v2/transport/grpc"
"github.com/micro/go-plugins/registry/consul/v2"
)
/*
Example usage of top level service initialisation
*/
type Greeter struct{}
func (g *Greeter) Hello(ctx context.Context, req *proto.Request, rsp *proto.Response) error {
rsp.Greeting = "Hello " + req.Name
return nil
}
func main() {
//reg := consul.NewRegistry()
// Create a new service. Optionally include some options here.
service := micro.NewService(
micro.Name("greeter"),
micro.Version("latest"),
micro.Metadata(map[string]string{
"type": "helloworld",
}),
micro.Registry(reg),
micro.Transport(grpc.NewTransport()),
// Setup some flags. Specify --run_client to run the client
// Add runtime flags
// We could do this below too
micro.Flags(&cli.BoolFlag{
Name: "run_client",
Usage: "Launch the client",
}),
)
// Init will parse the command line flags. Any flags set will
// override the above settings. Options defined here will
// override anything set on the command line.
service.Init(
// Add runtime action
// We could actually do this above
micro.Action(func(c *cli.Context) error {
if c.Bool("run_client") {
runClient(service)
os.Exit(0)
}
return nil
}),
)
// By default we'll run the server unless the flags catch us
// Setup the server
// Register handler
proto.RegisterGreeterHandler(service.Server(), new(Greeter))
// Run the server
if err := service.Run(); err != nil {
fmt.Println(err)
}
}
两种形式应用插件,具体见[go-plugins应用阐明](https://github.com/micro/go-p…
- 第一种写到plugins.go中,编译的时候带上plugins
go build -o service ./main.go ./plugins.go
启动时通过命令行参数
或环境变量
指定
./service --registry=consul
- 间接在import中申明,并在micro.NewService()参数中指定,如上文例子
reg := consul.NewRegistry()
micro.Registry(reg),
那么插件是什么时候失效的呢,
micro.NewService()->newService(opts…)->newOptions(opts…)
// Registry sets the registry for the service
// and the underlying components
func Registry(r registry.Registry) Option {
return func(o *Options) {
o.Registry = r
// Update router
o.Router.Init(router.Registry(r))
// Update server
o.Server.Init(server.Registry(r))
// Update Broker
o.Broker.Init(broker.Registry(r))
}
}
micro.Registry(reg)
返回一个匿名函数设置了o.Registry为传进来的r
main()中申明的reg := consul.NewRegistry()
,在通过micro.Registry(reg),
指定consul为服务发现核心
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,
}
fmt.Println("1", transport.DefaultTransport)
for _, o := range opts {
o(&opt)
}
fmt.Println("2", opt.Transport)
return opt
}
在这里顺次调用了micro.NewService()参数中的设置函数设置各项参数,具体能够看下“函数选项模式 Functional Options”,
这里加了2行打印信息能够直观的查看状况,输入是
1 mdns 默认值
2 consul opts设置失效后,变为consul
发表回复