Skip to content

fix[apollo-configService]: Solve configService startup exception #3679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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