服务提供者
Openfeign远程调用服务提供者搭建
文章地址http://t.csdnimg.cn/06iz8
PaymentController【控制层】
 /**
     * 测试超时机制
     *
     * @return
     */
    @GetMapping("/timeout")
    public String TimeOut() {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "payment success";
    }相关接口
测试超时机制:http://localhost:8001/payment/timeout
服务消费者
Openfeign远程调用消费者搭建
文章地址http://t.csdnimg.cn/06iz8
依赖
<!-- resilience4j  -->
<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-spring-cloud2</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
</dependency>
application.yml
# 超时机制
resilience4j:
  timelimiter:
    instances:
      delay:
        # 设置超时时间 2秒
        timeoutDuration: 2   PaymentFeignService【接口层】
 /**
     * 支付服务-测试超时机制
     *
     * @return
     */
    @GetMapping("/payment/timeout")
    String paymentTimeout();
  OrderController【控制层】
    /**
     * 支付服务-测试超时降级
     *
     * @return
     */
    @GetMapping("/timeout")
    @TimeLimiter(name = "delay", fallbackMethod = "timeoutFallback")
    //超时降级注解:name:对应application.yml的超时降级配置名称;fallbackMethod:超时服务降级方法
    public CompletableFuture<String> paymentTimeout() {
        log.info("********* 进入方法 ******");
        //resilience4j一般用异步操作,此处使用lambda表达式
        CompletableFuture<String> completableFuture =
                CompletableFuture.supplyAsync((Supplier<String>) () -> (paymentFeignService.paymentTimeout()));
        log.info("********* 离开方法 ******");
        return completableFuture;
    }
    /**
     * 超时服务降级方法
     *
     * @param e
     * @return
     */
    public CompletableFuture<ResponseEntity> timeoutFallback(Exception e) {
        e.printStackTrace();
        return CompletableFuture.completedFuture(ResponseEntity.ok("超时啦"));
    }相关接口
测试服务调用:http://localhost:8004/order/index
测试超时机制:http://localhost:8004/order/timeout




















