亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

feign 如何獲取請求真實(shí)目的ip地址

 更新時(shí)間:2021年06月24日 12:05:25   作者:帆影匆匆  
這篇文章主要介紹了feign 獲取請求真實(shí)目的ip地址操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

需求

最近小編的項(xiàng)目中出現(xiàn)了很多feign 調(diào)用出現(xiàn) Read Time out 的異常,但因?yàn)闆]有集成鏈路追蹤的第三方框架,查不到原因。

所以想到打印請求的ip地址,判斷是指定的服務(wù)器出現(xiàn)的問題還是所有服務(wù)器都有這個問題,但是feign 打印異常日志不會顯示目的端地址,這就很難受了沒辦法只能自己改裝下

大致想法

需要改裝肯定需要知道feign 具體請求調(diào)用的源碼,大致需要知道下面幾個問題

  • feign 集成了ribbon 如何在負(fù)載均衡之后獲取真實(shí)的ip地址
  • feign 實(shí)際請求 http 源碼在哪
  • 能否替換 feign http 請求的組件

源碼解析

之前小編有兩篇文章分析過 feign相關(guān)的源碼

自定義 feign 調(diào)用實(shí)現(xiàn) hystrix 超時(shí)、異常熔斷

Feign 集成 Hystrix實(shí)現(xiàn)不同的調(diào)用接口不同的設(shè)置

這其中有個關(guān)鍵的源碼位置在于 InvocationHandler 的 invoke 方法,在feign 組件中大致有兩個類實(shí)現(xiàn)了此接口

FeignInvocationHandler
HystrixInvocationHandler

如果 項(xiàng)目中使用了 Hystrix 那么會用到HystrixInvocationHandler那個,否則一般是FeignInvocationHandler(自定義組件的除外)

那么此時(shí)只需要在invoke 方法中打個斷點(diǎn)就行

此時(shí)跟蹤到

feign.SynchronousMethodHandler#executeAndDecode
Object executeAndDecode(RequestTemplate template) throws Throwable {
    Request request = targetRequest(template);
	.......
    Response response;
    long start = System.nanoTime();
    try {
      // 真正執(zhí)行請求 
      response = client.execute(request, options);
      
      response.toBuilder().request(request).build();
    } catch (IOException e) {
      ....
      throw errorExecuting(request, e);
    }
    .....
  }

通過debug就知道這個 client 是

LoadBalancerFeignClient
org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient#execute
public Response execute(Request request, Request.Options options) throws IOException {
		try {
			URI asUri = URI.create(request.url());
			String clientName = asUri.getHost();
			URI uriWithoutHost = cleanUrl(request.url(), clientName);
			
			// 封裝 ribbon 請求組件
			FeignLoadBalancer.RibbonRequest ribbonRequest = new FeignLoadBalancer.RibbonRequest(
					this.delegate, request, uriWithoutHost);
			IClientConfig requestConfig = getClientConfig(options, clientName);
			// 這行是關(guān)鍵
			return 
					// 獲取 FeignLoadBalancer
					lbClient(clientName)
					// 負(fù)載之后請求真實(shí)的url
					// com.netflix.client.AbstractLoadBalancerAwareClient#executeWithLoadBalancer(....)
					.executeWithLoadBalancer(ribbonRequest,requestConfig)
					.toResponse();
		}
		catch (ClientException e) {
			....
			throw new RuntimeException(e);
		}
	}
com.netflix.client.AbstractLoadBalancerAwareClient#executeWithLoadBalancer(....)
public T executeWithLoadBalancer(final S request, final IClientConfig requestConfig) throws ClientException {
        LoadBalancerCommand<T> command = buildLoadBalancerCommand(request, requestConfig);
        try {
        	// 在com.netflix.loadbalancer.reactive.LoadBalancerCommand#submit 中會根據(jù) 負(fù)載均衡算法之后獲取到真實(shí)的ip地址
            return command.submit(
                new ServerOperation<T>() {
                    @Override
                    // 傳入的server 就是真實(shí)的ip
                    public Observable<T> call(Server server) {
                        URI finalUri = reconstructURIWithServer(server, request.getUri());
                        // 路徑替換把原本 http://client-name/xxxx 地址改為 http://127.0.0.1:9090/xxxx
                        S requestForServer = (S) request.replaceUri(finalUri);
                        try {
                        	// 請求父類中的 execute 方法,也就是 上面 lbClient(clientName) 返回的 FeignLoadBalancer
                            return Observable.just(AbstractLoadBalancerAwareClient.this.execute(requestForServer, requestConfig));
                        } 
                        catch (Exception e) {
                            return Observable.error(e);
                        }
                    }
                })
                .toBlocking()
                .single();
        } catch (Exception e) {
            Throwable t = e.getCause();
            if (t instanceof ClientException) {
                throw (ClientException) t;
            } else {
                throw new ClientException(e);
            }
        }        
    }
org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer#execute
@Override
 public RibbonResponse execute(RibbonRequest request, IClientConfig configOverride)
   throws IOException {
  Request.Options options;
  .....
  // 這里的 request 就是 `org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient#execute` 
  // 封裝的FeignLoadBalancer.RibbonRequest
  // request.client() 返回就是 feign.Client.Default  
  Response response = request.client().execute(request.toRequest(), options);
  return new RibbonResponse(request.getUri(), response);
 }
feign.Client.Default#execute
 @Override
    public Response execute(Request request, Options options) throws IOException {
      HttpURLConnection connection = convertAndSend(request, options);
      return convertResponse(connection).toBuilder().request(request).build();
    }

這里的request 中 url 就是真實(shí)的url資源路徑了

現(xiàn)在屢屢邏輯

org.springframework.cloud.openfeign.ribbon.LoadBalancerFeignClient和feign.Client.Default

都實(shí)現(xiàn)了 feign.Client 接口,但是 LoadBalancerFeignClient 實(shí)際上調(diào)用的還是 feign.Client.Default,無非做了自己處理(負(fù)載),有些類似于靜態(tài)代理

那么上面的問題就只剩下能否替換的問題了

@Configuration
class DefaultFeignLoadBalancedConfiguration {
	@Bean
	@ConditionalOnMissingBean
	public Client feignClient(CachingSpringLoadBalancerFactory cachingFactory,
				SpringClientFactory clientFactory) {
		return new LoadBalancerFeignClient(new Client.Default(null, null),
				cachingFactory, clientFactory);
	}
}

這就不需要我來過多解釋了,我們只需要自定義一個 LoadBalancerFeignClient 或者 實(shí)現(xiàn)Client的類就行 然后注入就行

實(shí)現(xiàn)代碼

我選擇的是 自定義實(shí)現(xiàn)一個 Client,去繼承 feign.Client.Default

@Slf4j
public class InFeignClient extends Client.Default {
    /**
     */
    public InFeignClient(SSLSocketFactory sslContextFactory, HostnameVerifier hostnameVerifier) {
        super(sslContextFactory, hostnameVerifier);
    }
    @Override
    public Response execute(Request request, Request.Options options) throws IOException {
        try {
            return super.execute(request, options);
        } catch (IOException e) {
            log.warn(" 請求 {} 異常 ======> {}", request.url(), e.getMessage());
            throw e;
        }
    }
}

然后將這個類替換

@Component
public class RestConfig {
    public CachingSpringLoadBalancerFactory cachingLBClientFactory(
            SpringClientFactory factory) {
        return new CachingSpringLoadBalancerFactory(factory);
    }
    @Bean
    public Client feignClient(SpringClientFactory clientFactory) {
        CachingSpringLoadBalancerFactory bean = cachingLBClientFactory(clientFactory);
        return new LoadBalancerFeignClient(new InFeignClient(null, null), bean, clientFactory);
    }    
}

相關(guān)文章

  • Mybatis Integer類型參數(shù)值為0時(shí)得到為空的解決方法

    Mybatis Integer類型參數(shù)值為0時(shí)得到為空的解決方法

    這篇文章主要介紹了Mybatis Integer類型參數(shù)值為0時(shí)得到為空的解決方法,有需要的朋友們可以學(xué)習(xí)下。
    2019-08-08
  • 解析Hibernate + MySQL中文亂碼問題

    解析Hibernate + MySQL中文亂碼問題

    如果持久化的類中有包括了漢字的String對象,那么對應(yīng)到數(shù)據(jù)庫中漢字的部分就會是亂碼。這主要是由于MySQL數(shù)據(jù)表的字符集與我們當(dāng)前使用的本地字符集不相同造成的
    2013-07-07
  • SpringMVC攔截器實(shí)現(xiàn)監(jiān)聽session是否過期詳解

    SpringMVC攔截器實(shí)現(xiàn)監(jiān)聽session是否過期詳解

    這篇文章主要介紹了SpringMVC攔截器實(shí)現(xiàn)監(jiān)聽session是否過期詳解,還是比較不錯的,這里分享給大家,供需要的朋友參考。
    2017-11-11
  • SpringBoot項(xiàng)目中公共字段填充的實(shí)現(xiàn)

    SpringBoot項(xiàng)目中公共字段填充的實(shí)現(xiàn)

    本文主要介紹了SpringBoot項(xiàng)目中公共字段填充的實(shí)現(xiàn),利用SpringBoot的Aop思想和自定義注解和反射機(jī)制的方法來實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10
  • SpringBoot使用過濾器、攔截器和監(jiān)聽器的案例代碼(Springboot搭建java項(xiàng)目)

    SpringBoot使用過濾器、攔截器和監(jiān)聽器的案例代碼(Springboot搭建java項(xiàng)目)

    這篇文章主要介紹了SpringBoot使用過濾器、攔截器和監(jiān)聽器(Springboot搭建java項(xiàng)目),本文是基于Springboot搭建java項(xiàng)目,結(jié)合案例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-02-02
  • 圖文詳解Java中的序列化機(jī)制

    圖文詳解Java中的序列化機(jī)制

    java中的序列化可能大家像我一樣都停留在實(shí)現(xiàn)Serializable接口上,對于它里面的一些核心機(jī)制沒有深入了解過。本文將通過示例帶大家深入了解Java中的序列化機(jī)制,需要的可以參考一下
    2022-10-10
  • kafka啟動報(bào)錯(Cluster ID)不匹配問題以及解決

    kafka啟動報(bào)錯(Cluster ID)不匹配問題以及解決

    這篇文章主要介紹了kafka啟動報(bào)錯(Cluster ID)不匹配問題以及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • java中判斷對象類型的3種方法舉例

    java中判斷對象類型的3種方法舉例

    在Java這種強(qiáng)類型語言中類型轉(zhuǎn)換、類型判斷是經(jīng)常遇到的,下面這篇文章主要給大家介紹了關(guān)于java中判斷對象類型的3種方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01
  • IDEA創(chuàng)建Java Web項(xiàng)目不能及時(shí)刷新HTML或JSP頁面問題

    IDEA創(chuàng)建Java Web項(xiàng)目不能及時(shí)刷新HTML或JSP頁面問題

    這篇文章主要介紹了IDEA創(chuàng)建Java Web項(xiàng)目不能及時(shí)刷新HTML或JSP頁面問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 利用java生成二維碼工具類示例代碼

    利用java生成二維碼工具類示例代碼

    二維碼對現(xiàn)在的人們來說再熟悉不過了,我們在開發(fā)的時(shí)候也經(jīng)常會用到二維碼,下面這篇文章主要給大家介紹了關(guān)于利用java生成二維碼工具類的相關(guān)資料,文中給了詳細(xì)的示例代碼,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-09-09

最新評論