Code optimization for ans starter

pull/254/head
得少 6 years ago
parent a4f8cc7d28
commit e163500980

@ -16,7 +16,7 @@ public class MigrateOnConditionClass extends MigrateOnCondition {
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
boolean result = isPresent(conditionOnClass[0], classLoader)
|| isPresent(conditionOnClass[1], classLoader);
log.info("the result of MigrateOnConditionClass is :" + result);
log.info("the result of match is :" + result);
return result;
}
}

@ -16,7 +16,7 @@ public class MigrateOnConditionMissingClass extends MigrateOnConditionClass {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
boolean result = !super.matches(context, metadata);
log.info("the result of MigrateOnConditionMissingClass is :" + result);
log.info("the result of match is :" + result);
return result;
}

@ -1,5 +1,15 @@
package org.springframework.cloud.alicloud.ans.migrate;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import org.aopalliance.aop.Advice;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.framework.ProxyFactory;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
@ -8,17 +18,6 @@ import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.atomic.AtomicBoolean;
import org.aopalliance.aop.Advice;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.framework.ProxyFactory;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
/**
* @author pbting
*/
@ -49,17 +48,21 @@ final class MigrateProxyManager {
}
static Object newLoadBalancerProxy(Object bean, ClassLoader classLoader,
IClientConfig clientConfig) {
final IClientConfig clientConfig) {
bean = springProxyFactory(bean, classLoader,
Arrays.asList(new AfterReturningAdvice() {
private final IClientConfig iclientConfig = clientConfig;
@Override
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) {
String methodName = method.getName();
if ("chooseServer".equals(methodName)) {
String serviceId = clientConfig.getClientName();
String serviceId = iclientConfig.getClientName();
Server server = (Server) returnValue;
server = ServerListInvocationHandler
.checkAndGetServiceServer(serviceId, server);
ServerListInvocationHandler.incrementCallService(serviceId,
server);
}

@ -1,16 +1,17 @@
package org.springframework.cloud.alicloud.ans.migrate;
import com.netflix.loadbalancer.ILoadBalancer;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import org.springframework.cloud.context.named.NamedContextFactory;
import org.springframework.cloud.endpoint.event.RefreshEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import com.netflix.loadbalancer.ILoadBalancer;
/**
* @author pbting
@ -22,13 +23,14 @@ public class MigrateRefreshEventListener implements ApplicationListener<RefreshE
private volatile String lastScaMigrateAnsSwitchValue = "true";
@Autowired
private Environment environment;
@Autowired
private NamedContextFactory namedContextFactory;
public MigrateRefreshEventListener() {
public MigrateRefreshEventListener(Environment environment,
NamedContextFactory namedContextFactory) {
this.environment = environment;
this.namedContextFactory = namedContextFactory;
}
@PostConstruct

@ -1,13 +1,16 @@
package org.springframework.cloud.alicloud.ans.migrate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.alicloud.ans.ConditionalOnAnsEnabled;
import org.springframework.cloud.alicloud.context.ans.AnsProperties;
import org.springframework.cloud.context.named.NamedContextFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
@Configuration
@EnableConfigurationProperties
@ -22,4 +25,12 @@ public class MigrationAutoconfiguration {
return new MigrateServiceRegistry(ansProperties, applicationContext);
}
@Bean
public MigrateRefreshEventListener migrateRefreshEventListener(
Environment environment,
@Qualifier(value = "springClientFactory") NamedContextFactory namedContextFactory) {
return new MigrateRefreshEventListener(environment, namedContextFactory);
}
}

@ -23,7 +23,10 @@ class ServerListInvocationHandler implements MethodInterceptor {
private final static Log log = LogFactory.getLog(ServerListInvocationHandler.class);
private final static ConcurrentMap<String, AnsServerList> SERVER_LIST_CONCURRENT_MAP = new ConcurrentHashMap<>();
private final static ConcurrentMap<String, ConcurrentMap<String, ServerWrapper>> CALL_SERVICE_COUNT = new ConcurrentHashMap<>();
private final static Set<String> INTERCEPTOR_METHOD_NAME = new ConcurrentSkipListSet();
private IClientConfig clientConfig;
@ -38,6 +41,7 @@ class ServerListInvocationHandler implements MethodInterceptor {
ServerListInvocationHandler(IClientConfig clientConfig) {
this.clientConfig = clientConfig;
this.ansServerList = new AnsServerList(clientConfig.getClientName());
SERVER_LIST_CONCURRENT_MAP.put(ansServerList.getDom(), ansServerList);
}
@Override
@ -122,6 +126,23 @@ class ServerListInvocationHandler implements MethodInterceptor {
return Collections.unmodifiableMap(CALL_SERVICE_COUNT);
}
static Server checkAndGetServiceServer(String serviceId, Server server) {
if (server != null) {
return 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;
}
static void incrementCallService(String serviceId, Server server) {
ConcurrentMap<String, ServerWrapper> concurrentHashMap = CALL_SERVICE_COUNT
.putIfAbsent(serviceId, new ConcurrentHashMap<>());

@ -5,6 +5,4 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.alicloud.ans.migrate.MigrateEndpointAutoConfiguration,\
org.springframework.cloud.alicloud.ans.migrate.MigrationAutoconfiguration
org.springframework.cloud.client.discovery.EnableDiscoveryClient=\
org.springframework.cloud.alicloud.ans.AnsDiscoveryClientAutoConfiguration
org.springframework.context.ApplicationListener=\
org.springframework.cloud.alicloud.ans.migrate.MigrateRefreshEventListener
org.springframework.cloud.alicloud.ans.AnsDiscoveryClientAutoConfiguration
Loading…
Cancel
Save