Hystrix的缓存

Scroll Down

断路器过滤器配置

import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;

import javax.servlet.*;
import java.io.IOException;

/**
 * @author 王守钰
 * @program quan
 * @date 2020年05月15日 13:12
 * @description: 断路器过滤器
 */
public class HystrixFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        HystrixRequestContext hystrixRequestContext = HystrixRequestContext.initializeContext();
        try {
            chain.doFilter(request, response);
        }catch (Exception e){

        }finally {
            hystrixRequestContext.shutdown();
        }
    }

    @Override
    public void destroy() {

    }
}

开启断路器拦截器配置

import com.wangshouyu.college.hzjxy.filter.HystrixFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 王守钰
 * @program quan
 * @date 2020年05月19日 12:44
 * @description: 熔断器配置
 */
@Configuration
public class HystrixConfig {

    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new HystrixFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }
}

缓存Command

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 王守钰
 * @program quan
 * @date 2020年05月22日 09:33
 * @description:
 */
@Slf4j
public class MyHystrixCommand extends HystrixCommand<String> {

    private String id;

    protected MyHystrixCommand(String id) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("HelloWorldGroup"))
                .andCommandKey(HystrixCommandKey.Factory.asKey("HelloWorld")));
        this.id = id;
    }

    @Override
    protected String run() throws Exception {
        log.info("say hello no cache");
        return "HelloWorld";
    }

    @Override
    protected String getCacheKey() {
        return "hello" + this.id;
    }
}

测试控制器

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 王守钰
 * @program quan-college
 * @date 2020年05月19日 18:20
 * @description: 熔断控制器
 */
@Slf4j
@RestController
@RequestMapping(value = "/hystrix")
public class HystrixController {

    @GetMapping(value = "/hello/{id}")
    public String sayHello(@PathVariable("id") String id ){
        MyHystrixCommand myHystrixCommand = new MyHystrixCommand(id);
        String result = myHystrixCommand.execute();
        log.info("from cache:{}", myHystrixCommand.isResponseFromCache());
        MyHystrixCommand myHystrixCommand1 = new MyHystrixCommand(id);
        String result1 = myHystrixCommand1.execute();
        log.info("from cache:{}", myHystrixCommand1.isResponseFromCache());
        log.info("result equals result1 : {}", result.equals(result1));
        return result1;
    }
}

执行结果

2020-05-22 09:53:27.522  INFO 17817 --- [lloWorldGroup-1] c.w.college.hystrix.MyHystrixCommand     : say hello no cache
2020-05-22 09:53:27.527  INFO 17817 --- [nio-8080-exec-1] c.w.college.hystrix.HystrixController    : from cache:false
2020-05-22 09:53:27.531  INFO 17817 --- [nio-8080-exec-1] c.w.college.hystrix.HystrixController    : from cache:true
2020-05-22 09:53:27.532  INFO 17817 --- [nio-8080-exec-1] c.w.college.hystrix.HystrixController    : result equals result1 : true

从上面我们可以看出第二次的请求并没有打印出no cache缓存测试也就生效拉。