SpringCloud组件-服务调用OpenFeign

OpenFeign简介

前面在使用Ribbon+RestTemplate时,利用RestTemplate对http请求的封装处理,形成了一套模版化的调用方法。但是在实际开发中,由于对服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以通常都会针对每个微服务自行封装一些客户端类来包装这些依赖服务的调用。所以,Feign在此基础上做了进一步封装,由他来帮助我们定义和实现依赖服务接口的定义。在Feign的实现下,我们只需创建一个接口并使用注解的方式来配置它(以前是Dao接口上面标注Mapper注解,现在是一个微服务接口上面标注一个Feign注解即可),即可完成对服务提供方的接口绑定,简化了使用Spring cloud Ribbon时,自动封装服务调用客户端的开发量。


服务调用

用在消费者上

  • 先建一个消费者工程,启动器加上激活注解@EnableFeignClients
package cn.edu.guet;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author pangjian
 * @ClassName OpenFeignMain
 * @Description TODO
 * @date 2021/9/13 10:43
 */
@SpringBootApplication
@EnableFeignClients
public class OpenFeignMain {

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

}
  • 然后建立调用服务业务接口,去调用服务提供者的控制方法
package cn.edu.guet.service;

import cn.edu.guet.pojo.Payment;
import cn.edu.guet.response.CommonResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @author pangjian
 * @Interface PaymentFeignService
 * @Description TODO
 * @date 2021/9/13 10:50
 */
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")   // 表明你找哪一个微服务
public interface PaymentFeignService {

    @GetMapping("/payment/get/{id}") // 调用微服务的处理方法
    CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);

}
  • 消费者控制层去调用业务接口方法就行了
/**
 * @author pangjian
 * @ClassName PaymentController
 * @Description TODO
 * @date 2021/9/13 10:52
 */
@RestController
@Slf4j
public class PaymentController {

    @Autowired
    private PaymentFeignService paymentFeignService;

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

}

超时控制

  • 模拟场景,消费提供者业务处理要3秒钟,而默认Feign客户端(消费者)只等待一秒钟,但是服务端处理需要超过1秒钟,导致Feign客户端不想等待了(默认只等一秒),直接返回报错。为了避免这样的情况,有时候我们需要设置Feign客户端的超时控制。
// 服务提供者处理要三秒
@GetMapping("payment/feign/timeout")
public String paymentFeignTimeout() {
    try {
        TimeUnit.SECONDS.sleep(3);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return "8001";
}

配置文件,解决问题

#设置feign客户端超时时间(openFeign默认支持ribbon)
ribbon:
	#指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
  ReadTimeout: 5000
	#指的是建立连接后从服务器读取到可用资源所用的时间
  ConnectTimeout: 5000

日志增强

配置bean加入到ioc容器中

package cn.edu.guet.config;

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author pangjian
 * @ClassName FeignConfig
 * @Description TODO
 * @date 2021/9/24 16:17
 */
@Configuration
public class FeignConfig {

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

}
logging:
  level:
    # feign 日志以什么级别监控哪个接口
    cn.edu.guet.service.PaymentFeignService: debug

后台日志详情
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值