SpringCloud

shuyepl 2024-03-03 15:54:22
Categories: Tags:

OpenFeign

简单使用

OpenFeign的使用主要都在服务调用端了,分别需要一个Feign接口(与被调用方服务对齐)、@EnableFeignClients@EnableFeign(name = "服务名", path = "controller上的path")

// name 服务名
// path controller对应的路径
@FeignClient(name = "stock-service", path = "/stock")
public interface StockFeignService {
        @RequestMapping("/reduct")
        public String reduct();
}
@SpringBootApplication
@EnableFeignClients
public class OrderOpenFeignApplication {
    public static void main( String[] args ) {
        SpringApplication.run(OrderOpenFeignApplication.class, args);
    }
}
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    StockFeignService stockFeignService;

    @RequestMapping("/add")
    public String add() {
        System.out.println("下单成功!");
        String msg = stockFeignService.reduct();
        return "Hello World" + msg;
    }
}

日志配置

OpenFeign的日志级别有4种:

注意:Feign的默认日志级别为debug,spring的为info,要修改spring的日志级别才能看见Feign的日志。

# 只修改feign包的日志级别
logging:
  level:
    com.shuyepl.order.feign: debug

注意:上面的路径中间用隔开,不要用/

1. 配置类方式

如果在配置类上方使用@Configuration注解则该配置全局生效,如果想使用第二种局部配置的方法,则不能在这个配置类上面使用@Configuration注解。

/**
 * @Configuration注解加上就是全局配置,不加上这个注解
 * 想使用局部配置,这个注解就得去掉
 */
@Configuration
public class FeignConfig {
    /**
     * 配置日志级别
     * 
     * @return
     */
    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
}

2. 局部配置一

将上方全局配置方式配置类上方的@Configuration去掉,在@FeignClient注解中指定使用的配置类

@FeignClient(name = "stock-service", path = "/stock", configuration = FeignConfig.class)
public interface StockFeignService {
        @RequestMapping("/reduct")
        public String reduct();
}

3. 局部配置二

在配置文件中进行局部配置

# 配置文件中进行Feign的局部配置
feign:
  client:
    config:
      product-service:    # 服务名
        logger-level: BASIC

契约配置

使项目即支持Feign的原生注解,也支持SpringMVC的注解

配置类方式

/**
* 契约配置,使其支持Feign原生注解
* @return
*/
public Contract feignContract() {
    return new Contract.Default();
}

配置文件

feign:
  client:
    config:
      product-service:    # 服务名
        logger-level: BASIC
        contract: feign.Contract.Default  # 修改成Feign原生注解方式,product只能使用feign原生的注解,不能使用SpringMVC的注解

ProductFeignService

@FeignClient(name = "product-service", path = "/product")
public interface ProductFeignService {
    @RequestLine("GET /reduct")
    public String reduct();
}

超时时间配置

全局配置

/**
* 超时时间配置
*/
@Bean
public Request.Options options() {
    // 第一个参数为连接超时时间(默认2s),第二个为请求处理超时时间(默认5s),单位都是ms
    return new Request.Options(5000, 10000);
}

yml局部配置

# 配置文件中进行Feign的局部配置
feign:
  client:
    config:
      product-service:    # 服务名
        logger-level: BASIC
        contract: feign.Contract.Default  # 修改成Feign原生注解方式,product只能使用feign原生的注解,不能使用SpringMVC的注解
        connectTimeout: 5000 # 连接超时时间,默认2s
        readTimeout: 10000   # 请求处理超时时间,默认5s

自定义拦截器

public class CustomFeignInterceptor implements RequestInterceptor {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void apply(RequestTemplate requestTemplate) {
        logger.info("feign拦截器!");
    }
}

配置方法一

    @Bean
    public CustomFeignInterceptor feignAuthRequestInterceptor() {
        return new CustomFeignInterceptor();
    }

配置方法二

# 配置文件中进行Feign的局部配置
feign:
  client:
    config:
      product-service:    # 服务名
        logger-level: BASIC
        contract: feign.Contract.Default  # 修改成Feign原生注解方式,product只能使用feign原生的注解,不能使用SpringMVC的注解
        connectTimeout: 5000 # 连接超时时间,默认2s
        readTimeout: 3000   # 请求处理超时时间,默认5s
        requestInterceptors:
          - com.shuyepl.order.interceptor.feign.CustomFeignInterceptor

参考资料: