赞
踩
这里前提是你已经根据官网或者其他视频教程初步搭建了openFeign的环境,但是运行过程中踩坑到了一些问题汇总。我花了一晚上时间才终于成功解决。
// R<> 是我自定义的一个全局统一返回对象,
@Data
public class R<T> implements Serializable {
/** 错误码 */
private Integer code;
/** 错误消息 */
private String message;
/** 泛型响应数据 */
private T data;
/// 需要被调用的服务中心方法,by orange-authority @PostMapping("/token") public JwtToken token(@RequestBody UsernameAndPassword usernameAndPassword) throws Exception{ log.info("request to get token with param: [{}]", JSONUtil.toJsonStr(usernameAndPassword)); String token = orangeUserService.generateToken(usernameAndPassword.getUsername(), usernameAndPassword.getPassword(), true); return new JwtToken(token); } /// feign-client 中需要加入R<>, by orange-nacos-client @FeignClient( contextId = "AccountFeignClient", value = "orange-spring-service-account", fallbackFactory = AccountFeignClientFallbackFactory.class ) public interface AccountFeignClient { @GetMapping("/orange-spring-service-account/account/get-user-info-by-id") public R<UserInfo> getUserInfoById(@RequestParam Long id); }
以下是我个人理解,如有错误还望指出:
因为Feign是基于Ribbon, 至于为什么同时要设置hystrix and ribbon 的超时时间呢?因为ribbon重试机制, 当超出总时间上限 Hystrix熔断, 下面是我的配置信息,可以参考一下
# Feign 的相关配置 #ribbon ribbon: # ribbon 的超时时间 ConnectTimeout: 1000 ReadTimeout: 1000 # OkToRetryOnAllOperations: true # MaxAutoRetries: 1 # MaxAutoRetriesNextServer: 1 # 全局指定 hystrix 默认配置 hystrix: command: default: execution: isolation: thread: #断路器超时时间 timeoutInMilliseconds: 5000
@Slf4j @Configuration public class FeignConfig { @Bean public RequestInterceptor headerInterceptor() { return template -> { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); if (null != attributes) { HttpServletRequest request = attributes.getRequest(); Enumeration<String> headerNames = request.getHeaderNames(); if (null != headerNames) { while (headerNames.hasMoreElements()) { String name = headerNames.nextElement(); String values = request.getHeader(name); template.header(name, values); } } } }; } }
hystrix:
command:
default:
execution:
isolation:
# 线程池隔离策略
# strategy: THREAD
# 线程池信号量隔离策略
strategy: SEMAPHORE
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。