Feign介绍及使用

菜单

    • Feign介绍
    • Feign基本使用
        • 1. 导入依赖
        • 2. 启用Feign
        • 3. 编写StoreClient接口
        • 4. 调用FeignClient
    • Feign自定义配置
        • 配置文件中配置
        • java代码中配置
    • Feign配置超时重试
        • 配置超时时间
        • 重试机制
    • Feigh切换 Client
      • 使用 OkHttp配置超时重试
        • 1. 引入okhttp依赖
        • 2. OkHttp配置类
        • 3. 开启okhttp

Feign介绍

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

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

Feign基本使用

1. 导入依赖

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

<dependency>
    <groupId>org.springframework.cloudgroupId>
    <artifactId>spring-cloud-starter-openfeignartifactId>
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.openfeigngroupId>
    <artifactId>feign-okhttpartifactId>
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 #接口请求超时时间

你可能感兴趣的:(笔记,java,spring,cloud)