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(); EnumerationheaderNames = 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
2.
HttpEntity httpEntity = HttpEntityUtil.getEntity(request,这里写请求参数);
String resultString = restTemplate.postForObject(url,httpEntity,String.class);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)