1.feign 调用超时控制逻辑
openfign 中配置默认的链接时间是 10S 读取时间是60S
可以在YML文件中修改配置
spring: cloud:nacos:server-addr: 127.0.0.1:8848config:namespace: 51d656b5-cfe8-4cd0-95ad-91811cd88dc6#openfeign 相关配置openfeign:client:config:
# 默认配置 (若没有配置指定服务则按默认服务走 )default:connect-timeout: 3000read-timeout: 3000
(配置了产品服务 则调用产品服务时 按照这个配置信息走 "product"为服务名 )product:connect-timeout: 6000read-timeout: 6000
其他可配置参数
public static class FeignClientConfiguration {private Logger.Level loggerLevel;private Integer connectTimeout;private Integer readTimeout;private Class<Retryer> retryer;private Class<ErrorDecoder> errorDecoder;private List<Class<RequestInterceptor>> requestInterceptors;private Class<ResponseInterceptor> responseInterceptor;private Map<String, Collection<String>> defaultRequestHeaders = new HashMap();private Map<String, Collection<String>> defaultQueryParameters = new HashMap();private Boolean dismiss404;private Class<Decoder> decoder;private Class<Encoder> encoder;private Class<Contract> contract;private ExceptionPropagationPolicy exceptionPropagationPolicy;private List<Class<Capability>> capabilities;private Class<QueryMapEncoder> queryMapEncoder;private MicrometerProperties micrometer;private Boolean followRedirects;private String url;
2.feign 重试调用机制
@Configuration
public class OrderConfig {@BeanRetryer retryer(){
// 选用openfeign 默认重试机制return new Retryer.Default(2L,3L,8);}
}
若 Default 方法中没有参数 则用底层的默认参数,若设置了参数 则根据设置参数来执行
public Default(long period, long maxPeriod, int maxAttempts) {this.period = period;this.maxPeriod = maxPeriod;//重试次数this.maxAttempts = maxAttempts;this.attempt = 1;}
3.openfeign拦截器
响应拦截器用的不多 只展示请求拦截器用法
建立拦截器文件
package order.intercaptor;import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;import java.util.UUID;// 需继承 openFeign 中 的请求 拦截器 RequestInterceptor
@Component
public class XTokenRequestInterceptor implements RequestInterceptor {@Overridepublic void apply(RequestTemplate requestTemplate) {// 增加请求头 信息requestTemplate.header("X-TOKEN", UUID.randomUUID().toString().substring(0,10));// 增加请求体 信息//requestTemplate.body();}
}
此时 调用方 可以在请求头中获得 X-TOKEN 信息
如调的Product服务 将请求头的信息进行打印
@RestController
@RequestMapping("/product")
public class ProductController {@Autowiredprivate ProductService productService;@GetMapping("/getProduct/{id}")public String getProduct(@PathVariable("id") Integer id, HttpServletRequest request){String header = request.getHeader("X-TOKEN");System.out.println("调用产品服务,hander信息:"+header);String result = productService.getProduct();return result ;}}
控制台打印
4.结合 sentinel 进行兜底回调
在发生意外返回时,进行的一种操作
(1)在对应的服务中加入依赖
<!-- sentinel--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency>
(2)YML 文件中 开启 feign 和 sentinel的兜底
feign:sentinel:enabled: true
(3)@FeignClient注解中配置回调文件
@FeignClient(value = "product",fallback = ProductFeignFallBack.class)
public interface ProductFeign {@GetMapping("/product/getProduct/{id}")String getProduct(@RequestParam("id") Integer id);
}
fallback = ProductFeignFallBack.class 为指定的回调文件
(4)回调文件实现 Feign文件 ,并实现兜底方法 ;(如返回默认信息,或报错提示)
需交给容器管理 使用 @Component注解
package order.feign;import org.springframework.stereotype.Component;@Component
public class ProductFeignFallBack implements ProductFeign{@Overridepublic String getProduct(Integer id) {System.out.println("兜底接口");return "兜底接口";}
}