import (
"github.com/micro/go-micro"
_ "github.com/micro/go-plugins/broker/rabbitmq"
_ "github.com/micro/go-plugins/registry/kubernetes"
_ "github.com/micro/go-plugins/transport/nats"
)
func main() {
service := micro.NewService(
// Set service name
micro.Name("my.service"),
)
// Parse CLI flags
service.Init()
}
通过CLI标志使用
通过CLI标志激活
go run service.go --broker=rabbitmq --registry=kubernetes --transport=nats
直接使用插件
CLI标志提供了初始化插件的简单方法,但您可以自己做同样的事情。
import (
"github.com/micro/go-micro"
"github.com/micro/go-plugins/registry/kubernetes"
)
func main() {
registry := kubernetes.NewRegistry() //a default to using env vars for master API
service := micro.NewService(
// Set service name
micro.Name("my.service"),
// Set service registry
micro.Registry(registry),
)
}
// For local use
go build -i -o micro ./main.go ./plugins.go
// For docker image
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' -i -o micro ./main.go ./plugins.go