Reactive services were defined by the Reactive Manifesto. They are built to be more flexible, loosely-coupled and scalable and also they are qualified based on the four principles: responsive, resilient, elastic and message driven. Java and the Java EE platform offers a pretty good structure and libraries to implement reactive services and transform it in a microservices architecture designed.
4. Microservices
• Small components
• Isolated deployment
• Independent technology
• Separate infrastructure
"Small independent component with well-
defined boundaries that’s doing one thing, but
doing it well"
6. Resilient
• How to support it on microservices?
• Central point of configuration
• Service registry and discovery
• Routing features
• Load balancing
• Failover
• Monitoring
8. 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
15. Spring Cloud Security
Discovery
Client
Relying Party
Resource
Server
Get an access token
& an ID Token (JWT)
Use an access token
Authorization
Server
Iden.ty Provider or
IDP or
OpenID Provider or
OP
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
16. Message-Driven
• How to support it on microservices?
• Asynchronous communication
• Non blocking I/O
• Distributed
• Consistency
• Event sourcing
• CQRS
18. Reactive Alternatives at Java EE
JMS EJB 3
Message-Driven
Beans
Asynchronous
Session Beans
CDI
Events
Observers
Servlet
Asynchronous
NIO
JAX-RS
Async on Server
Async on Client
WebSocket
Async Remote
Endpoints
Concurrency
Utilities
19. 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
20. 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);
}
}
21. 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);
}
22. 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);
}
}