Java Hystrix请求缓存示例

Java Hystrix请求缓存示例,java,caching,fault-tolerance,hystrix,Java,Caching,Fault Tolerance,Hystrix,我试图弄清楚如何工作,但没有遵循他们在文档中提供的wiki或端到端示例 基本上,我有以下HystrixCommand子类: public class GetFizzCommand extends HystrixCommand<Fizz> { private Long id; private Map<Long,Fizz> fizzCache = new HashMap<Long,Fizz>(); void doExecute(Long

我试图弄清楚如何工作,但没有遵循他们在文档中提供的wiki或端到端示例

基本上,我有以下
HystrixCommand
子类:

public class GetFizzCommand extends HystrixCommand<Fizz> {
    private Long id;
    private Map<Long,Fizz> fizzCache = new HashMap<Long,Fizz>();

    void doExecute(Long id) {
        this.id = id;
        execute();
    }

    @Override
    public Fizz run() {
        return getFizzSomehow();
    }

    @Override
    public Fizz getFallback() {
        // Consult a cache somehow.
        // Perhaps something like a Map<Long,Fizz> where the 'id' is the key (?)
        // If the 'id' exists in the cache, return it. Otherwise, give up and return
        // NULL.
        fizzCache.get(id);
    }
}
public类GetFizzCommand扩展了HystrixCommand{
私人长id;
私有映射fizzCache=newhashmap();
void doExecute(长id){
this.id=id;
执行();
}
@凌驾
公共汽笛跑(){
返回getfizz();
}
@凌驾
公共Fizz getFallback(){
//以某种方式查阅缓存。
//也许有点像地图,其中“id”是关键(?)
//如果缓存中存在“id”,请将其返回。否则,请放弃并返回
//空。
fizzCache.get(id);
}
}
所以我觉得我在这里是违反规则的。我相信Hystrix提供了内置缓存,这一点可以从一个例子中得到证明,但我找不到任何有效的例子。如果已经提供了一些现成的东西,我不想在这里重新发明轮子,并将缓存构建到我的命令中


所以我问:Hystrix的请求缓存(确切地说)是什么样子的?如何将条目添加到缓存中?如何/何时刷新缓存?是否可配置(到期日、最大大小等)?

根据您链接的文档

通过在
HystrixCommand
对象上实现
getCacheKey()
方法来启用请求缓存

您尚未实现
getCacheKey()

这是(同样,根据文件)

通常,此上下文将通过包装用户请求或其他生命周期挂钩的
ServletFilter
进行初始化和关闭

然后我相信您不能更改这样的方法签名(
doExecute()
不是接口的一部分),而是将参数传递给您的命令构造函数,并请使用
@Override
注释
execute
,因此如果您忘记了,则会出现编译器错误,然后

HystrixRequestContext context = HystrixRequestContext.initializeContext();
GetFizzCommand commandA = new GetFizzCommand(2L);
GetFizzCommand commandB = new GetFizzCommand(2L);
Fizz a = commandA.execute(); // <-- should not be cached
Fizz b = commandB.execute(); // <-- should be cached.
HystrixRequestContext=HystrixRequestContext.initializeContext();
GetFizzCommand命令A=新的GetFizzCommand(2L);
GetFizzCommand commandB=新的GetFizzCommand(2L);

Fizz a=commandA.execute();// 您的控制器中需要一个HystrixRequestContext

//init
HystrixRequestContext context = HystrixRequestContext.initializeContext();
// get cache logic ...

//close
context.close();
更好的方法是添加一个过滤器类

import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import org.springframework.stereotype.Component;

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

@Component
@WebFilter(urlPatterns = "/*", asyncSupported = true)
public class HystrixRequestContextFilter implements Filter {
  @Override
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HystrixRequestContext context = HystrixRequestContext.initializeContext();
    try {
      filterChain.doFilter(servletRequest, servletResponse);
    } finally {
      context.close();
    }
  }
}

如果使用
@HystrixCommand
,可以在方法参数上使用
@com.netflix.hystrix.contrib.javanica.cache.annotation.CacheKey
,生成请求缓存密钥

@组件
公共类SpringBootMockRemoteService{
@HystrixCommand(fallbackMethod=“fallback”)
公共字符串getRemoteValue(@CacheKey字符串用户名)引发中断异常{
线程。睡眠(3000);
返回“成功”;
}
字符串回退(){
返回“回退”;
}
}

请求缓存键将是参数
username

的值,您可以读取HystrixCommand的实现。一个起点是方法(链接到grepcode.com)。
//init
HystrixRequestContext context = HystrixRequestContext.initializeContext();
// get cache logic ...

//close
context.close();
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import org.springframework.stereotype.Component;

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

@Component
@WebFilter(urlPatterns = "/*", asyncSupported = true)
public class HystrixRequestContextFilter implements Filter {
  @Override
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HystrixRequestContext context = HystrixRequestContext.initializeContext();
    try {
      filterChain.doFilter(servletRequest, servletResponse);
    } finally {
      context.close();
    }
  }
}