some thoughts on Microservice

Be in the Horzion project for months now, learn lots of new things and it might be a good time to document some learnings.

To switch from monolithic service to micro service means a total mightset change. The frist thing is to become a DevOps. You need to write/debug/test your code, build/deploy the code locally and remotely and monitor the service. This is very chanllenging in mature company because thses functions are isolated into different teams/organizations.

Here are some components in building microservice:

  • service register/discover
  • config management
  • (distributed) cache
  • log/alert/monitor
  • (distrobued) database
  • authentication for internal and external users

something need special handling:

  • how to handle exception: you can throws an exception in one service and hope another service catch it in old fasion.
  • how to pass data between services: if several services need to process the same request at different stage. if you need to merge the reply from couple services? or some service need the same intermedian data?
  • how to resolve service dependency: the business logic can be sync or async.
  • how to handle failover: if a service not available, will it retry and how?
  • how to document your service: this will impact how user can easily use the APIs

More to add later…

Hypermedia API

What is the term? “The simultaneous presentation of information and controls such that the information becomes the affordance through which the user obtains choices and selects actions.” – Roy Fielding

To understand this, we need first to understand what is the benefit: decouple client and server of RESTful API. In current API development, the client follows the API specification and any change at server side will break the client code.

How to solve this? Image if the client follow the action told by the server and the code is adaptive enough to the actions. Then the server can evolve independently.

HATEOAS, an abbreviation for Hypermedia as the Engine of Application State, is a constraint on the REST application architecture.

Check out some examples:

Ref:

Maven common command

create a new project

  • mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false (The groupId is the name space, for example com.autofei. The artifactId is the project name)
  • mvn eclipse:eclipse (convert maven project into eclipse project)
  • mvn package (create jar file)
  • java -cp target/{jar-name} {main-class-name}

create a new web project

  • mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
  • mvn eclipse:eclipse -Dwtpversion=2.0 (convert maven project into eclipse web project)
  • mvn package
  • deploy war file into container

list all template: mvn archetype:generate

run compile: mvn compile

run liquibase: mvn -P update-db process-resources

run install with multi threads: mvn clean -Dmaven.test.skip=true install -T 16

run gwt: mvn clean -Pgwt-dev-auto -Dmaven.test.skip=true install -T 16

reference: http://www.mkyong.com/tutorials/maven-tutorials/

Perl execute shell command line – Quick Tutorial

There are several ways to do this, please check the following code to see what is the different:

#!/usr/bin/perl

use strict;

print "*** backtick ***\n";
my $result = `ls`;
print "$result\n";

print "*** system ***\n";
my $result2 = system ("ls");
print "system result: $result2\n";

print "\n*** readpipe ***\n";
my $result3 = readpipe ("ls");
print "$result3\n";

print "*** exec ***\n";
exec ("ls");

In a nutshell:

  • exec: does not return anything, it simply executes the command
  • system: creates a fork process and waits to see if the command succeeds or fails – returning a value
  • backtick and readpipe: used to capture the output of a system call