apollo源码(3)-configservice配置读取接口

1. 概述

在上文,我们看到通知变化接口,仅返回通知相关的信息,而不包括配置相关的信息。所以 Config Service 需要提供配置读取的接口

1.1 配置读取流程图

 

2. ConfigController类

配置 Controller ,仅提供 configs/{appId}/{clusterName}/{namespace:.+} 接口,提供配置读取的功能

 

2.1 构造方法

private static final Splitter X_FORWARDED_FOR_SPLITTER = Splitter.on(",").omitEmptyStrings().trimResults();
private static final Type configurationTypeReference = new TypeToken<Map<String, String>>() {}.getType();
@Autowired
private ConfigService configService;
@Autowired
private AppNamespaceServiceWithCache appNamespaceService;
@Autowired
private NamespaceUtil namespaceUtil;
@Autowired
private InstanceConfigAuditUtil instanceConfigAuditUtil;
@Autowired
private Gson gson

 

2.2 queryConfig方法

 

1: @RequestMapping(value = "/{appId}/{clusterName}/{namespace:.+}", method = RequestMethod.GET)
 2: public ApolloConfig queryConfig(@PathVariable String appId, @PathVariable String clusterName,
 3: @PathVariable String namespace,
 4: @RequestParam(value = "dataCenter", required = false) String dataCenter,
 5: @RequestParam(value = "releaseKey", defaultValue = "-1") String clientSideReleaseKey,
 6: @RequestParam(value = "ip", required = false) String clientIp,
 7: @RequestParam(value = "messages", required = false) String messagesAsString,
 8: HttpServletRequest request, HttpServletResponse response) throws IOException {
 9: String originalNamespace = namespace;
10: // 若 Namespace 名以 .properties 结尾,移除该结尾,并设置到 ApolloConfigNotification 中。例如 application.properties => application 。
11: // strip out .properties suffix
12: namespace = namespaceUtil.filterNamespaceName(namespace);
13: // 获得归一化的 Namespace 名字。因为,客户端 Namespace 会填写错大小写。
14: //fix the character case issue, such as FX.apollo <-> fx.apollo
15: namespace = namespaceUtil.normalizeNamespace(appId, namespace);
16: 
17: // 若 clientIp 未提交,从 Request 中获取。
18: if (Strings.isNullOrEmpty(clientIp)) {
19: clientIp = tryToGetClientIp(request);
20: }
21: 
22: // 解析 messagesAsString 参数,创建 ApolloNotificationMessages 对象。
23: ApolloNotificationMessages clientMessages = transformMessages(messagesAsString);
24: 
25: // 创建 Release 数组
26: List<Release> releases = Lists.newLinkedList();
27: // 获得 Namespace 对应的 Release 对象
28: String appClusterNameLoaded = clusterName;
29: if (!ConfigConsts.NO_APPID_PLACEHOLDER.equalsIgnoreCase(appId)) {
30: // 获得 Release 对象
31: Release currentAppRelease = configService.loadConfig(appId, clientIp, appId, clusterName, namespace, dataCenter, clientMessages);
32: if (currentAppRelease != null) {
33: // 添加到 Release 数组中。
34: releases.add(currentAppRelease);
35: // 获得 Release 对应的 Cluster 名字
36: // we have cluster search process, so the cluster name might be overridden
37: appClusterNameLoaded = currentAppRelease.getClusterName();
38: }
39: }
40: // 若 Namespace 为关联类型,则获取关联的 Namespace 的 Release 对象
41: // if namespace does not belong to this appId, should check if there is a public configuration
42: if (!namespaceBelongsToAppId(appId, namespace)) {
43: // 获得 Release 对象
44: Release publicRelease = this.findPublicConfig(appId, clientIp, clusterName, namespace, dataCenter, clientMessages);
45: // 添加到 Release 数组中
46: if (!Objects.isNull(publicRelease)) {
47: releases.add(publicRelease);
48: }
49: }
50: // 若获得不到 Release ,返回状态码为 404 的响应
51: if (releases.isEmpty()) {
52: response.sendError(HttpServletResponse.SC_NOT_FOUND, String.format("Could not load configurations with appId: %s, clusterName: %s, namespace: %s",
53: appId, clusterName, originalNamespace));
54: Tracer.logEvent("Apollo.Config.NotFound", assembleKey(appId, clusterName, originalNamespace, dataCenter));
55: return null;
56: }
57: 
58: // 记录 InstanceConfig
59: auditReleases(appId, clusterName, dataCenter, clientIp, releases);
60: 
61: // 计算 Config Service 的合并 ReleaseKey
62: String mergedReleaseKey = releases.stream().map(Release::getReleaseKey).collect(Collectors.joining(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR));
63: // 对比 Client 的合并 Release Key 。若相等,说明没有改变,返回状态码为 302 的响应
64: if (mergedReleaseKey.equals(clientSideReleaseKey)) {
65: // Client side configuration is the same with server side, return 304
66: response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
67: Tracer.logEvent("Apollo.Config.NotModified", assembleKey(appId, appClusterNameLoaded, originalNamespace, dataCenter));
68: return null;
69: }
70: 
71: // 创建 ApolloConfig 对象
72: ApolloC
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值