package com.gavin.service.impl;
import com.gavin.config.UserTimeoutCommand;
import com.gavin.huawei.CommonResp;
import com.gavin.huawei.ResultMessage;
import com.gavin.service.ProductService;
import com.google.common.net.HttpHeaders;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheKey;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheRemove;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult;
import com.netflix.hystrix.exception.HystrixBadRequestException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import sun.net.www.http.HttpClient;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
* @author Gavin
*/
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private RestTemplate restTemplate;
//HystrixCommand??? ????÷??????Hystrix??????,?????????????????õ????
@HystrixCommand(fallbackMethod = "fallback1")
@Override
public ResultMessage timeOut() {
//注解的形式
String url = "http://USER/user/timeout";
return restTemplate.getForObject(url, ResultMessage.class);
}
@Override
@CacheResult(cacheKeyMethod = "getMsg")//
@HystrixCommand(commandKey = "key1", fallbackMethod = "fallback2")
public ResultMessage exception(String msg) {
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("msg", msg);
String url = "http://USER/user/exc/{msg}";
return restTemplate.getForObject(url, ResultMessage.class, paramMap);
}
public String getMsg(String msg) {
return msg;
}
/**
* @CatchResult/@CatchKey优先级高于cacheKeyMethod
* @param msg
*/
@Override
@CacheRemove(commandKey = "key1", cacheKeyMethod = "getMsg")//将请求结果删除
@HystrixCommand
public void flushCacheKey(String msg) {
System.out.println("flush success");
}
/**
*
* CacheResult 缓存结果 CacheKey 缓存参数 msg
* 对于熔断器,当抛出HystrixBadRequestException时不会出发getfallback,而是会抛出异常
* ignoreExceptions --忽略某些异常
* @CacheKey用于参数上,可标记缓存的键,若没有标注,则使用所有参数
* @param msg
* @return
*/
@Override
@CacheResult(cacheKeyMethod = "") //缓存关键字,对于本次请求来讲 ,cacheKeyMethod指定缓存key的生成方法;
@HystrixCommand(commandKey = "key2",fallbackMethod = "fallback3",ignoreExceptions = HystrixBadRequestException.class) //
public ResultMessage exception2(@CacheKey("msg") String msg) {
String url = "http://USER/user/exc2";
Map<String, Object> paramMap = new HashMap();
paramMap.put("msg", msg);
System.out.println(msg);
return restTemplate.postForObject(url, paramMap, ResultMessage.class);
}
//代码的形式实现熔断--实现HystrixCommand<ResultMessage>
/**
*
* @return
*/
@Override
public ResultMessage timeout2() {
HystrixCommandGroupKey userGroup = HystrixCommandGroupKey.Factory.asKey("userGroup");
com.netflix.hystrix.HystrixCommand.Setter setter= com.netflix.hystrix.HystrixCommand.Setter.withGroupKey(userGroup);
UserTimeoutCommand userTimeoutCommand = new UserTimeoutCommand(setter, restTemplate);
return userTimeoutCommand.execute();
// 异步执行
// Future<ResultMessage> queue = userTimeoutCommand.queue();
// try{
// return queue.get();
// }catch (Exception ex){
// return userTimeoutCommand.getFallback()_;
// }
}
public ResultMessage fallback1() {
return new ResultMessage(false, "failed");
}
public ResultMessage fallback2(String msg) {
return new ResultMessage(false, "failed" + msg);
}
public ResultMessage fallback3(String msg, Throwable ex) {
ex.printStackTrace();
return new ResultMessage(false, "post failed" + msg);
}
}