View Javadoc
1   package cn.home1.cloud.config.server.monitor;
2   
3   import lombok.extern.slf4j.Slf4j;
4   
5   import org.apache.commons.lang3.StringUtils;
6   import org.springframework.beans.factory.annotation.Value;
7   import org.springframework.cloud.config.monitor.PropertyPathNotificationExtractor;
8   
9   import java.util.Map;
10  import java.util.Set;
11  import java.util.regex.Matcher;
12  import java.util.regex.Pattern;
13  
14  /**
15   * 2017/1/17 yanzhang153
16   */
17  @Slf4j
18  public abstract class AbstraceNotificationExtractor implements PropertyPathNotificationExtractor {
19  
20      private static final Pattern PATH_PATTERN = Pattern.compile("^(https?)://(.+)/(.*)/commit/[a-zA-Z0-9]{7,40}");
21      private static final String ALL_APPLICATION_STR = "application";
22  
23      @Value("${spring.cloud.config.server.common-config.prefix:home1-oss-common}")
24      private String commonConfigPrefix;
25  
26      @Value("${spring.cloud.config.server.common-config.suffix:-common}")
27      private String commonConfigSuffix;
28  
29      protected void addAllPaths(final Set<String> paths, final Map<String, Object> commit, final String name) {
30          if (paths.contains(ALL_APPLICATION_STR)) {
31              return;
32          }
33          // example:
34          // http://gitlab/configserver/oss-todomvc-app-config/commit/929f67f2b38a6269e7ad63f606c9d89a7d8eb79f
35          final String url = (String) commit.get("url");
36          log.debug("config server monitor url:{}", url);
37          if (StringUtils.isBlank(url)) {
38              return;
39          }
40          final Matcher matcher = PATH_PATTERN.matcher(url);
41          if (matcher.matches()) {
42              final String repository = matcher.group(3);
43              log.debug("will add application pattern, repository name :{}, current result:{}", repository, paths);
44              final int lastIndex = repository.lastIndexOf('-');
45              if (lastIndex <= 0) {
46                  log.info("the suffix of repository name is not ends with -xxx (-config), ignored.");
47                  return;
48              }
49              final String applicationName = repository.substring(0, lastIndex);
50              String pattern = null;
51              if (applicationName.startsWith(commonConfigPrefix)) {
52                  pattern = ALL_APPLICATION_STR;// to all application
53              } else {
54                  pattern = applicationName;
55                  final int idx = applicationName.indexOf('-', 1);
56                  if (idx > 0) {
57                      pattern = applicationName.substring(0, idx);
58                  }
59                  pattern = pattern + "*";
60              }
61              if (paths.add(pattern)) {
62                  log.info("add refresh pattern {}, applicationName:{}", pattern, applicationName);
63              }
64          }
65      }
66  }