Intro to Spring Cloud &
Microservices
By Eugene Hanikblum
• Adopting a microservice architecture provides unique opportunities
to add failover and resiliency to your systems, so your components
can handle load spikes and errors gracefully. Microservices make
change less expensive too. It can also be a good idea when you have a
large team working on a single product. Your project can likely be
broken up into components that can function independently of one
another. Once components can function independently, they can be
built, tested, and deployed independently. This gives an organization
and its teams the agility to develop and deploy very quickly.
History of Microservices
• According to Wikipedia, the term “microservice” was first used as a
common architecture style at a workshop of software architects near
Venice in May 2011. In May 2012, the same group decided
“microservices” was a more appropriate name.
• Adrian Cockcroft, who was at Netflix at the time, described this
architecture as “fine-grained SOA”. Martin Fowler and James Lewis
wrote an article titled simply Microservices on March 25, 2014. Years
later, this is still considered the definitive article for microservices.
Organizations and Conway’s Law
• Technology has traditionally been organized into technology layers: UI
team, database team, operations team.
• When teams are separated along these lines, even simple changes
can lead to a cross-team project sucking down huge chunks of time
and budget.
Conway’s Law
“Any organization that designs a system (defined broadly) will produce
a design whose structure is a copy of the organization’s communication
structure.”
— Melvyn Conway, 1967
Microservices Architecture Philosophy
The philosophy of a microservices architecture is essentially equal to the Unix philosophy of “Do
one thing and do it well”.
The characteristics of a microservices architecture are as follows:
• Componentization via services
• Organized around business capabilities
• Products not projects
• Smart endpoints and dumb pipes
• Decentralized governance
• Decentralized data management
• Infrastructure automation
• Design for failure
• Evolutionary design
Why Microservices?
• For most developers, dev teams, and organizations, it’s easier to work
on small “do one thing well” services. No single program represents
the whole application, so services can change frameworks (or even
languages) without a massive cost. As long as the services use a
language agnostic protocol (HTTP or lightweight messaging),
applications can be written in several different platforms - Java, Ruby,
Node, Go, .NET, etc. - without issue.
• Platform-as-a-Service (PaaS) providers and containers have made it
easy to deploy microservices. All the technologies needed to support
a monolith (e.g. load balancing, discovery, process monitoring) are
provided by the PaaS, outside of your container. Deployment effort
comes close to zero.
Are Microservices the Future?
• Architecture decisions, like adopting microservices, are usually only
evident several years after you make them. Microservices have been
successful at companies like LinkedIn, Twitter, Facebook, Amazon, and
Netflix. But that doesn’t mean they’ll be successful for your
organization.
• Component boundaries are hard to define. If you’re not able to create
your components cleanly, you’re just shifting complexity from inside a
component to the connections between the components. Also, team
capabilities are something to consider.
Are Microservices the Future?
“You shouldn’t start with a microservices architecture. Instead, begin
with a monolith, keep it modular, and split it into microservices once
the monolith becomes a problem.”
— Martin Fowler
Spring microservices main pieces
• Spring Boot
• Spring Cloud
• Netflix Eureka
Netflix Eureka
• Netflix Eureka is a REST-based service that is primarily used in the
cloud infrastructure for locating services for the purpose of load
balancing and failover of middle-tier servers.
Spring Cloud
• Spring Cloud provides tools for developers to quickly build some of
the common patterns in distributed systems (e.g. configuration
management, service discovery, circuit breakers, intelligent routing,
micro-proxy, etc.). Coordination of distributed systems leads to
boilerplate patterns. Using Spring Cloud, developers can quickly stand
up services and applications that implement those patterns. They will
work well in any distributed environment, including the developer’s
own laptop, bare metal data centers, and managed platforms
Spring Cloud Netflix
• Spring Cloud Netflix provides Netflix OSS integrations for Spring Boot
applications. Patterns provided include Service Discovery (Eureka),
Circuit Breaker (Hystrix), Intelligent Routing (Zuul) and Client Side
Load Balancing (Ribbon).
Building simple spring cloud topology
• It will consists of :
• Configuration server
• Discovery server (eureka)
• Custom service (spring microservice)
Configuration server
Configuration Server
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
Configuration Server
spring:
cloud:
config:
server:
git:
uri: https://github.com/honeyflower-studios/cloud-configuration
server:
port: 9999
Discovery server
Discovery Server
@SpringBootApplication
@EnableEurekaServer
public class DiscoveryServerApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryServerApplication.class, args);
{
{
Discovery Server
server:
port: 2000
eureka:
client:
registerWithEureka: false
fetchRegistry: false
Discovery server (UI)
Custom service
• The next server we are going to create —is User Service. This would
be the service that will have some logic and will be functional service.
In our case it will just send us information —“Hi, this is message from
user-service”.
User service
User service
@SpringBootApplication
@EnableDiscoveryClient
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}
User service
spring:
application:
name: user-service
cloud:
config:
uri: http://localhost:9999
server:
port: 7001
User service
User service (registered with eureka)
User service (hello-world controller)
@RestController
@RequestMapping(value = "/user")
public class UserController {
@GetMapping("/message")
public String sendMessage(){
return "Hi, this is message from user-service";
}
}
User service
Section 2
Extending out topology
Extending our platform
In this part we will speak about communication between microservices,
and will add an API Gateway to our project
payment service
payment service
@RestController
@RequestMapping(value = "/payments")
public class PaymentController {
@GetMapping(value = "/message")
public String getMessage(){
return "Hi, this is message from payment-service";
}
}
payment service
spring:
application:
name: payment-service
cloud:
config:
uri: http://localhost:9999
server:
port: 7002 #tell to use this port, in case server will not find remote configuration
payment service
Conclusion
Awesome!
Now we have payment-service working and available on
localhost:8002. So, any time we can make a request to it and got
expected response. And if we want something from user-serverice,
that we built, we need to call localhost:8001.
Why Cloud Gateway
But what does that mean? If we have 10, or 30 microservices, do we
need to remember all DNS names, or hosts and ports where they are
running in order to make requests to them? If so —that would be very
strange and uncomfortable ;)
Spring Cloud Zuul
• Zuul is JVM-based router and load balancer;
• Zuul can be used for many API Gateway needs;
What is Spring Cloud Zuul
Zuul server should be working like a Gateway.
You will make requests to one server(Zuul server),
and it in his turn will redirect them to corresponding microservices
Gateway server
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
Gateway server
@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class GatewayServerApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayServerApplication.class, args);
}
}
Gateway server
spring:
application:
name: gateway-server
cloud:
config:
uri: http://localhost:9999
Gateway server
Gateway server (registered with eureka)
Gateway server (proxy mode)
• Once it started and registered with eureka we can use general api
entrypoint instead of directly pointing to every specific microservice’s
endpoint.
Hystrix – load balancer & circuit breaker
Why Load balancing / circuit breaker
An issue that occurs when having a microservice architecture is the
possibility of failures to happen. With many services communicating
over the network, a network issue might occur. You don’t want users to
be stumbling over a page loading very slowly (due to network
timeouts) and then arriving on an error page.
What is Hystrix
• A latency and fault tolerance library designed to isolate points of
access to remote systems, services and 3rd party libraries, stop
cascading failure and enable resilience in complex distributed systems
where failure is inevitable.
• In a distributed environment, inevitably some of the many service
dependencies will fail. Hystrix is a library that helps you control the
interactions between these distributed services by adding latency
tolerance and fault tolerance logic. Hystrix does this by isolating
points of access between the services, stopping cascading failures
across them, and providing fallback options, all of which improve your
system’s overall resiliency.
How Hystrix works
Hystrix is watching methods for failing calls to related services. If there
is such a failing method, it will open the circuit, which means, it
forwards the call to a fallback method. In case the services is restored it
will close the circuit and the applications acts as expected again.
user-service : adding hystrix
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
user-service - Adding failure tolerance
capabilities
@EnableCircuitBreaker
@SpringBootApplication
@EnableDiscoveryClient
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}
@Configuration
class Config {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
user-service - Adding failure tolerance
capabilities
@RestController
@RequestMapping("/hello/client")
public class HelloController {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "fallback", groupKey = "Hello",
commandKey = "hello", threadPoolKey = "helloThread")
@GetMapping("/sayhello")
public String hello() {
String url = "http://payment-service/payments/pay";
return restTemplate.getForObject(url, String.class)+ " including client";
}
public String fallback(Throwable hystrixCommand) {
return "Fall Back Hello world";
}
user-service config (in config repo)
server:
port: 8001
eureka:
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://localhost:2000/eureka
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 60000
user-service
Invoking our load balanced endpoint
Testing fallback capabilities
• Payment service is up :
• Payment service is down (fallback method invoked as result of rest api
failure)
How all this works together
user-service payment-service
http / message broker
That’s fine, but how can I monitor this
Hystrix dashboard
Hystrix dahboard
• The Hystrix Dashboard displays the health of each circuit breaker in
an efficient manner.
Hystrix Dashboard
Hystrix Dashboard : streamer side
• Each microservice that uses hystrix circuit beraker automatically
exposed /hystrix.stream endpoint.
• Then we feed hystrix dashboard with this endpoint url to get stats
Wait… Should I monitor every endpoint one by one ?
- No, there is plenty of solutions such as “Turbine” aggregator.
- But unfortunately this isn’t covered in current presentation 
Hope you enjoyed this session! 
Thanks
@hanikblum
https://www.linkedin.com/in/evgeny-hanikblum-378a993/

Intro to spring cloud &microservices by Eugene Hanikblum

  • 1.
    Intro to SpringCloud & Microservices By Eugene Hanikblum
  • 2.
    • Adopting amicroservice architecture provides unique opportunities to add failover and resiliency to your systems, so your components can handle load spikes and errors gracefully. Microservices make change less expensive too. It can also be a good idea when you have a large team working on a single product. Your project can likely be broken up into components that can function independently of one another. Once components can function independently, they can be built, tested, and deployed independently. This gives an organization and its teams the agility to develop and deploy very quickly.
  • 3.
    History of Microservices •According to Wikipedia, the term “microservice” was first used as a common architecture style at a workshop of software architects near Venice in May 2011. In May 2012, the same group decided “microservices” was a more appropriate name. • Adrian Cockcroft, who was at Netflix at the time, described this architecture as “fine-grained SOA”. Martin Fowler and James Lewis wrote an article titled simply Microservices on March 25, 2014. Years later, this is still considered the definitive article for microservices.
  • 4.
    Organizations and Conway’sLaw • Technology has traditionally been organized into technology layers: UI team, database team, operations team. • When teams are separated along these lines, even simple changes can lead to a cross-team project sucking down huge chunks of time and budget.
  • 5.
    Conway’s Law “Any organizationthat designs a system (defined broadly) will produce a design whose structure is a copy of the organization’s communication structure.” — Melvyn Conway, 1967
  • 6.
    Microservices Architecture Philosophy Thephilosophy of a microservices architecture is essentially equal to the Unix philosophy of “Do one thing and do it well”. The characteristics of a microservices architecture are as follows: • Componentization via services • Organized around business capabilities • Products not projects • Smart endpoints and dumb pipes • Decentralized governance • Decentralized data management • Infrastructure automation • Design for failure • Evolutionary design
  • 7.
    Why Microservices? • Formost developers, dev teams, and organizations, it’s easier to work on small “do one thing well” services. No single program represents the whole application, so services can change frameworks (or even languages) without a massive cost. As long as the services use a language agnostic protocol (HTTP or lightweight messaging), applications can be written in several different platforms - Java, Ruby, Node, Go, .NET, etc. - without issue. • Platform-as-a-Service (PaaS) providers and containers have made it easy to deploy microservices. All the technologies needed to support a monolith (e.g. load balancing, discovery, process monitoring) are provided by the PaaS, outside of your container. Deployment effort comes close to zero.
  • 8.
    Are Microservices theFuture? • Architecture decisions, like adopting microservices, are usually only evident several years after you make them. Microservices have been successful at companies like LinkedIn, Twitter, Facebook, Amazon, and Netflix. But that doesn’t mean they’ll be successful for your organization. • Component boundaries are hard to define. If you’re not able to create your components cleanly, you’re just shifting complexity from inside a component to the connections between the components. Also, team capabilities are something to consider.
  • 9.
    Are Microservices theFuture? “You shouldn’t start with a microservices architecture. Instead, begin with a monolith, keep it modular, and split it into microservices once the monolith becomes a problem.” — Martin Fowler
  • 10.
    Spring microservices mainpieces • Spring Boot • Spring Cloud • Netflix Eureka
  • 11.
    Netflix Eureka • NetflixEureka is a REST-based service that is primarily used in the cloud infrastructure for locating services for the purpose of load balancing and failover of middle-tier servers.
  • 12.
    Spring Cloud • SpringCloud provides tools for developers to quickly build some of the common patterns in distributed systems (e.g. configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy, etc.). Coordination of distributed systems leads to boilerplate patterns. Using Spring Cloud, developers can quickly stand up services and applications that implement those patterns. They will work well in any distributed environment, including the developer’s own laptop, bare metal data centers, and managed platforms
  • 13.
    Spring Cloud Netflix •Spring Cloud Netflix provides Netflix OSS integrations for Spring Boot applications. Patterns provided include Service Discovery (Eureka), Circuit Breaker (Hystrix), Intelligent Routing (Zuul) and Client Side Load Balancing (Ribbon).
  • 14.
    Building simple springcloud topology • It will consists of : • Configuration server • Discovery server (eureka) • Custom service (spring microservice)
  • 15.
  • 16.
    Configuration Server @SpringBootApplication @EnableConfigServer public classConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
  • 17.
  • 18.
  • 19.
    Discovery Server @SpringBootApplication @EnableEurekaServer public classDiscoveryServerApplication { public static void main(String[] args) { SpringApplication.run(DiscoveryServerApplication.class, args); { {
  • 20.
  • 21.
  • 22.
    Custom service • Thenext server we are going to create —is User Service. This would be the service that will have some logic and will be functional service. In our case it will just send us information —“Hi, this is message from user-service”.
  • 23.
  • 24.
    User service @SpringBootApplication @EnableDiscoveryClient public classUserServiceApplication { public static void main(String[] args) { SpringApplication.run(UserServiceApplication.class, args); } }
  • 25.
  • 26.
  • 27.
  • 28.
    User service (hello-worldcontroller) @RestController @RequestMapping(value = "/user") public class UserController { @GetMapping("/message") public String sendMessage(){ return "Hi, this is message from user-service"; } }
  • 29.
  • 30.
  • 31.
    Extending our platform Inthis part we will speak about communication between microservices, and will add an API Gateway to our project
  • 32.
  • 33.
    payment service @RestController @RequestMapping(value ="/payments") public class PaymentController { @GetMapping(value = "/message") public String getMessage(){ return "Hi, this is message from payment-service"; } }
  • 34.
    payment service spring: application: name: payment-service cloud: config: uri:http://localhost:9999 server: port: 7002 #tell to use this port, in case server will not find remote configuration
  • 35.
  • 36.
    Conclusion Awesome! Now we havepayment-service working and available on localhost:8002. So, any time we can make a request to it and got expected response. And if we want something from user-serverice, that we built, we need to call localhost:8001.
  • 37.
    Why Cloud Gateway Butwhat does that mean? If we have 10, or 30 microservices, do we need to remember all DNS names, or hosts and ports where they are running in order to make requests to them? If so —that would be very strange and uncomfortable ;)
  • 38.
    Spring Cloud Zuul •Zuul is JVM-based router and load balancer; • Zuul can be used for many API Gateway needs;
  • 39.
    What is SpringCloud Zuul Zuul server should be working like a Gateway. You will make requests to one server(Zuul server), and it in his turn will redirect them to corresponding microservices
  • 40.
  • 41.
    Gateway server @SpringBootApplication @EnableZuulProxy @EnableDiscoveryClient public classGatewayServerApplication { public static void main(String[] args) { SpringApplication.run(GatewayServerApplication.class, args); } }
  • 42.
  • 43.
  • 44.
  • 45.
    Gateway server (proxymode) • Once it started and registered with eureka we can use general api entrypoint instead of directly pointing to every specific microservice’s endpoint.
  • 46.
    Hystrix – loadbalancer & circuit breaker
  • 47.
    Why Load balancing/ circuit breaker An issue that occurs when having a microservice architecture is the possibility of failures to happen. With many services communicating over the network, a network issue might occur. You don’t want users to be stumbling over a page loading very slowly (due to network timeouts) and then arriving on an error page.
  • 48.
    What is Hystrix •A latency and fault tolerance library designed to isolate points of access to remote systems, services and 3rd party libraries, stop cascading failure and enable resilience in complex distributed systems where failure is inevitable. • In a distributed environment, inevitably some of the many service dependencies will fail. Hystrix is a library that helps you control the interactions between these distributed services by adding latency tolerance and fault tolerance logic. Hystrix does this by isolating points of access between the services, stopping cascading failures across them, and providing fallback options, all of which improve your system’s overall resiliency.
  • 49.
    How Hystrix works Hystrixis watching methods for failing calls to related services. If there is such a failing method, it will open the circuit, which means, it forwards the call to a fallback method. In case the services is restored it will close the circuit and the applications acts as expected again.
  • 50.
    user-service : addinghystrix <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
  • 51.
    user-service - Addingfailure tolerance capabilities @EnableCircuitBreaker @SpringBootApplication @EnableDiscoveryClient public class UserServiceApplication { public static void main(String[] args) { SpringApplication.run(UserServiceApplication.class, args); } } @Configuration class Config { @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); } }
  • 52.
    user-service - Addingfailure tolerance capabilities @RestController @RequestMapping("/hello/client") public class HelloController { @Autowired private RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "fallback", groupKey = "Hello", commandKey = "hello", threadPoolKey = "helloThread") @GetMapping("/sayhello") public String hello() { String url = "http://payment-service/payments/pay"; return restTemplate.getForObject(url, String.class)+ " including client"; } public String fallback(Throwable hystrixCommand) { return "Fall Back Hello world"; }
  • 53.
    user-service config (inconfig repo) server: port: 8001 eureka: client: registerWithEureka: true fetchRegistry: true serviceUrl: defaultZone: http://localhost:2000/eureka hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 60000
  • 54.
  • 55.
    Invoking our loadbalanced endpoint
  • 56.
    Testing fallback capabilities •Payment service is up : • Payment service is down (fallback method invoked as result of rest api failure)
  • 57.
    How all thisworks together user-service payment-service http / message broker
  • 58.
    That’s fine, buthow can I monitor this
  • 59.
  • 60.
    Hystrix dahboard • TheHystrix Dashboard displays the health of each circuit breaker in an efficient manner.
  • 61.
  • 62.
    Hystrix Dashboard :streamer side • Each microservice that uses hystrix circuit beraker automatically exposed /hystrix.stream endpoint. • Then we feed hystrix dashboard with this endpoint url to get stats
  • 63.
    Wait… Should Imonitor every endpoint one by one ? - No, there is plenty of solutions such as “Turbine” aggregator. - But unfortunately this isn’t covered in current presentation 
  • 64.
    Hope you enjoyedthis session!  Thanks @hanikblum https://www.linkedin.com/in/evgeny-hanikblum-378a993/