github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/docs/blog/_posts/2017-06-06-functions.md (about)

     1  ---
     2  layout: post
     3  title: Functions with Micro
     4  date:   2017-06-06 09:00:00
     5  ---
     6  <br>
     7  As technology evolves so do our programming models. We've gone from monoliths to microservices 
     8  and more recently started to push this separation even further towards functions.
     9  
    10  Micro looks to simplify distributed systems development, with [go-micro](https://github.com/micro/go-micro) 
    11  providing a pluggable framework for microservices. Go-micro has historically included a high level [Service](https://godoc.org/github.com/micro/go-micro#Service) 
    12  interface, encapsulating the lower level requirements for microservices. 
    13  
    14  Today we're introducing the [Function](https://godoc.org/github.com/micro/go-micro#Function) 
    15  interface, a one time executing Service.
    16  
    17  <script src="https://gist.github.com/asim/bfbaf036c90761879dbf6e939e5172e4.js"></script>
    18  
    19  ### The Inspiration
    20  
    21  Ben Firshman open sourced a project last year called [Funker](https://github.com/bfirsh/funker), functions as docker containers. The concept is very 
    22  simple but also extremely clever. 
    23  
    24  Functions could quite simply be programs with one method, listening on the network for a request and exiting after 
    25  executing once, leveraging docker swarm services for lifecycle management, discovery, etc.
    26  
    27  This sparked the inspiration for including functions as part of go-micro.
    28  
    29  ### Why Functions?
    30  
    31  The function programming model is the evolution of microservices. As our scale requirements increase both technically and organisationally 
    32  there's a need to decouple systems and teams so they can operate independently.
    33  
    34  In the past 5 years we've seen the emergence of microservices as a way of dealing with those scaling requirements. The microservices 
    35  architecture pattern is of course nothing new but we've now started to define best practices which help us build better software. 
    36  
    37  Functions push us into a new realm of possibility in terms of simplifying distributed systems development and solving software problems. 
    38  Going back to the unix philosophy, "do one thing and do it well", functions truly embody that philosophy even more so than microservices.
    39  
    40  While infrastructure helps us build scalable systems, remember that microservices and functions are software architecture patterns 
    41  and programming models, so with that we need tools which help us to write software using those patterns.
    42  
    43  ### Example Function
    44  
    45  Here's a straight forward example of writing a function with go-micro. 
    46  
    47  As you can tell it looks almost identical to a service definition. That's because underneath the covers they are exactly the 
    48  same except for one small detail, functions exit after one execution of a handler or subscriber.
    49  
    50  Functions give you the same functionality as services, letting you leverage all the existing micro ecosystem tooling.
    51  
    52  <script src="https://gist.github.com/asim/7d70cf1160ad1279597f12985fe3fbd5.js"></script>
    53  
    54  ### Running Functions
    55  
    56  As previously stated, functions in micro are one time executing services, the function will exit after completing a request. This then 
    57  poses the question, how do we keep functions running?
    58  
    59  There is an abundance of existing tooling out there for process lifecycle management, so feel free to use any of your favourite 
    60  process managers.
    61  
    62  However the micro toolkit now includes a convenience tool called [**micro run**](https://m3o.com/docs/run.html).
    63  
    64  Here's how to run a function:
    65  
    66  ```
    67  micro run -r github.com/micro/examples/function
    68  ```
    69  
    70  The **micro run** command fetches, builds and executes from source. The `-r` flag tells it restart the function on exit. 
    71  It's currently a simple and experimental tool for running micro based microservices and functions. From source to running in one command.
    72  
    73  There will be a separate post for the run command once it's more stable.
    74  
    75  ### Summary
    76  
    77  Functions are a natural extension of microservices as the next programming model to help simplify distributed systems development. 
    78  Micro treats functions as a first class citizen.
    79  
    80  While functions have been added to go-micro, it does not mean 100% of your software needs to be written with them. It's important 
    81  to understand when monoliths, microservices or functions are appropriate.
    82  
    83  Look to see more on integrating micro functions with existing systems and serverless tooling in the near future.
    84  
    85  <center><p>...</p></center>
    86  If you want to learn more about the services we offer or microservices, checkout the [website](https://m3o.com) or 
    87  visit [GitHub](https://github.com/tickoalcantara12/micro).
    88  
    89  Follow us on [Twitter](https://twitter.com/m3ocloud) or join the [Slack](http://slack.m3o.com) community.
    90  
    91