Call Rest Api by Feign

功能: 通过 Feign 调用远程 Api。

pom.xml

添加依赖 feign-jacksonfeign-slf4j

<!-- Feign -->
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-jackson</artifactId>
    <version>${feign.version}</version>
</dependency>
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-slf4j</artifactId>
    <version>${feign.version}</version>
    <exclusions>
        <exclusion>
            <artifactId>feign-core</artifactId>
            <groupId>io.github.openfeign</groupId>
        </exclusion>
    </exclusions>
</dependency>
Call Rest Api by FeignClient

之前写过一篇使用Feign.builder()来发送 HTTP 请求的博客,对应的接口注解使用的是@RequestLine而不是常用的@RequestMapping

@RequestLinefeign-core 包提供的注解,@RequestMapping@PostMapping@GetMapping等是 spring-web 包提供的注解,两个使用方法不大一样。

Feign Retry 重试

为 Spring Cloud Ribbon 配置请求重试(Camden.SR2+) 里说是通过 spring.cloud.loadbalancer.retry.enabled 参数来开启重试机制,但是经过测试发现是通过 ribbon.OkToRetryOnAllOperations 设置为 true 来开启重试,该属性默认值为 false

Feign Hystrix 熔断

熔断示例

bootstrap.yml 中开启熔断,设置熔断的超时时间。

# Feign
feign:
  hystrix:
    enabled: true

# 熔断
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            # 超时时间
            timeoutInMilliseconds: 5000