View Javadoc
1   package cn.home1.cloud.netflix.eureka.server;
2   
3   import com.netflix.appinfo.InstanceInfo;
4   
5   import org.aspectj.lang.ProceedingJoinPoint;
6   import org.aspectj.lang.annotation.Around;
7   import org.aspectj.lang.annotation.Aspect;
8   import org.springframework.beans.factory.annotation.Value;
9   import org.springframework.context.annotation.Bean;
10  import org.springframework.context.annotation.Configuration;
11  import org.springframework.util.StringUtils;
12  
13  /**
14   * see: https://stackoverflow.com/questions/42296407/spring-cloud-discovery-for-multiple-service-versions
15   */
16  @Configuration
17  @Aspect
18  public class EurekaDashboardLabeler {
19  
20      private static EurekaDashboardLabeler instance = new EurekaDashboardLabeler();
21      @Value("${eureka.instance.metadata.keys.instance-group:instance-group}")
22      private String instanceGroupKey;
23  
24      /**
25       * Singleton pattern used by LTW then Spring
26       */
27      public static EurekaDashboardLabeler aspectOf() {
28          return instance;
29      }
30  
31      @Around("execution(public * com.netflix.appinfo.InstanceInfo.getId())")
32      public String versionLabelAppInstances(final ProceedingJoinPoint jp) throws Throwable {
33          final String instanceId = (String) jp.proceed();
34          for (final StackTraceElement ste : Thread.currentThread().getStackTrace()) {
35              // limit to EurekaController#populateApps in order to avoid side effects
36              if (ste.getClassName().contains("EurekaController")) {
37                  final InstanceInfo info = (InstanceInfo) jp.getThis();
38                  final String instanceGroup = info.getMetadata().get(this.instanceGroupKey);
39                  if (StringUtils.hasText(instanceGroup)) {
40                      return String.format("%s [%s]", instanceId, instanceGroup);
41                  }
42                  break;
43              }
44          }
45          return instanceId;
46      }
47  
48      @Bean("post-construct-labeler")
49      public EurekaDashboardLabeler init() {
50          return EurekaDashboardLabeler.aspectOf();
51      }
52  }