Feign 實現微服務調用及進行服務熔斷與降級


本文為博主原創,未經允許不得轉載:  

  1. Feign 日志級別配置

  2. Feign client 封裝調用

  3. Feign 定義熔斷降級方法

  4. 通過 FallbackFactory 工廠 實現降級

  5. 配置 @FeignClient 的 configuration 屬性

  6. 配置http 連接池

  7. feign 配合 hystrix 超時熔斷配置

  

  Feign 是NetFlix 開源的聲明式的 HTTP 客戶端。一般在服務消費端實現 Feign 的客戶端,進行服務調用。

  1. Feign 日志級別配置

    Feign 默認實現調用時,不會打印調用的請求日志。配置 feign client 調用日志打印:

feign:
  client:
    config:
      # 全局配置
      default:
        loggerLevel: full

   Feign 日志級別及打印內容:

日志級別 打印內容
NONE(默認值) 不打印任何日志
BASIC 僅記錄請求方法,url,響應狀態以及執行時間
HEADERS 在BASIC的基礎上,記錄了請求和響應的HEADER
FULL 記錄請求和響應的header,body和元數據

  2.Feign client 封裝調用:

     2.1 添加 feign 的pom 依賴:

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>

    2.2 定義Feign client接口,並在啟動類添加 @EnableFeignClients 注解

import com.contentcenter.domain.dto.user.UserDTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(name
= "user-center") // name為調用的服務模塊名稱 public interface UserCenterFeignClient { /** * http://user-center/users/{id} * * @param id * @return */ @GetMapping("/users/{id}") UserDTO findById(@PathVariable Integer id); }

    2.3 非用戶中心可通過在自己側封裝的 Feign client 進行調用,獲取用戶數據:

    @Autowire
    private UserCenterFeignClient userFeignClient;
    
    @Test
    public void test(){
        UserDTO userDto = userFeignClient.findById(11);
        system.out.println(JSON.toJsonString(userDto));
    }

  

  3. Feign 定義熔斷降級方法

    3.1 自定義實現 FeignClient 的接口返回

@Component
public class UserCenterFeignClientFallback implements UserCenterFeignClient {
    @Override
    public UserDTO findById(Integer id) {
        UserDTO userDTO = new UserDTO();
        userDTO.setWxNickname("流控/降級返回的用戶");
        return userDTO;
    }
}

    3.2 在定義Feign client 時指定 fallback 方法

import com.contentcenter.domain.dto.user.UserDTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(name = "user-center",fallback = UserCenterFeignClientFallback.class)
public interface UserCenterFeignClient {
    /**
     * http://user-center/users/{id}
     *
     * @param id
     * @return
     */
    @GetMapping("/users/{id}")
    UserDTO findById(@PathVariable Integer id);
}

 

  4. 通過 FallbackFactory 工廠 實現降級

      通過 FallbackFactory 實現的降級可以捕獲 熔斷的異常信息,而通過方法實現的熔斷,則不能獲取熔斷的異常信息。

    所以推薦使用 FallbackFactory 進行降級實現。

import com.contentcenter.domain.dto.user.UserDTO;
import com.contentcenter.feignclient.UserCenterFeignClient;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class UserCenterFeignClientFallbackFactory implements FallbackFactory<UserCenterFeignClient> {
    @Override
    public UserCenterFeignClient create(Throwable cause) {
        return new UserCenterFeignClient() {
     // 當 UserCenterFeignClient 有多個方法時,在這里逐個重寫實現即可 @Override
public UserDTO findById(Integer id) { log.warn("遠程調用被限流/降級了", cause); UserDTO userDTO = new UserDTO(); userDTO.setWxNickname("流控/降級返回的用戶"); return userDTO; } }; } }

  調用方式:

@FeignClient(name = "user-center",fallbackFactory = UserCenterFeignClientFallbackFactory.class)

 

  5. 配置 @FeignClient 的 configuration 屬性

    通過@FeignClient 的 configuration 屬性可以實現對 feign請求的自定義封裝。如實現攔截配置,對 feign 請求添加認證請求頭,添加 ssl 認證等等。

    具體可參考我的這篇文章: spring cloud 通過feign請求設置請求頭    https://www.cnblogs.com/zjdxr-up/p/14152438.html

   

  6. 配置http 連接池

    添加依賴

    <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-httpclient</artifactId>
        </dependency>

    添加配置:

feign:
  httpclient:
    # 讓feign使用apache httpclient做請求;而不是默認的urlconnection
    enabled: true
    # feign的最大連接數
    max-connections: 200
    # feign單個路徑的最大連接數
    max-connections-per-route: 50

 

   7. feign 配合 hystrix 超時熔斷配置:

      Spring Cloud Feign HTTP請求異常 Fallback 容錯機制,它是基於Hystrix實現的,所以要通過配置參數 feign.hystrix.enabled=true 

    開啟該功能,以及配置 hystrix 超時時間。

feign:
  hystrix:
    enabled:true 
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 1000

 

 

   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM