34
Efficiently building and deploying MICROSERVIC ES

JavaOne: Efficiently building and deploying microservices

Embed Size (px)

DESCRIPTION

Since Martin Fowler’s article on microservices in the beginning of 2014, there has been a lot of controversy about the topic. Although most articles talk about microservices from an architectural perspective, this session intends to go further and also provide examples of and best practices for building and deploying polyglot applications in an enterprise Java environment. In the session, the build process focuses on efficiency and shows that microservices don’t necessarily cause overhead for a project. Microservices don't imply copying and pasting the same boilerplate code over and over. The deployment process in the presentation is, of course, automated but also demonstrates best practices for integration testing between different active services.

Citation preview

Page 1: JavaOne: Efficiently building and deploying microservices

Efficiently

building

and deployingMICROSERVICES

Page 2: JavaOne: Efficiently building and deploying microservices

Bart Blommaerts

Me• [email protected]• @DaggieBe

HP Enterprise Services• EMEA Java SME• Technical Lead at Flemish Government

Web• https://github.com/bart-blommaerts/• http://www.daggie.be

Page 3: JavaOne: Efficiently building and deploying microservices

MICROSERVICES

Page 4: JavaOne: Efficiently building and deploying microservices

Why?

Monoliths …• Don’t scale easily• Are difficult

– To understand– To maintain – To test– To deploy

• Make it difficult to adopt new technology

A monolithic application is like a house of cards ...

Page 5: JavaOne: Efficiently building and deploying microservices

Small

Do 1 thing well• 1 responsibility• About use case / business capability

Not about LOC

Stateless

Page 6: JavaOne: Efficiently building and deploying microservices

Small

Decomposition• A monolitic application has disadvantages• Decompose the monolith into microservices

Page 7: JavaOne: Efficiently building and deploying microservices

Smart endpoints and dumb pipes

Loosely coupled• Make the service smart. Keep the communication dumb.

• Favor coarse-grained over fine-grained communication.

Page 8: JavaOne: Efficiently building and deploying microservices

Smart endpoints and dumb pipes

Synchronous• HTTP request / response

Page 9: JavaOne: Efficiently building and deploying microservices

Smart endpoints and dumb pipes

Synchronous• HTTP request / response

Page 10: JavaOne: Efficiently building and deploying microservices

Smart endpoints and dumb pipes

Asynchronous• Lightweight messaging

Page 11: JavaOne: Efficiently building and deploying microservices

Smart endpoints and dumb pipes

Asynchronous• Event-driven

Page 12: JavaOne: Efficiently building and deploying microservices

Decentralized governance

API interface• Language agnostic• Multiple versions allowed / encouraged• Publish anything of interest. Don’t wait to be asked.• Evolutionary

Disposable

Page 13: JavaOne: Efficiently building and deploying microservices

Decentralized data management

Polyglot persistence• Each service owns it’s data storage

Page 14: JavaOne: Efficiently building and deploying microservices

Decentralized data management

Eventual consistency• Transactions impose coupling• Synchronizing databases might be needed

Do 1 thing

Page 15: JavaOne: Efficiently building and deploying microservices

Design for failure

Self-monitoring• Application will use services as components: many moving parts• Any service can fail (or be unreachable):

– Detect quickly– Restore automatically (if possible)

Page 16: JavaOne: Efficiently building and deploying microservices

Automation

Infrastructure (deployment)• Large number of services will require automation

– Use existing solutions (eg. Jenkins, Bamboo, ..)• Automation as an enabler for microservices

Page 17: JavaOne: Efficiently building and deploying microservices

Recap

SmallSmart endpoints and dumb

pipesDecentralised governance

Decentralised data management

Design for failureAutomation

Page 18: JavaOne: Efficiently building and deploying microservices

building

and deploying

Page 19: JavaOne: Efficiently building and deploying microservices

Building: Synchronous

Person• Spring Boot• Tomcat

@ComponentScan@EnableAutoConfigurationpublic class PersonApplication {

public static void main(String[] args) {SpringApplication.run(PersonApplication.class,

args);}

}

Page 20: JavaOne: Efficiently building and deploying microservices

Building: Synchronous

Address• DropWizard• Jetty

@Overridepublic void run(AddressConfiguration configuration, Environment environment) {

final AddressResource resource = new AddressResource();final AddressHealthCheck addressHealthCheck = new

AddressHealthCheck();

environment.healthChecks().register("address", addressHealthCheck);

environment.jersey().register(resource);}

Page 21: JavaOne: Efficiently building and deploying microservices

Building: Synchronous

PersonAddress• DropWizard• Jetty• Calling Person and Address service, using Jersey.

@Overridepublic void run(PersAddrConfiguration configuration, Environment environment) {

final Client client = new JerseyClientBuilder(environment).using(

configuration.getJerseyClientConfiguration()).build(getName());

final PersonAddressResource resource = new PersonAddressResource(client);

Page 22: JavaOne: Efficiently building and deploying microservices

Building: Asynchronous

Person Publisher• Spring Boot• Tomcat• Rabbit MQ

@AutowiredRabbitTemplate rabbitTemplate;

rabbitTemplate.convertAndSend(QUEUE_NAME, repository.getAllPersons());

Page 23: JavaOne: Efficiently building and deploying microservices

Building: Asynchronous: Person Publisher@BeanQueue queue() {

return new Queue(QUEUE_NAME, false);}

@BeanTopicExchange exchange() {

return new TopicExchange(TOPIC_EXCHANGE);}

@BeanBinding binding(Queue queue, TopicExchange exchange) {

return BindingBuilder.bind(queue).to(exchange).with(QUEUE_NAME);}

Page 24: JavaOne: Efficiently building and deploying microservices

Building: Asynchronous

Person Listener• Spring Boot• Tomcat• Rabbit MQ

Application:@Beanprivate MessageListenerAdapter listenerAdapter(PersonListener listener) {

return new MessageListenerAdapter(listener, "receiveMessage");}…PersonListener: public void receiveMessage(Map<Integer, Person> message)

Page 25: JavaOne: Efficiently building and deploying microservices

Building: Sample

Address Publisher

Page 26: JavaOne: Efficiently building and deploying microservices

Deploying

Automation• Simple sample application: 5 servlet containers, 1 messaging queue• Configure a deployment pipeline• Use deployment automation from the start• Consider a PaaS

– Cloud Foundry, OpenShift, ...

Page 27: JavaOne: Efficiently building and deploying microservices

Efficiently

Page 28: JavaOne: Efficiently building and deploying microservices

Efficiently

Synchronous vs Asynchronous• Decide early• Rule of thumb

– Reading: synchronous– Updating: asynchronous

Page 29: JavaOne: Efficiently building and deploying microservices

Decentralised governance

Service templating• Microservices are language agnostic

– But don’t change technology because you can. Change because it makes sense.

• Start with a common technology stack

Modularity• Services can be modules of the system

– Own life cycle– Independently deployable– But .. “Options” in regard to re-use

Page 30: JavaOne: Efficiently building and deploying microservices

Deploying

DevOps• Own your service

– Eat your own dog food• Monitor the monitoring ..

– Use the monitoring to make the service self-operational• Log everything

Page 31: JavaOne: Efficiently building and deploying microservices

Efficiently

Tracking• Microservices will be using other microservices• Keep track of

– a correlation id between services– The ‘age’ of the data / response

Page 32: JavaOne: Efficiently building and deploying microservices

Efficiently

Tracking: CorrelationId

Publisher:protected Message createMessage(Object object, MessageProperties messageProperties) throws MessageConversionException {

messageProperties.setCorrelationId(correlationId.getBytes());return super.createMessage(object, messageProperties);

}

Listener: protected Object extractMessage(Message message) throws Exception {

byte[] correlationId = message.getMessageProperties().getCorrelationId();

Page 33: JavaOne: Efficiently building and deploying microservices

Efficiently

Free Lunch?• Complexity• DRY• Latency and marshalling• Versioning• API Interface• Architect security in from the beginning

http://highscalability.com/blog/2014/4/8/microservices-not-a-free-lunch.html

Page 34: JavaOne: Efficiently building and deploying microservices

Thank you