spring-cloud-feign实战笔记
创始人
2024-06-02 06:34:44

feign 配置

  1. 针对单个feign接口进行配置
    feign:client:config:# feignName 注意这里与contextId一致,不能写成name(FeignClientFactoryBean#configureFeign)# 不能写成 client-b (微服务名称),否则不生效helloFeignClient: # contextIdconnectTimeout: 50000 # 连接超时时间readTimeout: 50000 # 读超时时间loggerLevel: full #配置Feign的日志级别#default:# 其他默认配置
    
  2. feign 全局默认配置
    feign:client:config:default:connectTimeout: 50000 # 连接超时时间readTimeout: 50000 # 读超时时间loggerLevel: full #配置Feign的日志级别
    
  3. feign开启gzip支持
    feign:compression:request:enabled: truemime-types: "text/xml, application/xml, application/json"min-request-size: 2048response:enabled: true # 配置相应GZIP压缩
    

开启gzip支持后接口调用处理方式一

  1. feign接口使用ResponseEntity接收数据
    @FeignClient(contextId = "testFeignClient", name = "client-a", configuration = FeignConfig.class)
    public interface TestFeignClient {@GetMapping(value = "/userInfo")ResponseEntity userInfoCompress(@RequestParam("username") String username, @RequestParam("address") String address) ;
    }
    
  2. 编写单元测试(注意需要对byte[] 数组使用Gzip解压)
    @Slf4j
    public class TestFeignClientTest extends BaseJunitTest {@Autowiredprivate TestFeignClient testFeignClient ;@Testpublic void userInfoCompress() throws IOException {String username = "张三" ;String address = "北京" ;ResponseEntity responseEntity = testFeignClient.userInfoCompress(username, address);byte[] compressed = responseEntity.getBody();String decompressValue = GzipUtils.decompress(compressed);log.info("value : {}", decompressValue);}
    }
    
  3. 编写gzip解压缩工具类
    public final class GzipUtils {public static String decompress(byte [] compressed) throws IOException {final StringBuilder output = new StringBuilder() ;try(GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(compressed));BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(gis, StandardCharsets.UTF_8))){String line ;while ((line = bufferedReader.readLine()) != null){output.append(line) ;}return output.toString() ;}}
    }
    

开启gzip支持后接口调用处理方式二

  1. 编写Decoder (内部使用方式一的Gzip解压缩工具类GzipUtils)
    public class FeignResponseDecoder implements Decoder {private final Decoder delegate;public FeignResponseDecoder(Decoder delegate) {Objects.requireNonNull(delegate, "Decoder must not be null. ");this.delegate = delegate;}@Overridepublic Object decode(Response response, Type type) throws IOException {Collection values = response.headers().get(HttpEncoding.CONTENT_ENCODING_HEADER);if (Objects.nonNull(values) && !values.isEmpty() && values.contains(HttpEncoding.GZIP_ENCODING)) {byte[] compressed = Util.toByteArray(response.body().asInputStream());if ((compressed == null) || (compressed.length == 0)) {return delegate.decode(response, type);}//decompression part//after decompress we are delegating the decompressed response to default//decoderif (isCompressed(compressed)) {String decompressValue = GzipUtils.decompress(compressed);Response decompressedResponse = response.toBuilder().body(decompressValue.getBytes()).build();return delegate.decode(decompressedResponse, type);} else {return delegate.decode(response, type);}} else {return delegate.decode(response, type);}}private static boolean isCompressed(final byte[] compressed) {return (compressed[0] == (byte) (GZIPInputStream.GZIP_MAGIC)) && (compressed[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8));}
    }
    
  2. 将Decoder加入到Spring容器管理
    @Configuration
    public class AppConfig{@Beanpublic Decoder GZIPResponseDecoder(ObjectFactory messageConverters) {Decoder decoder = new FeignResponseDecoder(new SpringDecoder(messageConverters));return decoder;}
    }
    
  3. feign接口使用普通java对象接收数据
    @FeignClient(contextId = "testFeignClient", name = "client-a", configuration = FeignConfig.class)
    public interface TestFeignClient {@GetMapping("/userInfo")UserInfoVO userInfo(@RequestParam("username") String username, @RequestParam("address") String address) ;
    }
    
  4. 编写单元测试
    @Slf4j
    public class TestFeignClientTest extends BaseJunitTest {@Autowiredprivate TestFeignClient testFeignClient ;@Testpublic void userInfo(){String username = "张三" ;String address = "北京" ;UserInfoVO userInfo = testFeignClient.userInfo(username, address);log.info("user info : {}", userInfo);}
    }
    

其他知识点补充

  1. SpringBoot服务提供者开启gizp压缩
    server:port: 7070compression:enabled: true
    

相关内容

热门资讯

关爱“一老一小”推动人口高质量... 本报讯(记者 徐顺凯)“一老一小”是千家万户的牵挂,也是民生健康保障的重点。记者从省卫生健康委了解到...
10招搞好你的英语自我介绍 英...  以下10个句子可以帮助你更好地在面试中做自我介绍。选择最适合你的那些。  1.“can adapt...
面试:如何让你的英文自我介绍更...  第1招:立即抓住眼球  “I can summarize who I am in three wo...
最新或2023(历届)公务员面...  作为公考面试的自我认知类常见考题,假如公务员面试中出现有“考生自我介绍”这一题,那通常会出现在面试...
职场面试:自我介绍不是编故事 ...  编造个人辉煌履历  应届毕业生韩佳突然接到了吉龙型材厂的面试通知,他赶紧匆匆赶去面试。面试官看了看...