github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/docs/blog/_posts/2016-03-20-micro.md (about)

     1  ---
     2  layout:	post
     3  title:	Micro - a microservices toolkit
     4  date:	2016-03-20 09:00:00
     5  ---
     6  <br>
     7  By now you may have heard of this new phenomenon, <a href="http://en.wikipedia.org/wiki/Microservices">**microservices**</a>. 
     8  If you're not yet familiar and interested in learning more, checkout our introductory post 
     9  <a href="{{ site.baseurl }}/2016/03/17/introduction.html">here</a>.
    10  
    11  In this blog post we're going to discuss <b>[Micro](https://github.com/tickoalcantara12/micro)</b>, an open source microservices toolkit. 
    12  Micro provides the core requirements for building and managing microservices. It consists of a set of libraries and tools 
    13  which are primarily geared towards development in the programming language [Go](https://golang.org/), however looks to 
    14  solve for other languages via HTTP using a [Sidecar](https://github.com/tickoalcantara12/micro/tree/master/car). 
    15  
    16  Before we get to the details of Micro, let's talk about why we decided to dedicate our time to it.
    17  
    18  ##### Development or Deployment
    19  
    20  It's clear from our past experiences and what we're seeing in the industry that there's a need for a focus on development 
    21  rather than deployment. PaaS solutions are readily available. Companies like AWS, Google and Microsoft 
    22  are providing feature rich platforms while also rapidly moving towards supporting container orchestration if not already 
    23  doing so. All of this gives us access to large scale compute with the click of a few buttons.
    24  
    25  This new world sounds great. You might say this solves all your problems, right? Well, while we now have access to 
    26  massive scale computing power, we're still lacking the tools that enable us to write software that's 
    27  capable of leveraging it. Not only that, in this new world, containers are likely more ephemeral, coming and going 
    28  as the runtime reschedules them or machines they're running on fail. 
    29  
    30  ##### Scaling challenges
    31  
    32  The other issue we've seen time and time again is the way in which organisations fall victim to their monolithic 
    33  architectures. With a need to grow at a blistering pace, there's the tendency to cram features into the 
    34  existing system and incur technical debt which snowballs into an unmanageable situation. Aside from this, 
    35  as the organisation attempts to grow the engineering team, it becomes infinitely more difficult for developers 
    36  to collaborate on a single code base or do feature development in parallel without being blocked at time 
    37  of release.
    38  
    39  There's an inevitable need to rearchitect and an eventual path to SOA or microservice based 
    40  architectures. Companies end up taking on an R&D effort in house, learning by trial and error. If only 
    41  there were tools to help create scalable systems, reduce the likely hood of R&D and provide domain expertise 
    42  from those with past experience.
    43  
    44  ##### Enter Micro 
    45  
    46  At Micro we're building a microservice ecosystem that includes tools, services and solutions for microservice 
    47  development. We're building the foundation of that ecosystem with a tool by the same name. 
    48  <b>[Micro](https://github.com/tickoalcantara12/micro)</b> is a microservice toolkit, which will enable the creation of a 
    49  scalable architectures and increase speed of execution. 
    50  
    51  Let's dig into the features of Micro.
    52  
    53  ###### Go Micro
    54  
    55  [**Go Micro**](https://github.com/micro/go-micro) is a pluggable RPC framework used to build microservices in Go. 
    56  It delivers the essential features required to create, discover and communicate with services. The core of any good 
    57  microservice architecture begins by addressing service discovery, synchronous and asynchronous communication. 
    58  
    59  Included packages and features:
    60  
    61  - Registry - Client side service discovery
    62  - Transport - Synchronous communication
    63  - Broker - Asynchronous comunication
    64  - Selector - Node filtering and load balancing
    65  - Codec - Message encoding/decoding
    66  - Server - RPC server building on the above
    67  - Client - RPC client building on the above
    68  
    69  Where Go Micro differs from most libraries is it's pluggable architecture. This allows the implementation and backend 
    70  system for every package to be swapped out. For example; the default service discovery mechanism for the registry is 
    71  [Consul](https://www.consul.io) but this can easily be swapped with a plugin for etcd, zookeeper or anything else 
    72  you choose to implement. Plugins we're implementing can be found at [github.com/micro/go-plugins](https://github.com/micro/go-plugins). 
    73  
    74  The value in a pluggable system is the ability to choose the platform used to support your microservices without 
    75  having to rewrite any code. Go Micro requires zero code changes, just a mere import of a plugin and you're done.
    76  
    77  Go Micro is the starting point for writing microservices. The [**readme**](https://github.com/micro/go-micro) provides 
    78  an overview of how to write, run and query a service. There's a greeter example here [micro/examples/greeter](https://github.com/micro/examples/tree/master/greeter) 
    79  and more example services throughout the repo [github.com/micro](https://github.com/micro).
    80  
    81  <a href="https://github.com/micro/go-micro"><i class="fab fa-github fa-2x"></i> go micro</a>
    82  
    83  ###### Sidecar
    84  
    85  So Go Micro provides a way to write services in Go but how about other languages? How do we create a polygot ecosystem 
    86  where anyone can leverage the advantages of Micro? While Micro is written in Go, we wanted to allow a quicky and easy 
    87  way to integrate applications written in any language.
    88  
    89  Enter the [**Sidecar**](https://github.com/tickoalcantara12/micro/tree/master/car), a lightweight companion service which is 
    90  conceptually “attached” to the main (aka Parent) application and complements it by providing the features of the 
    91  Micro system that are otherwise available using the Go Micro library. The sidecar is a process that runs alongside 
    92  your application, delivering the features of Go Micro via a HTTP interface.
    93  
    94  Features of the sidecar:
    95  
    96  - Registration with discovery system
    97  - Host discovery of other services
    98  - Healthchecking of the main application
    99  - A proxy to make RPC requests
   100  - PubSub via WebSockets
   101  
   102  <p/>
   103  <img src="{{ site.baseurl }}/blog/images/sidecar.png" style="width: 100%; max-width: 500px; height: auto;" />
   104  
   105  Examples of using the Sidecar with ruby or python can be found here [micro/examples/greeter](https://github.com/micro/examples/tree/master/greeter). 
   106  We'll look to add more sample code in the near future to help with understanding how to integrate the sidecar.
   107  
   108  <a href="https://github.com/tickoalcantara12/micro/tree/master/car"><i class="fab fa-github fa-2x"></i> sidecar</a>
   109  
   110  ###### API
   111  
   112  Making RPC requests from one service to another is pretty straight forward with Go Micro but not ideal for external 
   113  access. Instances of a service can fail, they may be rescheduled elsewhere or end up binding to any random port. 
   114  The [**API**](https://github.com/tickoalcantara12/micro/tree/master/api) provides a single entry point to query microservices 
   115  and should be used as the gateway for external access.
   116  
   117  The API provides a few different types of request handlers. 
   118  
   119  ###### 1. /rpc
   120  
   121  Individual services can be queried via RPC using the /rpc endpoint. Example:
   122  
   123  	curl \
   124  		-d "service=go.micro.srv.greeter" \
   125  		-d "method=Say.Hello" \
   126  		-d "request={\"name\": \"John\"}" \
   127  		http://localhost:8080/rpc
   128  	
   129  	{"msg":"Hello John"}
   130  
   131  <p/>
   132  
   133  ###### 2. api.Request
   134  
   135  The API can be used to breakdown URLs to be served by individual microservices. This is a powerful method of API composition. 
   136  Here the API uses the first part of the request path along with a namespace component to determine 
   137  the service to route requests to. HTTP requests are then converted to an [api.Request](https://github.com/tickoalcantara12/micro/blob/master/api/proto/api.proto) 
   138  and forwarded appropriately.
   139  
   140  At Micro we use a pattern of creating API microservices to serve requests at the edge. Separating 
   141  the responsibility of backend versus frontend services.
   142  
   143  An example of API request handling:
   144  
   145  Request
   146  
   147  	GET /greeter/say/hello?name=John
   148  
   149  Becomes
   150  	
   151  	service: go.micro.api.greeter (default namespace go.micro.api is applied)
   152  	method: Say.Hello
   153  	request {
   154  		"method": "GET",
   155  		"path": "/greeter/say/hello",
   156  		"get": {
   157  			"name": "John"
   158  		}
   159  	}
   160  	
   161  
   162  The structure of an api.Request and api.Response:
   163   
   164  	syntax = "proto3";
   165  	
   166  	message Pair {
   167  		optional string key = 1;
   168  		repeated string values = 2;
   169  	}
   170  
   171  	message Request {
   172  		optional string method = 1;   // GET, POST, etc
   173  		optional string path = 2;     // e.g /greeter/say/hello
   174  		map<string, Pair> header = 3; 
   175  		map<string, Pair> get = 4;    // The URI query params
   176  		map<string, Pair> post = 5;   // The post body params
   177  		optional string body = 6;     // raw request body; if not application/x-www-form-urlencoded
   178  	}
   179  
   180  	message Response {
   181  		optional int32 statusCode = 1;
   182  		map<string, Pair> header = 2;
   183  		optional string body = 3;
   184  	}
   185  
   186  An example of how to create an API service can be found here. [Greeter API](https://github.com/tickoalcantara12/micro/blob/master/examples/greeter/api/api.go)
   187  
   188  ###### 3. proxy
   189  
   190  The final method of request handling for the API is a reverse proxy. Just as above, the API uses the request 
   191  path and a namespace component to determine the service to route requests to. By providing reverse proxying 
   192  and microservice request routing we're able to support REST, a widely sought after requirement. 
   193  
   194  The proxy can be enabling by passing the `--api_handler=proxy` flag. 
   195  
   196  An example of how to build a RESTful API can be found here [micro/examples/greeter/api](https://github.com/micro/examples/tree/master/greeter/api/rest).
   197  
   198  <a href="https://github.com/tickoalcantara12/micro/tree/master/api"><i class="fab fa-github fa-2x"></i> api</a>
   199  
   200  ###### Web UI
   201  
   202  The web UI provides a simple dashboard for observing and interacting with a running system. Not only that but it also 
   203  provides a reverse proxy much like the API. Our goal with a "web proxy" is to enable the development of 
   204  web apps as microservices. Again, just like the API, the request path is used along with a namespace to determine 
   205  the service to route requests to. The web proxy also supports web sockets as we see realtime being a core part 
   206  of delivering web apps.
   207  
   208  <p/>
   209  <a href="{{ site.baseurl }}/blog/images/web.png">
   210    <img src="{{ site.baseurl }}/blog/images/web.png" style="width: 100%; height: auto; margin: 0;" />
   211  </a>
   212  
   213  <a href="https://github.com/tickoalcantara12/micro/tree/master/web"><i class="fab fa-github fa-2x"></i> web</a>
   214  
   215  ###### CLI
   216  
   217  The CLI is a command line tool which provides a way to observe, interact and manage services in a running environment. 
   218  The current feature set allows you to inspect the registry, check basic health of services and execute queries against 
   219  services themselves.
   220  
   221  <p/>
   222  <a href="{{ site.baseurl }}/blog/images/cli.png">
   223    <img src="{{ site.baseurl }}/blog/images/cli.png" style="width: 100%; height: auto; margin: 0;" />
   224  </a>
   225  
   226  The other nifty feature, is the ability to use the Sidecar as a proxy for the CLI. It's as simple as specifying 
   227  the address for the sidecar as a flag `--proxy_address=example.proxy.com` when executing the CLI.
   228  
   229  <a href="https://github.com/tickoalcantara12/micro/tree/master/cli"><i class="fab fa-github fa-2x"></i> cli</a>
   230  
   231  ##### Putting it all together
   232  
   233  We've written an example of full end-to-end flow through the system using a simple greeter service. 
   234  
   235  The flow is as follows:
   236  
   237  1. HTTP GET request is made to the micro API at path /greeter/say/hello with the query name=John. 
   238  2. The API translates this using the default namespace to the api service go.micro.api.greeter and method Say.Hello. 
   239  The request is structured as an api.Request.
   240  3. The API using Go Micro, queries the registry to find all the nodes for the service go.micro.api.greeter and 
   241  forwards the request to one of the nodes.
   242  4. The greeter api parses the request, generates a hello.Request and makes a request to the rpc service go.micro.srv.greeter. 
   243  Again the same registry/discovery mechanism is used to find the nodes for the service.
   244  5. The greeter rpc service responds with a hello.Response.
   245  6. The greeter api translates the response into a api.Response and passes it back to the API.
   246  7. The micro API parses the response and responds to the client's HTTP request.
   247  
   248  
   249  <p/>
   250  <img src="{{ site.baseurl }}/blog/images/greeter.png" style="width: 100%; margin: 0; height: auto;" />
   251  
   252  In a more complex example, an API service may call out to many other RPC services, aggregate and transform 
   253  the data and then pass back a final summarised result to the client. This allows you to maintain a 
   254  consistent external entry point and change services in the background without the knowledge of the client.
   255  
   256  <a href="https://github.com/micro/examples/tree/master/greeter"><i class="fab fa-github fa-2x"></i> greeter service</a>
   257  
   258  ##### Demo
   259  
   260  If you want to kick the tyres on a running system, checkout our demo at [web.micro.pm](http://web.micro.pm).
   261  
   262  We're running Micro On Kubernetes using Google Container Engine. The demo is open source if you want to run it yourself. 
   263  You can find the k8s config here [github.com/micro/kubernetes](https://github.com/micro/kubernetes).
   264  
   265  ##### Summary 
   266  
   267  Micro provides the fundamental building blocks for writing and managing microservices. Go Micro includes 
   268  the core requirements; discovery, client/server and pub/sub. The CLI let's you interact with your 
   269  environment and services. The sidecar enables integration of any non Micro application. The API is a 
   270  single entry point for rpc requests and enables the creation of REST endpoints. With pluggable interfaces 
   271  you can pick and choose the systems you want to leverage in building your architecture.
   272  
   273  Our goal at Micro is to enable development at scale, increase speed of execution and provide value starting from 
   274  the very beginning of the developer lifecycle. We feel Micro is the best way to do all those things.
   275  Over time the ecosystem of tools will grow to include more feature rich services for discovery, routing 
   276  and obversability. 
   277  
   278  If you want to learn more about the services we offer or microservices, checkout the website [micro.mu](https://m3o.com) or 
   279  the github [repo](https://github.com/tickoalcantara12/micro).
   280  
   281  Follow us on Twitter at [@MicroHQ](https://twitter.com/m3ocloud) or join the [Slack](https://slack.m3o.com) 
   282  community [here](http://slack.m3o.com).
   283  
   284  <h6><a href="https://github.com/tickoalcantara12/micro"><i class="fab fa-github fa-2x"></i> Micro</a></h6>