Dave
Assistant Engineer
Assistant Engineer
  • UID627
  • Fans2
  • Follows0
  • Posts55
Reads:874Replies:0

Leveraging Alibaba Cloud Container Service to develop Spring Cloud Microservice Application (4) 

Created#
More Posted time:Jan 5, 2017 16:03 PM
This article explores how to leverage service orchestration of Alibaba Cloud Container Service to achieve high application availability and use Spring Cloud Hystrix Circuit Breaker.

Service Downgrade and Fault-tolerance
Spring Cloud offers Netflix Hystrix, an intelligent circuit breaker capability. The circuit breaker can be called for failed service. Whenever a service call times out or fails, the circuit breaker can be called for the failed service to prevent as many errors as possible that could impact the system.
We will briefly discuss how to add Hystrix support to an application. For additional details, refer to Spring Cloud Netflix document.
Add the dependent packages of a project on Hystrix and Eureka in the build.gradle file.
dependencies {
    compile('org.springframework.cloud:spring-cloud-starter-hystrix')
    compile('org.springframework.cloud:spring-cloud-starter-eureka')
   ...
}


Add an @EnableCircuitBreaker annotation to an application. Since Hystrix dependency has been adopted, this is actually using Hystrix's circuit breaker capability.
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class FoobarHystrixApplication {

    public static void main(String[] args) {
        SpringApplication.run(FoobarHystrixApplication.class, args);
    }
}


When a service is called, a fallback method can be configured using the @HystrixCommand for downstream services that possibly encounter errors. The method will be called once a call times out or fails.
@Service
public class BarService {

    @Autowired
    @LoadBalanced
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "getMessageFallback")
    public String getMessage() {
        return restTemplate.getForObject("http://bar/message", String.class);
    }

    public String getMessageFallback(){
        return "Bar service (fallback result)";
    }
}


dopting Hystrix enables circuit breaking at the service level when an error occurs. The fallback method, when it is made available, can return the temporary alternative results when downstream services fail, which downgrades service to a certain degree.
Cross-zone Deployment
One of the ways to achieve high application availability is to deploy container instances in multiple zones. Alibaba Cloud Container Service supports cross-zone orchestration and other deployment modes.
For example, if we want to deploy an application, follow this architecture:
 
Nodes in a container cluster are distributed in two zones. Every single service has at least two container instances, and different container instances are not supposed to be in the same zone. Apply for an external Server Load Balancer (SLB) to drive traffic for cross-zone load balancing.
The following declaration can be made in the deployment description document:
...
  svc1:
    image: xxx
...
  svc2:
    image: xxx
    environment:
      - affinity:service!=svc1
...      
  foo:
    image: xxx
    labels:
      aliyun.scale: "2"
    environment:
      - 'availability:az==2'
...


In the svc2 description, affinity:service!=svc2 indicates that svc2 cannot be deployed on the nodes where svc1 resides.
In the foo description, aliyun.scale: "2" indicates that there are two nodes available and must be located in different zones ('availability:az==2').
Reschedule Container When Node Fails
The Container Service supports Docker container rescheduling: When a node fails, the container can be automatically rescheduled to another available node to run automatically. By default, the container rescheduling policy is disabled. You can use the following configuration to enable the rescheduling policy as needed.
redis:
  image: redis
  environment:
    - reschedule:on-node-failure


Summary
This post demonstrates how to leverage service orchestration of Alibaba Cloud Container Service to achieve high application availability and use Spring Cloud Hystrix Circuit Breaker.
Guest