Skip to content

Commit

Permalink
fix[apollo-configService]: Solve configService startup exception (apo…
Browse files Browse the repository at this point in the history
  • Loading branch information
klboke authored May 18, 2021
1 parent d60bf88 commit 1cc8f55
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions apollo-assembly/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ eureka:
healthcheck:
enabled: true
eurekaServiceUrlPollIntervalSeconds: 60
fetch-registry: false
registerWithEureka: false
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,29 @@


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;

import java.util.List;

@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;
}

/**
Expand All @@ -27,6 +35,19 @@ public List<String> 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);
Expand Down
3 changes: 3 additions & 0 deletions apollo-configservice/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ eureka:
healthcheck:
enabled: true
eurekaServiceUrlPollIntervalSeconds: 60
fetch-registry: false
registerWithEureka: false

management:
health:
status:
Expand Down

0 comments on commit 1cc8f55

Please sign in to comment.