Post on 16-Apr-2017
Criando Micro-serviços Reativos com JavaRodrigo Cândido da Silva
@rcandidosilva
About Me• Software Architect
• http://integritastech.com • JUG Leader do GUJavaSC
• http://gujavasc.org • Twitter
• @rcandidosilva • Contatos
• http://rodrigocandido.me
Agenda• Monolito vs. Micro-serviços • Manifesto Reativo • Resiliência • Message-driven • Demo • Perguntas
Monolito vs. Micro-serviços
Micro-serviços
• Pequenos • Deployment interdependente • Independente de tecnologia • Infra-estrutura separada
"Small independent component with well-defined boundaries that’s doing one thing, but
doing it well"
Manifesto Reativo
Resiliência• Como suportar isso com micro-serviços?
• Central point of configuration • Service registry and discovery • Routing features • Load balancing • Failover • Monitoring
Spring BootSpring Cloud
Spring Cloud + Netflix OSS
Spring Cloud + Netflix OSS“Casamento perfeito para criação de micro-
serviços resilientes“
Gerenciamento de Configuração Spring Cloud Config + Bus
Descoberta de Serviços Netflix Eureka
Balanceamento de Carga Netflix Ribbon
Circuit Breaker Netflix Hystrix + Turbine
Proxy Server Netflix Zuul
Segurança Spring Cloud Security
Spring Cloud Config
Netflix Eureka
Netflix Ribbon
Netflix Hystrix• Circuit Breaker Pattern
Hystrix Dashboard
Netflix Zuul
Spring Cloud Security
Discovery
Client Relying Party
Resource Server
Get an access token
& an ID Token (JWT)
Use an access token
Authorization Server
Iden.tyProviderorIDPor
OpenIDProviderorOP
Authorization Endpoint
Token Endpoint
Important Stuff
Userinfo Endpoint
Registration Endpoint
JWKS Endpoint
JWKS Endpoint
Validate (JWT)
ID Token
/.well-known /webfinger /openid-configura.on
Check Session IFrame
End Session Endpoint
Message-Driven • Como suportar isso com micro-serviços?
• Comunicação assíncrona • Non blocking I/O • Distribuição • Consistência • Event sourcing • CQRS
Reactive Programming• Asynchronous communication and data streams
• reactive-streams.org
Alternativas Reativas com Java EE
JMS EJB 3
Message-Driven Beans
Asynchronous Session Beans
CDIEvents
Observers
ServletAsynchronous
NIO
JAX-RSAsync on Server
Async on Client
WebSocket
Async Remote Endpoints Concurrency
Utilities
Project Reactor• Biblioteca para implementação de non-blocking apps • Interage com Java 8 functional API • Oferece duas composable API reativas
• Flux[N] and Mono[0|1] • Suporta escalabilidade in-memory roteando com Bus extensions
• Suporte portável para micro-serviços
REST Endpoint@RestControllerpublic class UserRestController {
private static final List<User> users = new ArrayList<>();
static { users.add(new User(1, "Rodrigo", "C", "da Silva")); users.add(new User(2, "Israel", "B", "Rodriguez")); users.add(new User(3, "Bruno", "", "Souza")); users.add(new User(4, "Edson", "", "Yanaga")); }
@RequestMapping(method = RequestMethod.GET, value = "/users") public List<User> getUsers() { return users; }
@RequestMapping(method = RequestMethod.GET, value = "/user/{id}") public User getUser(@PathVariable("id") Integer id) { return users.stream().filter(g -> g.getId() == id) .collect(Collectors.toList()).get(0); }
}
REST Proxy@Componentpublic class UserServiceProxy {
@Autowired UserService service;
@HystrixCommand(fallbackMethod = "defaultUsersObservable") public Observable<List<User>> getUsersObservable() { return new ObservableResult<List<User>>() { @Override public List<User> invoke() { return service.getUsers(); } }; }
public Observable<User> defaultUsersObservable() { return null; }} @FeignClient("USER-SERVICE")
public interface UserService {
@RequestMapping(value = "/users", method = RequestMethod.GET) List<User> getUsers();
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET) User getUser(@PathVariable("id") Integer id);}
REST Async Client
@RestControllerpublic class APIController {
@Autowired GroupServiceProxy groupService;
@Autowired UserServiceProxy userService;
@RequestMapping(method = RequestMethod.GET, value = "/userGroups") public UserGroup getUserGroups() { Observable<List<Group>> groups = groupService.getGroupsObservable(); Observable<List<User>> users = userService.getUsersObservable();
Observable<UserGroup> userGroupObservable = Observable.zip(groups, users, (g, u) -> new UserGroup(u, g));
return userGroupObservable.toList().toBlocking().single().get(0); }
}
Demo• Reactive Microservices • https://github.com/rcandidosilva/reactive-microservices
Outras Alternativas
Perguntas
?
Referências• http://projects.spring.io/spring-boot/ • http://projects.spring.io/spring-cloud/ • https://netflix.github.io/ • http://www.reactive-streams.org/ • http://www.reactivemanifesto.org/ • https://github.com/reactivemanifesto/website-manifesto/tree/master/public/pdf • https://projectreactor.io/ • http://reactivex.io/ • http://www.kennybastani.com/2016/04/event-sourcing-microservices-spring-
cloud.html