github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/docs/v2/framework/runtime.md (about)

     1  ---
     2  title: Runtime
     3  keywords: runtime
     4  tags: [micro, runtime]
     5  sidebar: home_sidebar
     6  permalink: /runtime
     7  summary: Micro is a runtime for microservices development
     8  ---
     9  
    10  # Overview
    11  
    12  Micro addresses the key requirements for building scalable systems. It takes the microservice architecture pattern and transforms it into 
    13  a set of tools which act as the building blocks of a platform. Micro deals with the complexity of distributed systems and provides 
    14  simple abstractions already understood by developers.
    15  
    16  Technology is constantly evolving. The infrastructure stack is always changing. Micro is a pluggable toolkit which addresses these issues. 
    17  Plug in any stack or underlying technology. Build future-proof systems using micro.
    18  
    19  ## Features
    20  
    21  The runtime is composed of the following features:
    22  
    23  - **API Gateway:** A single http entry point with dynamic request routing using service discovery. The API gateway allows you to build a scalable 
    24  microservice architecture on the backend and consolidate serving a public api on the frontend. The micro api provides powerful routing 
    25  via discovery and pluggable handlers to serve http, grpc, websockets, publish events and more.
    26  
    27  - **Interactive CLI:** A CLI to describe, query and interact directly with your platform and services from the terminal. The CLI 
    28  gives you all the commands you expect to understand what's happening with your micro services. It also includes an interactive mode.
    29  
    30  - **Service Proxy:** A transparent proxy built on [Go Micro](https://github.com/micro/go-micro) and the [MUCP](https://github.com/micro/protocol) 
    31  protocol. Offload service discovery, load balancing, message encoding, middleware, transport and broker plugins to a single a location. 
    32  Run it standalone or alongside your service.
    33  
    34  - **Service Templates:** Generate new service templates to get started quickly. Micro provides predefined templates for writing micro services. 
    35  Always start in the same way, build identical services to be more productive.
    36  
    37  - **SlackOps Bot:** A bot which runs on your platform and lets you manage your applications from Slack itself. The micro bot enables ChatOps 
    38  and gives you the ability to do everything with your team via messaging. It also includes ability to create slack commmands as services which 
    39  are discovered dynamically.
    40  
    41  - **Web Dashboard:** The web dashboard allows you to explore your services, describe their endpoints, the request and response formats and even 
    42  query them directly. The dashboard is also includes a built in CLI like experience for developers who want to drop into the terminal on the fly.
    43  
    44  ## Install Micro
    45  
    46  ```shell
    47  go get github.com/tickoalcantara12/micro/v2
    48  ```
    49  
    50  Or via Docker
    51  
    52  ```shell
    53  docker pull micro/micro
    54  ```
    55  
    56  Latest release binaries
    57  
    58  ```
    59  # MacOS
    60  curl -fsSL https://raw.githubusercontent.com/micro/micro/master/scripts/install.sh | /bin/bash
    61  
    62  # Linux
    63  wget -q  https://raw.githubusercontent.com/micro/micro/master/scripts/install.sh -O - | /bin/bash
    64  
    65  # Windows
    66  powershell -Command "iwr -useb https://raw.githubusercontent.com/micro/micro/master/scripts/install.ps1 | iex"
    67  ```
    68  
    69  ## Dependencies
    70  
    71  The micro runtime has two dependencies: 
    72  
    73  - [Service Discovery](#service-discovery) - used for name resolution
    74  - [Protobuf](#protobuf) - used for code generation
    75  
    76  ## Service Discovery
    77  
    78  Service discovery is used for name resolution, routing and centralising metadata. All services are register themselves in 
    79  discovery so they can be found by other services.
    80  
    81  Micro uses the [go-micro](https://github.com/micro/go-micro) registry interface for service discovery. Multicast DNS is the default 
    82  implementation. This enables a zeroconf setup so you don't have to change anything for local development. In case you're using docker, 
    83  kubernetes or need something more resilient we recommend etcd.
    84  
    85  ### Etcd
    86  
    87  Etcd is an alternative highly available service discovery mechanism
    88  
    89  ```shell
    90  # install
    91  brew install etcd
    92  
    93  # run
    94  etcd
    95  ```
    96  
    97  Pass `--registry=etcd` or set the env var `MICRO_REGISTRY=consul` for any command
    98  
    99  ```shell
   100  # Use flag
   101  micro --registry=etcd list services
   102  
   103  # Use env var
   104  MICRO_REGISTRY=etcd micro list services`
   105  ```
   106  
   107  Specify the address of etcd where it's not on the same host.
   108  
   109  ```
   110  MICRO_REGISTRY_ADDRESS=10.0.0.1:2379
   111  ```
   112  
   113  See [go-plugins](https://github.com/micro/go-plugins) for more service discovery plugins.
   114  
   115  ## Protobuf
   116  
   117  Protobuf is used for code generation. It reduces the amount of boilerplate code needed to be written.
   118  
   119  ```
   120  # install protobuf
   121  brew install protobuf
   122  
   123  # install protoc-gen-go
   124  go get github.com/golang/protobuf/{proto,protoc-gen-go}
   125  
   126  # install protoc-gen-micro
   127  go get github.com/tickoalcantara12/micro/v2/cmd/protoc-gen-micro@master
   128  ```
   129  
   130  See [protoc-gen-micro](https://github.com/tickoalcantara12/micro/tree/master/cmd/protoc-gen-micro) for more details.
   131  
   132  ## Writing a service
   133  
   134  Micro includes new template generation to speed up writing applications
   135  
   136  For full details on writing services see [**go-micro**](https://github.com/micro/go-micro).
   137  
   138  ### Generate template
   139  
   140  Here we'll quickly generate an example template using `micro new`
   141  
   142  Specify a path relative to $GOPATH
   143  
   144  ``` 
   145  micro new github.com/micro/example
   146  ```
   147  
   148  The command will output
   149  
   150  ```
   151  example/
   152  	Dockerfile	# A template docker file
   153  	README.md	# A readme with command used
   154  	handler/	# Example rpc handler
   155  	main.go		# The main Go program
   156  	proto/		# Protobuf directory
   157  	subscriber/	# Example pubsub Subscriber
   158  ```
   159  
   160  Compile the protobuf code using `protoc`
   161  
   162  ```
   163  protoc --proto_path=. --micro_out=. --go_out=. proto/example/example.proto
   164  ```
   165  
   166  Now run it like any other go application
   167  
   168  ```
   169  # Run the micro service
   170  micro server
   171  
   172  # cd into your service directory
   173  cd github.com/micro/example
   174  
   175  # run the server
   176  micro run --server .
   177  ```
   178  
   179  ## Example
   180  
   181  Now we have a running application using `micro new` template generation, let's test it out.
   182  
   183  - [List services](#list-services)
   184  - [Get service](#get-service)
   185  - [Call service](#call-service)
   186  - [Run API](#run-api)
   187  - [Call API](#call-api)
   188  
   189  ### List services
   190  
   191  Each service registers with discovery so we should be able to find it.
   192  
   193  ```shell
   194  micro list services
   195  ```
   196  
   197  Output
   198  ```
   199  etcd
   200  go.micro.srv.example
   201  topic:topic.go.micro.srv.example
   202  ```
   203  
   204  The example app has registered with the fully qualified domain name `go.micro.srv.example`
   205  
   206  ### Get Service
   207  
   208  Each service registers with a unique id, address and metadata.
   209  
   210  ```shell
   211  micro get service go.micro.srv.example
   212  ```
   213  
   214  Output
   215  ```
   216  service  go.micro.srv.example
   217  
   218  version latest
   219  
   220  ID	Address	Port	Metadata
   221  go.micro.srv.example-437d1277-303b-11e8-9be9-f40f242f6897	192.168.1.65	53545	transport=http,broker=http,server=rpc,registry=etcd
   222  
   223  Endpoint: Example.Call
   224  Metadata: stream=false
   225  
   226  Request: {
   227  	name string
   228  }
   229  
   230  Response: {
   231  	msg string
   232  }
   233  
   234  
   235  Endpoint: Example.PingPong
   236  Metadata: stream=true
   237  
   238  Request: {}
   239  
   240  Response: {}
   241  
   242  
   243  Endpoint: Example.Stream
   244  Metadata: stream=true
   245  
   246  Request: {}
   247  
   248  Response: {}
   249  
   250  
   251  Endpoint: Func
   252  Metadata: subscriber=true,topic=topic.go.micro.srv.example
   253  
   254  Request: {
   255  	say string
   256  }
   257  
   258  Response: {}
   259  
   260  
   261  Endpoint: Example.Handle
   262  Metadata: subscriber=true,topic=topic.go.micro.srv.example
   263  
   264  Request: {
   265  	say string
   266  }
   267  
   268  Response: {}
   269  ```
   270  
   271  ### Call service
   272  
   273  Make an RPC call via the CLI. The query is sent as json.
   274  
   275  ```shell
   276  micro call go.micro.srv.example Example.Call '{"name": "John"}'
   277  ```
   278  
   279  Output
   280  ```
   281  {
   282  	"msg": "Hello John"
   283  }
   284  ```
   285  
   286  Look at the [cli doc](https://micro.dev/docs/cli.html) for more info.
   287  
   288  Now let's test call the service via HTTP.
   289  
   290  ### Run API
   291  
   292  The micro api is a http gateway which dynamically routes to backend services
   293  
   294  Let's run it so we can query the example service.
   295  
   296  ```
   297  MICRO_API_HANDLER=rpc \
   298  MICRO_API_NAMESPACE=go.micro.srv \
   299  micro api
   300  ```
   301  
   302  Some info:
   303  
   304  - `MICRO_API_HANDLER` sets the http handler
   305  - `MICRO_API_NAMESPACE` sets the service namespace
   306  
   307  ### Call API
   308  
   309  Make POST request to the api using json
   310  ```
   311  curl -XPOST -H 'Content-Type: application/json' -d '{"name": "John"}' http://localhost:8080/example/call
   312  ```
   313  
   314  Output
   315  ```
   316  {"msg":"Hello John"}
   317  ```
   318  
   319  See the [api doc](https://micro.dev/docs/api.html) for more info.
   320  
   321  ## Plugins
   322  
   323  Micro is built on [go-micro](https://github.com/micro/go-micro) making it a pluggable toolkit.
   324  
   325  Go-micro provides abstractions for distributed systems infrastructure which can be swapped out.
   326  
   327  ### Pluggable Features
   328  
   329  The micro features which are pluggable:
   330  
   331  - broker - pubsub message broker
   332  - registry - service discovery 
   333  - selector - client side load balancing
   334  - transport - request-response or bidirectional streaming
   335  - client - the client which manages the above features
   336  - server - the server which manages the above features
   337  
   338  Find plugins at [go-plugins](https://github.com/micro/go-plugins)
   339  
   340  ### Using Plugins
   341  
   342  Integrate go-micro plugins by simply linking them in a separate file
   343  
   344  Create a plugins.go file
   345  ```go
   346  import (
   347  	// etcd v3 registry
   348  	_ "github.com/micro/go-plugins/registry/etcdv3"
   349  	// nats transport
   350  	_ "github.com/micro/go-plugins/transport/nats"
   351  	// kafka broker
   352  	_ "github.com/micro/go-plugins/broker/kafka"
   353  )
   354  ```
   355  
   356  ### Building Binary
   357  
   358  Rebuild the micro binary using the Go toolchain
   359  
   360  ```shell
   361  # For local use
   362  go build -i -o micro ./main.go ./plugins.go
   363  
   364  # For docker image
   365  CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' -i -o micro ./main.go ./plugins.go
   366  ```
   367  
   368  ### Enable Plugins
   369  
   370  Enable the plugins with command line flags or env vars
   371  
   372  ```shell
   373  # flags
   374  micro --registry=etcdv3 --transport=nats --broker=kafka [command]
   375  
   376  # env vars
   377  MICRO_REGISTRY=etcdv3 MICRO_TRANSPORT=nats MICRO_BROKER=kafka micro [command]
   378  ```
   379