2. Speaker
• Orkhan Gasimov, Software Engineer
– 14 years of software engineering;
– variety of technologies (languages & frameworks);
– solution design and implementation;
• Teaching training courses.
– Architecture.
– Java.
– JavaScript / TypeScript.
• Author of training courses.
– Spring Cloud.
– Akka for Java.
3. Spring Cloud
• Why?
– Provide the Spring developer with an
easily consumable set of tools to build
distributed systems.
Motivation
4. Spring Cloud
• Why?
– Provide the Spring developer with an
easily consumable set of tools to build
distributed systems.
• How?
– Wrapping other implementation stacks,
that are then consumed via the familiar
tools.
Motivation
Process
5. Spring Cloud
• Why?
– Provide the Spring developer with an
easily consumable set of tools to build
distributed systems.
• How?
– Wrapping other implementation stacks,
that are then consumed via the familiar
tools.
• What?
– Set of tools for developers to quickly
build some of the common patterns in
distributed systems.
Process
Motivation
Product
7. Spring Cloud
• Spring Cloud Netflix
• Spring Cloud Sleuth
• Spring Cloud Security
• Spring Cloud Streams
• Spring Cloud Bus
• Spring Cloud Config
8. Examples
• A few notes about code examples:
– We will see
• Spring Cloud project names.
• Simplified Java code examples.
• Basic configuration options.
– We omit
• Dependencies – easy to find at official website.
• Extensive configuration options – available in official documentation.
10. Microservices
• Approach or Architecture?
– Approach
• Introduces general guidelines on ways of performing
the work.
– Architecture
• Defines the structured solution that meets all of the
technical and operational requirements.
11. Microservices
• Approach from organizational point of view.
– Which tools are going to be used?
– What technology stacks are available?
– How processes are going to be organized?
– Which protocols will be used?
– How the deployment will be organized?
12. Microservices
• Architecture from development point of view.
– Which elements will be used to build the software?
– How relations between elements will be organized?
– How elements will be structured?
– How elements, relations and structure are configured?
21. Core Components
• Spring Cloud
– built on top of Spring Boot.
– ready for microservice development.
• Multiple implementations of common patterns.
– E.g. support for Eureka, ZooKeeper and Consul.
22. Core Components
• Spring Cloud Netflix.
– Discovery server and client.
– Latency and fault tolerance library.
– Client-side load balancing over RestTemplate.
– Declarative REST client.
– Edge proxy for API gateway implementations.
23. Core Components – Service Discovery
• A simple discovery server implementation using Spring Cloud looks like:
– By default Eureka will be available at http://localhost:8761/eureka
– Custom settings should be configured in bootstrap.yml (or .properties)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class DiscoveryServer {
public static void main(String[] args) {
SpringApplication.run(DiscoveryServer.class, args);
}
}
24. Core Components – Service Discovery
• A simple discovery server configuration looks like:
spring:
application:
name: DiscoveryServer
server:
port: 8761
eureka:
server:
enable-self-preservation: false
25. Core Components – Service Discovery
• Eureka
– Self-Preservation
– Peer Awareness
– Availability Zones
– Regions
27. Core Components – Service Discovery
• A simple service with discovery client looks like:
@SpringBootApplication
@EnableEurekaClient //@EnableDiscoveryClient
@RestController
public class HelloService {
@RequestMapping("/hello")
public String sayHello(@RequestParam String name) {
return "Hello " + name;
}
public static void main(String[] args) {
SpringApplication.run(HelloService.class, args);
}
}
28. Core Components – Service Discovery
• A simple service configuration looks like:
spring:
application:
name: HelloService
eureka:
client:
fetchRegistry: true
registerWithEureka: true
serviceUrl:
defaultZone: http://localhost:8761/eureka
29. Core Components – Load Balancing
• How do we load balance between instances of HelloService?
30. Core Components – Load Balancing
• Ribbon – the client-side load
balancer.
• Ribbon supports auto-retry, time-out
and other useful configurable
features.
31. Core Components – Load Balancing
• Spring Cloud implements Ribbon as a wrapper over RestTemplate.
• Default load balancing logic is round-robin.
32. Core Components – Load Balancing
@Bean
@LoadBalanced
public RestTemplate restTmpl() {
return new RestTemplate();
}
@RestController
public class HelloWorldRest {
@Autowired
private RestTemplate restTmpl;
@RequestMapping("/hello-world")
public String sayHello() {
String url = "http://HelloService/hello?name=World";
return restTmpl.getForObject(url, String.class);
}
}
33. Core Components – Circuit Breaker
• Remote service fails or is not available:
34. Core Components – Circuit Breaker
• Remote service fails or is not available:
– Long-running requests that result in failure.
35. Core Components – Circuit Breaker
• Remote service fails or is not available:
– Long-running requests that result in failure.
– Users wait to get the failure response.
36. Core Components – Circuit Breaker
• Remote service fails or is not available:
– Long-running requests that result in failure.
– Users wait to get the failure response.
• Failure continues for some unpredictable time:
37. Core Components – Circuit Breaker
• Remote service fails or is not available:
– Long-running requests that result in failure.
– Users wait to get the failure response.
• Failure continues for some unpredictable time:
– More dependent services can be blocked.
38. Core Components – Circuit Breaker
• Remote service fails or is not available:
– Long-running requests that result in failure.
– Users wait to get the failure response.
• Failure continues for some unpredictable time:
– More dependent services can be blocked.
– Longer response times, more users have to wait…
39. Core Components – Circuit Breaker
• Some of service dependencies will inevitably fail.
40. Core Components – Circuit Breaker
• Some of service dependencies will inevitably fail.
– Cascading failures turn into a chain reaction.
41. Core Components – Circuit Breaker
• Some of service dependencies will inevitably fail.
– Cascading failures turn into a chain reaction.
• Hystrix helps to control the interactions between distributed services by
adding latency tolerance and fault tolerance logic.
42. Core Components – Circuit Breaker
• Hystrix – the circuit breaker.
– Isolates points of access between services.
– Stops cascading failures.
– Provides fallback options.
43. Core Components – Circuit Breaker
@SpringBootApplication
@EnableHystrix //@EnableCircuitBreaker
@RestController
//other annotations – Eureka, etc.
public class HelloWorldService {
//beans, autowires, main method...
@HystrixCommand(fallbackMethod = "helloFallback")
@RequestMapping("/hello-world")
public String sayHello() {
String url = "http://HelloService/hello?name=World";
return restTmpl.getForObject(url, String.class);
}
private String helloFallback() {
return "Sorry World, try again later please.";
}
}
45. Core Components – REST Client
• Feign – declarative REST client.
– Integrated support for Eureka, Ribbon and Hystrix.
46. Core Components – REST Client
• Feign – declarative REST client.
– Integrated support for Eureka, Ribbon and Hystrix.
– Enabled by adding @EnableFeignClients to your configuration class.
47. Core Components – REST Client
• Feign – declarative REST client.
– Integrated support for Eureka, Ribbon and Hystrix.
– Enabled by adding @EnableFeignClients to your configuration class.
@FeignClient(name = "HelloService")
public interface HelloClient {
@RequestMapping("/hello")
String sayHello(@RequestParam String name);
}
48. Core Components – REST Client
• Feign client with Hystrix fallbacks:
@FeignClient(name = "HelloService", fallback = HelloFallback.class)
public interface HelloClient {
@RequestMapping("/hello")
String sayHello(@RequestParam String name);
}
@Component
public class HelloFallback implements HelloClient {
@Override
public String sayHello(String name) {
return "Sorry " + name + ", try again later please";
}
}
49. Core Components
• Let’s imagine we are building an
application with microservices
architecture.
• Services depend on other services.
50. Core Components
• Let’s imagine we are building an
application with microservices
architecture.
• Services depend on other services.
• Services find each other through
service discovery.
51. Core Components
• Let’s imagine we are building an
application with microservices
architecture.
• Services depend on other services.
• Services find each other through
service discovery.
• How do clients integrate with our
microservices?
52. Core Components – API Gateway
• API gateway is the single entry
point for clients.
53. Core Components – API Gateway
• API gateway is the single entry
point for clients.
• The API gateway handles requests
in one of two ways:
54. Core Components – API Gateway
• API gateway is the single entry
point for clients.
• The API gateway handles requests
in one of two ways:
– Simply proxy/route requests to the
appropriate service.
55. Core Components – API Gateway
• API gateway is the single entry
point for clients.
• The API gateway handles requests
in one of two ways:
– Simply proxy/route requests to the
appropriate service.
– Expose a different API for each
client.
56. Core Components – API Gateway
• API gateway – the single entry point to your microservices
– eliminates the hassle of dealing with your internal infrastructure.
• Zuul – the edge proxy which is integrated with Eureka, Ribbon & Hystrix.
64. Instrumentation
• Turbine AMQP allows any application post metrics to the single stream.
– Turbine Server – aggregates all metrics sent to the stream.
66. Instrumentation
• Integrated tools for metrics and tracing
– Hystrix Stream and Hystrix Dashboard
• near real-time monitoring of circuit breakers at a single host.
– Turbine
• the Hystrix Stream aggregator that allows to monitor all nodes in cluster.
– Turbine AMQP
• the Hystrix Stream aggregator that allows to monitor all applications in network.
– Sleuth & Zipkin
• distributed tracing of cascading calls between microservices.
68. Security
• Implementations:
– API Gateway / Perimeter Security;
– Everybody Can Auth (with HTTP Basic);
– Basic + Central Auth DB;
– Sessions Everywhere;
– API Tokens;
– SAML;
– Etc.
69. Security
• Common concerns:
– Central user store bottleneck;
– Lack of single sign on;
– Statelessness;
– Exposure of user credentials;
– Lack of fine grained authorization;
– Interoperability with non browser clients;
84. Security – SSO with Zuul
• To enable SSO at Zuul:
@SpringBootApplication
@EnableZuulProxy
@EnableOAuth2Sso
public class ApiGateway {
//main method...
}
85. Security
• Spring Cloud Security
– OAuth2 for delegated authorization.
– JWT for self-contained tokens.
– OpenID Connect for delegated authentication.
– SSO with Zuul at API gateway.
99. Configuration Management
• Configuring applications separately is uncomfortable.
– Application are deployed to different hosts.
– Different environments (DEV, RC, PROD).
101. Configuration Management
• Spring Cloud Config – server and client support for external configuration.
• Features:
– HTTP-based API for external configuration.
102. Configuration Management
• Spring Cloud Config – server and client support for external configuration.
• Features:
– HTTP-based API for external configuration.
– Encrypt and decrypt property values.
103. Configuration Management
• Spring Cloud Config – server and client support for external configuration.
• Features:
– HTTP-based API for external configuration.
– Encrypt and decrypt property values.
– Git as default repository storage.
• File-based, SVN and other options are available.
104. Configuration Management
• Config Server is the central place to manage external properties for
applications across all environments.
105. Configuration Management
Config Server application:
Config Server configuration: Config Client configuration:
@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
public static void main(String[] args) {
SpringApplication.run(ConfigServer.class, args);
}
}
spring.cloud.config.server.git:
uri: http://git.xyz/config-repo
username: user
password: passkey
spring:
application.name: AppName
cloud.config.uri: http://host:8182