github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/docs/v2/plugins/plugins-framework.md (about) 1 --- 2 title: Framework Plugins 3 keywords: plugins 4 tags: [plugins] 5 sidebar: home_sidebar 6 permalink: /plugins-framework 7 summary: 8 --- 9 10 Micro is a pluggable toolkit and framework. The internal features can be swapped out with any [go-plugins](https://github.com/micro/go-plugins). 11 12 The toolkit has a separate plugin interface. Learn more at [micro/plugin](https://github.com/tickoalcantara12/micro/tree/master/plugin). 13 14 Below is info on go-micro plugin usage. 15 16 ## Usage 17 18 Plugins can be added to go-micro in the following ways. By doing so they'll be available to set via command line args or environment variables. 19 20 Import the plugins in a Go program then call service.Init to parse the command line and environment variables. 21 22 ```go 23 import ( 24 "github.com/micro/go-micro/v2" 25 _ "github.com/micro/go-plugins/broker/rabbitmq" 26 _ "github.com/micro/go-plugins/registry/kubernetes" 27 _ "github.com/micro/go-plugins/transport/nats" 28 ) 29 30 func main() { 31 service := micro.NewService( 32 // Set service name 33 micro.Name("my.service"), 34 ) 35 36 // Parse CLI flags 37 service.Init() 38 } 39 ``` 40 41 ### Flags 42 43 Specify the plugins as flags 44 45 ```shell 46 go run service.go --broker=rabbitmq --registry=kubernetes --transport=nats 47 ``` 48 49 ### Env 50 51 Use env vars to specify the plugins 52 53 ``` 54 MICRO_BROKER=rabbitmq \ 55 MICRO_REGISTRY=kubernetes \ 56 MICRO_TRANSPORT=nats \ 57 go run service.go 58 ``` 59 60 ### Options 61 62 Import and set as options when creating a new service 63 64 ```go 65 import ( 66 "github.com/micro/go-micro/v2" 67 "github.com/micro/go-plugins/registry/kubernetes" 68 ) 69 70 func main() { 71 registry := kubernetes.NewRegistry() //a default to using env vars for master API 72 73 service := micro.NewService( 74 // Set service name 75 micro.Name("my.service"), 76 // Set service registry 77 micro.Registry(registry), 78 ) 79 } 80 ``` 81 82 ## Build 83 84 An anti-pattern is modifying the `main.go` file to include plugins. Best practice recommendation is to include 85 plugins in a separate file and rebuild with it included. This allows for automation of building plugins and 86 clean separation of concerns. 87 88 Create file plugins.go 89 90 ```go 91 package main 92 93 import ( 94 _ "github.com/micro/go-plugins/broker/rabbitmq" 95 _ "github.com/micro/go-plugins/registry/kubernetes" 96 _ "github.com/micro/go-plugins/transport/nats" 97 ) 98 ``` 99 100 Build with plugins.go 101 102 ```shell 103 go build -o service main.go plugins.go 104 ``` 105 106 Run with plugins 107 108 ```shell 109 MICRO_BROKER=rabbitmq \ 110 MICRO_REGISTRY=kubernetes \ 111 MICRO_TRANSPORT=nats \ 112 service 113 ``` 114 115 ## Rebuild Toolkit 116 117 If you want to integrate plugins simply link them in a separate file and rebuild 118 119 Create a plugins.go file 120 121 ```go 122 import ( 123 // etcd v3 registry 124 _ "github.com/micro/go-plugins/registry/etcdv3" 125 // nats transport 126 _ "github.com/micro/go-plugins/transport/nats" 127 // kafka broker 128 _ "github.com/micro/go-plugins/broker/kafka" 129 ) 130 ``` 131 132 Build binary 133 134 ```shell 135 // For local use 136 go build -i -o micro ./main.go ./plugins.go 137 138 // For docker image 139 CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' -i -o micro ./main.go ./plugins.go 140 ``` 141 142 Flag usage of plugins 143 144 ```shell 145 micro --registry=etcdv3 --transport=nats --broker=kafka 146 ``` 147 148 ## Repository 149 150 The go-micro plugins can be found in [github.com/micro/go-plugins](https://github.com/micro/go-plugins). 151 152