hystrix 的相关代码学习2
parent
a29e4e7b11
commit
0d79252d6a
@ -0,0 +1,75 @@
|
||||
package org.example.hystrix;
|
||||
|
||||
import com.netflix.hystrix.HystrixCommand;
|
||||
import com.netflix.hystrix.HystrixCommandGroupKey;
|
||||
import com.netflix.hystrix.HystrixCommandKey;
|
||||
import com.netflix.hystrix.HystrixRequestCache;
|
||||
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategyDefault;
|
||||
import java.util.List;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.example.service.NacosClientService4HystrixDemo;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* <h1>带有缓存功能的 Hystrix</h1>
|
||||
* */
|
||||
@Slf4j
|
||||
public class CacheHystrixCommand extends HystrixCommand<List<ServiceInstance>> {
|
||||
|
||||
/** 需要保护的服务 */
|
||||
private final NacosClientService4HystrixDemo nacosClientService;
|
||||
|
||||
/** 方法需要传递的参数 */
|
||||
private final String serviceId;
|
||||
|
||||
private static final HystrixCommandKey CACHED_KEY =
|
||||
HystrixCommandKey.Factory.asKey("CacheHystrixCommand");
|
||||
|
||||
public CacheHystrixCommand(NacosClientService4HystrixDemo nacosClientService, String serviceId) {
|
||||
|
||||
super(
|
||||
HystrixCommand.Setter
|
||||
.withGroupKey(HystrixCommandGroupKey
|
||||
.Factory.asKey("CacheHystrixCommandGroup"))
|
||||
.andCommandKey(CACHED_KEY)
|
||||
);
|
||||
|
||||
this.nacosClientService = nacosClientService;
|
||||
this.serviceId = serviceId;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<ServiceInstance> run() throws Exception {
|
||||
|
||||
log.info("CacheHystrixCommand In Hystrix Command to get service instance:" +
|
||||
" [{}], [{}]", this.serviceId, Thread.currentThread().getName());
|
||||
return this.nacosClientService.getNacosClientInfo(this.serviceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getCacheKey() {
|
||||
// fixme 可以进行自定义, 只要保证唯一的请求返回唯一的值即可
|
||||
return serviceId;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<ServiceInstance> getFallback() {
|
||||
// todo 这里可以自己添加一些 warning 日志来进行表示
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* <h2>根据缓存 key 清理在一次 Hystrix 请求上下文中的缓存</h2>
|
||||
* */
|
||||
public static void flushRequestCache(String serviceId) {
|
||||
|
||||
HystrixRequestCache.getInstance(
|
||||
CACHED_KEY,
|
||||
HystrixConcurrencyStrategyDefault.getInstance()
|
||||
).clear(serviceId);
|
||||
log.info("flush request cache in hystrix command: [{}], [{}]",
|
||||
serviceId, Thread.currentThread().getName());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue