1. 程式人生 > 其它 >SpingCloud(H版&alibaba)框架開發教程-25 OpenFeign服務呼叫、超時處理、日誌增強

SpingCloud(H版&alibaba)框架開發教程-25 OpenFeign服務呼叫、超時處理、日誌增強

技術標籤:SpingCloud(H版&alibaba)spring bootspring cloudspring cloud alibabafeignopenfeign

2020最新版SpringCloud(H版&alibaba)框架開發教程-周陽

cloud-consumer-feign-order80模組

微服務模組

1. 建module

New --> Module --> Maven[Module SDK:1.8.0_191] --> name[cloud-consumer-feign-order80] --> Finish

2. 改pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>cloud2020</artifactId> <groupId
>
com.antherd.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-consumer-feign-order80</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</
groupId
>
<artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.2.1.RELEASE</version> </dependency> <!--eureka client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--引入公共api包--> <dependency> <groupId>com.antherd.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> </project>

3. 寫yml

server:
  port: 80

eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka

4. 主啟動
新建類:com.antherd.springcloud.OrderFeignMain80

package com.antherd.srpingcloud;

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

@SpringBootApplication
@EnableFeignClients
public class OrderFeignMain80 {

  public static void main(String[] args) {

    SpringApplication.run(OrderFeignMain80.class, args);
  }
}

新建類:com.antherd.srpingcloud.service.PaymentFeignService

package com.antherd.srpingcloud.service;

import com.antherd.springcloud.entities.CommonResult;
import com.antherd.springcloud.entities.Payment;
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;

@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {

  @GetMapping(value = "/payment/{id}")
  CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
}

新建類:com.antherd.srpingcloud.contorller.OrderFeignController

package com.antherd.srpingcloud.contorller;

import com.antherd.springcloud.entities.CommonResult;
import com.antherd.springcloud.entities.Payment;
import com.antherd.srpingcloud.service.PaymentFeignService;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class OrderFeignController {

  @Resource
  private PaymentFeignService paymentFeignService;

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

5. 啟動測試

啟動:cloud-eureka-server7001,cloud-eureka-server7002,cloud-provider-payment8001,cloud-provider-payment8002,cloud-consumer-order80五個模組

訪問:http://localhost/consumer/payment/1

6. 超時處理

預設Feign客戶端只等待一秒鐘,但是服務端處理需要超過一秒鐘,導致Feign客戶端不想等待了,直接返回報錯。為了避免這樣的情況,有時候我們需要設定Feign客戶端的超時控制。

下面演示Feign超時報錯:
cloud-provider-payment8001、cloud-provider-payment8002模組

com.antherd.springcloud.controller.PaymentController新增新介面:

@GetMapping(value = "/payment/feign/timeout")
public String paymentFeignTimeout() throws InterruptedException {
  TimeUnit.SECONDS.sleep(3);
  return serverPort;
}

cloud-consumer-feign-order80模組

com.antherd.srpingcloud.service.PaymentFeignService新增新方法:

@GetMapping(value = "/payment/feign/timeout")
String paymentFeignTimeout();

com.antherd.srpingcloud.contorller.OrderFeignController新增新介面:

@GetMapping(value = "/consumer/payment/feign/timeout")
String paymentFeignTimeout() {
  // openfeign-ribbon客戶端預設等待一秒鐘
  return paymentFeignService.paymentFeignTimeout();
}

訪問:http://localhost:8001/payment/feign/timeouthttp://localhost/consumer/payment/feign/timeout 看一下效果吧

修改cloud-consumer-feign-order80的yml配置檔案,設定Feign客戶端的超時控制
新增如下配置:

# 設定feign客戶端超時時間(OpenFeign預設支援ribbon)
ribbon:
# 指的是建立連線所用的時間,適用於網路狀況正常的情況下,兩端連線所用的時間
  ConnectTimeout: 5000
# 指的是建立連線後從伺服器讀取到可用資源所用的時間
  ReadTimeout: 5000

訪問:http://localhost/consumer/payment/feign/timeout 看一下效果

7. 日誌增強

Feign提供了日誌列印功能,我們可以通過配置來調整日誌級別,從而瞭解Feign中Http請求的細節(對Feign介面的呼叫情況進行監控和輸出

日誌級別:

  • NONE:預設的,不顯示任何日誌
  • BASIC:僅記錄請求方法、URL、響應狀態碼及執行時間
  • HEADERS:除了BASIC中定義的資訊之外,還有請求和響應頭資訊
  • FULL:除了HEADERS中定義的資訊之外,還有請求和響應的正文及元資料

新建類:com.antherd.srpingcloud.config.FeignConfig

package com.antherd.srpingcloud.config;

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

@Configuration
public class FeignConfig {

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

修改cloud-consumer-feign-order80的yml配置檔案
新增如下配置:

logging:
  level:
    # feign日誌以什麼級別監控哪個介面
    com.antherd.springcloud.service.PaymentFeignService: debug

訪問:http://localhost/consumer/payment/feign/timeout 看一下效果