Support plugin loading on service.Init

This commit is contained in:
Asim Aslam 2019-09-09 20:17:36 -07:00
parent 3bfbcd5e6a
commit a5ce3e32da
3 changed files with 38 additions and 0 deletions

View File

@ -92,6 +92,11 @@ var (
EnvVar: "MICRO_CLIENT_POOL_TTL", EnvVar: "MICRO_CLIENT_POOL_TTL",
Usage: "Sets the client connection pool ttl. e.g 500ms, 5s, 1m. Default: 1m", Usage: "Sets the client connection pool ttl. e.g 500ms, 5s, 1m. Default: 1m",
}, },
cli.StringSliceFlag{
Name: "plugin",
EnvVar: "MICRO_PLUGIN",
Usage: "Comma separated list of plugins e.g /path/to/plugin.so",
},
cli.IntFlag{ cli.IntFlag{
Name: "register_ttl", Name: "register_ttl",
EnvVar: "MICRO_REGISTER_TTL", EnvVar: "MICRO_REGISTER_TTL",

View File

@ -32,3 +32,15 @@ var (
func NewPlugin() Plugin { func NewPlugin() Plugin {
return &plugin{} return &plugin{}
} }
func Build(path string, c *Config) error {
return DefaultPlugin.Build(path, c)
}
func Load(path string) (*Config, error) {
return DefaultPlugin.Load(path)
}
func Init(c *Config) error {
return DefaultPlugin.Init(c)
}

View File

@ -3,6 +3,7 @@ package micro
import ( import (
"os" "os"
"os/signal" "os/signal"
"strings"
"sync" "sync"
"syscall" "syscall"
@ -10,7 +11,9 @@ import (
"github.com/micro/go-micro/config/cmd" "github.com/micro/go-micro/config/cmd"
"github.com/micro/go-micro/debug/handler" "github.com/micro/go-micro/debug/handler"
"github.com/micro/go-micro/metadata" "github.com/micro/go-micro/metadata"
"github.com/micro/go-micro/plugin"
"github.com/micro/go-micro/server" "github.com/micro/go-micro/server"
"github.com/micro/go-micro/util/log"
) )
type service struct { type service struct {
@ -44,6 +47,24 @@ func (s *service) Init(opts ...Option) {
} }
s.once.Do(func() { s.once.Do(func() {
// setup the plugins
for _, p := range strings.Split(os.Getenv("MICRO_PLUGIN"), ",") {
if len(p) == 0 {
continue
}
// load the plugin
c, err := plugin.Load(p)
if err != nil {
log.Fatal(err)
}
// initialise the plugin
if err := plugin.Init(c); err != nil {
log.Fatal(err)
}
}
// Initialise the command flags, overriding new service // Initialise the command flags, overriding new service
_ = s.opts.Cmd.Init( _ = s.opts.Cmd.Init(
cmd.Broker(&s.opts.Broker), cmd.Broker(&s.opts.Broker),