Microservice-oriented architecture using python

 a friendly co-living among the python frameworks

Thu Oct 15 2015

Jhon Roa

Full Stack Developer and Tech Lead @ Associate.io

Microservices

Microservices

divide and conquer 

Calculator

UI (web)

API

/add

/subtract

/multiply

/divide

APIs !

UI (web)

APIs

/add

/subtract

/multiply

/divide

(flask)

(django)

http

Let's see some code!

flask

@app.route("/add")
def add():
    try:
        first_number = int(request.args.get('a'))
        second_number = int(request.args.get('b'))
        return jsonify({'result': first_number + second_number})
    except Exception as e:
        return jsonify({'result': "There was an error" + str(e)})



@app.route("/subtract")
def subtract():
    try:
        first_number = int(request.args.get('a'))
        second_number = int(request.args.get('b'))
        return jsonify({'result': first_number - second_number})
    except Exception as e:
        return jsonify({'result': "There was an error" + str(e)})

django

class Multiply(APIView):

    renderer_classes = (JSONPRenderer,)

    @staticmethod
    def get(request):
        try:
            first_number = int(request.GET.get('a'))
            second_number = int(request.GET.get('b'))
            return Response({'result': first_number * second_number})
        except Exception as e:
            return Response({'result': 'there was an error ' + str(e)})



class Divide(APIView):

    renderer_classes = (JSONPRenderer,)

    @staticmethod
    def get(request):
        try:
            first_number = int(request.GET.get('a'))
            second_number = int(request.GET.get('b'))
            return Response({'result': first_number / second_number})
        except Exception as e:
            return Response({'result': 'there was an error ' + str(e)})

Web client

function callAddService(firstAddNumberValue, secondAddNumberValue){
    var url = "https://flask-calculator-microservice.herokuapp.com/add?a=
              "+firstAddNumberValue+"&b="+secondAddNumberValue;
	basicAjaxCall(url,callAddServiceSuccess);
}


function callMultiplyService(firstAddNumberValue, secondAddNumberValue){
    var url = "https://django-calculator-microservice.herokuapp.com/multiply?a=
              "+firstAddNumberValue+"&b="+secondAddNumberValue;
    basicAjaxCall(url,callMultiplyServiceSuccess);
}


function basicAjaxCall(url, successCallback){
    $.ajax({
      url: url,
      dataType: "jsonp",
      success : successCallback
    });
}

Is NOT about integration of frameworks

Is about DIVIDING in groups of  features

Taking advantage of needed frameworks

____________________________

/add

/subtract

/multiply

/divide

(flask)

(django)

/derivative

/integrate

(tornado)

(pyramid)

/series

/sequences

not only for python frameworks

but for many frameworks in different languages

Ruby

Ruby on Rails

Sinatra

Javascript

NodeJs + ExpressJs

PHP

Laravel

Codeigniter

Java

Spring framework

Cake php

____________________________

/add

/subtract

/multiply

/divide

(flask)

/derivative

/integrate

(NodeJs + express JS)

(laravel)

/series

/sequences

(sinatra)

Q & A

References

Source code

Social

Thanks!

Made with Slides.com