1.yml配置:
feign: ? hystrix: ? ? enabled: true # 开启熔断 ? client: ? ? config: ? ? ? default: ? #配置全局的feign的配置 如果有指定的服务配置 默认的配置不会生效 ? ? ? ? connectTimeout: 60000 # 指定的是消费者连接服务提供者的连接超时时间,单位是毫秒 ? ? ? ? readTimeout: 20000 ?# 指定的是调用服务提供者的服务的超时时间,单位是毫秒 ? ? ? ? requestInterceptors: # 指定的是feign 自定义拦截器的相对路径 ? ? ? ? ? - net.zlw.cloud.workflow.config.FeignConfigration ? ? ? ? loggerLevel: full # 配置日志等级 #hystrix 配置 hystrix: ? command: ? ? default: ? ? ? execution: ? ? ? ? timeout: ? ? ? ? ? #如果enabled设置为false,则请求超时交给ribbon控制 ? ? ? ? ? enabled: true ? ? ? ? isolation: ? ? ? ? ? strategy: SEMAPHORE # 隔离策略 ? ? ? ? ? thread: ? ? ? ? ? ? # 熔断器超时时间,默认:1000/毫秒 ? ? ? ? ? ? timeoutInMilliseconds: 20000
-------------------------------------------------------------------------------------------------------------------------------
重点:strategy: SEMAPHORE # 隔离策略
2.实现??RequestInterceptor
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
public class FeignConfigration implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
Enumeration<String> headerNames = request.getHeaderNames();
if (headerNames != null) {
while (headerNames.hasMoreElements()) {
String name = headerNames.nextElement();
String values = request.getHeader(name);
if (name.equals("hytoken")||name.equals("HYTOKEN")) {
template.header(name, values);
break;
}
}
}
}
}
3.FeignClient
@FeignClient(name = "em-api",configuration = FeignConfigration.class)
restTemplate调用 token?
1.工具类
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.util.MultiValueMap;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class HttpEntityUtil {
public static HttpEntity getEntity(HttpServletRequest request,MultiValueMap<Object,Object> param){
String hytoken = request.getHeader("hytoken");
HttpHeaders header = new HttpHeaders();
if (hytoken != null && !"".equals(hytoken)) {
header.put("hytoken", Collections.singletonList(hytoken));
} else {
List<String> cookies = new ArrayList<>();
Cookie[] cookie = request.getCookies();
for (int i = 0; i < cookie.length; i++) {
if (cookie[i].getName().equals("SESSION")) {
cookies.add(cookie[i].getName() + "=" + cookie[i].getValue());
break;
}
}
header.put(HttpHeaders.COOKIE, cookies);
}
HttpEntity httpEntity = new HttpEntity(param, header);
return httpEntity;
}
}
2.
HttpEntity httpEntity = HttpEntityUtil.getEntity(request,这里写请求参数);
String resultString = restTemplate.postForObject(url,httpEntity,String.class);
|