I recently wrote a small project using a microservices architecture, including OpenFeign and Hystrix. In the use of the process can be said to be frequent problems, and encountered problems are not easy to solve.

  1. The new version of Hystrix doesn’t work

The first is that fallback doesn’t work while using OpenFeign. After much thought and a lot of research, most of it says OpenFeign doesn’t turn on Hystrix by default. You need to set up

feign:
  hystrix:
    enabled: true
Copy the code

But after setting it, it still doesn’t work. Last but not least, the openFeign version changed. I used the latest 3.0.3 version and then went back to 2.2.6. Without poking fun at SpringCloud’s downward compatibility issues, there are libraries that can be moved in or out of a new release. In this way, you can only be familiar with a version of SpringCloud at a certain stage, and then use it for a long time. Even if there are bugs or serious defects, it is difficult to easily update to a new version. One is the high cost of learning, and the other is how to upgrade old projects. .net Core, by contrast, is much better. Add token headers to openFeign requests. This is relatively simple

@Configuration
public class FeignConfig implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        var token = getToken();
        requestTemplate.header("X-Token",getToken());
    }
    private String getToken(a){

        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if(servletRequestAttributes ! =null) {
            HttpServletRequest request = servletRequestAttributes.getRequest();
            if(request ! =null) {
                return request.getHeader("X-Token"); }}return ""; }}Copy the code

But in the actual use of the process found the fourth problem. Can’t get it 3. RequestContextHolder. GetRequestAttributes (); Common instructions for empty networks are to add a RequestContextListner configuration

 @Bean
    public RequestContextListener requestContextListener(a){
        return new RequestContextListener();
    }
Copy the code

But it didn’t work, and adding to it didn’t work. Finally, somewhere in the corner, a problem with Hystrix’s quarantine policy turned on.

hystrix:
  command:
    default:
      execution:
        isolation:
          strategy: SEMAPHORE
Copy the code

Relatively simple to solve the problem, then the solutions are not official are recommended, the official recommendation custom concurrency strategy, the need to write a class, let its inheritance HystrixConcurrencyStrategy class, rewrite wrapCallable method, for the sake of time, the project first run, Then try something else.

Refer to the article: blog.csdn.net/amosjob/art…