1、Apollo服务端获取应用配置源码解析

Apollo通过请求URL如:http://host:port/configs/{appId}/{clusterName}/{namespace}?ip=XXX&dataCenter=xxx&releaseKey=XXXX,来拉取应用的配置。那么服务端的逻辑是如何查询应用配置的呢?我们来分析一下。

源码分析

服务端提供配置查询的逻辑在ConfigController#queryConfig方法中。

@RequestMapping(value = "/{appId}/{clusterName}/{namespace:.+}", method = RequestMethod.GET)
public ApolloConfig queryConfig(@PathVariable String appId, @PathVariable String clusterName,
                                @PathVariable String namespace,
                                @RequestParam(value = "dataCenter", required = false) String
                                    dataCenter,
                                @RequestParam(value = "releaseKey", defaultValue = "-1") String
                                    clientSideReleaseKey,
                                @RequestParam(value = "ip", required = false) String clientIp,
                                HttpServletRequest request,
                                HttpServletResponse response) throws IOException {
  String originalNamespace = namespace;
  //strip out .properties suffix
  namespace = namespaceUtil.filterNamespaceName(namespace);@1

  if (Strings.isNullOrEmpty(clientIp)) {@2
    clientIp = tryToGetClientIp(request);
  }

  List<Release> releases = Lists.newLinkedList();

  String appClusterNameLoaded = clusterName;
  if (!ConfigConsts.NO_APPID_PLACEHOLDER.equalsIgnoreCase(appId)) {
    Release currentAppRelease = loadConfig(appId, clientIp, appId, clusterName, namespace,
        dataCenter);@3

    if (currentAppRelease != null) {
      releases.add(currentAppRelease);
      //we have cluster search process, so the cluster name might be overridden
      appClusterNameLoaded = currentAppRelease.getClusterName();
    }
  }

  //if namespace does not belong to this appId, should check if there is a public configuration
  if (!namespaceBelongsToAppId(appId, namespace)) {@4
    Release publicRelease = this.findPublicConfig(appId, clientIp, clusterName, namespace,
        dataCenter);
    if (!Objects.isNull(publicRelease)) {
      releases.add(publicRelease);
    }
  }

  if (releases.isEmpty()) {@5
    response.sendError(HttpServletResponse.SC_NOT_FOUND,
        String.format(
            "Could not load configurations with appId: %s, clusterName: %s, namespace: %s",
            appId, clusterName, originalNamespace));
    Tracer.logEvent("Apollo.Config.NotFound",
        assembleKey(appId, clusterName, originalNamespace, dataCenter));
    return null;
  }

  auditReleases(appId, clusterName, dataCenter, clientIp, releases);

  String mergedReleaseKey = FluentIterable.from(releases).transform(
      input -> input.getReleaseKey()).join(STRING_JOINER);

  if (mergedReleaseKey.equals(clientSideReleaseKey)) {@6
    // Client side configuration is the same with server side, return 304
    response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
    Tracer.logEvent("Apollo.Config.NotModified",
        assembleKey(appId, appClusterNameLoaded, originalNamespace, dataCenter));
    return null;
  }

  ApolloConfig apolloConfig = new ApolloConfig(appId, appClusterNameLoaded, originalNamespace,
      mergedReleaseKey);
  apolloConfig.setConfigurations(mergeReleaseConfigurations(releases));

  Tracer.logEvent("Apollo.Config.Found", assembleKey(appId, appClusterNameLoaded,
      originalNamespace, dataCenter));
  return apolloConfig;
}

代码@1:删除namespace名的.properties后缀。

代码@2:如果clientIp为空,获取请求IP。

代码@3:如果appId不等于ApolloNoAppIdPlaceHolder,从Release表查询发布的配置。

代码@4:如果namespace不属于该appId,从Release表查询发布的公共配置。

代码@5:如果私有和公共的配置都没有查询到,返回404。

代码@6:如果client请求的releaseKey和查询到的releaseKey值一样,则说明客户端配置是最新的,返回304.

总结

为了更直观理解服务端查询配置的流程,下面给出一张上述代码的流程图。
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要编译和搭建Apollo配置中心的码,您可以按照以下步骤进行: 1. 下载码:访问Apollo的官方GitHub仓库(https://github.com/ctripcorp/apollo),找到并下载码包。 2. 配置数据库:Apollo使用MySQL作为配置存储的数据库,您需要安装并配置MySQL数据库,并创建一个用于存储Apollo配置的数据库。 3. 导入数据库脚本:在Apollo码的scripts目录中,有一个createTable.sql文件,您需要先创建该数据库中的表结构,可以使用命令行或可视化工具导入该脚本。 4. 修改数据库连接配置:在Apollo码的apollo-configservice子项目中,找到src/main/resources/application-github.properties文件,将文件中的数据库连接配置修改为您自己的配置。 5. 编译码:进入到Apollo码的根目录,执行mvn clean package命令进行编译,这将生成一个target目录,其中包含了编译后的可执行文件。 6. 部署和运行:将编译后的target目录中的可执行文件部署到您的服务器上,执行启动脚本即可启动Apollo配置中心。您可以根据需要,修改可执行文件中的配置参数来适应您的环境需求。 在搭建Apollo配置中心之前,您还需要确保服务器上已经安装了Java环境和Maven构建工具。此外,Apollo还涉及到其他组件和依赖,需要您按照文档中的说明进行安装和配置。 运行成功后,您可以通过访问配置中心的页面,进行应用创建、配置管理和发布等操作。同时,Apollo还提供了丰富的扩展接口和功能,可以满足不同场景的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值