mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
add test case of ANS
This commit is contained in:
parent
dbfc363c02
commit
11db5e8728
@ -21,7 +21,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
|||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.cloud.alicloud.ans.migrate.MigrateOnConditionMissingClass;
|
|
||||||
import org.springframework.cloud.alicloud.ans.registry.AnsAutoServiceRegistration;
|
import org.springframework.cloud.alicloud.ans.registry.AnsAutoServiceRegistration;
|
||||||
import org.springframework.cloud.alicloud.ans.registry.AnsRegistration;
|
import org.springframework.cloud.alicloud.ans.registry.AnsRegistration;
|
||||||
import org.springframework.cloud.alicloud.ans.registry.AnsServiceRegistry;
|
import org.springframework.cloud.alicloud.ans.registry.AnsServiceRegistry;
|
||||||
@ -30,7 +29,6 @@ import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationC
|
|||||||
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
|
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Conditional;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -38,7 +36,6 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableConfigurationProperties
|
@EnableConfigurationProperties
|
||||||
@Conditional(MigrateOnConditionMissingClass.class)
|
|
||||||
@ConditionalOnClass(name = "org.springframework.boot.web.context.WebServerInitializedEvent")
|
@ConditionalOnClass(name = "org.springframework.boot.web.context.WebServerInitializedEvent")
|
||||||
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
|
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
|
||||||
@ConditionalOnAnsEnabled
|
@ConditionalOnAnsEnabled
|
||||||
|
@ -22,12 +22,7 @@ import com.alibaba.ans.shaded.com.taobao.vipserver.client.core.Host;
|
|||||||
import org.springframework.cloud.client.ServiceInstance;
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xiaolongzuo
|
* @author xiaolongzuo
|
||||||
|
@ -19,11 +19,9 @@ package org.springframework.cloud.alicloud.ans;
|
|||||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.cloud.alicloud.ans.migrate.MigrateOnConditionMissingClass;
|
|
||||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||||
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration;
|
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Conditional;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -31,7 +29,6 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
* @author pbting
|
* @author pbting
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@Conditional(MigrateOnConditionMissingClass.class)
|
|
||||||
@ConditionalOnMissingBean(DiscoveryClient.class)
|
@ConditionalOnMissingBean(DiscoveryClient.class)
|
||||||
@EnableConfigurationProperties
|
@EnableConfigurationProperties
|
||||||
@AutoConfigureBefore(SimpleDiscoveryClientAutoConfiguration.class)
|
@AutoConfigureBefore(SimpleDiscoveryClientAutoConfiguration.class)
|
||||||
|
@ -19,8 +19,8 @@ package org.springframework.cloud.alicloud.ans.endpoint;
|
|||||||
import com.alibaba.ans.core.NamingService;
|
import com.alibaba.ans.core.NamingService;
|
||||||
import com.alibaba.ans.shaded.com.taobao.vipserver.client.core.Host;
|
import com.alibaba.ans.shaded.com.taobao.vipserver.client.core.Host;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.slf4j.Logger;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||||
import org.springframework.cloud.alicloud.context.ans.AnsProperties;
|
import org.springframework.cloud.alicloud.context.ans.AnsProperties;
|
||||||
@ -37,7 +37,7 @@ import java.util.Set;
|
|||||||
@Endpoint(id = "ans")
|
@Endpoint(id = "ans")
|
||||||
public class AnsEndpoint {
|
public class AnsEndpoint {
|
||||||
|
|
||||||
private static final Log log = LogFactory.getLog(AnsEndpoint.class);
|
private static final Logger log = LoggerFactory.getLogger(AnsEndpoint.class);
|
||||||
|
|
||||||
private AnsProperties ansProperties;
|
private AnsProperties ansProperties;
|
||||||
|
|
||||||
|
@ -1,31 +0,0 @@
|
|||||||
package org.springframework.cloud.alicloud.ans.migrate;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
|
||||||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.ConcurrentMap;
|
|
||||||
|
|
||||||
@Endpoint(id = "migrate")
|
|
||||||
public class MigrateEndpoint {
|
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(MigrateEndpoint.class);
|
|
||||||
|
|
||||||
public MigrateEndpoint() {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return ans endpoint
|
|
||||||
*/
|
|
||||||
@ReadOperation
|
|
||||||
public Map<String, ConcurrentMap<String, ServerWrapper>> invoke() {
|
|
||||||
|
|
||||||
Map<String, ConcurrentMap<String, ServerWrapper>> result = ServerListInvocationHandler
|
|
||||||
.getServerRegistry();
|
|
||||||
|
|
||||||
log.info("migrate server list :" + result);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,21 +0,0 @@
|
|||||||
package org.springframework.cloud.alicloud.ans.migrate;
|
|
||||||
|
|
||||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Conditional;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author pbting
|
|
||||||
*/
|
|
||||||
@ConditionalOnWebApplication
|
|
||||||
@ConditionalOnClass(value = Endpoint.class)
|
|
||||||
@Conditional(MigrateOnConditionClass.class)
|
|
||||||
public class MigrateEndpointAutoConfiguration {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public MigrateEndpoint ansEndpoint() {
|
|
||||||
return new MigrateEndpoint();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,48 +0,0 @@
|
|||||||
package org.springframework.cloud.alicloud.ans.migrate;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
|
||||||
import org.springframework.context.annotation.Condition;
|
|
||||||
import org.springframework.context.annotation.ConditionContext;
|
|
||||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
|
||||||
import org.springframework.util.ClassUtils;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author pbting
|
|
||||||
*/
|
|
||||||
public abstract class MigrateOnCondition implements Condition, BeanClassLoaderAware {
|
|
||||||
|
|
||||||
final String[] conditionOnClass = new String[] {
|
|
||||||
"org.springframework.cloud.consul.serviceregistry.ConsulAutoServiceRegistration",
|
|
||||||
"org.springframework.cloud.netflix.eureka.serviceregistry.EurekaAutoServiceRegistration" };
|
|
||||||
|
|
||||||
ClassLoader classLoader;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
|
||||||
this.classLoader = classLoader;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public abstract boolean matches(ConditionContext context,
|
|
||||||
AnnotatedTypeMetadata metadata);
|
|
||||||
|
|
||||||
boolean isPresent(String className, ClassLoader classLoader) {
|
|
||||||
if (classLoader == null) {
|
|
||||||
classLoader = ClassUtils.getDefaultClassLoader();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
forName(className, classLoader);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch (Throwable throwable) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Class<?> forName(String className, ClassLoader classLoader)
|
|
||||||
throws ClassNotFoundException {
|
|
||||||
return classLoader != null ? classLoader.loadClass(className)
|
|
||||||
: Class.forName(className);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
package org.springframework.cloud.alicloud.ans.migrate;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.context.annotation.ConditionContext;
|
|
||||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author pbting
|
|
||||||
*/
|
|
||||||
public class MigrateOnConditionClass extends MigrateOnCondition {
|
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory
|
|
||||||
.getLogger(MigrateOnConditionClass.class);
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
|
||||||
boolean result = isPresent(conditionOnClass[0], classLoader)
|
|
||||||
|| isPresent(conditionOnClass[1], classLoader);
|
|
||||||
log.info("the result of matcher is " + result);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
package org.springframework.cloud.alicloud.ans.migrate;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.context.annotation.ConditionContext;
|
|
||||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author pbting
|
|
||||||
*/
|
|
||||||
public class MigrateOnConditionMissingClass extends MigrateOnConditionClass {
|
|
||||||
private static final Logger log = LoggerFactory
|
|
||||||
.getLogger(MigrateOnConditionMissingClass.class);
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
|
||||||
boolean result = !super.matches(context, metadata);
|
|
||||||
log.info(" the result of matcher is " + result);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,97 +0,0 @@
|
|||||||
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.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.aop.AfterReturningAdvice;
|
|
||||||
import org.springframework.aop.framework.ProxyFactory;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.concurrent.ConcurrentSkipListSet;
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author pbting
|
|
||||||
*/
|
|
||||||
final class MigrateProxyManager {
|
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(MigrateProxyManager.class);
|
|
||||||
private final static AtomicBoolean IS_PROXY = new AtomicBoolean(true);
|
|
||||||
|
|
||||||
private final static Set<String> SERVICES_ID = new ConcurrentSkipListSet<>();
|
|
||||||
|
|
||||||
private static Object springProxyFactory(Object target, ClassLoader classLoader,
|
|
||||||
List<Advice> adviceList, Class... interfaces) {
|
|
||||||
final ProxyFactory proxyFactory = new ProxyFactory(interfaces);
|
|
||||||
proxyFactory.setTarget(target);
|
|
||||||
adviceList.forEach(advice -> proxyFactory.addAdvice(advice));
|
|
||||||
return proxyFactory.getProxy(classLoader);
|
|
||||||
}
|
|
||||||
|
|
||||||
static Object newServerListProxy(Object bean, ClassLoader classLoader,
|
|
||||||
IClientConfig clientConfig) {
|
|
||||||
bean = springProxyFactory(bean, classLoader,
|
|
||||||
Arrays.asList(new ServerListInvocationHandler(clientConfig)),
|
|
||||||
new Class[] { ServerList.class });
|
|
||||||
log.info("[service id]" + clientConfig.getClientName()
|
|
||||||
+ " new a ServerList proxy instance for spring cloud netflix to spring cloud alibaba ");
|
|
||||||
collectServiceId(clientConfig.getClientName());
|
|
||||||
return bean;
|
|
||||||
}
|
|
||||||
|
|
||||||
static Object newLoadBalancerProxy(Object bean, ClassLoader classLoader,
|
|
||||||
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 = iclientConfig.getClientName();
|
|
||||||
Server server = (Server) returnValue;
|
|
||||||
server = ServerListInvocationHandler
|
|
||||||
.checkAndGetServiceServer(serviceId, server);
|
|
||||||
ServerListInvocationHandler.incrementCallService(serviceId,
|
|
||||||
server);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}), new Class[] { ILoadBalancer.class });
|
|
||||||
log.info("[service id]" + clientConfig.getClientName()
|
|
||||||
+ " new a ILoadBalancer proxy instance for spring cloud netflix to spring cloud alibaba ");
|
|
||||||
return bean;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void migrateProxyClose() {
|
|
||||||
IS_PROXY.set(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void migrateProxyUp() {
|
|
||||||
IS_PROXY.set(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
static boolean isMigrateProxy() {
|
|
||||||
|
|
||||||
return IS_PROXY.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void collectServiceId(String serviceId) {
|
|
||||||
SERVICES_ID.add(serviceId);
|
|
||||||
}
|
|
||||||
|
|
||||||
static Set<String> getServicesId() {
|
|
||||||
|
|
||||||
return Collections.unmodifiableSet(SERVICES_ID);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,78 +0,0 @@
|
|||||||
package org.springframework.cloud.alicloud.ans.migrate;
|
|
||||||
|
|
||||||
import com.netflix.loadbalancer.ILoadBalancer;
|
|
||||||
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;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author pbting
|
|
||||||
*/
|
|
||||||
@Component
|
|
||||||
public class MigrateRefreshEventListener implements ApplicationListener<RefreshEvent> {
|
|
||||||
|
|
||||||
private final static int CHECK_INTERVAL = 1;
|
|
||||||
|
|
||||||
private final static String MIGRATE_SWITCH = "spring.cloud.alicloud.migrate.ans.switch";
|
|
||||||
|
|
||||||
private volatile String lastScaMigrateAnsSwitchValue = "true";
|
|
||||||
|
|
||||||
private Environment environment;
|
|
||||||
|
|
||||||
private NamedContextFactory namedContextFactory;
|
|
||||||
|
|
||||||
public MigrateRefreshEventListener(Environment environment,
|
|
||||||
NamedContextFactory namedContextFactory) {
|
|
||||||
this.environment = environment;
|
|
||||||
this.namedContextFactory = namedContextFactory;
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
public void initTimerCheck() {
|
|
||||||
Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(
|
|
||||||
() -> onApplicationEvent(null), CHECK_INTERVAL, CHECK_INTERVAL,
|
|
||||||
TimeUnit.SECONDS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onApplicationEvent(RefreshEvent event) {
|
|
||||||
String value = environment.getProperty(MIGRATE_SWITCH, "true");
|
|
||||||
// check 1: check the value
|
|
||||||
if (value.equals(lastScaMigrateAnsSwitchValue)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
updateLastScaMigrateAnsResetValue(value);
|
|
||||||
|
|
||||||
// step 1: migrate up
|
|
||||||
if ("true".equals(value)) {
|
|
||||||
MigrateProxyManager.migrateProxyUp();
|
|
||||||
serviceIdContextInit();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// step 2: migrate close
|
|
||||||
if ("false".equals(value)) {
|
|
||||||
MigrateProxyManager.migrateProxyClose();
|
|
||||||
serviceIdContextInit();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void serviceIdContextInit() {
|
|
||||||
namedContextFactory.destroy();
|
|
||||||
// initializer each spring context for service id
|
|
||||||
MigrateProxyManager.getServicesId().forEach(serviceId -> namedContextFactory
|
|
||||||
.getInstance(serviceId, ILoadBalancer.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
private synchronized void updateLastScaMigrateAnsResetValue(String value) {
|
|
||||||
this.lastScaMigrateAnsSwitchValue = value;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,52 +0,0 @@
|
|||||||
package org.springframework.cloud.alicloud.ans.migrate;
|
|
||||||
|
|
||||||
import com.netflix.client.config.IClientConfig;
|
|
||||||
import com.netflix.loadbalancer.ILoadBalancer;
|
|
||||||
import com.netflix.loadbalancer.ServerList;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.beans.BeansException;
|
|
||||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
|
||||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
||||||
|
|
||||||
public class MigrateRibbonBeanPostProcessor
|
|
||||||
implements BeanPostProcessor, BeanClassLoaderAware {
|
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(MigrateOnCondition.class);
|
|
||||||
|
|
||||||
private ClassLoader classLoader;
|
|
||||||
private IClientConfig clientConfig;
|
|
||||||
|
|
||||||
public MigrateRibbonBeanPostProcessor(IClientConfig clientConfig) {
|
|
||||||
this.clientConfig = clientConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object postProcessAfterInitialization(Object bean, String beanName)
|
|
||||||
throws BeansException {
|
|
||||||
|
|
||||||
// step 1 : check the bean whether proxy or not
|
|
||||||
if (!MigrateProxyManager.isMigrateProxy()) {
|
|
||||||
log.info("Migrate proxy is Close.");
|
|
||||||
return bean;
|
|
||||||
}
|
|
||||||
|
|
||||||
// step 2 : proxy the designated bean
|
|
||||||
if (bean instanceof ServerList) {
|
|
||||||
bean = MigrateProxyManager.newServerListProxy(bean, classLoader,
|
|
||||||
clientConfig);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bean instanceof ILoadBalancer) {
|
|
||||||
bean = MigrateProxyManager.newLoadBalancerProxy(bean, classLoader,
|
|
||||||
clientConfig);
|
|
||||||
}
|
|
||||||
return bean;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
|
||||||
this.classLoader = classLoader;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,52 +0,0 @@
|
|||||||
package org.springframework.cloud.alicloud.ans.migrate;
|
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.boot.web.context.WebServerInitializedEvent;
|
|
||||||
import org.springframework.cloud.alicloud.ans.registry.AnsRegistration;
|
|
||||||
import org.springframework.cloud.alicloud.ans.registry.AnsServiceRegistry;
|
|
||||||
import org.springframework.cloud.alicloud.context.ans.AnsProperties;
|
|
||||||
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.context.event.EventListener;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author pbting
|
|
||||||
*/
|
|
||||||
@Component
|
|
||||||
public class MigrateServiceRegistry {
|
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory
|
|
||||||
.getLogger(MigrateServiceRegistry.class);
|
|
||||||
|
|
||||||
private AtomicBoolean running = new AtomicBoolean(false);
|
|
||||||
|
|
||||||
private ServiceRegistry serviceRegistry;
|
|
||||||
private AnsRegistration ansRegistration;
|
|
||||||
|
|
||||||
public MigrateServiceRegistry(AnsProperties ansProperties,
|
|
||||||
ApplicationContext context) {
|
|
||||||
this.ansRegistration = new AnsRegistration(ansProperties, context);
|
|
||||||
this.ansRegistration.init();
|
|
||||||
this.serviceRegistry = new AnsServiceRegistry();
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventListener(WebServerInitializedEvent.class)
|
|
||||||
public void onApplicationEvent(WebServerInitializedEvent event) {
|
|
||||||
int serverPort = event.getWebServer().getPort();
|
|
||||||
this.ansRegistration.setPort(serverPort);
|
|
||||||
log.info("[ Migrate ] change the port to " + serverPort);
|
|
||||||
if (!this.running.get()) {
|
|
||||||
long s = System.currentTimeMillis();
|
|
||||||
log.info("[Migrate] start to registry server to ANS");
|
|
||||||
this.serviceRegistry.register(this.ansRegistration);
|
|
||||||
log.info("[migrate] end to registry server to ANS cost time with "
|
|
||||||
+ (System.currentTimeMillis() - s) + " ms.");
|
|
||||||
this.running.set(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,39 +0,0 @@
|
|||||||
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;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author pbting
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
@EnableConfigurationProperties
|
|
||||||
@Conditional(MigrateOnConditionClass.class)
|
|
||||||
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
|
|
||||||
@ConditionalOnAnsEnabled
|
|
||||||
public class MigrationAutoconfiguration {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public MigrateServiceRegistry migrationManger(AnsProperties ansProperties,
|
|
||||||
ApplicationContext applicationContext) {
|
|
||||||
|
|
||||||
return new MigrateServiceRegistry(ansProperties, applicationContext);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public MigrateRefreshEventListener migrateRefreshEventListener(
|
|
||||||
Environment environment,
|
|
||||||
@Qualifier(value = "springClientFactory") NamedContextFactory namedContextFactory) {
|
|
||||||
|
|
||||||
return new MigrateRefreshEventListener(environment, namedContextFactory);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,170 +0,0 @@
|
|||||||
package org.springframework.cloud.alicloud.ans.migrate;
|
|
||||||
|
|
||||||
import com.netflix.client.config.IClientConfig;
|
|
||||||
import com.netflix.loadbalancer.Server;
|
|
||||||
import org.aopalliance.intercept.MethodInterceptor;
|
|
||||||
import org.aopalliance.intercept.MethodInvocation;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.cloud.alicloud.ans.ribbon.AnsServer;
|
|
||||||
import org.springframework.cloud.alicloud.ans.ribbon.AnsServerList;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.util.concurrent.ConcurrentMap;
|
|
||||||
import java.util.concurrent.ConcurrentSkipListSet;
|
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author pbting
|
|
||||||
*/
|
|
||||||
class ServerListInvocationHandler implements MethodInterceptor {
|
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory
|
|
||||||
.getLogger(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;
|
|
||||||
private AnsServerList ansServerList;
|
|
||||||
private AtomicBoolean isFirst = new AtomicBoolean(false);
|
|
||||||
|
|
||||||
static {
|
|
||||||
INTERCEPTOR_METHOD_NAME.add("getInitialListOfServers");
|
|
||||||
INTERCEPTOR_METHOD_NAME.add("getUpdatedListOfServers");
|
|
||||||
}
|
|
||||||
|
|
||||||
ServerListInvocationHandler(IClientConfig clientConfig) {
|
|
||||||
this.clientConfig = clientConfig;
|
|
||||||
this.ansServerList = new AnsServerList(clientConfig.getClientName());
|
|
||||||
SERVER_LIST_CONCURRENT_MAP.put(ansServerList.getDom(), ansServerList);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Object invoke(MethodInvocation invocation) throws Throwable {
|
|
||||||
String methodName = invocation.getMethod().getName();
|
|
||||||
// step 1: check the method is not interceptor
|
|
||||||
if (!INTERCEPTOR_METHOD_NAME.contains(methodName)) {
|
|
||||||
return invocation.proceed();
|
|
||||||
}
|
|
||||||
|
|
||||||
// step 2: interceptor the method
|
|
||||||
List<Server> serverList = (List<Server>) invocation.proceed();
|
|
||||||
long s = System.currentTimeMillis();
|
|
||||||
log.info("[ START ] merage server list for " + clientConfig.getClientName());
|
|
||||||
serverList = mergeAnsServerList(serverList);
|
|
||||||
log.info("[ END ] merage server list for " + clientConfig.getClientName()
|
|
||||||
+ ", cost time " + (System.currentTimeMillis() - s) + " ms .");
|
|
||||||
return serverList;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 后台线程 和 Eureka 两个注册中心进行 Merage 的时候,List 表中始终保持是有效的 Server. 即不考虑 ANS 客户端本地容灾的情况
|
|
||||||
*/
|
|
||||||
private List<Server> mergeAnsServerList(final List<Server> source) {
|
|
||||||
if (isFirst.compareAndSet(false, true)) {
|
|
||||||
return source;
|
|
||||||
}
|
|
||||||
|
|
||||||
// step 1: get all of server list and filter the alive
|
|
||||||
List<AnsServer> ansServerList = filterAliveAnsServer(
|
|
||||||
this.ansServerList.getInitialListOfServers());
|
|
||||||
|
|
||||||
if (ansServerList.isEmpty()) {
|
|
||||||
return source;
|
|
||||||
}
|
|
||||||
|
|
||||||
log.info("[" + this.clientConfig.getClientName() + "] Get Server List from ANS:"
|
|
||||||
+ ansServerList + "; loadbalancer server list override before:" + source);
|
|
||||||
|
|
||||||
// step 2:remove servers of that have in load balancer list
|
|
||||||
final Iterator<Server> serverIterator = source.iterator();
|
|
||||||
while (serverIterator.hasNext()) {
|
|
||||||
final Server server = serverIterator.next();
|
|
||||||
ansServerList.forEach(ansServer -> {
|
|
||||||
if (server.getHostPort()
|
|
||||||
.equals(ansServer.getHealthService().toInetAddr())) {
|
|
||||||
// by: ZoneAffinityPredicate
|
|
||||||
serverIterator.remove();
|
|
||||||
log.info("Source Server is remove " + server.getHostPort()
|
|
||||||
+ ", and from ANS Server is override:"
|
|
||||||
+ ansServer.toString());
|
|
||||||
}
|
|
||||||
// fix bug: mast be set the zone, update server list,will filter
|
|
||||||
ansServer.setZone(server.getZone());
|
|
||||||
ansServer.setSchemea(server.getScheme());
|
|
||||||
ansServer.setId(ansServer.getHealthService().toInetAddr());
|
|
||||||
ansServer.setReadyToServe(true);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
ansServerList.forEach(ansServer -> source.add(ansServer));
|
|
||||||
log.info("[" + this.clientConfig.getClientName() + "] "
|
|
||||||
+ "; loadbalancer server list override after:" + source);
|
|
||||||
// override
|
|
||||||
return source;
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<AnsServer> filterAliveAnsServer(List<AnsServer> sourceServerList) {
|
|
||||||
final List<AnsServer> resultServerList = new LinkedList<>();
|
|
||||||
sourceServerList.forEach(ansServer -> {
|
|
||||||
boolean isAlive = ansServer.isAlive(3);
|
|
||||||
if (isAlive) {
|
|
||||||
resultServerList.add(ansServer);
|
|
||||||
}
|
|
||||||
log.warn(ansServer.toString() + " isAlive :" + isAlive);
|
|
||||||
});
|
|
||||||
return resultServerList;
|
|
||||||
}
|
|
||||||
|
|
||||||
static Map<String, ConcurrentMap<String, ServerWrapper>> getServerRegistry() {
|
|
||||||
|
|
||||||
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<>());
|
|
||||||
|
|
||||||
if (concurrentHashMap == null) {
|
|
||||||
concurrentHashMap = CALL_SERVICE_COUNT.get(serviceId);
|
|
||||||
}
|
|
||||||
|
|
||||||
String ipPort = server.getHostPort();
|
|
||||||
ServerWrapper serverWraper = concurrentHashMap.putIfAbsent(ipPort,
|
|
||||||
new ServerWrapper(server, new AtomicLong()));
|
|
||||||
|
|
||||||
if (serverWraper == null) {
|
|
||||||
serverWraper = concurrentHashMap.get(ipPort);
|
|
||||||
}
|
|
||||||
serverWraper.setServer(server);
|
|
||||||
serverWraper.getCallCount().incrementAndGet();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
package org.springframework.cloud.alicloud.ans.migrate;
|
|
||||||
|
|
||||||
import com.netflix.loadbalancer.Server;
|
|
||||||
|
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author pbting
|
|
||||||
*/
|
|
||||||
public class ServerWrapper {
|
|
||||||
|
|
||||||
private Server server;
|
|
||||||
private AtomicLong callCount;
|
|
||||||
|
|
||||||
public ServerWrapper() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public ServerWrapper(Server server, AtomicLong callCount) {
|
|
||||||
this.server = server;
|
|
||||||
this.callCount = callCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Server getServer() {
|
|
||||||
return server;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setServer(Server server) {
|
|
||||||
this.server = server;
|
|
||||||
}
|
|
||||||
|
|
||||||
public AtomicLong getCallCount() {
|
|
||||||
return callCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCallCount(AtomicLong callCount) {
|
|
||||||
this.callCount = callCount;
|
|
||||||
}
|
|
||||||
}
|
|
@ -18,7 +18,6 @@ package org.springframework.cloud.alicloud.ans.registry;
|
|||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration;
|
import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration;
|
||||||
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
|
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
|
||||||
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
|
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
|
||||||
@ -34,7 +33,6 @@ public class AnsAutoServiceRegistration
|
|||||||
private static final Logger log = LoggerFactory
|
private static final Logger log = LoggerFactory
|
||||||
.getLogger(AnsAutoServiceRegistration.class);
|
.getLogger(AnsAutoServiceRegistration.class);
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private AnsRegistration registration;
|
private AnsRegistration registration;
|
||||||
|
|
||||||
public AnsAutoServiceRegistration(ServiceRegistry<AnsRegistration> serviceRegistry,
|
public AnsAutoServiceRegistration(ServiceRegistry<AnsRegistration> serviceRegistry,
|
||||||
|
@ -41,7 +41,7 @@ public class AnsServiceRegistry implements ServiceRegistry<AnsRegistration> {
|
|||||||
public void register(AnsRegistration registration) {
|
public void register(AnsRegistration registration) {
|
||||||
|
|
||||||
if (!registration.isRegisterEnabled()) {
|
if (!registration.isRegisterEnabled()) {
|
||||||
log.info("Registration is disabled...");
|
log.warn("Registration is disabled...");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (StringUtils.isEmpty(registration.getServiceId())) {
|
if (StringUtils.isEmpty(registration.getServiceId())) {
|
||||||
@ -63,14 +63,13 @@ public class AnsServiceRegistry implements ServiceRegistry<AnsRegistration> {
|
|||||||
NamingService.regDom(dom, registration.getHost(), registration.getPort(),
|
NamingService.regDom(dom, registration.getHost(), registration.getPort(),
|
||||||
registration.getRegisterWeight(dom), registration.getCluster(),
|
registration.getRegisterWeight(dom), registration.getCluster(),
|
||||||
tags);
|
tags);
|
||||||
log.info("INFO_ANS_REGISTER, " + dom + " "
|
log.info("INFO_ANS_REGISTER, {} {}:{} register finished", dom,
|
||||||
+ registration.getAnsProperties().getClientIp() + ":"
|
registration.getAnsProperties().getClientIp(),
|
||||||
+ registration.getAnsProperties().getClientPort()
|
registration.getAnsProperties().getClientPort());
|
||||||
+ " register finished");
|
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
log.error("ERR_ANS_REGISTER, " + dom + " register failed..."
|
log.error("ERR_ANS_REGISTER, {} register failed...{},", dom,
|
||||||
+ registration.toString() + ",", e);
|
registration.toString(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -90,8 +89,8 @@ public class AnsServiceRegistry implements ServiceRegistry<AnsRegistration> {
|
|||||||
registration.getPort(), registration.getCluster());
|
registration.getPort(), registration.getCluster());
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
log.error("ERR_ANS_DEREGISTER, de-register failed..."
|
log.error("ERR_ANS_DEREGISTER, de-register failed...{},",
|
||||||
+ registration.toString() + ",", e);
|
registration.toString(), e);
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info("De-registration finished.");
|
log.info("De-registration finished.");
|
||||||
|
@ -19,9 +19,7 @@ package org.springframework.cloud.alicloud.ans.ribbon;
|
|||||||
import com.netflix.client.config.IClientConfig;
|
import com.netflix.client.config.IClientConfig;
|
||||||
import com.netflix.loadbalancer.ServerList;
|
import com.netflix.loadbalancer.ServerList;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.cloud.alicloud.ans.migrate.MigrateOnConditionMissingClass;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Conditional;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -29,7 +27,6 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
* @author pbting
|
* @author pbting
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@Conditional(MigrateOnConditionMissingClass.class)
|
|
||||||
public class AnsRibbonClientConfiguration {
|
public class AnsRibbonClientConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -16,16 +16,12 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.alicloud.ans.ribbon;
|
package org.springframework.cloud.alicloud.ans.ribbon;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import com.alibaba.ans.shaded.com.taobao.vipserver.client.core.Host;
|
import com.alibaba.ans.shaded.com.taobao.vipserver.client.core.Host;
|
||||||
import com.netflix.loadbalancer.Server;
|
import com.netflix.loadbalancer.Server;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.net.Socket;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xiaolongzuo
|
* @author xiaolongzuo
|
||||||
*/
|
*/
|
||||||
@ -38,8 +34,7 @@ public class AnsServer extends Server {
|
|||||||
public AnsServer(final Host host, final String dom) {
|
public AnsServer(final Host host, final String dom) {
|
||||||
super(host.getIp(), host.getPort());
|
super(host.getIp(), host.getPort());
|
||||||
this.host = host;
|
this.host = host;
|
||||||
this.metadata = new HashMap();
|
this.metadata = Collections.emptyMap();
|
||||||
this.metadata.put("source", "ANS");
|
|
||||||
metaInfo = new MetaInfo() {
|
metaInfo = new MetaInfo() {
|
||||||
@Override
|
@Override
|
||||||
public String getAppName() {
|
public String getAppName() {
|
||||||
@ -64,33 +59,6 @@ public class AnsServer extends Server {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isAlive() {
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param timeOut Unit: Seconds
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public boolean isAlive(long timeOut) {
|
|
||||||
try {
|
|
||||||
String hostName = this.host.getHostname();
|
|
||||||
hostName = hostName != null && hostName.trim().length() > 0 ? hostName
|
|
||||||
: this.host.getIp();
|
|
||||||
Socket socket = new Socket();
|
|
||||||
socket.connect(new InetSocketAddress(hostName, this.host.getPort()),
|
|
||||||
(int) TimeUnit.SECONDS.toMillis(timeOut));
|
|
||||||
socket.close();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
catch (IOException e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public MetaInfo getMetaInfo() {
|
public MetaInfo getMetaInfo() {
|
||||||
return metaInfo;
|
return metaInfo;
|
||||||
|
@ -16,23 +16,19 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.alicloud.ans.ribbon;
|
package org.springframework.cloud.alicloud.ans.ribbon;
|
||||||
|
|
||||||
import com.alibaba.ans.core.NamingService;
|
|
||||||
import com.alibaba.ans.shaded.com.taobao.vipserver.client.core.Host;
|
|
||||||
|
|
||||||
import com.netflix.client.config.IClientConfig;
|
|
||||||
import com.netflix.loadbalancer.AbstractServerList;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.alibaba.ans.core.NamingService;
|
||||||
|
import com.alibaba.ans.shaded.com.taobao.vipserver.client.core.Host;
|
||||||
|
import com.netflix.client.config.IClientConfig;
|
||||||
|
import com.netflix.loadbalancer.AbstractServerList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xiaolongzuo
|
* @author xiaolongzuo
|
||||||
* @author pbting
|
|
||||||
*/
|
*/
|
||||||
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) {
|
||||||
@ -64,12 +60,10 @@ public class AnsServerList extends AbstractServerList<AnsServer> {
|
|||||||
List<AnsServer> result = new ArrayList<AnsServer>(hosts.size());
|
List<AnsServer> result = new ArrayList<AnsServer>(hosts.size());
|
||||||
for (Host host : hosts) {
|
for (Host host : hosts) {
|
||||||
if (host.isValid()) {
|
if (host.isValid()) {
|
||||||
AnsServer ansServer = hostToServer(host);
|
result.add(hostToServer(host));
|
||||||
if (ansServer.isAlive(CONNECT_TIME_OUT)) {
|
|
||||||
result.add(ansServer);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,19 +0,0 @@
|
|||||||
package org.springframework.cloud.alicloud.ans.ribbon;
|
|
||||||
|
|
||||||
import com.netflix.client.config.IClientConfig;
|
|
||||||
import org.springframework.cloud.alicloud.ans.migrate.MigrateOnConditionClass;
|
|
||||||
import org.springframework.cloud.alicloud.ans.migrate.MigrateRibbonBeanPostProcessor;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Conditional;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
@Conditional(MigrateOnConditionClass.class)
|
|
||||||
public class MigrateRibbonCofiguration {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
public MigrateRibbonBeanPostProcessor migrateBeanPostProcessor(IClientConfig clientConfig) {
|
|
||||||
|
|
||||||
return new MigrateRibbonBeanPostProcessor(clientConfig);
|
|
||||||
}
|
|
||||||
}
|
|
@ -34,7 +34,6 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
@ConditionalOnBean(SpringClientFactory.class)
|
@ConditionalOnBean(SpringClientFactory.class)
|
||||||
@ConditionalOnRibbonAns
|
@ConditionalOnRibbonAns
|
||||||
@AutoConfigureAfter(RibbonAutoConfiguration.class)
|
@AutoConfigureAfter(RibbonAutoConfiguration.class)
|
||||||
@RibbonClients(defaultConfiguration = { AnsRibbonClientConfiguration.class,
|
@RibbonClients(defaultConfiguration = AnsRibbonClientConfiguration.class)
|
||||||
MigrateRibbonCofiguration.class })
|
|
||||||
public class RibbonAnsAutoConfiguration {
|
public class RibbonAnsAutoConfiguration {
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,4 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
|||||||
org.springframework.cloud.alicloud.ans.endpoint.AnsEndpointAutoConfiguration,\
|
org.springframework.cloud.alicloud.ans.endpoint.AnsEndpointAutoConfiguration,\
|
||||||
org.springframework.cloud.alicloud.ans.ribbon.RibbonAnsAutoConfiguration,\
|
org.springframework.cloud.alicloud.ans.ribbon.RibbonAnsAutoConfiguration,\
|
||||||
org.springframework.cloud.alicloud.ans.AnsAutoConfiguration,\
|
org.springframework.cloud.alicloud.ans.AnsAutoConfiguration,\
|
||||||
org.springframework.cloud.alicloud.ans.migrate.MigrateEndpointAutoConfiguration,\
|
|
||||||
org.springframework.cloud.alicloud.ans.migrate.MigrationAutoconfiguration,\
|
|
||||||
org.springframework.cloud.alicloud.ans.AnsDiscoveryClientAutoConfiguration
|
org.springframework.cloud.alicloud.ans.AnsDiscoveryClientAutoConfiguration
|
@ -71,8 +71,6 @@ public class AnsServerListTests {
|
|||||||
|
|
||||||
PowerMockito.mockStatic(NamingService.class);
|
PowerMockito.mockStatic(NamingService.class);
|
||||||
when(NamingService.getHosts(anyString())).thenReturn(hosts);
|
when(NamingService.getHosts(anyString())).thenReturn(hosts);
|
||||||
PowerMockito.stub(PowerMockito.method(AnsServer.class, "isAlive", long.class))
|
|
||||||
.toReturn(true);
|
|
||||||
|
|
||||||
IClientConfig clientConfig = mock(IClientConfig.class);
|
IClientConfig clientConfig = mock(IClientConfig.class);
|
||||||
when(clientConfig.getClientName()).thenReturn("test-service");
|
when(clientConfig.getClientName()).thenReturn("test-service");
|
||||||
@ -101,8 +99,6 @@ public class AnsServerListTests {
|
|||||||
when(NamingService.getHosts(eq("test-service"))).thenReturn(
|
when(NamingService.getHosts(eq("test-service"))).thenReturn(
|
||||||
hosts.stream().filter(Host::isValid).collect(Collectors.toList()));
|
hosts.stream().filter(Host::isValid).collect(Collectors.toList()));
|
||||||
|
|
||||||
PowerMockito.stub(PowerMockito.method(AnsServer.class, "isAlive", long.class))
|
|
||||||
.toReturn(true);
|
|
||||||
|
|
||||||
IClientConfig clientConfig = mock(IClientConfig.class);
|
IClientConfig clientConfig = mock(IClientConfig.class);
|
||||||
when(clientConfig.getClientName()).thenReturn("test-service");
|
when(clientConfig.getClientName()).thenReturn("test-service");
|
||||||
@ -132,8 +128,6 @@ public class AnsServerListTests {
|
|||||||
PowerMockito.mockStatic(NamingService.class);
|
PowerMockito.mockStatic(NamingService.class);
|
||||||
when(NamingService.getHosts(eq("test-service"))).thenReturn(
|
when(NamingService.getHosts(eq("test-service"))).thenReturn(
|
||||||
hosts.stream().filter(Host::isValid).collect(Collectors.toList()));
|
hosts.stream().filter(Host::isValid).collect(Collectors.toList()));
|
||||||
PowerMockito.stub(PowerMockito.method(AnsServer.class, "isAlive", long.class))
|
|
||||||
.toReturn(true);
|
|
||||||
|
|
||||||
IClientConfig clientConfig = mock(IClientConfig.class);
|
IClientConfig clientConfig = mock(IClientConfig.class);
|
||||||
when(clientConfig.getClientName()).thenReturn("test-service");
|
when(clientConfig.getClientName()).thenReturn("test-service");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user