github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/docs/v2/library/go-plugins.md (about)

     1  ---
     2  title: Plugins
     3  keywords: go-micro, framework, plugins
     4  tags: [go-micro, framework, plugins]
     5  sidebar: home_sidebar
     6  permalink: /go-plugins
     7  summary: Go Micro is a pluggable framework
     8  ---
     9  
    10  # Overview
    11  
    12  Go Micro is built on Go interfaces. Because of this the implementation of these interfaces are pluggable.
    13  
    14  By default go-micro only provides a few implementation of each interface at the core but it's completely pluggable. 
    15  There's already dozens of plugins which are available at [github.com/micro/go-plugins](https://github.com/micro/go-plugins). 
    16  Contributions are welcome! Plugins ensure that your Go Micro services live on long after technology evolves.
    17  
    18  ### Add plugins
    19  
    20  If you want to integrate plugins simply link them in a separate file and rebuild
    21  
    22  Create a plugins.go file and import the plugins you want:
    23  
    24  ```go
    25  package main
    26  
    27  import (
    28          // consul registry
    29          _ "github.com/micro/go-plugins/registry/consul"
    30          // rabbitmq transport
    31          _ "github.com/micro/go-plugins/transport/rabbitmq"
    32          // kafka broker
    33          _ "github.com/micro/go-plugins/broker/kafka"
    34  )
    35  ```
    36  
    37  Build your application by including the plugins file:
    38  
    39  ```shell
    40  # assuming files main.go and plugins.go are in the top level dir
    41   
    42  # For local use
    43  go build -o service *.go
    44  ```
    45  
    46  Flag usage of plugins:
    47  
    48  ```shell
    49  service --registry=etcdv3 --transport=nats --broker=kafka
    50  ```
    51  
    52  Or what's preferred is using environment variables for 12-factor apps
    53  
    54  ```
    55  MICRO_REGISTRY=consul \
    56  MICRO_TRANSPORT=rabbitmq \
    57  MICRO_BROKER=kafka \
    58  service
    59  ```
    60  
    61  ### Plugin Option
    62  
    63  Alternatively you can set the plugin as an option to a service directly in code
    64  
    65  ```go
    66  package main
    67  
    68  import (
    69          "github.com/micro/go-micro/v2" 
    70          // consul registry
    71          "github.com/micro/go-plugins/registry/consul"
    72          // rabbitmq transport
    73          "github.com/micro/go-plugins/transport/rabbitmq"
    74          // kafka broker
    75          "github.com/micro/go-plugins/broker/kafka"
    76  )
    77  
    78  func main() {
    79  	registry := consul.NewRegistry()
    80  	broker := kafka.NewBroker()
    81  	transport := rabbitmq.NewTransport()
    82  
    83          service := micro.NewService(
    84                  micro.Name("greeter"),
    85                  micro.Registry(registry),
    86                  micro.Broker(broker),
    87                  micro.Transport(transport),
    88          )
    89  
    90  	service.Init()
    91  	service.Run()
    92  }
    93  ```
    94  
    95  ### Write Plugins
    96  
    97  Plugins are a concept built on Go's interface. Each package maintains a high level interface abstraction. 
    98  Simply implement the interface and pass it in as an option to the service.
    99  
   100  The service discovery interface is called [Registry](https://pkg.go.dev/github.com/micro/go-micro/v2/registry#Registry). 
   101  Anything which implements this interface can be used as a registry. The same applies to the other packages.
   102  
   103  ```go
   104  type Registry interface {
   105      Register(*Service, ...RegisterOption) error
   106      Deregister(*Service) error
   107      GetService(string) ([]*Service, error)
   108      ListServices() ([]*Service, error)
   109      Watch() (Watcher, error)
   110      String() string
   111  }
   112  ```
   113  
   114  Browse [go-plugins](https://github.com/micro/go-plugins) to get a better idea of implementation details.
   115