openfeign、nacos获取接口提供方真实IP

源码分析

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);
			// 这行是关键
			return 
					// 获取 FeignLoadBalancer
					lbClient(clientName)
					// 负载之后请求真实的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 中会根据 负载均衡算法之后获取到真实的ip地址

            return command.submit(
                new ServerOperation<T>() {
                    @Override
                    // 传入的server 就是真实的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 就是真实的url资源路径了

现在屡屡逻辑

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

都实现了 feign.Client 接口,但是 LoadBalancerFeignClient 实际上调用的还是 feign.Client.Default,无非做了自己处理(负载),有些类似于静态代理

相关推荐
尘浮生1 分钟前
Java项目实战II基于Spring Boot的光影视频平台(开发文档+数据库+源码)
java·开发语言·数据库·spring boot·后端·maven·intellij-idea
尚学教辅学习资料9 分钟前
基于SpringBoot的医药管理系统+LW示例参考
java·spring boot·后端·java毕业设计·医药管理
雷神乐乐25 分钟前
File.separator与File.separatorChar的区别
java·路径分隔符
小刘|30 分钟前
《Java 实现希尔排序:原理剖析与代码详解》
java·算法·排序算法
逊嘘1 小时前
【Java语言】抽象类与接口
java·开发语言·jvm
morris1311 小时前
【SpringBoot】Xss的常见攻击方式与防御手段
java·spring boot·xss·csp
七星静香1 小时前
laravel chunkById 分块查询 使用时的问题
java·前端·laravel
Jacob程序员1 小时前
java导出word文件(手绘)
java·开发语言·word
ZHOUPUYU1 小时前
IntelliJ IDEA超详细下载安装教程(附安装包)
java·ide·intellij-idea