最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Cloud Feign高級應用實例詳解

 更新時間:2019年12月10日 14:27:54   投稿:yaominghui  
這篇文章主要介紹了Spring Cloud Feign高級應用實例詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

這篇文章主要介紹了Spring Cloud Feign高級應用實例詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

1.使用feign進行服務間的調用

Spring boot2X Consul如何使用Feign實現服務調用

2.開啟gzip壓縮

Feign支持對請求與響應的壓縮,以提高通信效率,需要在服務消費者配置文件開啟壓縮支持和壓縮文件的類型

添加配置

feign.compression.request.enabled=true
feign.compression.response.enabled=true
feign.compression.request.mime-types=text/xml,application/xml,application/json
feign.compression.request.min-request-size=2048

3.開啟日志

配置

logging.level.com.xyz.comsumer.feign.RemoteHelloService=debug

添加FeignLogConfig類

package com.xyz.comsumer.configure;

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

@Configuration
public class FeignLogConfig {
  @Bean
  Logger.Level feignLogger(){
    return Logger.Level.FULL;
  }
}

輸出

2019-12-07 23:23:03.630 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] <--- HTTP/1.1 200 (671ms)
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] content-length: 14
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] content-type: text/plain;charset=UTF-8
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] date: Sat, 07 Dec 2019 15:23:03 GMT
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] vary: Origin
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] vary: Access-Control-Request-Method
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] vary: Access-Control-Request-Headers
2019-12-07 23:23:03.631 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] 
2019-12-07 23:23:03.632 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] hello,provider
2019-12-07 23:23:03.632 DEBUG 2668 --- [vice-provider-1] c.xyz.comsumer.feign.RemoteHelloService : [RemoteHelloService#hello] <--- END HTTP (14-byte body)

說明:

  Feign日志記錄只能響應DEBUG日志級別

對每一個Feign客戶端,可以配置一個Logger.Level對象,通過該對象控制日志輸出內容。

Logger.Level有如下幾種選擇:

NONE, 不記錄日志 (默認)。

BASIC, 只記錄請求方法和URL以及響應狀態(tài)代碼和執(zhí)行時間。

HEADERS, 記錄請求和應答的頭的基本信息。

FULL, 記錄請求和響應的頭信息,正文和元數據。

4.替換JDK默認的URLConnection為okhttp

在默認情況下 spring cloud feign在進行各個子服務之間的調用時,http組件使用的是jdk的HttpURLConnection

服務之間調用使用的HttpURLConnection,效率非常低

為了提高效率,可以通過連接池提高效率

使用okhttp,能提高qps,因為okhttp有連接池和超時時間進行調優(yōu)

添加依賴

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

修改配置

禁用默認的http,啟用okhttp

feign.okhttp.enabled=true
feign.httpclient.enabled=false

添加FeignOkHttpConfig類

package com.xyz.comsumer.configure;

import feign.Feign;
import okhttp3.ConnectionPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
public class FeignOkHttpConfig {

  @Bean
  public okhttp3.OkHttpClient okHttpClient(){
    return new okhttp3.OkHttpClient.Builder()
        .readTimeout(10, TimeUnit.SECONDS)
        .connectTimeout(10, TimeUnit.SECONDS)
        .writeTimeout(20, TimeUnit.SECONDS)
        .retryOnConnectionFailure(true)
        .connectionPool(new ConnectionPool())
        .build();
  }
}

5.超時設置

Feign調用服務的默認時長是1秒鐘

hystrix的超時時間

hystrix.command.default.execution.timeout.enabled=true
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000

說明:

  hystrix.command.default.execution.timeout.enabled 執(zhí)行是否啟用超時,默認啟用true

  hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds 命令執(zhí)行超時時間,默認1000ms

  常用的配置還有

    hystrix.command.default.execution.isolation.strategy 隔離策略,默認是Thread, 可選THREAD|SEMAPHORE,建議選擇SEMAPHORE

Feign 的負載均衡底層用的就是 Ribbon

ribbon的超時時間

ribbon.ReadTimeout=10000
ribbon.ConnectTimeout=10000

6.使用hystrix進行熔斷、降級處理

feign啟用hystrix,才能熔斷、降級

feign.hystrix.enabled=true

hystrix服務降級處理

RemoteHelloServiceFallbackImpl

package com.xyz.comsumer.feign.fallback;

import com.xyz.comsumer.feign.RemoteHelloService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class RemoteHelloServiceFallbackImpl implements RemoteHelloService {
  private final Logger logger = LoggerFactory.getLogger(RemoteHelloServiceFallbackImpl.class);

  @Override
  public String hello() {
    logger.error("feign 查詢信息失敗:{}");
    return null;
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

庄河市| 景谷| 志丹县| 尤溪县| 谷城县| 宣恩县| 福建省| 固阳县| 兴文县| 阿拉善右旗| 宁阳县| 龙江县| 宜宾县| 类乌齐县| 洛宁县| 葵青区| 五指山市| 承德县| 南丰县| 平果县| 犍为县| 新晃| 新竹县| 汤阴县| 玉环县| 高安市| 道孚县| 山阳县| 新津县| 柘城县| 唐河县| 山东省| 灌南县| 和林格尔县| 刚察县| 乡宁县| 梧州市| 讷河市| 新宾| 怀化市| 卢龙县|