Microservices and modularity with java

Post on 08-Jan-2017

428 views 2 download

transcript

Microservices and modularity with Java

Elek Márton@anzix

2015 DecemberDPC Consulting

Agenda● Microservice definition● Core implementation patterns

People need modularity not microservices

Microservices is one way to achieve modularity.

Other ways: OSGi, SPI, java9 jigsaw

What are microservices?The fire making rule:● Use more sticks than you think they would be

enough● Use a little bit smaller sticks than you think would

be enough

Informal definitionMicroservices when you have smaller, separated services as usual● Many times they are not micro (group of services)● Many times REST is used (but not always)● Separated means different process/JVM instances

More formal definition● “an approach to developing a single application as

a suite of small services, ● each running in its own process ● and communicating with lightweight mechanisms,

often an HTTP resource API.

These services are built around business capabilities and independently deployable by fully automated deployment machinery.”

Lewis, Fowler

Microservices and RESTREST is just one solution to communicate● between services● or call any of the service

Microservices vs SOAEven if the “Microservices != SOA” a very common statement there are a lot of shared concept.● Usually we have new tools (using existing solutions

differently) not totally new concept

Implementation patterns

Monolith application

Spring boot example@EnableAutoConfiguration

@RestController

@ComponentScan

public class TimeStarter {

@Autowired

TimeService timerService;

@RequestMapping("/now")

public Date now() {

return timerService.now();

}

public static void main(String[] args) {

SpringApplication.run(TimeStarter.class, args);

}

}

Modular monolith

API gateway

API gatewayGoals:● Hide available microservices behind a service

facade pattern– Routing, Authorization– Deployment handling, Canary testing– Logging, SLA

Implementations:● Spring cloud: Netflix Zuul● Netflix Zuul based implementation● Twitter Finagle based implementation● Amazon API gateway● Simple Nginx reverse proxy configuration

Spring Cloud + Zuul example

@EnableAutoConfiguration

@ComponentScan

@EnableEurekaClient

@EnableZuulProxy

public class ApiGatewayStarter {

public static void main(String[] args) {

SpringApplication.run(ApiGatewayStarter.class, args);

}

}

Service registry

Service registryGoal:● Store the location and state of the available

services– Health check– DNS interface

Implementations:● Spring cloud: Eureka● Netflix eureka based implementation● Consul.io● etcd, zookeeper● Simple: DNS or hosts file

Eureka

Eureka server@SpringBootApplication

@EnableEurekaServer

public class EurekaStarter {

public static void main(String[] args) {

new SpringApplicationBuilder(EurekaStarter.class).web(true).run(args);

}

}

Eureka client@EnableAutoConfiguration

@RestController

@ComponentScan

@EnableEurekaClient

public class TimeStarter {

@Autowired

TimeService tttService;

@RequestMapping("/now")

public Date now() {return tttService.now();}

public static void main(String[] args) {

SpringApplication.run(TimeStarter.class, args);

}

}

SIMPLE Discovery client

@Service

public class Client {

@Autowired

private DiscoveryClient discoveryClient;

public void list() {

for (ServiceInstance instance : discoveryClient.getInstances("BOOTSTRAP")) {

System.out.println(instance.getHost());

System.out.println(instance.getPort());

System.out.println(instance.getUri());

}

}

}

Config service

Monolith configuration● Store configuration outside of the project

– Versioning? Who did what● Store configuration in separated versioned project

– Which binary version need which config version?● Store configuration with the source code

– Sensitive configuration?

Config ServiceGoals:● One common place for all of the configuration

– Versioning– Auditing– Multiple environment support

Solutions:● Spring Cloud config service● Zookeeper● Most of the service registry have key->value store● Any persistence datastore

Config server@SpringBootApplication

@EnableConfigServer

@EnableEurekaClient

public class ConfigServer {

public static void main(String[] args) {

new SpringApplicationBuilder(ConfigServer.class).run(args);

}

}

Config repositoryelek@sc /tmp/config-repo [master] > ls -lah

drwxr-xr-x 3 elek elek 100 Dec 14 14:32 .

drwxrwxrwt 57 root root 1.8K Dec 14 14:37 ..

-rw-r--r-- 1 elek elek 55 Dec 14 14:16 application.properties

drwxr-xr-x 8 elek elek 260 Dec 14 14:40 .git

-rw-r--r-- 1 elek elek 12 Dec 14 14:32 timer.properties

Config client@Service

public class TimeService {

@Value("${version:Unknown}")

String version;

public Date now() {

return new Date();

}

public String version() {

return version;

}

}

Monitoring

MonitoringGoal:● Combined business log● Health check, exceptions, what is running?● Metrics

Solutions (for one of the goals):● Netflix Spectator, Servo, Atlas● Spring cloud: Hystrix (circuit breaker)● ELK: ElasticSearch + Logstash + Kibana● Cloud based solutions (logentries...)● Metrics: Dropwizard based implementations

Kibana (from ELK)

Hystrix

Hystrix@Service

public class TimeService {

@HystrixCommand(fallbackMethod = "safeNow")

public Date now() {

return new Date();

}

public Date safeNow() {

return new Date(0);

}

}

Inter-module calls

Inter-module calls

Inter-module callsGoal:● RPC calls between modules ● Protocol● Usage of the Service Registry

Solutions:● Spring cloud/Netflix: ribbon● Simple REST calls● Message Oriented solutions:

– Rabbit MQ, ZMQ, ...

Distributed tracing

Distributed tracing (simlified)

Distributed tracingGoal:● Global information about latency and errors

– Service enter/exit point measurement– Transaction identification

Implementations:● Twitter’s Zipkin● Apache (Cloudera) HTrace● Spring cloud Sleuth

Zipkin

source: https://bitbucket.org/aktivecortex/aktivecortex/wiki/tracing.md

Summary

Advantages of monolithModular monolith Microservices

Advantages of microservices● Modular, loosely coupled● Easily scalable to multiple machines● Separated, modular development lifecycle● Partial deploy without restart [like OSGi]

● Supports heterogeneous architecture (tech stack migration)

Summary● Focus on modularity● Be familiar with the microservice related tools: they

could be useful even with monolith applications…

Q&A

Márton ElekDPC consulting http://dpc.hutwitter: @anzixhttp://blog.dpc.hu, http://blog.anzix.net