当前位置: 首页 > article >正文

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,无非做了自己处理(负载),有些类似于静态代理


http://www.kler.cn/news/135876.html

相关文章:

  • new/delete 和malloc/free的区别
  • uni-app 使用vscode开发uni-app
  • 接口自动化和UI自动化的区别
  • 实现CAS自旋锁
  • 工程项目立项需要做哪些准备?
  • 视频转码方法:多种格式视频批量转FLV视频的技巧
  • 【Linux网络】详解使用http和ftp搭建yum仓库,以及yum网络源优化
  • git常用命令和参数有哪些?【git看这一篇就够了】
  • 【开题报告】基于SpringBoot的网上摄影工作室的设计与实现
  • 前端面试考核点【更持续新中】
  • 根据nginx日志统计页面访问次数
  • 指针变量和地址
  • 11.1 文件拷贝移动与删除
  • 【Java】异常处理(一)
  • K8S基础笔记
  • 极域电子教室-教师机无法找到学生机
  • ArcEngine:如何进行缩放图层、属性信息显示、状态栏显示?
  • 关于这个“这是B站目前讲的最好的【Transformer实战】教程!“视频的目前可以运行的源代码GPU版本
  • 采集1688整店商品(店铺所有商品、店铺列表api)
  • 北京君正客户应用案例:掌静脉3D人脸猫眼视屏智能锁
  • 信息机房监控系统(动环辅助监控系统)
  • 4种经典的限流算法与集群限流
  • MAC地址_MAC地址格式_以太网的MAC帧_基础知识
  • IDEA运行thymeleaf的html文件打开端口为63342且连不上数据库
  • pnpm : 无法加载文件 E:\Soft\PromSoft\nodejs\node_global\pnpm.ps1,
  • 理解Android线程基础与多线程编程模型
  • CTFHub Git泄露
  • Spring Framework IOC依赖查找 - 按类型查找解析
  • Linux-CentOS重要模块
  • 编写高效的消息传递代码-对消息进行降维