go-micro.dev/v5@v5.12.0/internal/website/docs/plugins.md (about)

     1  ---
     2  layout: default
     3  ---
     4  
     5  # Plugins
     6  
     7  Plugins are scoped under each interface directory within this repository. To use a plugin, import it directly from the corresponding interface subpackage and pass it to your service via options.
     8  
     9  Common interfaces and locations:
    10  - Registry: `go-micro.dev/v5/registry/*` (e.g. `consul`, `etcd`, `nats`, `mdns`)
    11  - Broker: `go-micro.dev/v5/broker/*` (e.g. `nats`, `rabbitmq`, `http`, `memory`)
    12  - Transport: `go-micro.dev/v5/transport/*` (e.g. `nats`, default `http`)
    13  - Server: `go-micro.dev/v5/server/*` (e.g. `grpc` for native gRPC compatibility)
    14  - Client: `go-micro.dev/v5/client/*` (e.g. `grpc` for native gRPC compatibility)
    15  - Store: `go-micro.dev/v5/store/*` (e.g. `postgres`, `mysql`, `nats-js-kv`, `memory`)
    16  - Auth, Cache, etc. follow the same pattern under their respective directories.
    17  
    18  ## Registry Examples
    19  
    20  Consul:
    21  ```go
    22  import (
    23      "go-micro.dev/v5"
    24      "go-micro.dev/v5/registry/consul"
    25  )
    26  
    27  func main() {
    28      reg := consul.NewConsulRegistry()
    29      svc := micro.NewService(
    30          micro.Registry(reg),
    31      )
    32      svc.Init()
    33      svc.Run()
    34  }
    35  ```
    36  
    37  Etcd:
    38  ```go
    39  import (
    40      "go-micro.dev/v5"
    41      "go-micro.dev/v5/registry/etcd"
    42  )
    43  
    44  func main() {
    45      reg := etcd.NewRegistry()
    46      svc := micro.NewService(micro.Registry(reg))
    47      svc.Init()
    48      svc.Run()
    49  }
    50  ```
    51  
    52  ## Broker Examples
    53  
    54  NATS:
    55  ```go
    56  import (
    57      "go-micro.dev/v5"
    58      bnats "go-micro.dev/v5/broker/nats"
    59  )
    60  
    61  func main() {
    62      b := bnats.NewNatsBroker()
    63      svc := micro.NewService(micro.Broker(b))
    64      svc.Init()
    65      svc.Run()
    66  }
    67  ```
    68  
    69  RabbitMQ:
    70  ```go
    71  import (
    72      "go-micro.dev/v5"
    73      "go-micro.dev/v5/broker/rabbitmq"
    74  )
    75  
    76  func main() {
    77      b := rabbitmq.NewBroker()
    78      svc := micro.NewService(micro.Broker(b))
    79      svc.Init()
    80      svc.Run()
    81  }
    82  ```
    83  
    84  ## Transport Example (NATS)
    85  ```go
    86  import (
    87      "go-micro.dev/v5"
    88      tnats "go-micro.dev/v5/transport/nats"
    89  )
    90  
    91  func main() {
    92      t := tnats.NewTransport()
    93      svc := micro.NewService(micro.Transport(t))
    94      svc.Init()
    95      svc.Run()
    96  }
    97  ```
    98  
    99  ## gRPC Server/Client (Native gRPC Compatibility)
   100  
   101  For native gRPC compatibility (required for `grpcurl`, polyglot gRPC clients, etc.), use the gRPC server and client plugins. Note: This is different from the gRPC transport.
   102  
   103  ```go
   104  import (
   105      "go-micro.dev/v5"
   106      grpcServer "go-micro.dev/v5/server/grpc"
   107      grpcClient "go-micro.dev/v5/client/grpc"
   108  )
   109  
   110  func main() {
   111      svc := micro.NewService(
   112          micro.Server(grpcServer.NewServer()),
   113          micro.Client(grpcClient.NewClient()),
   114      )
   115      svc.Init()
   116      svc.Run()
   117  }
   118  ```
   119  
   120  See [Native gRPC Compatibility](guides/grpc-compatibility.md) for a complete guide.
   121  
   122  ## Store Examples
   123  
   124  Postgres:
   125  ```go
   126  import (
   127      "go-micro.dev/v5"
   128      postgres "go-micro.dev/v5/store/postgres"
   129  )
   130  
   131  func main() {
   132      st := postgres.NewStore()
   133      svc := micro.NewService(micro.Store(st))
   134      svc.Init()
   135      svc.Run()
   136  }
   137  ```
   138  
   139  NATS JetStream KV:
   140  ```go
   141  import (
   142      "go-micro.dev/v5"
   143      natsjskv "go-micro.dev/v5/store/nats-js-kv"
   144  )
   145  
   146  func main() {
   147      st := natsjskv.NewStore()
   148      svc := micro.NewService(micro.Store(st))
   149      svc.Init()
   150      svc.Run()
   151  }
   152  ```
   153  
   154  ## Notes
   155  - Defaults: If you don’t set an implementation, Go Micro uses sensible in-memory or local defaults (e.g., mDNS for registry, HTTP transport, memory broker/store).
   156  - Options: Each plugin exposes constructor options to configure addresses, credentials, TLS, etc.
   157  - Imports: Only import the plugin you need; this keeps binaries small and dependencies explicit.