P70: Service downgrade failed -> successful

1 Service circuit breaker and downgrade

  • Service degradation is a way for a service to protect itself, or a way to protect downstream services. It is used to ensure that the service will not become unavailable due to a sudden increase in requests and that the service will not crash.

  • Although service degradation will cause request failure, it will not cause blocking.

2 questions

In the article saving service, add a code delay of 3 seconds to trigger service degradation (more than 2 seconds). The service downgrade code has been written and the article saving service can be called normally, but the service downgrade has never been triggered.

3 solutions

After repeated checks, the service downgrade code, configuration, bean scanning, and feign remote calling are all correct. Continue to look for the cause of the failure:

Problem 1: The front-end nginx was started as another project, causing the front-end to be unable to log in. This was discovered when the service was restarted later.

Question 2: saveArticle() has not been called to save the article. Debug found that the self-media news status is 0 (draft). Only when the status is 1 (to be reviewed) will the subsequent article saving code be called.

Question 3: After repeated testing, the article information was saved without triggering service downgrade. After restarting the service and running the program, the service downgrade was triggered again. After careful consideration, it was because the article service code was modified (delay 3s) without restarting, so the service downgrade was never triggered.

Note: Because it is a feign remote call, when used for debugging, the code in saveArticle() cannot be debugged.

4 Implementation code

①: Write downgrade logic in heima-leadnews-feign-api

package com.heima.apis.article.fallback;
import com.heima.apis.article.IArticleClient;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Component;
/**
 * feign失败配置
 * @author itheima
 */
@Component
public class IArticleClientFallback implements IArticleClient {
    @Override
    public ResponseResult saveArticle(ArticleDto dto)  {
        System.err.println("==============IArticleClient::saveArticle  触发服务降级===============");
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"获取数据失败");
    }
}

Add classes to self-media microservices and scan packages for downgraded code classes.

package com.heima.wemedia.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.heima.apis.article.fallback")
public class InitConfig {
}

②: Point to the downgrade code in the remote interface

package com.heima.apis.article;
import com.heima.apis.article.fallback.IArticleClientFallback;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@FeignClient(value = "leadnews-article",fallback = IArticleClientFallback.class)
public interface IArticleClient {
    @PostMapping("/api/v1/article/save")
    public ResponseResult saveArticle(@RequestBody ArticleDto dto);
}

③: The client enables downgrading heima-leadnews-wemedia

Add the following content in the nacos configuration center of wemedia to enable service degradation. You can also specify the timeout time for the service response.

feign:
  # 开启feign对hystrix熔断降级的支持
  hystrix:
    enabled: true
  # 修改调用超时时间
  client:
    config:
      default:
        connectTimeout: 2000
        readTimeout: 2000

④:Test

Add code to the saveArticle method in the ApArticleServiceImpl class

try {
    Thread.sleep(3000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

When conducting news review tests on the self-media side, service degradation may occur.

Guess you like

Origin blog.csdn.net/qq_1910444202/article/details/135030858