SpringCloud (6) --- blown downgrade understand, Hystrix real SpringCloud (5) --- Feign service call

 

First, the concept

   1. Why do we need to downgrade fuse

( 1) demand background

   It is the system load is too high, abnormal conditions or traffic bursts introduced networks, common solution.

   In a distributed system, a service depends on more than one service, there may be a service call fails, such as overtime, abnormal, how can we ensure that in the case of a dependency problem, and will not lead to overall service failure.

   For example: a micro-services business logic complexity, in the case of a timeout under heavy load conditions.

  Internal conditions: The program bug cause an infinite loop, there is a slow query, the program logic does not lead to run out of memory

  External conditions: hacker attacks, promotions, third-party systems respond slowly.

(2) Solutions

   The core idea of ​​the failure to solve the interface-level priority is to protect the core business and give priority to the vast majority of users. Such as login function is very important, when traffic is too high, turning off the registration function, login to free up resources.

(3) resolution strategy

  Fuse, demotion, current limiting, queuing.

    2. What is the fuse

     General is a service fault, or is caused by abnormal, similar to the real world 'fuse', when an abnormal condition is triggered, directly fuse the entire service rather than service have been waiting for this time out, in order to prevent to prevent failure of the entire system.

The use of a number of protective measures. Overload protection. A service functions such as X-dependent B services in an interface, when the response is very slow service interface B, X A service function response will be slowed down, resulting in a further thread A services are stuck in the X function

Other features on, A service will slow down or master card. At this point we need fusing mechanism, i.e., A B service is not requested this interface, but may downgrade directly.

   3. What is downgraded

      Server when pressure surge, according to the current business situation and flow of some services and strategically pages of downgraded. In order to ease pressure on the server resources to ensure the normal operation of the core business, while maintaining the customer and

Get the correct corresponding most customers.

Automatically downgrade : Timeout, the number of failures, the fault current limiter

 (1) configured timeout (asynchronous mechanism to detect reply circumstances);

 (2) api instability of the number of calls to a certain number downgrade (asynchronous mechanism to detect cases reply);

 (3) calls a remote service failure (dns, http service error status code, network failure, Rpc service exception), direct downgrade.

Artificial downgrade : spike, double XI promote downgrade non-essential services.

  4, fuse and downgrade the similarities and differences

The same point :

   1) From the availability and reliability of the trigger, in order to prevent system crashes

   2) allow end-users to experience some of the features that temporarily can not be used

difference:

    1) Service fuse failure is generally caused by downstream services, service degradation and system load is generally considered as a whole, is controlled by the caller

   2) trigger different reasons explained above, the color of the font

   5, fuse to explain the process of downgrade

Hystrix provides several key parameters as follows to configure a fuse:

circuitBreaker.requestVolumeThreshold // sliding window size, default is 20 
circuitBreaker.sleepWindowInMilliseconds // too long, the fuse is detected again whether to open, the default is 5000, that is, 5s bell 
circuitBreaker.errorThresholdPercentage // error rate, the default 50%

Three parameters together, the meaning of the expression is:

    Whenever 20 requests, 50% have failed, the fuse will open, this time calling this service, will return to direct failure, no adjusting remote service. 5s until after the bell, re-testing of the trigger conditions, to determine whether the fuse is closed, or continue to open.

This is inside a very critical point, after reaching the fuse, then go behind it directly tune the micro service. Since then the abnormal service or not to adjust the tone micro occur when this happens is not possible directly to the first error information to the user, so for fusing

We can consider downgrading strategy. The so-called demotion, when a service is blown, the server will no longer be called, this time a client can prepare their own local fallback callback returns a default value. 

In doing so, although the reduction in the level, but whatever the outcome is available, stronger than hang directly, of course, this also depends on appropriate business scenarios.

 

Two, Hystrix real

 

      Use the components include: Eureka, Feign includes the following three items:

 

    (1) Eureka-server: 7001 Registration Authority

 

    (2) product-server: 8001 Goods Micro Services

 

    (3) order-server: 9001 Order microService

 

Registry, micro goods and services, have been set up before the blog, written not repeat here. Here only write micro-order-server service. Specific fancy blog entry: SpringCloud (5) --- Feign service call

    1, pom.xml

        <!--hystrix依赖,主要是用  @HystrixCommand -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

   2, application.yml

Copy the code
Server: 
  Port: 9001 

# designated registration center address 
Eureka: 
  Client: 
    serviceUrl: 
      defaultzone: HTTP: // localhost: 7001 / Eureka / 

# service name 
the Spring: 
  the Application: 
    name: the Order-Service 
    
# open feign support hystrix (Note that certain to turn the old version default support, the new version off by default) 
# # modified to call timeout (default is 1 second, even if the timeout) 
Feign: 
  hystrix: 
    Enabled: to true 
  Client: 
    config: 
      default: 
        connectTimeout: 2000 
        ReadTimeout: 2000
Copy the code

 3, SpringBoot startup class

Copy the code
@SpringBootApplication
@EnableFeignClients
//添加熔断降级注解
@EnableCircuitBreaker
public class OrderApplication {

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

}
Copy the code

   4、ProductClient

Copy the code
/ ** 
 * Goods Services client 
 * name = "product-service" is the name of the server you are calling 
 * fallback = ProductClientFallback.class, followed by a downgrade your custom class, the class must implement downgrade ProductClient 
 * / 
@FeignClient ( = name "Product--Service", fallback = ProductClientFallback.class) 
public interface ProductClient { 

    // this is equivalent to a combination of HTTP: // Product-Service-/ API / V1 / Product / Find 
    @GetMapping ( "/ API / V1 / Product / Find ") 
    String the findById (@RequestParam (value =" ID ") int ID); 

}
Copy the code

    5, ProductClientFallback class downgraded

Copy the code
/ ** 
 * for goods and services, was wrong to downgrade 
 * / 
@Component 
public class ProductClientFallback the implements ProductClient { 

    @Override 
    public String findById (int the above mentioned id) { 

        System.out.println ( "ProductClientFallback in downscaling methods"); 

        // this the interface logic gai some downgrade ........ 
        return null; 
    } 
}
Copy the code

     6、OrderController类

    Note : Parameter Type saveOrderFail method fallbackMethod = "saveOrderFail" in the number, and save exactly to order, otherwise they will not find saveOrderFail reported method.

Copy the code
@RestController 
@RequestMapping ( "API / V1 / Order") 
public class OrderController { 

    @Autowired 
    Private ProductOrderService productOrderService; 

    @RequestMapping ( "Save") 
    // when calling microcells will be downgraded to saveOrderFail abnormal process 
    @HystrixCommand (fallbackMethod = " saveOrderFail ") 
    public Object Save (@RequestParam (" user_id ") int the userId, @RequestParam (" the product_id ") int the productId) { 

        return productOrderService.save (the userId, the productId); 
    } 

    // noted that the method and api signatures must be the method is consistent 
    Private Object saveOrderFail (the userId int, int the productId) { 

        System.out.println ( "Controller degradation in method"); 

        the Map <String, Object> = new new MSG the HashMap <> ();
        msg.put("code", -1);
        msg.put ( "msg", "number of people buying too much, you squeezed out, wait a retry"); 
        return msg; 
    } 
}
Copy the code

      7, test

(1) normal

First order service (order) and commercial services (product) started simultaneously, as shown:

Orders normal service calls Goods

(2) exceptions

I will now Goods Micro service stopped: only start micro-service orders, when to call a timeout of goods and services, of course there will be exceptions.

In tune interface method has been successfully downgraded to find in

In order to see implemented in controller and method of degradation degraded ProductClientFallback methods, their order is not fixed, it is possible to perform the method demoting controller, the method may also be performed first ProductClientFallback degraded.

Which thread depends on the specific implementation of the right to obtain cpu.

 

Third, the combination of analog fuse redis downgrade abnormal alarm notification combat

The main treatment is to improve and perfect service fuse, alarm system redis perfect combination to simulate a single SMS to inform the user fails.

    1, pom.xml

    <!--springboot整合redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

    2, application.yml

Copy the code
#服务的名称
#redis
spring:
  application:
    name: order-service
  redis:
    database: 0
    host: 127.0.0.1
    port: 6379
    timeout: 2000
Copy the code

   3、OrderController类

主要看降级方法的不同

Copy the code
@RestController
@RequestMapping("api/v1/order")
public class OrderController {

    @Autowired
    private ProductOrderService productOrderService;

    //添加bean
    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping("save")
    //当调用微服务出现异常会降级到saveOrderFail方法中
    @HystrixCommand(fallbackMethod = "saveOrderFail")
    public Object save(@RequestParam("user_id")int userId, @RequestParam("product_id") int productId,HttpServletRequest request){

        return productOrderService.save(userId, productId);
    }

    //注意,方法签名一定要要和api方法一致
    private Object saveOrderFail(int userId, int productId, HttpServletRequest request){
        
        //监控报警
        String saveOrderKye = "save-order";
        //有数据代表20秒内已经发过
        String sendValue = redisTemplate.opsForValue().get(saveOrderKye);
        final String ip = request.getRemoteAddr();

        //新启动一个线程进行业务逻辑处理
        new Thread( ()->{
            if (StringUtils.isBlank(sendValue)) {
                System.out.println("紧急短信,用户下单失败,请离开查找原因,ip地址是="+ip);
                //发送一个http请求,调用短信服务 TODO
                redisTemplate.opsForValue().set(saveOrderKye, "save-order-fail", 20, TimeUnit.SECONDS);

            }else{
                System.out.println("已经发送过短信,20秒内不重复发送");
            }
        }).start();

        The Map <String, Object> msg = new new HashMap <> (); 
        msg.put ( "code", -1); 
        msg.put ( "msg", "number of people buying too much, you squeezed out, wait a heavy test "); 
        return MSG; 
    } 

}
Copy the code

     4, the test

    When the 20 seconds of continuous retransmission request already sent a message to remind.

 The official document: https: //github.com/Netflix/Hystrix/wiki/Configuration#execution.isolation.strategy

 

 I only occasionally to quiet down, past all the wondering about it. Those who have in the old days there have been naive, even stupid, not worth condemnation. After all, the next day, still very long. Continue to encourage their own,

 At daybreak, but also a new starting point, but also the unknown journey (Col. 8)

 

Reprinted to: https://www.cnblogs.com/qdhxhz/p/9581440.html

Guess you like

Origin www.cnblogs.com/wllcs/p/12001894.html