Feign详解

Feign为声明式http客户端

Feign的使用步骤

1.在调用服务的pom中引入依赖

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

2.在application中添加注解

@MapperScan("cn.itcast.order.mapper")
@SpringBootApplication
@EnableFeignClients

3.编写Feign接口

@FeignClient("userservice")
public interface UserClient {
    @GetMapping("/user/{id}")
    User findById(@PathVariable("id") Long id);
}

Feign的自定义配置

feign.Logger.Level 修改日志级别  NONE BASIC HEADERS FULL       

feign.codec.Decoder 相应结果的解析器

feign.codec.Encoder   请求参数编码

feign.Contract 支持的注解格式

feign.Retryer 失败重试机制

一般需要配置的是日志级别

修改Feign日志级别的方式:

一 配置文件方式

全局生效 默认就是全局,default可以改为对应的服务名称

feign:
  client:
    config:
      default:
        loggerLevel: FULL

一 JAVA代码方式

声明一个Bean新建配置类

public class DefaultFeignConfiguration {
    @Bean
    public Logger.Level logLevel(){
        return  Logger.Level.BASIC;
    }
}

application中添加配置,或者可在客户端类中添加

@MapperScan("cn.itcast.order.mapper")
@SpringBootApplication
@EnableFeignClients(defaultConfiguration = DefaultFeignConfiguration.class)

 

posted @ 2021-10-22 14:59  zuiAI0658  阅读(203)  评论(0)    收藏  举报