gitee.com/sasukebo/go-micro/v4@v4.7.1/plugins/README.md (about)

     1  # Plugins [![License](https://img.shields.io/:license-apache-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![GoDoc](https://godoc.org/gitee.com/sasukebo/go-micro/plugins?status.svg)](https://godoc.org/gitee.com/sasukebo/go-micro/plugins)
     2  
     3  Go plugins is a place for community maintained plugins.
     4  
     5  ## Overview
     6  
     7  Micro tooling is built on a powerful pluggable architecture. Plugins can be swapped out with zero code changes.
     8  This repository contains plugins for all micro related tools. Read on for further info.
     9  
    10  ## Getting Started
    11  
    12  * [Contents](#contents)
    13  * [Usage](#usage)
    14  * [Build Pattern](#build-pattern)
    15  * [Contributions](#contributions)
    16  
    17  ## Contents
    18  
    19  Contents of this repository:
    20  
    21  | Directory | Description                                                     |
    22  | --------- | ----------------------------------------------------------------|
    23  | Broker    | PubSub messaging; NATS, NSQ, RabbitMQ, Kafka                    |
    24  | Client    | RPC Clients; gRPC, HTTP                                         |
    25  | Codec     | Message Encoding; BSON, Mercury                                 |
    26  | Micro     | Micro Toolkit Plugins                                           |
    27  | Registry  | Service Discovery; Etcd, Gossip, NATS                           |
    28  | Selector  | Load balancing; Label, Cache, Static                            |
    29  | Server    | RPC Servers; gRPC, HTTP                                         |
    30  | Transport | Bidirectional Streaming; NATS, RabbitMQ                         | 
    31  | Wrapper   | Middleware; Circuit Breakers, Rate Limiting, Tracing, Monitoring|
    32  
    33  ## Usage
    34  
    35  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.
    36  
    37  Import the plugins in a `plugins.go` file
    38  
    39  ```go
    40  package main
    41  
    42  import (
    43  	_ "gitee.com/sasukebo/go-micro/plugins/broker/rabbitmq/v4"
    44  	_ "gitee.com/sasukebo/go-micro/plugins/registry/kubernetes/v4"
    45  	_ "gitee.com/sasukebo/go-micro/plugins/transport/nats/v4"
    46  )
    47  ```
    48  
    49  Create your service and ensure you call `service.Init`
    50  
    51  ```go
    52  package main
    53  
    54  import (
    55  	"gitee.com/sasukebo/go-micro/v4"
    56  )
    57  
    58  func main() {
    59  	service := micro.NewService(
    60  		// Set service name
    61  		micro.Name("my.service"),
    62  	)
    63  
    64  	// Parse CLI flags
    65  	service.Init()
    66  }
    67  ```
    68  
    69  Build your service
    70  
    71  ```
    72  go build -o service ./main.go ./plugins.go
    73  ```
    74  
    75  ### Env
    76  
    77  Use environment variables to set the
    78  
    79  ```
    80  MICRO_BROKER=rabbitmq \
    81  MICRO_REGISTRY=kubernetes \ 
    82  MICRO_TRANSPORT=nats \ 
    83  ./service
    84  ```
    85  
    86  ### Flags
    87  
    88  Or use command line flags to enable them
    89  
    90  ```shell
    91  ./service --broker=rabbitmq --registry=kubernetes --transport=nats
    92  ```
    93  
    94  ### Options
    95  
    96  Import and set as options when creating a new service
    97  
    98  ```go
    99  import (
   100  	"gitee.com/sasukebo/go-micro/v4"
   101  	"gitee.com/sasukebo/go-micro/plugins/registry/kubernetes/v4"
   102  )
   103  
   104  func main() {
   105  	registry := kubernetes.NewRegistry() //a default to using env vars for master API
   106  
   107  	service := micro.NewService(
   108  		// Set service name
   109  		micro.Name("my.service"),
   110  		// Set service registry
   111  		micro.Registry(registry),
   112  	)
   113  }
   114  ```
   115  
   116  ## Build
   117  
   118  An anti-pattern is modifying the `main.go` file to include plugins. Best practice recommendation is to include
   119  plugins in a separate file and rebuild with it included. This allows for automation of building plugins and
   120  clean separation of concerns.
   121  
   122  Create file plugins.go
   123  
   124  ```go
   125  package main
   126  
   127  import (
   128  	_ "gitee.com/sasukebo/go-micro/plugins/broker/rabbitmq/v4"
   129  	_ "gitee.com/sasukebo/go-micro/plugins/registry/kubernetes/v4"
   130  	_ "gitee.com/sasukebo/go-micro/plugins/transport/nats/v4"
   131  )
   132  ```
   133  
   134  Build with plugins.go
   135  
   136  ```shell
   137  go build -o service main.go plugins.go
   138  ```
   139  
   140  Run with plugins
   141  
   142  ```shell
   143  MICRO_BROKER=rabbitmq \
   144  MICRO_REGISTRY=kubernetes \
   145  MICRO_TRANSPORT=nats \
   146  service
   147  ```