06 OpenFeign服务调用

OpenFeign服务调用

官网文档:https://cloud.spring.io/spring-cloud-static/Hoxton.SR1/reference/htmlsingle/#spring-cloud-openfeign

Feign是一个声明式的web服务客户端,让编写web服务客户端变得非常容易**,只需创建一个接口并在接口上添加注解即可。

OpenFeign的使用(也是在消费者端)

  1. 新建模块cloud-consumer-feign-order80
  2. pom
<dependencies>
    <!-- openfeign -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <!-- eureka-client -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!-- 引用自己定义的api通用包,可以使用Payment支付Entity -->
    <dependency>
        <groupId>com.angenin.springcloud</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!--热部署-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
  1. yml 文件
server:
  port: 80


eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
  1. 主启动类
@EnableFeignClients //激活feign
@SpringBootApplication
public class OrderFeignMain80 {

    public static void main(String[] args) {
        SpringApplication.run(OrderFeignMain80.class, args);
    }
}
  1. 在springcloud包下新建service.PaymentFeignService接口
    (业务逻辑接口+@FeignClient配置调用provider服务。)

新建PaymentFeignService接口并新增注解@FeignClient

//Feign封装了Ribbon和RestTemplate,实现负载均衡和发送请求
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")    //作为feign的接口,找CLOUD-PAYMENT-SERVICE服务
public interface PaymentFeignService {

    //直接复制8001的方法
    @GetMapping("/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id);
}
  1. 在springcloud包下新建controller.OrderFeignController
@Slf4j
@RestController
public class OrderFeignController {

    @Resource
    private PaymentFeignService paymentFeignService;

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

1638450608200

OpenFeign超时控制

提供者在处理服务时用了3秒,提供者认为花3秒是正常,而消费者只愿意等1秒,1秒后,提供者会没返回数据,消费者就会造成超时调用报错。
所以需要双方约定好时间,不使用默认的。

模拟超时出错的情况

  1. 在8001的PaymentController里添加:(模拟服务处理时间长)
    @GetMapping("/payment/feign/timeout")
    public String paymentFeignTimeout(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverPort;
    }
  1. 在80的PaymentFeignService中添加:
@GetMapping("/payment/feign/timeout")
public String paymentFeignTimeout();
  1. 然后在80的OrderFeignController中添加
    @GetMapping("/consumer/payment/feign/timeout")
    public String paymentFeignTimeout(){
        //openFeign-ribbon,客户端一般默认等待1秒
        return paymentFeignService.paymentFeignTimeout();
    }

启动server集群,提供者8001,消费者80
http://localhost/consumer/payment/feign/timeout
三秒后报错。

默认Feign 客户端只等待一秒,但是服务端处理需要超过1秒,导致Feign不想等待了,直接返回报错。为了避免这样的情况,有时候需要设置 Feign客户端的超市控制

  1. 在80的yml中添加:
#没提示不管它,可以设置
ribbon:
  #指的是建立连接后从服务器读取到可用资源所用的时间
  ReadTimeout: 5000
  #指的是建立连接使用的时间,适用于网络状况正常的情况下,两端连接所用的时间
  ConnectTimeout: 5000

重新访问http://localhost/consumer/payment/feign/timeout,3秒后显示。

OpenFeign日志打印功能

  1. 配置日志bean
    在80的springcloud包下新建config.FeignConfig
import feign.Logger;	//不要导错包

@Configuration
public class FeignConfig {

    @Bean
    Logger.Level feignLoggerLevel(){
        //打印最详细的日志
        return Logger.Level.FULL;
    }
}
  1. 80 的 yml 文件中添加
#开启日志的feign客户端
logging:
  level:
    #feign日志以什么级别监控哪个接口
    com.angenin.springcloud.service.PaymentFeignService: debug	#写你们自己的包名

  1. 启动项目,http://localhost/consumer/payment/feign/timeout
posted @   flypiggg  阅读(120)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示