Skip to content

Commit

Permalink
Fixed bug: service could not register to Consul.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gannalyo committed Nov 28, 2019
1 parent e3a8331 commit 665bd81
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.health.model.Check;
import com.ecwid.consul.v1.session.model.NewSession;
import org.apache.servicecomb.saga.alpha.core.cache.ITxleCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.cloud.consul.ConditionalOnConsulEnabled;
import org.springframework.cloud.consul.ConsulProperties;
Expand All @@ -35,13 +38,16 @@
*/
@ConditionalOnConsulEnabled
@AutoConfigureAfter(ConsulProperties.class)
public class TxleConsulClient {
public class TxleConsulClient implements ApplicationRunner {
private final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

@Autowired(required = false)
private ConsulProperties consulProperties;
private ConsulClient consulClient;

@Autowired
private ITxleCache txleCache;

@Value("${spring.application.name:\"\"}")
private String serverName;

Expand All @@ -66,15 +72,6 @@ public ConsulClient getConsulClient() {
return consulClient;
}

@PostConstruct
private void init() {
if (!enabled) {
return;
}
this.initConsulCluster();
this.registerConsulSession();
}

private void initConsulCluster() {
try {
if (consulServers != null && consulServers.length() > 0) {
Expand All @@ -90,6 +87,9 @@ private void initConsulCluster() {
}

public void setAvailableConsulClient() {
if (consulClient != null) {
return;
}
for (Map.Entry<String, ConsulClient> entry : consulClientMap.entrySet()) {
try {
if (entry.getValue().getStatusLeader().getValue() != null) {
Expand Down Expand Up @@ -177,12 +177,32 @@ public void destroyConsulCriticalServices() {
}
}

@PostConstruct
void init() {
if (enabled) {
this.initConsulCluster();
this.registerConsulSession();
}
}

@Override
public void run(ApplicationArguments applicationArguments) throws Exception {
if (enabled) {
// To execute refresh method after starting server rather than after initializing.
txleCache.refreshServiceListCache(true);
// txleCache.synchronizeCacheFromLeader();
// scheduler.scheduleWithFixedDelay(() -> {
// removeExpiredCache(txSuspendStatusCache);
// removeExpiredCache(txAbortStatusCache);
// }, 1, 1, TimeUnit.MINUTES);
}
}

@PreDestroy
void shutdown() {
if (!enabled) {
return;
if (enabled) {
this.destroyConsulCriticalServices();
}
this.destroyConsulCriticalServices();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.lang.invoke.MethodHandles;
import java.net.InetAddress;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
* @author Gannalyo
Expand Down Expand Up @@ -223,21 +224,6 @@ private void removeTxStatusCache(ConcurrentSkipListSet<CacheEntity> txStatusCach
}
}

@PostConstruct
void init() {
this.refreshServiceListCache(true);
// this.synchronizeCacheFromLeader();
scheduler.scheduleWithFixedDelay(() -> {
// removeExpiredCache(txSuspendStatusCache);
// removeExpiredCache(txAbortStatusCache);
}, 1, 1, TimeUnit.MINUTES);
}

@PreDestroy
void shutdown() {
this.refreshServiceListCache(true);
}

private void removeExpiredCache(ConcurrentSkipListSet<CacheEntity> cache) {
Iterator<CacheEntity> iterator = cache.iterator();
// Use LinkedList due to remove frequently later.
Expand Down

0 comments on commit 665bd81

Please sign in to comment.