48
Microservices and modularity with Java Elek Márton @anzix 2015 December DPC Consulting

Microservices and modularity with java

Embed Size (px)

Citation preview

Page 1: Microservices and modularity with java

Microservices and modularity with Java

Elek Márton@anzix

2015 DecemberDPC Consulting

Page 2: Microservices and modularity with java

Agenda● Microservice definition● Core implementation patterns

Page 3: Microservices and modularity with java

People need modularity not microservices

Page 4: Microservices and modularity with java

Microservices is one way to achieve modularity.

Other ways: OSGi, SPI, java9 jigsaw

Page 5: Microservices and modularity with java

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

Page 6: Microservices and modularity with java

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

Page 7: Microservices and modularity with java

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

Page 8: Microservices and modularity with java

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

Page 9: Microservices and modularity with java

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

Page 10: Microservices and modularity with java

Implementation patterns

Page 11: Microservices and modularity with java

Monolith application

Page 12: Microservices and modularity with java

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);

}

}

Page 13: Microservices and modularity with java

Modular monolith

Page 14: Microservices and modularity with java
Page 15: Microservices and modularity with java

API gateway

Page 16: Microservices and modularity with java

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

Page 17: Microservices and modularity with java

Spring Cloud + Zuul example

@EnableAutoConfiguration

@ComponentScan

@EnableEurekaClient

@EnableZuulProxy

public class ApiGatewayStarter {

public static void main(String[] args) {

SpringApplication.run(ApiGatewayStarter.class, args);

}

}

Page 18: Microservices and modularity with java
Page 19: Microservices and modularity with java

Service registry

Page 20: Microservices and modularity with java

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

Page 21: Microservices and modularity with java

Eureka

Page 22: Microservices and modularity with java

Eureka server@SpringBootApplication

@EnableEurekaServer

public class EurekaStarter {

public static void main(String[] args) {

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

}

}

Page 23: Microservices and modularity with java

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);

}

}

Page 24: Microservices and modularity with java

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());

}

}

}

Page 25: Microservices and modularity with java

Config service

Page 26: Microservices and modularity with java

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?

Page 27: Microservices and modularity with java

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

Page 28: Microservices and modularity with java

Config server@SpringBootApplication

@EnableConfigServer

@EnableEurekaClient

public class ConfigServer {

public static void main(String[] args) {

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

}

}

Page 29: Microservices and modularity with java

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

Page 30: Microservices and modularity with java

Config client@Service

public class TimeService {

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

String version;

public Date now() {

return new Date();

}

public String version() {

return version;

}

}

Page 31: Microservices and modularity with java

Monitoring

Page 32: Microservices and modularity with java

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

Page 33: Microservices and modularity with java

Kibana (from ELK)

Page 34: Microservices and modularity with java

Hystrix

Page 35: Microservices and modularity with java

Hystrix@Service

public class TimeService {

@HystrixCommand(fallbackMethod = "safeNow")

public Date now() {

return new Date();

}

public Date safeNow() {

return new Date(0);

}

}

Page 36: Microservices and modularity with java

Inter-module calls

Page 37: Microservices and modularity with java

Inter-module calls

Page 38: Microservices and modularity with java

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, ...

Page 39: Microservices and modularity with java

Distributed tracing

Page 40: Microservices and modularity with java

Distributed tracing (simlified)

Page 41: Microservices and modularity with java

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

Page 42: Microservices and modularity with java

Zipkin

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

Page 43: Microservices and modularity with java

Summary

Page 44: Microservices and modularity with java

Advantages of monolithModular monolith Microservices

Page 45: Microservices and modularity with java

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)

Page 46: Microservices and modularity with java

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

could be useful even with monolith applications…

Page 48: Microservices and modularity with java

Q&A

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