天天看點

SpringCloud09-OpenFeign服務接口調用1、概述2、OpenFeign的使用3、OpenFeign逾時控制4、OpenFeign日志列印功能

1、概述

1.1 什麼是OpenFeign

官網位址:https://cloud.spring.io/spring-cloud-static/Hoxton.SR1/reference/htmlsingle/#spring-cloud-openfeign

Feign是一個聲明性web服務用戶端。它使編寫web服務用戶端變得更容易。使用Feign建立一個接口并對其進行注釋。它有可插入的注釋支援,包括外部注釋和JAX-RS注釋。Feign還支援可插入的編碼器和解碼器。Spring Cloud增加了對Spring MVC注釋的支援,以及對使用Spring Web中預設使用的httpMessageConverter的支援。Spring Cloud內建了Ribbon和Eureka以及Spring Cloud LoadBalancer,在使用Feign時提供了一個負載均衡的http用戶端

他的使用方式是:定義一個服務接口,然後在上面添加注釋;

總結:Feign是一個聲明式的Web服務用戶端,讓編寫Web服務用戶端變得非常容易,隻需 建立一個接口,并在接口上添加注解即可。

github位址:https://github.com/spring-cloud/spring-cloud-openfeign

1.2 OpenFeign能幹嘛

OpenFeign是內建了Feign的,Feign旨在使編寫Java Http用戶端變得更加容易;

前面在使用Ribbon+RestTemplate的時候,利用RestTemplate對Http請求的封裝處理,形成了一套模闆化的調用方法。但是實際開發中,由于對服務依賴的調用可能不止一處,**往往一個接口會被多處調用,是以通常都會針對每個微服務自行封裝一些用戶端類來包裝這些依賴服務的調用。**是以,Feign在此基礎上做了進一步的封裝,由他來幫助我們定義和實作依賴服務接口的定義。在Feign的實作下,我們隻需建立一個接口并使用注解的方式來配置它(以前是Dao接口上面标注Mapper注解,現在是一個微服務接口上面标注一個Feign注解即可);即可完成對服務提供方的接口綁定,簡化了使用Spring Cloud Ribbon時,自動封裝服務調用用戶端的開發量。

Feign內建了Ribbon,利用Ribbon維護了Payment支付服務的服務清單資訊,并且通過輪詢實作了用戶端的負載均衡。而與Ribbon不同的是,通過Feign隻需要定義服務綁定接口,且以聲明式的方式,優雅而簡單的實作了服務調用。

1.3 OpenFeign和Feign的差別

OpenFeign Feign
OpenFeign是SpringCloud在Feign的基礎上支援了SpringMVC的注解,如@RequestMapping等。OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,并通過動态代理的方式産生實作類,實作類中做負載均衡并調用其他服務。 Feign是SpringCloud元件中的一個輕量級RESTful的Http服務用戶端,Feign内置了Ribbon,用來做用戶端負載均衡,去調用服務注冊中心的服務。Feign的使用方式是:使用Feign的注解定義接口,調用這個接口,就可以調用服務注冊中心的服務。

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-openfeign</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-feign</artifactId>

</dependency>

2、OpenFeign的使用

微服務調用接口[email protected],OpenFeign是在用戶端使用的

2.1 建立cloud-consumer-openfeign-order80

SpringCloud09-OpenFeign服務接口調用1、概述2、OpenFeign的使用3、OpenFeign逾時控制4、OpenFeign日志列印功能

2.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>springcloud2020</artifactId>
        <groupId>com.zdw.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consumer-openfeign-order80</artifactId>
    <description>訂單消費者之feign</description>

    <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>
        <dependency>
            <groupId>com.zdw.springcloud</groupId>
            <artifactId>cloud-api-common</artifactId>
            <version>1.0-SNAPSHOT</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>
           

2.3 寫yml

server:
  port: 80
spring:
  application:
    name: cloud-consumer-openfeign-order

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      # defaultZone: http://localhost:7001/eureka
      # 叢集版
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
           

2.4 主啟動

package com.zdw.springcloud;

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

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients   
public class OrderOpenFeignMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderOpenFeignMain80.class,args);
    }
}
           

@EnableFeignClients :開啟OpenFeign的支援

2.5 業務代碼

2.5.1 @FeignClient修飾業務邏輯接口

package com.zdw.springcloud.service;

import com.zdw.springcloud.entities.CommonResult;
import com.zdw.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;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * OpenFeign的業務邏輯接口類,裡面的方法和8001的paymentController的方法以及路徑都是一樣的
 */
@Component
@FeignClient(name = "CLOUD-PAYMENT-SERVICE") //指定要從哪個服務調用
public interface PaymentOpenFeignService {
    @PostMapping(value = "payment/create")
    public CommonResult create(@RequestBody Payment payment);

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

    @GetMapping(value = "/payment/lb")
    public String getPaymentLB();
}
           

注意:@Component 這個注解要加,要交給Spring的IOC容器管理

@FeignClient(name = “CLOUD-PAYMENT-SERVICE”) :指定要從哪個服務調用

2.5.2 controller編寫

package com.zdw.springcloud.controller;

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

import javax.annotation.Resource;

@RestController
@Slf4j
public class OrderOpenFeignController {

    @Resource
    PaymentOpenFeignService paymentOpenFeignService;

    @GetMapping(value = "/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
        log.info("===OpenFeign80服務被調用了,會去調用8001和8002的叢集服務");
        return paymentOpenFeignService.get(id);
    }
}
           

2.6 測試

啟動Eureka7001和7002,然後啟動payment8001和8002,最後啟動新建立的這個order80服務:

Feign自帶負載均衡配置項,是以我們通路:http://localhost/consumer/payment/get/1 可以看到也是8001和8002交替出現,輪詢的負載均衡機制已經得到了實作了。

3、OpenFeign逾時控制

3.1 逾時設定,故意設定逾時示範出錯情況

3.1.1 payment8001和payment8002的controller添加方法

在cloud-provider-payment8001和cloud-provider-payment8002的PaymentController中添加如下方法:

@GetMapping(value = "/payment/feign/timeout")
public String paymentFeignTimeout() {
    try {
        // 暫停3秒鐘
        TimeUnit.SECONDS.sleep(3);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return serverPort;
}
           

3.1.2 order80的OpenFeign接口添加方法

在cloud-consumer-openfeign-order80的PaymentOpenFeignService添加方法:

/**
     * 模拟feign逾時
     */
@GetMapping(value = "/payment/feign/timeout")
String paymentFeignTimeout();
           

3.1.3 order80的controller添加方法

在cloud-consumer-openfeign-order80的OrderOpenFeignController添加方法:

@GetMapping(value = "/consumer/payment/feign/timeout")
public String paymentFeignTimeout() {
    // openfeign-ribbon, 用戶端一般預設等待1秒鐘
    return paymentOpenFeignService.paymentFeignTimeout();
}
           

3.2 測試

啟動服務之後,浏覽器通路:http://localhost/consumer/payment/feign/timeout ,會看到報錯:

SpringCloud09-OpenFeign服務接口調用1、概述2、OpenFeign的使用3、OpenFeign逾時控制4、OpenFeign日志列印功能

OpenFeign預設等待1秒鐘,超過後報錯,而我們的8001和8002是延遲了3秒。導緻用戶端不想等待了,直接傳回報錯,為了避免這樣的情況,有時候我們需要設定Feign用戶端的逾時時間。

3.3 設定Feign用戶端的逾時時間

修改cloud-consumer-openfeign-order80的application.yml,添加逾時設定:

server:
  port: 80
spring:
  application:
    name: cloud-consumer-openfeign-order

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      # defaultZone: http://localhost:7001/eureka
      # 叢集版
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
      
# 設定feign用戶端逾時時間(OpenFeign預設支援ribbon)
ribbon:
  # 指的是建立連接配接所用的時間,适用于網絡狀态正常的情況下,兩端連接配接所用的時間
  ReadTimeout: 5000
  # 指的是建立連接配接後從伺服器讀取到可用資源所用的時間
  ConnectTimeout: 5000

           

設定的逾時時間的 5000,就是5秒。

3.4 重新測試

浏覽器通路:http://localhost/consumer/payment/feign/timeout 在等待了3秒之後,成功了傳回了,不會報錯。

4、OpenFeign日志列印功能

4.1 概述

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

有以下日志級别:

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

4.2 配置日志Bean

在cloud-consumer-openfeign-order80中建立一個配置類:OpenFeignConfig:

package com.zdw.springcloud.config;

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

@Configuration
public class OpenFeignConfig {

    /**
     * feignClient配置日志級别
     */
    @Bean
    public Logger.Level feignLoggerLevel() {
        // 請求和響應的頭資訊,請求和響應的正文及中繼資料,注意Logger是feign.Logger
        return Logger.Level.FULL;
    }
}
           

4.3 yml中添加配置

在cloud-consumer-openfeign-order80的application.yml中,設定feign日志以什麼級别監控哪個接口,logging是頂格寫的。

logging:
  level:
    # 配置 feign日志以什麼級别監控哪個接口
    com.zdw.springcloud.service.PaymentOpenFeignService: debug
           

4.4 測試

啟動之後,浏覽器通路:http://localhost/consumer/payment/feign/timeout

看到控制台的列印資訊:

2020-04-26 13:21:25.116 DEBUG 19688 --- [p-nio-80-exec-1] c.z.s.service.PaymentOpenFeignService    : [PaymentOpenFeignService#paymentFeignTimeout] <--- HTTP/1.1 200 (3299ms)
2020-04-26 13:21:25.116 DEBUG 19688 --- [p-nio-80-exec-1] c.z.s.service.PaymentOpenFeignService    : [PaymentOpenFeignService#paymentFeignTimeout] connection: keep-alive
2020-04-26 13:21:25.116 DEBUG 19688 --- [p-nio-80-exec-1] c.z.s.service.PaymentOpenFeignService    : [PaymentOpenFeignService#paymentFeignTimeout] content-length: 4
2020-04-26 13:21:25.116 DEBUG 19688 --- [p-nio-80-exec-1] c.z.s.service.PaymentOpenFeignService    : [PaymentOpenFeignService#paymentFeignTimeout] content-type: text/plain;charset=UTF-8
2020-04-26 13:21:25.116 DEBUG 19688 --- [p-nio-80-exec-1] c.z.s.service.PaymentOpenFeignService    : [PaymentOpenFeignService#paymentFeignTimeout] date: Sun, 26 Apr 2020 05:21:25 GMT
2020-04-26 13:21:25.116 DEBUG 19688 --- [p-nio-80-exec-1] c.z.s.service.PaymentOpenFeignService    : [PaymentOpenFeignService#paymentFeignTimeout] keep-alive: timeout=60
2020-04-26 13:21:25.116 DEBUG 19688 --- [p-nio-80-exec-1] c.z.s.service.PaymentOpenFeignService    : [PaymentOpenFeignService#paymentFeignTimeout] 
2020-04-26 13:21:25.118 DEBUG 19688 --- [p-nio-80-exec-1] c.z.s.service.PaymentOpenFeignService    : [PaymentOpenFeignService#paymentFeignTimeout] 8001
2020-04-26 13:21:25.118 DEBUG 19688 --- [p-nio-80-exec-1] c.z.s.service.PaymentOpenFeignService    : [PaymentOpenFeignService#paymentFeignTimeout] <--- END HTTP (4-byte body)
           

繼續閱讀