共计 2897 个字符,预计需要花费 8 分钟才能阅读完成。
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
正文完