首页 > 其他分享 >RibbonRoutingFilter是如何工作的

RibbonRoutingFilter是如何工作的

时间:2023-02-10 10:24:11浏览次数:33  
标签:return final RequestContext 工作 RibbonRoutingFilter public 如何 context response

在讲RibbonRoutingFilter是如何工作之前,也有一些比较重要的类需要去提前了解。


重要的类

RequestContext

请求上下文,用于存储线程中对应的请求以及响应

public class RequestContext extends ConcurrentHashMap<String, Object> {
    protected static final ThreadLocal<? extends RequestContext> threadLocal = new ThreadLocal<RequestContext>() {
    public static RequestContext getCurrentContext() {
        if (testContext != null) return testContext;

        RequestContext context = threadLocal.get();
        return context;
    }
    public void setRequest(HttpServletRequest request) {
        put("request", request);
    }
    public HttpServletRequest getRequest() {
        return (HttpServletRequest) get("request");
    }
    public void setResponse(HttpServletResponse response) {
        set("response", response);
    }
    public HttpServletResponse getResponse() {
        return (HttpServletResponse) get("response");
    }
    public void setRouteHost(URL routeHost) {
        set("routeHost", routeHost);
    }
    public URL getRouteHost() {
        return (URL) get("routeHost");
    }
}

RibbonCommandContext

ribbon命令上下文,其实就是要用于封装ribbon命令的一个容器。

public class RibbonCommandContext {
    服务的id
    private final String serviceId;
    请求的方法   
    private final String method;
    路径
    private final String uri;
    是否运行重试
    private final Boolean retryable;
    请求头
    private final MultiValueMap<String, String> headers;
    路径上的入参
    private final MultiValueMap<String, String> params;
}

RibbonRoutingFilter的工作原理

public class RibbonRoutingFilter extends ZuulFilter {
    @Override
    public Object run() {
        RequestContext context = RequestContext.getCurrentContext();
        this.helper.addIgnoredHeaders();
        try {
            1.将请求上下文转化成ribbon的命令上下文
            RibbonCommandContext commandContext = buildCommandContext(context);
            2.进行转发,获取响应---这一步的实现才是我们需要关注的。
            ClientHttpResponse response = forward(commandContext);
            setResponse(response);
            3.返回响应
            return response;
        }
        catch (ZuulException ex) {
            throw new ZuulRuntimeException(ex);
        }
        catch (Exception ex) {
            throw new ZuulRuntimeException(ex);
        }
    }
        
    获取响应的方法如下,其实我最关注的是ribbon如何知道服务id所映射的真实地址。
    protected ClientHttpResponse forward(RibbonCommandContext context) throws Exception {
        Map<String, Object> info = this.helper.debug(context.getMethod(),
                context.getUri(), context.getHeaders(), context.getParams(),
                context.getRequestEntity());
        核心代码在于此处
        RibbonCommand command = this.ribbonCommandFactory.create(context);
        try {
            ClientHttpResponse response = command.execute();
            this.helper.appendDebug(info, response.getRawStatusCode(),
                    response.getHeaders());
            return response;
        }
        catch (HystrixRuntimeException ex) {
            return handleException(info, ex);
        }

    }
}

到最后其实生成请求,如何进行访问?
那么路由规则与真实host地址是如何完成映射的?
那就需要查看路由加载器的实现。这个会在后面说到

标签:return,final,RequestContext,工作,RibbonRoutingFilter,public,如何,context,response
From: https://www.cnblogs.com/jimoliunian/p/17107986.html

相关文章

  • 浏览器如何使用 HTTP 防止限制 ip
    当我们浏览网页尤其是频发刷新网页时,会跳出来验证码。主要因为频繁刷新导致目标网页限制了您本地ip,正常过一段时间也或许恢复,如果遇到紧急的事情急需访问,不妨试试下面的操......
  • JavaScript如何做空字符串的校验
    通过正则表达式校验写法:1if(str&&/[^\s]/.test(str)){2//不为空3}else{4//为空5}注释:-正则表达式中\s是指空白,包括空格、换行、tab缩进等所有......
  • select() 出现interrupted system call如何解决
    select出现 returnsinterruptedsystemcall的报错处理方式 linux系统编程之信号(七):被信号中断的系统调用和库函数处理方式 https://www.cnblogs.com/mickole/p/319......
  • Java 中如何限制方法的返回时间
    最近在研究ChatGPT的API调用。因为ChatGPT的API调用时间通常超过30秒。所以我们希望在程序中限制这个方法的执行时间,不要让方法花太长时间去执行了。JDK方......
  • 测试最佳实践以及如何改进您的写作
    本周在我的收件箱中,我收到了一些很好的读者问题。我会尝试在这里回答他们,以便每个人都能从回复中受益!提高您作为开发人员的写作水平我想知道你在这里和很多旧文章中提......
  • 如何在游戏中实现一个好看的金币效果
    本文首发于微信公众号【小蚂蚁教你做游戏】,欢迎关注领取更多学习做游戏的原创教程资料,每天学点儿游戏开发知识。嗨!大家好,我是小蚂蚁。如图,是我最新上线的游戏【彩虹星球大冒......
  • 推进行业生态发展完善,中国信通院第八批RPA评测工作正式启动
    随着人工智能、云计算、大数据等新兴数字技术的高速发展,数字劳动力应用实践步伐加快,以数字生产力、数字创造力为基础的数字经济占比逐年上升。近年来,机器人流程自动化(Roboti......
  • JAVA中如何判断一个ResultSet结果集是否为空
    问题描述ResultSet表示select语句的查询结果集。ResultSet对象具有指向其当前数据行的指针,最初,指针被置于第一行记录之前,通过next()方法可以将指针移动到下一行记......
  • 3.7如何避免计算机计算出错
    计算机计算出错有两种避免该问题的方法:   首先是回避策略,即无视这些错误。根据程序目的的不同,有时一些微小的偏差并不会造成什么问题。一般来讲,在科学技术计算领域,......
  • ChatGPT真的可以取代基础工作岗位吗?
    最近关于人工智能对话工具ChatGPT的话题,甚嚣尘上。前几天有报道说ChatGPT通过了谷歌L3级别的面试,虽然说L3是谷歌工程团队的入门级职位。舆论和一些自媒体对这个事保持着......