Feign

本文介绍了如何在SpringCloud中使用OpenFeign创建声明式HTTP客户端,包括添加依赖、启动注解、细粒度和全局配置选项,以及支持的配置项如日志级别、重试策略等。还讨论了多参数请求构造,如GET和POST方法。
摘要由CSDN通过智能技术生成

一、使用

        定义:Netflix开源的声明式HTTP客户端

        Github地址:GitHub - OpenFeign/feign: Feign makes writing java http clients easier

        加依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        启动注解:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

}

        远程HTTP客户端:

@FeignClient(name = "user")
public interface UserFeignClient {

    @RequestMapping("/test")
    String test();

}

        服务调用方式

    @Autowired
    private UserFeignClient userFeignClient;

    @GetMapping("/testFeignClient")
    public String testFeignClient(){
        String test = this.userFeignClient.test();
        return test;
    }

二、细粒度配置自定义

        1、java代码

        新增配置类

@Configuration
public class UserFeignConfiguration {

    @Bean
    public Logger.Level level(){
        return Logger.Level.FULL;
    }
}

        修改feign接口

@FeignClient(name = "user", configuration = UserFeignConfiguration.class)
public interface UserFeignClient {

    @RequestMapping("/test")
    String test();

}

        修改配置 

logging:
  level:
    com.example.demo.feignclient.UserFeignClient: debug

       2、配置属性

logging:
  level:
    com.example.demo.feignclient.UserFeignClient: debug

spring:
  cloud:
    openfeign:
      client:
        config:
          user:
            loggerLevel: full

三、全局配置

        1、java代码,启动类添加注解

@EnableFeignClients(defaultConfiguration = UserFeignConfiguration.class)

        2、配置属性

spring:
  cloud:
    openfeign:
      client:
        config:
          default:
            loggerLevel: full

四、支持的配置项

        1、java代码

配置项作用
Logger.Level指定日志级别
Retryer指定重试策略
ErrorDecoder指定错误解码器
Request.Options超时时间
Collection<RequestInterceptor>拦截器
SetterFactory用于设置Hystrix的配置属性,Feign整合Hystrix才会用

        2、属性配置

spring:
  cloud:
    openfeign:
      client:
        config:
          <feignName>:
            connectTimeout: 5000    #连接超时时间
            readTimeout: 5000         #读取超时时间
            loggerLevel: full             #日志级别
            errorDecoder: com.example.SimpleErrorDecoder                   #错误解码器
            retryer: com.example.SimpleRetryer                                       #重试策略
            requestInterceptors: com.example.FooRequestInterceptor     #拦截器
            # 是否对404错误码解码
            decode404: false
            encoder: com.example.SimpleEncoder #编码器
            decoder: com.example.SimpleDecoder #解码器
            contract: com.example.SimpleContract #契约 

五、多参数请求构造

        1、GET

        2、POST

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值