mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Code optimization for ans starter
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
package org.springframework.cloud.alicloud.ans.migrate;
|
||||
|
||||
import com.netflix.loadbalancer.ILoadBalancer;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.cloud.context.named.NamedContextFactory;
|
||||
@@ -9,11 +15,7 @@ import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import com.netflix.loadbalancer.ILoadBalancer;
|
||||
|
||||
/**
|
||||
* @author pbting
|
||||
@@ -22,6 +24,8 @@ import java.util.concurrent.TimeUnit;
|
||||
public class MigrateRefreshEventListener implements ApplicationListener<RefreshEvent> {
|
||||
private final static Log log = LogFactory.getLog(MigrateRefreshEventListener.class);
|
||||
|
||||
private final static int CHECK_INTERVAL = 1;
|
||||
|
||||
private final static String MIGRATE_SWITCH = "sca.migrate.ans.switch";
|
||||
|
||||
private volatile String lastScaMigrateAnsSwitchValue = "true";
|
||||
@@ -51,7 +55,7 @@ public class MigrateRefreshEventListener implements ApplicationListener<RefreshE
|
||||
e);
|
||||
}
|
||||
}
|
||||
}, 1, 1, TimeUnit.SECONDS);
|
||||
}, CHECK_INTERVAL, CHECK_INTERVAL, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -91,15 +91,15 @@ class ServerListInvocationHandler implements MethodInterceptor {
|
||||
.equals(ansServer.getHealthService().toInetAddr())) {
|
||||
// fix bug: mast be set the zone, update server list,will filter
|
||||
// by: ZoneAffinityPredicate
|
||||
ansServer.setZone(server.getZone());
|
||||
ansServer.setSchemea(server.getScheme());
|
||||
ansServer.setId(server.getId());
|
||||
ansServer.setReadyToServe(true);
|
||||
serverIterator.remove();
|
||||
log.info("Source Server is remove " + server.getHostPort()
|
||||
+ ", and from ANS Server is override:"
|
||||
+ ansServer.toString());
|
||||
}
|
||||
ansServer.setZone(server.getZone());
|
||||
ansServer.setSchemea(server.getScheme());
|
||||
ansServer.setId(ansServer.getHealthService().toInetAddr());
|
||||
ansServer.setReadyToServe(true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,11 +133,19 @@ class ServerListInvocationHandler implements MethodInterceptor {
|
||||
}
|
||||
|
||||
static Server checkAndGetServiceServer(String serviceId, Server server) {
|
||||
if (server == null) {
|
||||
log.warn(String.format("[%s] refers the server is null", server));
|
||||
if (server != null) {
|
||||
return server;
|
||||
}
|
||||
server = (server == null ? SERVER_LIST_CONCURRENT_MAP.get(serviceId)
|
||||
.getInitialListOfServers().get(0) : server);
|
||||
|
||||
log.warn(String.format("[%s] refers the server is null", server));
|
||||
|
||||
List<AnsServer> ansServerList = SERVER_LIST_CONCURRENT_MAP.get(serviceId)
|
||||
.getInitialListOfServers();
|
||||
|
||||
if (!ansServerList.isEmpty()) {
|
||||
return ansServerList.get(0);
|
||||
}
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
|
@@ -30,6 +30,8 @@ import java.util.List;
|
||||
*/
|
||||
public class AnsServerList extends AbstractServerList<AnsServer> {
|
||||
|
||||
private final static int CONNECT_TIME_OUT = 3;
|
||||
|
||||
private String dom;
|
||||
|
||||
public AnsServerList(String dom) {
|
||||
@@ -62,7 +64,7 @@ public class AnsServerList extends AbstractServerList<AnsServer> {
|
||||
for (Host host : hosts) {
|
||||
if (host.isValid()) {
|
||||
AnsServer ansServer = hostToServer(host);
|
||||
if (ansServer.isAlive(3)) {
|
||||
if (ansServer.isAlive(CONNECT_TIME_OUT)) {
|
||||
result.add(ansServer);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user