Feign介绍及使用

Feign介绍

Feign是一个声明式的Web服务客户端,它使得Web服务客户端的编写更加方便。

通过Feign,可以轻松地封装远程服务的调用,使得调用远程服务就像调用本地服务一样简单

Feign基本使用

1. 导入依赖

在商品模块的pom中引入feign依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2. 启用Feign

在主启动类上加上@EnableFeignClients注解,就代表启用了

@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class GoodsApplication {
    public static void main(String[] args) {
        SpringApplication.run(GoodsApplication.class,args);
    }
}
3. 编写StoreClient接口
//restTemplate.getForObject("http://service-store/store/" + skuID, String.class);
@FeignClient("service-store")
public interface StoreClient {
    
    @GetMapping("/store/{skuID}")
    Map<String,Object> fingByskuID(@PathVariable("skuID") Long skuID);
}
4. 调用FeignClient

使用FeignClient和RestTemplate获取库存服务数据的对比

使用FeignClient:

Map<String, Object> map = storeClient.fingByskuID(skuID);

使用RestTemplate:

 String res = restTemplate.getForObject("http://service-store/store/" + skuID, String.class);
 ObjectMapper objectMapper = new ObjectMapper();
 Map map = objectMapper.readValue(res, Map.class);//将json字符串转为map对象

Feign自定义配置

先设置日志以debug级别输出,

logging:
  level:
    com.maoqi.goods.clients: debug
配置文件中配置
feign:
  client:
    config:
      default: #default默认为全局配置,服务名为专属配置
        loggerLevel: FULL
java代码中配置

编写FeignConfig配置类

public class FeignConfig {
    @Bean
    public Logger.Level loggerLevel(){
        return Logger.Level.BASIC;
    }
}

在主启动类上设置注解

@EnableFeignClients(defaultConfiguration = FeignConfig.class)

或者在服务接口上设置注解

@FeignClient(value = "service-store",configuration = FeignConfig.class)

Feign配置超时重试

配置超时时间
feign:
  client:
    config:
      default: #default默认为全局配置,服务名为专属配置
        connectTimeout: 2000 #连接超时时间
        readTimeout: 2000 #接口请求超时时间

当连接上远程服务后,接口请求超时就会抛出以下异常

java.net.SocketTimeoutException: Read timed out

重试机制

在FeignConfig配置类加上retryer方法进行重试

//第一次重试时间50ms,最大重试时间2000ms,最大重试次数
@Bean
public Retryer retryer(){
    return new Retryer.Default(50, TimeUnit.SECONDS.toSeconds(2),3);
}

Feigh切换 Client

使用 OkHttp配置超时重试

1. 引入okhttp依赖

在商品模块的pom中引入feign依赖

<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-okhttp</artifactId>
</dependency>
2. OkHttp配置类

创建FeignOkhttpConfig配置类

设置超时以及重试

@Data
@Component
@ConfigurationProperties(prefix = "feign.okhttp")
public class OkhttpProperties {
    private Long connectTimeout;
    private Long readTimeout;
}
@Configuration
@Component
@ConditionalOnClass({OkHttpClient.class})
@ConditionalOnProperty({"feign.okhttp.enabled"})
public class FeignOkhttpConfig {

    @Bean
    public okhttp3.OkHttpClient okHttpClient(OkhttpProperties okhttpProperties){
        return new okhttp3.OkHttpClient.Builder()
                //设置连接超时
                .connectTimeout(okhttpProperties.getConnectTimeout(), TimeUnit.MILLISECONDS)
                //设置接口读取超时
                .readTimeout(okhttpProperties.getReadTimeout(),TimeUnit.MILLISECONDS)
                //是否自动重连
                .retryOnConnectionFailure(true)
                .connectionPool(new ConnectionPool())
                //构建OkHttpClient对象
                .build();
    }
}
3. 开启okhttp

关闭feign中默认的httpclient并开启okhttp

feign:
  httpclient:
    enabled: false
  okhttp:
    enabled: true
    connectTimeout: 2000 #连接超时时间
    readTimeout: 2000 #接口请求超时时间
  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿柒爱吃鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值