You are on page 1of 27

Reactive Microservices with Java and

Java EE
Rodrigo Cândido da Silva @rcandidosilva
Israel Boza Rodriguez @IsraKaos
Agenda
• Monolithic vs. Microservices
• Reactive Manifesto
• Resilience
• Message-driven
• Demo
• Q&A
Monolithic vs. Microservices
Microservices
"Small independent component with well-
defined boundaries that’s doing one thing, but
doing it well"

• Small components
• Isolated deployment
• Independent technology
• Separate infrastructure
Reactive Manifesto
Resilient
• How to support it on microservices?
• Central point of configuration
• Service registry and discovery
• Routing features
• Load balancing
• Failover
• Monitoring
Spring Cloud + Netflix OSS

Spring Cloud Spring Boot


Spring Cloud + Netflix OSS
“Nice match to build resilient microservices with
Java"

Configuration Management Spring Cloud Config + Bus

Service Registration and Discovery Netflix Eureka

Load Balacing Netflix Ribbon

Circuit Breaker Netflix Hystrix + Turbine

Proxy Server Netflix Zuul

Autenthication Spring Cloud Security


Spring Cloud Config
Netflix Eureka
Netflix Ribbon
Netflix Hystrix
• Circuit Breaker Pattern
Hystrix Dashboard
Netflix Zuul
Spring Cloud Security
Token
Endpoint
Authorization
Discovery Endpoint
Authorization
Server
s to ken JWKS

n a cces (JWT) Endpoint Iden.ty Provider or


Get a D Token
Validate IDP or
(JWT)
I Registration
ID Token & an Endpoint OpenID Provider or
/.well-known OP
/webfinger
/openid-configura.on
Client
Relying Party Check Session IFrame

Use an a End Session Endpoint


ccess to
JWKS
ken Userinfo
Endpoint Endpoint

Resource
Server
Important Stuff
Message-Driven
• How to support it on microservices?
• Asynchronous communication
• Non blocking I/O
• Distributed
• Consistency
• Event sourcing
• CQRS
Reactive Programming
• Asynchronous communication and data streams
• reactive-streams.org
Reactive Alternatives at Java EE
Message-Driven
Events
Beans
JMS EJB 3 CDI
Asynchronous
Observers
Session Beans

Asynchronous Async on Server

Servlet JAX-RS
NIO Async on Client

Async Remote
Endpoints
Concurrency
WebSocket Utilities
Project Reactor
• Library for building non-blocking apps
• Interacts with Java 8 functional API
• Offers two reactive composable API
• Flux[N] and Mono[0|1]
• Supports scalable in-memory routing with Bus
extensions
• Ported to support microservices
REST Endpoint
@RestController
public 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
@Component
public 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
@RestController
public 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
Other Alternatives
Q&A

?
References
• 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
Thank you!
Obrigado!

You might also like