1
0
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:
得少 2019-01-10 13:14:53 +08:00
parent c7663abffe
commit 840b35331b
5 changed files with 45 additions and 94 deletions

View File

@ -1,48 +1,15 @@
//package org.springframework.cloud.alibaba.cloud.example; package org.springframework.cloud.alibaba.cloud.example;
//
//import org.springframework.alicloud.env.extension.ImportExtraConfig; import org.springframework.alicloud.env.extension.ImportExtraConfig;
//import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
//import org.springframework.boot.SpringApplicationRunListener; import org.springframework.boot.autoconfigure.SpringBootApplication;
//import org.springframework.boot.autoconfigure.SpringBootApplication;
//import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication
//import org.springframework.core.env.ConfigurableEnvironment; @ImportExtraConfig(name = "/Users/toava/sms.properties")
// public class SmsApplication {
//@SpringBootApplication
//@ImportExtraConfig(name = "/Users/toava/sms.properties") public static void main(String[] args) {
//public class SmsApplication {
// SpringApplication.run(SmsApplication.class, args);
// public static void main(String[] args) { }
// }
// SpringApplication.run(SmsApplication.class, args);
// }
//
// public class EvnExtra implements SpringApplicationRunListener {
// @Override
// public void starting() {
//
// }
//
// @Override
// public void environmentPrepared(ConfigurableEnvironment environment) {
//
// }
//
// @Override
// public void contextPrepared(ConfigurableApplicationContext context) {
//
// }
//
// @Override
// public void contextLoaded(ConfigurableApplicationContext context) {
//
// }
//
// @Override
// public void finished(ConfigurableApplicationContext context,
// Throwable exception) {
// String ak = context.getEnvironment()
// .getProperty("spring.cloud.alicloud.access-key");
// System.err.println(ak);
// }
// }
//}

View File

@ -1,30 +0,0 @@
package org.springframework.cloud.alibaba.cloud.example.env;
import org.springframework.alicloud.env.extension.ImportExtraConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@ImportExtraConfig(name = "/Users/toava/sms.properties")
public class SmsApplication {
public static void main(String[] args) {
SpringApplication.run(SmsApplication.class, args);
}
@RestController
public class EnvExtraController{
@Value("${spring.cloud.alicloud.access-key:deshao}")
private String ak;
@GetMapping("/get-ak.do")
public String getAk(){
return ak;
}
}
}

View File

@ -1,6 +1,12 @@
package org.springframework.cloud.alicloud.ans.migrate; 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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.context.named.NamedContextFactory; 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.core.env.Environment;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct; import com.netflix.loadbalancer.ILoadBalancer;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/** /**
* @author pbting * @author pbting
@ -22,6 +24,8 @@ import java.util.concurrent.TimeUnit;
public class MigrateRefreshEventListener implements ApplicationListener<RefreshEvent> { public class MigrateRefreshEventListener implements ApplicationListener<RefreshEvent> {
private final static Log log = LogFactory.getLog(MigrateRefreshEventListener.class); 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 final static String MIGRATE_SWITCH = "sca.migrate.ans.switch";
private volatile String lastScaMigrateAnsSwitchValue = "true"; private volatile String lastScaMigrateAnsSwitchValue = "true";
@ -51,7 +55,7 @@ public class MigrateRefreshEventListener implements ApplicationListener<RefreshE
e); e);
} }
} }
}, 1, 1, TimeUnit.SECONDS); }, CHECK_INTERVAL, CHECK_INTERVAL, TimeUnit.SECONDS);
} }
@Override @Override

View File

@ -91,15 +91,15 @@ class ServerListInvocationHandler implements MethodInterceptor {
.equals(ansServer.getHealthService().toInetAddr())) { .equals(ansServer.getHealthService().toInetAddr())) {
// fix bug: mast be set the zone, update server list,will filter // fix bug: mast be set the zone, update server list,will filter
// by: ZoneAffinityPredicate // by: ZoneAffinityPredicate
ansServer.setZone(server.getZone());
ansServer.setSchemea(server.getScheme());
ansServer.setId(server.getId());
ansServer.setReadyToServe(true);
serverIterator.remove(); serverIterator.remove();
log.info("Source Server is remove " + server.getHostPort() log.info("Source Server is remove " + server.getHostPort()
+ ", and from ANS Server is override" + ", and from ANS Server is override"
+ ansServer.toString()); + 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) { static Server checkAndGetServiceServer(String serviceId, Server server) {
if (server == null) { if (server != null) {
log.warn(String.format("[%s] refers the server is null", server)); 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; return server;
} }

View File

@ -30,6 +30,8 @@ import java.util.List;
*/ */
public class AnsServerList extends AbstractServerList<AnsServer> { public class AnsServerList extends AbstractServerList<AnsServer> {
private final static int CONNECT_TIME_OUT = 3;
private String dom; private String dom;
public AnsServerList(String dom) { public AnsServerList(String dom) {
@ -62,7 +64,7 @@ public class AnsServerList extends AbstractServerList<AnsServer> {
for (Host host : hosts) { for (Host host : hosts) {
if (host.isValid()) { if (host.isValid()) {
AnsServer ansServer = hostToServer(host); AnsServer ansServer = hostToServer(host);
if (ansServer.isAlive(3)) { if (ansServer.isAlive(CONNECT_TIME_OUT)) {
result.add(ansServer); result.add(ansServer);
} }
} }