OpenFeign

概述

1、Feign 是一个声明式的网络服务客户端,它使编写 Web 服务客户端更容易

2、要使用 Feign,需要创建一个接口并对其添加注解

3、它有可插拔的注解支持,包括 Feign 注解和 JAX-RS 注解

4、Feign 还支持可插拔的编码器和解码器

5、Spring Cloud 增加对 Spring MVC 注解的支持,并支持使用 Spring Web 中默认使用的 HttpMessageConverters

6、Spring Cloud 集成 Eureka、Spring Cloud CircuitBreaker 以及 Spring Cloud LoadBalancer,以便在使用 Feign 时提供一个负载平衡的 http 客户端

 

使用示例

1、服务提供者

(1)Service 实现类,省略其 Service 接口、Dao 层

@Service
public class PaymentServiceImpl implements PaymentService {
    @Resource
    private PaymentDao paymentDao;

    @Override
    public Payment getPaymentById(Long id) {
        return paymentDao.getPaymentById(id);
    }
}

(2)Controller

@RestController
public class PaymentController {
    @Resource
    private PaymentService paymentService;

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id)
    {
        Payment payment = paymentService.getPaymentById(id);
        log.info("*****查询结果:{}",payment);
        if (payment != null) {
            return new CommonResult(200,"查询成功",payment);
        }else{
            return new CommonResult(444,"没有对应记录,查询ID: "+id,null);
        }
    }
}

2、服务消费者

(1)依赖

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

(2)Spring Boot 启动类

@SpringBootApplication
//开启@FeignClient所标注的类
@EnableFeignClients
public class OrderFeignMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderFeignMain80.class,args);
    }
}

(3)微服务接口 + @FeignClient 代替 Ribbon + RestTemplate

@Component
//value为服务提供者在注册中心的微服务名称,即spring.application.name
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
    @GetMapping(value = "/payment/get/{id}")
    CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
}
@RestController
public class OrderFeignController {
    @Resource
    private PaymentFeignService paymentFeignService;

    @GetMapping(value = "/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
        return paymentFeignService.getPaymentById(id);
    }
}

(4)Ribbon + RestTemplate

@Configuration
public class ApplicationContextBean {
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}
@RestController
public class OrderConsulController {

    public static final String INVOKE_URL = "http://cloud-provider-payment";

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult getPayment(@PathVariable Long id) {
        return restTemplate.getForObject(INVOKE_URL + "/payment/get/" + id, CommonResult.class, id);
    }
}

 

超时控制

1、OpenFeign 默认支持 Ribbon,超时由 Ribbon 控制

2、默认等待 1 秒

3、yaml 配置超时时间

#设置OpenFeign客户端超时时间,OpenFeign默认支持Ribbon
ribbon:
  #建立连接所用的时间(毫秒),适用于网络状况正常的情况下,两端连接所用的时间
  #DEFAULT_CONNECT_TIMEOUT=1000,即默认连接超时时间为1秒
  ConnectTimeout: 5000
  #建立连接后,从服务器读取到可用资源所用的时间(毫秒)
  #DEFAULT_READ_TIMEOUT = 1000,即默认读取超时时间为1秒
  ReadTimeout: 5000

 

日志增强

1、OpenFeign 提供日志打印功能

(1)可以通过配置来调整日志级别,从而了解 OpenFeign 中 Http 请求的细节

(2)即监控、输出 OpenFeign 接口的调用情况

2、日志级别

(1)NONE:默认,不显示任何日志

(2)BASIC:仅记录请求方法、URL、响应状态码、执行时间

(3)HEADERS:除了 BASIC 中定义的信息之外,还有请求、响应的头信息

(4)FULL:除了 HEADERS 中定义的信息之外,还有请求、响应的正文及元数据

3、配置类注入 Bean,配置 OpenFeign 日志详细程度

@Configuration
public class FeignConfig {
    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
}

4、yaml 配置,指定输出 OpenFeign 接口的日志

logging:
  level:
    # 因为Spring Boot默认日志级别为info,OpenFeign日志级别为 debug,所以OpenFeign的debug日志不会输出
    # logging.level=debug,对所有的日志级别进行配置
    # 以下只输出OpenFeign接口的日志
    OpenFeign接口全类名: debug
posted @   半条咸鱼  阅读(352)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示