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
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
有疑问加站长微信联系(非本文作者)