Nacos 配置中心之主动拉取

news2024/10/6 0:34:21

客户端

客户端的配置有两种方式来维持,一是客户端主动拉取,而是客户端长轮询更新

配置文件的种类
1、本地配置文件: 本地就已经存在的配置文件
2、 本地缓存文件: 从服务端获取的保存在了本地 (本地生成了文件)
3、 cacheData 缓存数据: 内存中缓存的配置文件数据

客户端主动获取
从源码的ConfigExample中,可以看到获取配置的方法:
NacosConfigService.getConfig

1、优先从本地配置中获取
2、本地配置中没有则会去服务端获取
3、 服务端异常且异常不是因为鉴权失败,则从本地缓存文件中获取

    
    private String getConfigInner(String tenant, String dataId, String group, long timeoutMs) throws NacosException {
        group = null2defaultGroup(group);
        ParamUtils.checkKeyParam(dataId, group);
        ConfigResponse cr = new ConfigResponse();

        cr.setDataId(dataId);
        cr.setTenant(tenant);
        cr.setGroup(group);

        // 1、优先使用本地配置
        String content = LocalConfigInfoProcessor.getFailover(agent.getName(), dataId, group, tenant);
        if (content != null) {
            LOGGER.warn("[{}] [get-config] get failover ok, dataId={}, group={}, tenant={}, config={}", agent.getName(),
                dataId, group, tenant, ContentUtils.truncateContent(content));
            cr.setContent(content);
            configFilterChainManager.doFilter(null, cr);
            content = cr.getContent();
            return content;
        }

        try {
            // 2、本地配置没有则会从服务端获取
            String[] ct = worker.getServerConfig(dataId, group, tenant, timeoutMs);
            cr.setContent(ct[0]);

            configFilterChainManager.doFilter(null, cr);
            content = cr.getContent();

            return content;
        } catch (NacosException ioe) {
            if (NacosException.NO_RIGHT == ioe.getErrCode()) {
                throw ioe;
            }
            LOGGER.warn("[{}] [get-config] get from server error, dataId={}, group={}, tenant={}, msg={}",
                agent.getName(), dataId, group, tenant, ioe.toString());
        }

        LOGGER.warn("[{}] [get-config] get snapshot ok, dataId={}, group={}, tenant={}, config={}", agent.getName(),
            dataId, group, tenant, ContentUtils.truncateContent(content));
        // 3、如果服务端获取异常,而异常不是403(鉴权失败),则从本地缓存中获取
        content = LocalConfigInfoProcessor.getSnapshot(agent.getName(), dataId, group, tenant);
        cr.setContent(content);
        configFilterChainManager.doFilter(null, cr);
        content = cr.getContent();
        return content;
    }

下面分析下从服务器端获取配置文件的方法


    public String[] getServerConfig(String dataId, String group, String tenant, long readTimeout)
        throws NacosException {
        String[] ct = new String[2];
        if (StringUtils.isBlank(group)) {
            group = Constants.DEFAULT_GROUP;
        }

        HttpResult result = null;
        try {
            List<String> params = null;
            if (StringUtils.isBlank(tenant)) {
                params = new ArrayList<String>(Arrays.asList("dataId", dataId, "group", group));
            } else {
                params = new ArrayList<String>(Arrays.asList("dataId", dataId, "group", group, "tenant", tenant));
            }
            // 发起请求:/v1/cs/configs
            result = agent.httpGet(Constants.CONFIG_CONTROLLER_PATH, null, params, agent.getEncode(), readTimeout);
        } catch (IOException e) {
            String message = String.format(
                "[%s] [sub-server] get server config exception, dataId=%s, group=%s, tenant=%s", agent.getName(),
                dataId, group, tenant);
            LOGGER.error(message, e);
            throw new NacosException(NacosException.SERVER_ERROR, e);
        }

        switch (result.code) {
            case HttpURLConnection.HTTP_OK:
                // 成功时候创建的本地缓存文件, 内容存的是返回的是result.content
                LocalConfigInfoProcessor.saveSnapshot(agent.getName(), dataId, group, tenant, result.content);
                ct[0] = result.content;
                if (result.headers.containsKey(CONFIG_TYPE)) {
                    ct[1] = result.headers.get(CONFIG_TYPE).get(0);
                } else {
                    ct[1] = ConfigType.TEXT.getType();
                }
                return ct;
            case HttpURLConnection.HTTP_NOT_FOUND:
                // 失败时候,创建的本地缓存,内容存的是 null
                LocalConfigInfoProcessor.saveSnapshot(agent.getName(), dataId, group, tenant, null);
                return ct;
            case HttpURLConnection.HTTP_CONFLICT: {
                LOGGER.error(
                    "[{}] [sub-server-error] get server config being modified concurrently, dataId={}, group={}, "
                        + "tenant={}", agent.getName(), dataId, group, tenant);
                throw new NacosException(NacosException.CONFLICT,
                    "data being modified, dataId=" + dataId + ",group=" + group + ",tenant=" + tenant);
            }
            case HttpURLConnection.HTTP_FORBIDDEN: {
                LOGGER.error("[{}] [sub-server-error] no right, dataId={}, group={}, tenant={}", agent.getName(), dataId,
                    group, tenant);
                throw new NacosException(result.code, result.content);
            }
            default: {
                LOGGER.error("[{}] [sub-server-error]  dataId={}, group={}, tenant={}, code={}", agent.getName(), dataId,
                    group, tenant, result.code);
                throw new NacosException(result.code,
                    "http error, code=" + result.code + ",dataId=" + dataId + ",group=" + group + ",tenant=" + tenant);
            }
        }
    }

服务端

ConfigController
进行参数的解析

    @GetMapping
    @Secured(action = ActionTypes.READ, parser = ConfigResourceParser.class)
    public void getConfig(HttpServletRequest request, HttpServletResponse response,
                          @RequestParam("dataId") String dataId, @RequestParam("group") String group,
                          @RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY)
                              String tenant,
                          @RequestParam(value = "tag", required = false) String tag)
        throws IOException, ServletException, NacosException {
        // check params
        ParamUtils.checkParam(dataId, group, "datumId", "content");
        ParamUtils.checkParam(tag);

        final String clientIp = RequestUtil.getRemoteIp(request);
        inner.doGetConfig(request, response, dataId, group, tenant, tag, clientIp);
    }
    /**
     * 同步配置获取接口
     */
    public String doGetConfig(HttpServletRequest request, HttpServletResponse response, String dataId, String group,
                              String tenant, String tag, String clientIp) throws IOException, ServletException {
        final String groupKey = GroupKey2.getKey(dataId, group, tenant);
        String autoTag = request.getHeader("Vipserver-Tag");
        String requestIpApp = RequestUtil.getAppName(request);
        // 读取读锁,获取失败会自旋重复获取10次
        int lockResult = tryConfigReadLock(groupKey);

        final String requestIp = RequestUtil.getRemoteIp(request);
        boolean isBeta = false;
        if (lockResult > 0) {
            FileInputStream fis = null;
            try {
                String md5 = Constants.NULL;
                long lastModified = 0L;
                CacheItem cacheItem = ConfigService.getContentCache(groupKey);
                if (cacheItem != null) {
                    if (cacheItem.isBeta()) {
                        if (cacheItem.getIps4Beta().contains(clientIp)) {
                            isBeta = true;
                        }
                    }
                    String configType = cacheItem.getType();
                    response.setHeader("Config-Type", (null != configType) ? configType : "text");
                }
                File file = null;
                ConfigInfoBase configInfoBase = null;
                PrintWriter out = null;
                if (isBeta) {
                    md5 = cacheItem.getMd54Beta();
                    lastModified = cacheItem.getLastModifiedTs4Beta();
                    if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) {
                        configInfoBase = persistService.findConfigInfo4Beta(dataId, group, tenant);
                    } else {
                        file = DiskUtil.targetBetaFile(dataId, group, tenant);
                    }
                    response.setHeader("isBeta", "true");
                } else {
                    if (StringUtils.isBlank(tag)) {
                        if (isUseTag(cacheItem, autoTag)) {
                            if (cacheItem != null) {
                                if (cacheItem.tagMd5 != null) {
                                    md5 = cacheItem.tagMd5.get(autoTag);
                                }
                                if (cacheItem.tagLastModifiedTs != null) {
                                    lastModified = cacheItem.tagLastModifiedTs.get(autoTag);
                                }
                            }
                            if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) {
                                configInfoBase = persistService.findConfigInfo4Tag(dataId, group, tenant, autoTag);
                            } else {
                                file = DiskUtil.targetTagFile(dataId, group, tenant, autoTag);
                            }

                            response.setHeader("Vipserver-Tag",
                                URLEncoder.encode(autoTag, StandardCharsets.UTF_8.displayName()));
                        } else {
                            md5 = cacheItem.getMd5();
                            lastModified = cacheItem.getLastModifiedTs();
                            if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) {
                                configInfoBase = persistService.findConfigInfo(dataId, group, tenant);
                            } else {
                                file = DiskUtil.targetFile(dataId, group, tenant);
                            }
                            if (configInfoBase == null && fileNotExist(file)) {
                                // FIXME CacheItem
                                // 不存在了无法简单的计算推送delayed,这里简单的记做-1
                                ConfigTraceService.logPullEvent(dataId, group, tenant, requestIpApp, -1,
                                    ConfigTraceService.PULL_EVENT_NOTFOUND, -1, requestIp);

                                // pullLog.info("[client-get] clientIp={}, {},
                                // no data",
                                // new Object[]{clientIp, groupKey});

                                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
                                response.getWriter().println("config data not exist");
                                return HttpServletResponse.SC_NOT_FOUND + "";
                            }
                        }
                    } else {
                        if (cacheItem != null) {
                            if (cacheItem.tagMd5 != null) {
                                md5 = cacheItem.tagMd5.get(tag);
                            }
                            if (cacheItem.tagLastModifiedTs != null) {
                                Long lm = cacheItem.tagLastModifiedTs.get(tag);
                                if (lm != null) {
                                    lastModified = lm;
                                }
                            }
                        }
                        if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) {
                            configInfoBase = persistService.findConfigInfo4Tag(dataId, group, tenant, tag);
                        } else {
                            file = DiskUtil.targetTagFile(dataId, group, tenant, tag);
                        }
                        if (configInfoBase == null && fileNotExist(file)) {
                            // FIXME CacheItem
                            // 不存在了无法简单的计算推送delayed,这里简单的记做-1
                            ConfigTraceService.logPullEvent(dataId, group, tenant, requestIpApp, -1,
                                ConfigTraceService.PULL_EVENT_NOTFOUND,
                                -1, requestIp);

                            // pullLog.info("[client-get] clientIp={}, {},
                            // no data",
                            // new Object[]{clientIp, groupKey});

                            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
                            response.getWriter().println("config data not exist");
                            return HttpServletResponse.SC_NOT_FOUND + "";
                        }
                    }
                }

                response.setHeader(Constants.CONTENT_MD5, md5);
                /**
                 *  禁用缓存
                 */
                response.setHeader("Pragma", "no-cache");
                response.setDateHeader("Expires", 0);
                response.setHeader("Cache-Control", "no-cache,no-store");
                if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) {
                    response.setDateHeader("Last-Modified", lastModified);
                } else {
                    fis = new FileInputStream(file);
                    response.setDateHeader("Last-Modified", file.lastModified());
                }

                if (STANDALONE_MODE && !PropertyUtil.isStandaloneUseMysql()) {
                    out = response.getWriter();
                    out.print(configInfoBase.getContent());
                    out.flush();
                    out.close();
                } else {
                    fis.getChannel().transferTo(0L, fis.getChannel().size(),
                        Channels.newChannel(response.getOutputStream()));
                }

                LogUtil.pullCheckLog.warn("{}|{}|{}|{}", groupKey, requestIp, md5, TimeUtils.getCurrentTimeStr());

                final long delayed = System.currentTimeMillis() - lastModified;

                // TODO distinguish pull-get && push-get
                // 否则无法直接把delayed作为推送延时的依据,因为主动get请求的delayed值都很大
                ConfigTraceService.logPullEvent(dataId, group, tenant, requestIpApp, lastModified,
                    ConfigTraceService.PULL_EVENT_OK, delayed,
                    requestIp);

            } finally {
                // 释放锁
                releaseConfigReadLock(groupKey);
                if (null != fis) {
                    fis.close();
                }
            }
        } else if (lockResult == 0) {

            // FIXME CacheItem 不存在了无法简单的计算推送delayed,这里简单的记做-1
            ConfigTraceService.logPullEvent(dataId, group, tenant, requestIpApp, -1,
                ConfigTraceService.PULL_EVENT_NOTFOUND, -1, requestIp);

            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            response.getWriter().println("config data not exist");
            return HttpServletResponse.SC_NOT_FOUND + "";

        } else {

            pullLog.info("[client-get] clientIp={}, {}, get data during dump", clientIp, groupKey);

            response.setStatus(HttpServletResponse.SC_CONFLICT);
            response.getWriter().println("requested file is being modified, please try later.");
            return HttpServletResponse.SC_CONFLICT + "";

        }

        return HttpServletResponse.SC_OK + "";
    }

在上面的方法里主要做了以下几件事
1、获取读锁,获取不到就自旋重复获取10次
2、根据beta、tag、autoTag来判断读什么配置
3、PropertyUtil.isDirectRead() 判断是读 mysql 还是配置文件
4、使用jdk的零拷贝传输直接将文件输入流转response输出流
5、释放读取
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/83481.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【分享】学浪PC端登录分析及实现

本文所有教程及源码、软件仅为技术研究。不涉及计算机信息系统功能的删除、修改、增加、干扰,更不会影响计算机信息系统的正常运行。不得将代码用于非法用途,如侵立删!学浪PC端登录分析及实现 环境 win10Fiddlerchrome学浪PC端登录:aHR0cHM6Ly9zdHVkZW50LWFwaS5peWluY2Fpc2…

AE 动效制作和交付方案

在界面设计中&#xff0c;设计师利用动效让整个界面更加活泼&#xff0c;给界面元素带来生命力&#xff0c;解决功能上的问题&#xff0c;在更好地展示产品功能的基础上&#xff0c;凸显品牌的特色。而作为用户&#xff0c;动效增强了体验者的审美感受、情感需要&#xff0c;让…

德才论

目录 1015:德才论 输入格式&#xff1a; 输出格式&#xff1a; 输入样例&#xff1a; 输出样例&#xff1a; 思路: 1.技巧&#xff1a; 1.2结构体代码: 2.分类: 1.德才分类 1.2德才分类代码: 2.cmp函数 2.1 cmp函数代码: 3.超时问题(易错&#xff0c;算法要优化) 总代码…

神经网络优化

提升深度神经网络&#xff1a;超参数调节&#xff0c;正则化&#xff0c;优化 之前已经学习了如何构建神经网络&#xff0c;本章将继续学习如何有效运行神经网络&#xff0c;内容涉及超参数调优&#xff0c;如何构建数据以及如何确保优化算法快速运行&#xff0c;从而使学习算…

LVM卷在线扩容报错:resize would cause inodes_count overflow

一、问题描述 某次在线环境&#xff0c;存储使用率告警在线扩容时&#xff0c;文件系统扩容失败&#xff0c;报错如下&#xff1a; Size of logical volume sihua/video changed from <162.00 TiB (42467321 extents) to <258.00 TiB (67633142 extents).Logical volu…

[附源码]计算机毕业设计付费自习室管理小程序Springboot程序

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; Springboot mybatis MavenVue等等组成&#xff0c;B/S模式…

【JDBC上篇】什么是JDBC

文章目录1、对JDBC本质的理解2、代码模拟JDBC的本质3、JDBC编程的六步4、通过类加载的方式注册驱动5、属性配置文件与JDBC6、处理查询结果集1、对JDBC本质的理解 Java DataBase Connectivity&#xff08;Java语言连接数据库&#xff09;&#xff0c;其本质是SUN公司指定的一套…

CSP22.3 T4通信系统管理

之前在CCF CSP认证2022年3月完整题解这篇博客记录了自己花了两天时间乱搞出来的方法&#xff0c;但是实际上动态维护区间最值&#xff0c;通过setsetset实现会更简洁&#xff0c;用优先队列需要额外开数组记录堆中节点的有效性。 而且在处理额度失效上&#xff0c;我也使用了最…

教你用响应式建站平台搭建网站

响应式网站搭建大家知道是什么吗&#xff1f;我们可以经常听到PC端网站、移动端网站&#xff0c;这些就是为特定终端而制作的网站版本&#xff0c;而响应式网站就是一个网站能够兼容多个终端&#xff0c;而不是为每个终端做一个特定的版本。那么我们怎么用响应式建站平台搭建网…

基于极限学习机(ELM)进行多变量用电量预测(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️❤️&#x1f4a5;&#x1f4a5;&#x1f4a5; &#x1f389;作者研究&#xff1a;&#x1f3c5;&#x1f3c5;&#x1f3c5;本科计算机专业&#xff0c;研究生电气学硕…

使用adb命令导出hprof文件

我们可以使用android studio profiler导入hprof文件分析android应用的内存问题。那么如何从手机上面导出这份文件呢&#xff1f; 把应用的heap 区的数据取出来保存在一个hprof文件上&#xff0c;然后把hprof文件导入到可以查看这个文件的工具上&#xff08;如android studio p…

Spring Security总结

目录 介绍 项目搭建 Security认证 UserDetailsService 内存认证 数据库认证 PasswordEncoder密码解析器 自定义登录页面 退出登录 CSRF防护 Remember me Security授权 RBAC 权限表设计 查询访问权限 配置类设置访问权限 自定义访问控制逻辑 注解设置访问权限 Secured Pre…

29.前端笔记-HTML-Html5的新特性

目录1、HtML5新增的语义化标签2、HTML5新增的多媒体标签&#xff08;1&#xff09;音频标签&#xff1a;< audio>audio的常见属性&#xff08;2&#xff09;视频标签&#xff1a;< video>video常见属性3、新增表单元素input的类型type4、新增表单属性1、HtML5新增的…

R语言中的生存分析Survival analysis晚期肺癌患者4例

第1部分&#xff1a;生存分析简介 最近我们被客户要求撰写关于生存分析的研究报告&#xff0c;包括一些图形和统计输出。本演示文稿将介绍生存分析 &#xff0c;参考&#xff1a; Clark, T., Bradburn, M., Love, S., & Altman, D. (2003). Survival analysis part I: Ba…

06 数学软件与建模---拟合

一、知识储备 1.曲线拟合问题的提法 已知一组&#xff08;二维&#xff09;数据&#xff0c;即平面上 n个点&#xff08;xi,yi) i1,…,n, 寻求一个函数&#xff08;曲线&#xff09;yf(x), 使 f(x) 在某种准则下与所有数据点最为接近&#xff0c;即曲线拟合得最好&#xff0…

前后端分离项目-Springboot 【后端框架搭建,SSM】

1.创建新项目 点击File->New->Project 选择Spring Initializr 填写信息 修改Group修改Artifact修改管理类型Maven&#xff08;带有文件目录&#xff09;修改Java version 选择依赖 这里只需要选择Web->Spring Web即可 创建的文件目录如下 2.创建文件目录 controlle…

【深入浅出Spring原理及实战】「原理分析专题」从零开始教你SpringEL表达式使用和功能分析讲解指南(上篇)

Spring EL表达式语言,这种语言jsp中学到的el,但是在整个spring之中其表达式语言要更加的复杂,而且支持度更加的广泛,最重要的是他可以进行方法的调用,对象的实例化,集合操作等等,但是唯一的难点就是:代码太复杂了,表达式太复杂了。深刻领会,spring中针对于字符串的改进,程序员使…

基于jsp+mysql+ssm医院出车管理与绩效分配系统-计算机毕业设计

项目介绍 本毕业设计主要实现集人性化、高效率、便捷等优点于一身的出车管理系统与绩效分配系统&#xff0c;完成系统用户管理、车辆信息管理、调度员信息管理、驾驶员信息管理、救护员信息管理、科室人员管理、院领导信息管理、调度信息管理、出车统计等功能模块。系统通过浏…

Spring Cloud微服务之loadbalancer负载平衡

Spring Cloud微服务之loadbalancer负载平衡 小学时候&#xff0c;曾经做过这样的数学题。 说有一个水池子&#xff0c;上面有一个排水管&#xff0c;下面有一个进水管&#xff0c;开一个进水管&#xff0c;6个小时灌满水池&#xff0c;开一个排水管&#xff0c;10个小时放光整…

给博客网站添加loading加载中动画代码

只需添加几行代码&#xff0c;就可以加强浏览者的体验,不在等待页面加载时感到枯燥&#xff0c;从而关闭网页&#xff0c;很多网站都会制作一个“网页正在加载中”的提示效果或显示加载进程&#xff0c;加载完成后提示消失&#xff0c;大部分都应用在网站的首页。当然网上很多教…