concept function in category microservice

appears as: function, function, functions, The function, functions
Bootstrapping Microservices with Docker, Kubernetes and Terraform

This is an excerpt from Manning's book Bootstrapping Microservices with Docker, Kubernetes and Terraform.

Listing 8.5 - Adding the next test (additions to chapter-8/example-1/src/math.test.js)
const { square } = require("./math");
 
describe("square function", () => {
 
    test("can square two", () => {
        #A
    });
 
    test("can square zero", () => { #B
 
        const result = square(0);
        expect(result).toBe(0);
    }); #B
 
    #C
   
});
 
#D
 

Now to isolate our code in the square function (which arguably isn’t much) we need to mock the multiply function. That means we must replace it with another function, one that we can control. We can do this using a primitive form of dependency injection (DI). DI is a technique where we inject dependencies into our code. We control what the dependencies are and that’s really useful for isolating code for unit testing. In this case we are injecting the multiply function:

Microservices in Action

This is an excerpt from Manning's book Microservices in Action.

It’s not a given that each function maps to a single microservice. You need to determine which functions are cohesive — they belong together. For example, transactions resulting from orders will be similar to transactions resulting from other events, such as dividends being paid on a share. Together, a group of functions forms a capability that one service may offer.

Listing 7.2 microservices-in-action/chapter-7/chassis/events_demo.py
from nameko.events import EventDispatcher, event_handler
from nameko.rpc import rpc
from nameko.timer import timer

class EventPublisherService:
 name = "publisher_service"    #1  
 dispatch = EventDispatcher()  #2  

    @rpc
    def publish(self, event_type, payload):
        self.dispatch(event_type, payload)

class AnEventListenerService:
 name = "an_event_listener_service"    #1  
 @event_handler("publisher_service", "an_event")    #3  
    def consume_an_event(self, payload):
        print("service {} received:".format(self.name), payload)

class AnotherEventListenerService:
    name = "another_event_listener_service"

    @event_handler("publisher_service", "another_event")
    def consume_another_event(self, payload):
        print("service {} received:".format(self.name), payload)

class ListenBothEventsService:
 name = "listen_both_events_service"    #1  
 @event_handler("publisher_service", "an_event")    #3  
    def consume_an_event(self, payload):
        print("service {} received:".format(self.name), payload)
 @event_handler("publisher_service", "another_event")    #3  
    def consume_another_event(self, payload):
        print("service {} received:".format(self.name), payload)

#1   Registers the service name, which allows you to refer to it on other services
#2   Allows this service to create events that’ll be routed to a queue in RabbitMQ
#3   By using this annotation, ListenBothEventsService will execute the function when the publisher service issues an event. The first argument of the annotation is the name of the service whose events will be listened to, and the second argument of the annotation is the name of the event.

In this section, we’ll explore two approaches for structuring teams — by function or across function — and their benefits and disadvantages in developing microservices.

Figure 13.4 Grouping into teams by function and project

c13_04.png