開發與維運

OpenFeign服務接口調用

OpenFeign服務接口調用

簡介

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

Feign 是一個聲明式WebService 客戶端。使用Feign能讓編寫Web Service更加簡單

它的使用方式 定義一個服務接口然後在上面添加註解。Feign也支持 可拔插式的編碼器和解碼器。Spring Cloud 對Feign 進行了封裝,使其支持了Spring MVC 標準註解和 HttpMessageConverters. Feign可以與Eureka 和 Ribbon 組合使用以支持負載均衡


  • Feign是一個聲明式的web服務客戶端,讓編寫web服務客戶端變得非常容易,只需創建一個接口並在接口上添加註解即可。

Feign與OpenFeign的區別

在這裡插入圖片描述


OpenFeign服務調用

1.引入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>

2.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

3.啟動類加入

@EnableFeignClients //激活feign

4.新建 Feign 包 創建Feign接口

@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE") //作為feign的接口,找CLOUD-PAYMENT-SERVICE服務 (必須是Eureka註冊中心的服務)
public interface PaymentFeignService {

    //調用Eureka 消費者接口
    @GetMapping("/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id);

}
  1. 按 順序啟動項目 (server集群,消費者,調用者),然後在瀏覽器輸入http://localhost/consumer/payment/get/1,成功查詢到數據,並且有負載均衡(輪詢)。

在這裡插入圖片描述

OpenFeign超時控制

提供者在處理服務時用了3秒,提供者認為花3秒是正常,而消費者只願意等1秒,1秒後,提供者會沒返回數據,消費者就會造成超時調用報錯。
所以需要雙方約定好時間,不使用默認的。
在這裡插入圖片描述
在80的yml中添加:

#沒提示不管它,可以設置
ribbon:
  #指的是建立連接後從服務器讀取到可用資源所用的時間
  ReadTimeout: 5000
  #指的是建立連接使用的時間,適用於網絡狀況正常的情況下,兩端連接所用的時間
  ConnectTimeout: 5000

OpenFeign日誌打印功能

Feign 提供了日誌打印功能,我們可以通過配置來調整日誌級別,從而瞭解 Feign 中 Http 請求的細節,就是對 Feign接口調用情況進行監控和輸出

日誌級別

在這裡插入圖片描述

---
###### 步驟

  1. 配置日誌bean
    在80的springcloud包下新建config.FeignConfig
import feign.Logger;    //不要導錯包

@Configuration
public class FeignConfig {

    @Bean
    Logger.Level feignLoggerLevel(){
        //打印最詳細的日誌
        return Logger.Level.FULL;
    }

}

2.在yml添加 :

#開啟日誌的feign客戶端
logging:
  level:
    #feign日誌以什麼級別監控哪個接口
    com.yxl.cloud.feign.PaymentFeignService: debug    #寫你們自己的包名

3.啟動項目

在這裡插入圖片描述


Leave a Reply

Your email address will not be published. Required fields are marked *