diff --git a/apollo-assembly/src/main/resources/application.yml b/apollo-assembly/src/main/resources/application.yml index 312f88ce394..0fc2c8f08b8 100644 --- a/apollo-assembly/src/main/resources/application.yml +++ b/apollo-assembly/src/main/resources/application.yml @@ -33,3 +33,5 @@ eureka: healthcheck: enabled: true eurekaServiceUrlPollIntervalSeconds: 60 + fetch-registry: false + registerWithEureka: false \ No newline at end of file diff --git a/apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/eureka/ApolloEurekaClientConfig.java b/apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/eureka/ApolloEurekaClientConfig.java index b8adfdbde63..00c7c31f755 100644 --- a/apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/eureka/ApolloEurekaClientConfig.java +++ b/apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/eureka/ApolloEurekaClientConfig.java @@ -2,8 +2,12 @@ import com.ctrip.framework.apollo.biz.config.BizConfig; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.cloud.context.scope.refresh.RefreshScope; import org.springframework.cloud.netflix.eureka.EurekaClientConfigBean; import org.springframework.context.annotation.Primary; +import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; @@ -11,12 +15,16 @@ @Component @Primary +@ConditionalOnProperty(value = {"eureka.client.enabled"}, havingValue = "true", matchIfMissing = true) public class ApolloEurekaClientConfig extends EurekaClientConfigBean { private final BizConfig bizConfig; + private final RefreshScope refreshScope; + private static final String EUREKA_CLIENT_BEAN_NAME = "eurekaClient"; - public ApolloEurekaClientConfig(final BizConfig bizConfig) { + public ApolloEurekaClientConfig(final BizConfig bizConfig, final RefreshScope refreshScope) { this.bizConfig = bizConfig; + this.refreshScope = refreshScope; } /** @@ -27,6 +35,19 @@ public List getEurekaServerServiceUrls(String myZone) { return CollectionUtils.isEmpty(urls) ? super.getEurekaServerServiceUrls(myZone) : urls; } + @EventListener + public void listenApplicationReadyEvent(ApplicationReadyEvent event) { + this.refreshEurekaClient(); + } + + private void refreshEurekaClient() { + if (!super.isFetchRegistry()) { + super.setFetchRegistry(true); + super.setRegisterWithEureka(true); + refreshScope.refresh(EUREKA_CLIENT_BEAN_NAME); + } + } + @Override public boolean equals(Object o) { return super.equals(o); diff --git a/apollo-configservice/src/main/resources/application.yml b/apollo-configservice/src/main/resources/application.yml index 52c0a5299ae..1474a77e498 100644 --- a/apollo-configservice/src/main/resources/application.yml +++ b/apollo-configservice/src/main/resources/application.yml @@ -33,6 +33,9 @@ eureka: healthcheck: enabled: true eurekaServiceUrlPollIntervalSeconds: 60 + fetch-registry: false + registerWithEureka: false + management: health: status: