mirror of
				https://github.com/go-micro/go-micro.git
				synced 2025-10-30 23:27:41 +02:00 
			
		
		
		
	Support plugin loading on service.Init
This commit is contained in:
		| @@ -92,6 +92,11 @@ var ( | ||||
| 			EnvVar: "MICRO_CLIENT_POOL_TTL", | ||||
| 			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{ | ||||
| 			Name:   "register_ttl", | ||||
| 			EnvVar: "MICRO_REGISTER_TTL", | ||||
|   | ||||
| @@ -32,3 +32,15 @@ var ( | ||||
| func NewPlugin() 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) | ||||
| } | ||||
|   | ||||
							
								
								
									
										21
									
								
								service.go
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								service.go
									
									
									
									
									
								
							| @@ -3,6 +3,7 @@ package micro | ||||
| import ( | ||||
| 	"os" | ||||
| 	"os/signal" | ||||
| 	"strings" | ||||
| 	"sync" | ||||
| 	"syscall" | ||||
|  | ||||
| @@ -10,7 +11,9 @@ import ( | ||||
| 	"github.com/micro/go-micro/config/cmd" | ||||
| 	"github.com/micro/go-micro/debug/handler" | ||||
| 	"github.com/micro/go-micro/metadata" | ||||
| 	"github.com/micro/go-micro/plugin" | ||||
| 	"github.com/micro/go-micro/server" | ||||
| 	"github.com/micro/go-micro/util/log" | ||||
| ) | ||||
|  | ||||
| type service struct { | ||||
| @@ -44,6 +47,24 @@ func (s *service) Init(opts ...Option) { | ||||
| 	} | ||||
|  | ||||
| 	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 | ||||
| 		_ = s.opts.Cmd.Init( | ||||
| 			cmd.Broker(&s.opts.Broker), | ||||
|   | ||||
		Reference in New Issue
	
	Block a user