Springcloud OpenFeign 的实现(二)

Springcloud OpenFeign 的实现(一)

一、Feign request/response 压缩

您可以考虑为您的外部请求启用请求或响应GZIP压缩。您可以通过启用以下属性之一来完成此操作:

复制代码
feign.compression.request.enabled=true
feign.compression.response.enabled=true

Feign 请求压缩给你的设置与你可能为你的Web服务器设置的类似:

复制代码
spring.cloud.openfeign.compression.request.enabled=true
spring.cloud.openfeign.compression.request.mime-types=text/xml,application/xml,application/json
spring.cloud.openfeign.compression.request.min-request-size=2048

这些属性允许您选择压缩介质类型和最小请求阈值长度。

对于OkHttpClient之外的http客户端,可以启用默认gzip解码器以UTF-8编码解码gzip响应:

复制代码
feign.compression.response.enabled=true
feign.compression.response.useGzipDecoder=true

二、Spring @MatrixVariable 的支持

Spring Cloud OpenFeign支持Spring@MatrixVariable注释。

如果将映射作为方法参数传递,则通过使用=连接映射中的键值对来创建@MatrixVariable路径段。

如果传递了不同的对象,则使用=将@MatrixVariable注释中提供的名称(如果已定义)或带注释的变量名称与提供的方法参数联接。

重要信息:

即使在服务器端,Spring也不要求用户将路径段占位符命名为与矩阵变量名称相同的名称,因为在客户端它将过于模糊,Spring Cloud OpenFeign要求您添加一个路径段占位符,其名称匹配@MatrixVariable注释(如果定义)中提供的名称或带注释的变量名称。

例如:

复制代码
@GetMapping("/objects/links/{matrixVars}")
Map<String, List<String>> getObjects(@MatrixVariable Map<String, List<String>> matrixVars);

请注意,变量名和路径段占位符都称为矩阵变量。

复制代码
@FeignClient("demo")
public interface DemoTemplate {

    @GetMapping(path = "/stores")
    CollectionModel<Store> getStores();
}

三、Feign Spring Cloud CircuitBreaker 的支持

如果Spring Cloud CircuitBreaker位于类路径上,并且feign.CircuitBreaker.enabled=true,则feign将用断路器包装所有方法。

要在每个客户端的基础上禁用Spring Cloud CircuitBreaker支持,请创建一个香草外观。具有"原型"范围的建造商,例如:

复制代码
@Configuration
public class FooConfiguration {
        @Bean
    @Scope("prototype")
    public Feign.Builder feignBuilder() {
        return Feign.builder();
    }
}

断路器名称遵循这种模式<feignClientName>_<calledMethod>。当调用名为foo的@FeignClient且调用的接口方法为bar时,断路器名称将为foo_bar。

四、在配置文件中配置CircuitBreaker

你可以在application.yml中配置属性来配置 CircuitBreaker。

例如,如果你有这个 Feign 客户端

复制代码
@FeignClient(url = "http://localhost:8080")
public interface TestClient {

    @GetMapping("Test")
    String getTest();
}

你可以通过以下方式使用配置属性来配置它

复制代码
spring:
  cloud:
    openfeign:
      circuitbreaker:
        enabled: true
        alphanumeric-ids:
          enabled: true
resilience4j:
  circuitbreaker:
    instances:
      DemoClientgetDemo:
        minimumNumberOfCalls: 60
  timelimiter:
    instances:
      DemoClientgetDemo:
        timeoutDuration: 10s

五、Feign Spring Cloud CircuitBreaker Fallback

Spring Cloud CircuitBreaker支持回退的概念:当电路开路或出现错误时执行的默认代码路径。要为给定的@FeignClient启用回退,请将回退属性设置为实现回退的类名。您还需要将实现声明为SpringBean。

复制代码
@FeignClient(name = "test", url = "http://localhost:${server.port}/", fallback = Fallback.class)
protected interface TestClient {

    @RequestMapping(method = RequestMethod.GET, value = "/hello")
    Hello getHello();

    @RequestMapping(method = RequestMethod.GET, value = "/hellonotfound")
    String getException();

}

@Component
static class Fallback implements TestClient {

    @Override
    public Hello getHello() {
        throw new NoFallbackAvailableException("Boom!", new RuntimeException());
    }

    @Override
    public String getException() {
        return "Fixed response";
    }

}

如果需要访问导致回退触发器的原因,可以在@FeignClient中使用fallbackFactory属性。

复制代码
@FeignClient(name = "testClientWithFactory", url = "http://localhost:${server.port}/",
        fallbackFactory = TestFallbackFactory.class)
protected interface TestClientWithFactory {

    @RequestMapping(method = RequestMethod.GET, value = "/hello")
    Hello getHello();

    @RequestMapping(method = RequestMethod.GET, value = "/hellonotfound")
    String getException();

}

@Component
static class TestFallbackFactory implements FallbackFactory<FallbackWithFactory> {

    @Override
    public FallbackWithFactory create(Throwable cause) {
        return new FallbackWithFactory();
    }

}

static class FallbackWithFactory implements TestClientWithFactory {

    @Override
    public Hello getHello() {
        throw new NoFallbackAvailableException("Boom!", new RuntimeException());
    }

    @Override
    public String getException() {
        return "Fixed response";
    }

}
相关推荐
独自归家的兔16 小时前
通义千问3-VL-Plus - 界面交互(本地图片改进)
java·人工智能·交互
浔川python社16 小时前
《C++ 小程序编写系列》(第四部):实战:简易图书管理系统(类与对象篇)
java·开发语言·apache
楠枬16 小时前
OpenFeign
java·spring cloud·微服务
是席木木啊16 小时前
基于MinIO Java SDK实现ZIP文件上传的方案与实践
java·开发语言
计算机毕设指导616 小时前
基于微信小程序的垃圾分类信息系统【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
毕设源码-赖学姐16 小时前
【开题答辩全过程】以 高校就业系统的实现为例,包含答辩的问题和答案
java·eclipse
一起养小猫16 小时前
《Java数据结构与算法》第四篇(四):二叉树的高级操作查找与删除实现详解
java·开发语言·数据结构·算法
Coder_Boy_16 小时前
【人工智能应用技术】-基础实战-小程序应用(基于springAI+百度语音技术)智能语音控制-单片机交互代码
java·人工智能·后端·嵌入式硬件
a努力。17 小时前
国家电网Java面试被问:二叉树的前序、中序、后序遍历
java·开发语言·后端·面试
賬號封禁中miu17 小时前
图论之最小生成树
java·数据结构·算法·图论