springCloud Learning 3 (Netflix Hystrix flexibility Client)

springcloud Collections: https://www.tapme.top/blog/detail/2019-02-28-11-33

See all the code used in this article at the bottom.

First, why have a client elastic mode

  All systems will experience a failure, the higher the probability of a distributed single point of failure. How to build an application to deal with failure is a key part of every software developer working. But usually when building systems, most engineers consider only the infrastructure or the complete failure of critical services, such as the use of cluster key servers, load balancing, and remote deployment among other technical services. Although these methods take into account the complete failure of the components of the system, but they solved the construction of the elastic system of a small part of the problem. When the service crashes, it is easy to detect the failure of the service, and so the application can bypass it. However, when the service is slow, detect this increasingly poor service performance and around it is very difficult because of the following reasons:

  • The downgrade is based on intermittent faults can begin, and the formation of irreversible momentum ---- may begin only a small part of the service invocation slowed until suddenly ran out of the application container threads (all the threads are waiting call complete) and complete collapse.
  • Applications are often designed to be completely remote troubleshooting resources, rather than partially degraded ---- In general, as long as the service is not completely dead, the application will continue to call the service until the resources are exhausted collapse.

  Poor performance of remote service will cause a lot of potential problems, they are not only difficult to detect, but also triggered a chain reaction, thus affecting the entire application ecosystem. Without proper safeguards, a poorly performing service can quickly wear down the entire application. Cloud-based, micro-services-based applications are particularly vulnerable to the impact of these types of terminals, because these applications by a large number of fine-grained composition of distributed services, these services involve different user infrastructure at the completion of the transaction.

Second, what is the client elastic mode

  The client mode is poor elastic protective remote resources (micro another service call or database query) from a crash or error in the performance of remote service. The goal of these models is to "fail fast" to make the client, such as database connections do not consume resources like the thread pool, but also avoid the problem of remote services to clients spread of consumers, causing "avalanche" effect. There are four client elastic spring cloud main mode of use:

  • Client load balancing (client load balance) mode

  Previous have said, I will not repeat them here.

  • Circuit breaker (circuit breaker) mode

  This pattern simulates a circuit breaker. With software breaker, when the remote service is called, the circuit breaker will monitor the call, if the call time is too long, the circuit breaker will intervene and interrupt calls. In addition, if the number of calls to a remote resource fails to reach a certain threshold, it will take a quick failure policy to prevent future calls to the remote resource failure.

  • Backup (fallback) mode

  When the remote call fails, the alternate execution path of the code, and attempts to process operations other ways, instead of generating an exception. That is a contingency measure to provide for remote operation, rather than simply throwing an exception.

  • Bulkhead (bulkhead) mode

  Bulkhead model is built on the basis of the concept of shipbuilding. We all know that a ship can be divided into multiple watertight compartments (bulkhead), so even a few parts of the breakdown leaking, the whole ship will not be submerged. This concept will be brought to a remote call, if all calls are handled using the same thread pool, it is likely that a remote call will slow down the entire application. In the bulkhead mode you can isolate each remote resource, and assign each thread pool, to make it independently of each other.

  The following figure shows how these patterns are applied to the micro-service:

Client elastic mode architecture

Three, spring cloud use

  Netflix Hystrix use of libraries to implement the elastic mode. Continue to use on a project to licensingservice service implementation resilient mode.

1, code changes

1, rely on the introduction of

  To modify the POM file, add the following two dependencies:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<!--本依赖不是必须的,spring-cloud-starter-hystrix已经带了,但是在Camden.SR5发行版本中使用了1.5.6,这个版本有一个不一致的地方,在没有后备的情况下会抛出java.lang.reflect.UndeclaredThrowableException而不是com.netflix.hystrix.exception.HystrixRuntimeException,
        在后续版本中修复了这个问题-->
<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-javanica</artifactId>
    <version>1.5.9</version>
</dependency>

  Then on startup class joined @EnableCircuitBreakerenabled Hystrix.

2, to achieve breaker

  First modify organizationservice project OrganizationController, analog delay, twice every thread to sleep 2 Miao

@RestController
public class OrganizationController {

    private static int count=1;

    @GetMapping(value = "/organization/{orgId}")
    public Object getOrganizationInfo(@PathVariable("orgId") String orgId) throws Exception{
        if(count%2==0){
            TimeUnit.SECONDS.sleep(2);
        }
        count++;
        Map<String, String> data = new HashMap<>(2);
        data.put("id", orgId);
        data.put("name", orgId + "公司");
        return data;
    }
}

  Just on the way to add @HystrixCommand, you can achieve a short-circuit timeout. If the scan to the Spring annotation annotation class, it will dynamically generate a proxy, to wrap this method, and all calls through a dedicated thread pool to manage remote invocation of the method.

  Modify licensingservice service OrganizationByRibbonService, OrganizationFeignClient, which give way to add @HystrixCommandannotations. Then access interface localhost: 10011 / licensingByRibbon / 11313 , localhost: 10011 / licensingByFeign / 11313 . Can be found in many visits to throw an error com.netflix.hystrix.exception.HystrixRuntimeException, circuit breakers to take effect by default when the operating time of 1s.

{
  "timestamp": 1543823192424,
  "status": 500,
  "error": "Internal Server Error",
  "exception": "com.netflix.hystrix.exception.HystrixRuntimeException",
  "message": "OrganizationFeignClient#getOrganization(String) timed-out and no fallback available.",
  "path": "/licensingByFeign/11313/"
}

  When the operation time may be modified by setting the parameter annotation. Set the timeout is greater than 2s after the operation when the error is not reported. (Do not know why the set failed Feign, ribbon normal.). Generally will write the configuration in the configuration file.

@HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "20000")
    })

3, backup processing

  Due to the existence of a "middleman" between itself and consumer resources remote resources, so developers can intercept service failures, and choose alternatives. Backup process carried out in Hystrix, a very easy to implement.

  1. Implemented in the ribbon

  Simply @HystrixCommandadding attributes fallbackMethod = "methodName" comment, then in the implementation fails, it performs the backup method. Note preparedness methods must be protected in the same class methods, and the method signature must be the same. OrganizationByRibbonService modified under the category licensingservice in the service package, read as follows:

@Component
public class OrganizationByRibbonService {

    private RestTemplate restTemplate;


    @Autowired
    public OrganizationByRibbonService(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
    },fallbackMethod = "getOrganizationWithRibbonBackup")
    public Organization getOrganizationWithRibbon(String id) throws Exception {
        ResponseEntity<Organization> responseEntity = restTemplate.exchange("http://organizationservice/organization/{id}",
                HttpMethod.GET, null, Organization.class, id);
        return responseEntity.getBody();
    }

    public Organization getOrganizationWithRibbonBackup(String id)throws Exception{
        Organization organization = new Organization();
        organization.setId("0");
        organization.setName("组织服务调用失败");
        return organization;
    }
}

  Start the application, many visits to localhost: 10011 / licensingByRibbon / 11313 / , can be found when the call fails, it will enable the backup method.

  1. In feign achieve in

  Implemented in the backup mode feign, feign need to write a class that implements the interface, and then specify the class in feign interface. To licensingservice example. First, add a client package OrganizationFeignClientImpl class code is as follows:

@Component
public class OrganizationFeignClientImpl implements OrganizationFeignClient{

    @Override
    public Organization getOrganization(String orgId) {
        Organization organization=new Organization();
        organization.setId("0");
        organization.setName("后备模式返回的数据");
        return organization;
    }
}

Then modify the interface OrganizationFeignClient notes, will @FeignClient("organizationservice")be changed @FeignClient(name="organizationservice",fallback = OrganizationFeignClientImpl.class.

  Restart the project, many visits to localhost: 10011 / licensingByFeign / 11313 / , can be found in the works back-up services.

  When confirming whether to enable back-up services, with two caveats:

  • Backup is a mechanism to provide programs of action in the resource operation or failure. Abnormal and then only if the reserve is only used to capture the logging operation, it would only need try..catch to capture HystrixRuntimeException exception.

  • Note that the operation performed by the backup method. If you call another distributed service back-up services, we need to pay attention to annotate backup method @HystrixCommand packaging method.

4, to achieve bulkhead mode

  In the application of micro-based services usually you need to call multiple micro service to accomplish specific tasks, under the NA bulkhead mode, the default is to use the same number of calls to the calling thread, and these threads to handle request the entire Java container and set aside. Therefore, in the presence of a large number of requests, a service performance problem causes all threads in the Java container is occupied, while blocking the new request, the final container complete collapse.

  Hystrix using a thread pool to delegate all calls to remote services, this thread pool by default with 10 worker threads. But this is prone to run slow service occupy all threads, all hystrix provides a mechanism for an easy to use, create 'bulkhead' remote resources between different calls, calls to isolate different services to different thread pool, to make it independently of each other.

  To achieve isolation of the thread pool, just in @HystrixCommandthe comments added to the thread pool, where to have a ribbon, for example (Feign similar). OrganizaitonByRibbonService modified under the category licensingservice in the service package, the getOrganizationWithRibbonannotation methods to read as follows:

@HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
    }, fallbackMethod = "getOrganizationWithRibbonBackup",
            threadPoolKey = "licenseByOrgThreadPool",
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize", value = "30"),
                    @HystrixProperty(name = "maxQueueSize", value = "10")
            })

If the maxQueueSizeproperty value is set to -1, will be used SynchronousQueueto save all incoming requests, the number of requests will be forced synchronization queue request is being processed can never exceed the size of the thread pool. Is set to a value greater than 1 is to be used LinkedBlockingQueue.

  Note : Sample Code attribute values are hard-coded into the Hystrix annotation. In practice environments, the configuration items are generally configured in Spring Cloud Config is to facilitate unified management.

The use all the code: click to jump

Original released in Benpian: FleyX personal blog

Guess you like

Origin www.cnblogs.com/wuyoucao/p/11021265.html