From a1a6ee244c9818ce35b2a2b08219ea22546a3e82 Mon Sep 17 00:00:00 2001 From: Weix Sun Date: Wed, 13 Apr 2022 19:00:41 +0800 Subject: [PATCH] Fixes gh-4032 (#4086) --- .../eureka/server/EurekaController.java | 23 ++++- .../eureka/server/EurekaProperties.java | 83 +++++++++++++++++++ .../server/EurekaServerAutoConfiguration.java | 8 +- .../eureka/server/EurekaServerBootstrap.java | 7 +- .../eureka/server/EurekaControllerTests.java | 5 +- 5 files changed, 114 insertions(+), 12 deletions(-) create mode 100644 spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaProperties.java diff --git a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaController.java b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaController.java index 266dd68eb1..2f744bf335 100644 --- a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaController.java +++ b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaController.java @@ -48,6 +48,7 @@ /** * @author Spencer Gibb * @author Gang Li + * @author Weix Sun */ @Controller @RequestMapping("${eureka.dashboard.path:/}") @@ -58,8 +59,20 @@ public class EurekaController { private ApplicationInfoManager applicationInfoManager; + private final EurekaProperties eurekaProperties; + + /** + * @deprecated in favour of + * {@link EurekaController#EurekaController(ApplicationInfoManager, EurekaProperties)} + */ + @Deprecated public EurekaController(ApplicationInfoManager applicationInfoManager) { + this(applicationInfoManager, null); + } + + public EurekaController(ApplicationInfoManager applicationInfoManager, EurekaProperties eurekaProperties) { this.applicationInfoManager = applicationInfoManager; + this.eurekaProperties = eurekaProperties; } @RequestMapping(method = RequestMethod.GET) @@ -116,8 +129,14 @@ protected void populateBase(HttpServletRequest request, Map mode private void populateHeader(Map model) { model.put("currentTime", StatusResource.getCurrentTimeAsString()); model.put("upTime", StatusInfo.getUpTime()); - model.put("environment", "N/A"); // FIXME: - model.put("datacenter", "N/A"); // FIXME: + if (eurekaProperties != null) { + model.put("environment", eurekaProperties.getEnvironment()); + model.put("datacenter", eurekaProperties.getDatacenter()); + } + else { + model.put("environment", "N/A"); + model.put("datacenter", "N/A"); + } PeerAwareInstanceRegistry registry = getRegistry(); model.put("registry", registry); model.put("isBelowRenewThreshold", registry.isBelowRenewThresold() == 1); diff --git a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaProperties.java b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaProperties.java new file mode 100644 index 0000000000..483c68afa5 --- /dev/null +++ b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaProperties.java @@ -0,0 +1,83 @@ +/* + * Copyright 2013-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.netflix.eureka.server; + +import java.util.Objects; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Configuration properties for the Eureka deployment. + * + * @author Weix Sun + */ +@ConfigurationProperties("eureka") +public class EurekaProperties { + + /** + * Eureka environment. Defaults to "test". + */ + private String environment = "test"; + + /** + * Eureka datacenter. Defaults to "default". + */ + private String datacenter = "default"; + + public String getEnvironment() { + return environment; + } + + public void setEnvironment(String environment) { + this.environment = environment; + } + + public String getDatacenter() { + return datacenter; + } + + public void setDatacenter(String datacenter) { + this.datacenter = datacenter; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + EurekaProperties that = (EurekaProperties) o; + return Objects.equals(datacenter, that.datacenter) && Objects.equals(environment, that.environment); + } + + @Override + public int hashCode() { + return Objects.hash(environment, datacenter); + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("EurekaProperties{"); + sb.append("environment='").append(environment).append('\''); + sb.append(", datacenter=").append(datacenter); + sb.append('}'); + return sb.toString(); + } + +} diff --git a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerAutoConfiguration.java b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerAutoConfiguration.java index 52d3f8b1a7..3ce51d75f0 100644 --- a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerAutoConfiguration.java +++ b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerAutoConfiguration.java @@ -73,11 +73,13 @@ * @author Gunnar Hillert * @author Biju Kunjummen * @author Fahim Farook + * @author Weix Sun */ @Configuration(proxyBeanMethods = false) @Import(EurekaServerInitializerConfiguration.class) @ConditionalOnBean(EurekaServerMarkerConfiguration.Marker.class) -@EnableConfigurationProperties({ EurekaDashboardProperties.class, InstanceRegistryProperties.class }) +@EnableConfigurationProperties({ EurekaDashboardProperties.class, InstanceRegistryProperties.class, + EurekaProperties.class }) @PropertySource("classpath:/eureka/server.properties") public class EurekaServerAutoConfiguration implements WebMvcConfigurer { @@ -113,8 +115,8 @@ public HasFeatures eurekaServerFeature() { @Bean @ConditionalOnProperty(prefix = "eureka.dashboard", name = "enabled", matchIfMissing = true) - public EurekaController eurekaController() { - return new EurekaController(this.applicationInfoManager); + public EurekaController eurekaController(EurekaProperties eurekaProperties) { + return new EurekaController(this.applicationInfoManager, eurekaProperties); } static { diff --git a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerBootstrap.java b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerBootstrap.java index 9ed08208f6..131fbc898c 100644 --- a/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerBootstrap.java +++ b/spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerBootstrap.java @@ -38,6 +38,7 @@ /** * @author Spencer Gibb + * @author Weix Sun */ public class EurekaServerBootstrap { @@ -67,7 +68,6 @@ public EurekaServerBootstrap(ApplicationInfoManager applicationInfoManager, Eure public void contextInitialized(ServletContext context) { try { - initEurekaEnvironment(); initEurekaServerContext(); context.setAttribute(EurekaServerContext.class.getName(), this.serverContext); @@ -93,11 +93,6 @@ public void contextDestroyed(ServletContext context) { log.info("Eureka Service is now shutdown..."); } - protected void initEurekaEnvironment() throws Exception { - log.info("Setting the eureka configuration.."); - - } - protected void initEurekaServerContext() throws Exception { // For backward compatibility JsonXStream.getInstance().registerConverter(new V1AwareInstanceInfoConverter(), XStream.PRIORITY_VERY_HIGH); diff --git a/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/EurekaControllerTests.java b/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/EurekaControllerTests.java index 729e55a549..c192d086de 100644 --- a/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/EurekaControllerTests.java +++ b/spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/EurekaControllerTests.java @@ -96,10 +96,13 @@ static void setInstance(ApplicationInfoManager infoManager) throws IllegalAccess void testStatus() throws Exception { Map model = new HashMap<>(); - EurekaController controller = new EurekaController(infoManager); + EurekaController controller = new EurekaController(infoManager, new EurekaProperties()); controller.status(new MockHttpServletRequest("GET", "/"), model); + assertThat((String) model.get("environment")).isEqualTo("test"); + assertThat((String) model.get("datacenter")).isEqualTo("default"); + Map app = getFirst(model, "apps"); Map instanceInfo = getFirst(app, "instanceInfos"); Map instance = getFirst(instanceInfo, "instances");