Wrapping the created ServerList in stub runner version (#241)

This commit is contained in:
Marcin Grzejszczak
2016-04-20 15:32:07 +02:00
parent e1bde47618
commit bb4df7903a
4 changed files with 167 additions and 88 deletions

View File

@@ -1,5 +1,7 @@
package io.codearte.accurest.stubrunner.spring.cloud.ribbon;
import com.netflix.loadbalancer.ServerList;
import io.codearte.accurest.stubrunner.spring.cloud.StubMapperProperties;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -8,10 +10,6 @@ import org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.context.annotation.Configuration;
import com.netflix.loadbalancer.ServerList;
import io.codearte.accurest.stubrunner.spring.cloud.StubMapperProperties;
@Configuration
@ConditionalOnClass(ServerList.class)
@ConditionalOnBean(StubMapperProperties.class)

View File

@@ -0,0 +1,61 @@
package io.codearte.accurest.stubrunner.spring.cloud.ribbon;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ServerList;
import io.codearte.accurest.stubrunner.StubFinder;
import io.codearte.accurest.stubrunner.spring.cloud.StubMapperProperties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* Ribbon AutoConfiguration that manipulates the service id to make the service
* be picked from the list of available WireMock instance if one is available.
*
* @author Marcin Grzejszczak
*/
class StubRunnerRibbonBeanPostProcessor implements BeanPostProcessor {
private final BeanFactory beanFactory;
private StubFinder stubFinder;
private StubMapperProperties stubMapperProperties;
private IClientConfig clientConfig;
StubRunnerRibbonBeanPostProcessor(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
private StubFinder stubFinder() {
if (stubFinder == null) {
stubFinder = this.beanFactory.getBean(StubFinder.class);
}
return stubFinder;
}
private StubMapperProperties stubMapperProperties() {
if (stubMapperProperties == null) {
stubMapperProperties = this.beanFactory.getBean(StubMapperProperties.class);
}
return stubMapperProperties;
}
private IClientConfig clientConfig() {
if (clientConfig == null) {
clientConfig = this.beanFactory.getBean(IClientConfig.class);
}
return clientConfig;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof ServerList && !(bean instanceof StubRunnerRibbonServerList)) {
return new StubRunnerRibbonServerList(stubFinder(), stubMapperProperties(), clientConfig(), (ServerList) bean);
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}

View File

@@ -1,33 +1,21 @@
package io.codearte.accurest.stubrunner.spring.cloud.ribbon;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import java.util.ArrayList;
import java.util.List;
import io.codearte.accurest.stubrunner.RunningStubs;
import io.codearte.accurest.stubrunner.StubConfiguration;
import io.codearte.accurest.stubrunner.StubFinder;
import io.codearte.accurest.stubrunner.spring.cloud.StubMapperProperties;
import io.codearte.accurest.stubrunner.util.StringUtils;
/**
* Ribbon AutoConfiguration that manipulates the service id to make the service
* be picked from the list of available WireMock instance if one is available.
*
* @author Marcin Grzejszczak
*/
@Configuration
public class StubRunnerRibbonConfiguration {
class StubRunnerRibbonConfiguration {
@Bean
StubRunnerRibbonBeanPostProcessor stubRunnerRibbonBeanPostProcessor(BeanFactory beanFactory) {
return new StubRunnerRibbonBeanPostProcessor(beanFactory);
}
@Bean
@ConditionalOnMissingBean
@@ -44,66 +32,4 @@ public class StubRunnerRibbonConfiguration {
}
};
}
@Bean
@Primary
@SuppressWarnings("unchecked")
public ServerList<?> stubRunnerRibbonServerList(final ServerList<?> serverList,
StubFinder stubFinder,
final StubMapperProperties stubMapperProperties,
IClientConfig clientConfig) {
String serviceName = clientConfig.getClientName();
String mappedServiceName = StringUtils
.hasText(stubMapperProperties.fromServiceIdToIvyNotation(serviceName)) ?
stubMapperProperties.fromServiceIdToIvyNotation(serviceName) : serviceName;
RunningStubs runningStubs = stubFinder.findAllRunningStubs();
final Map.Entry<StubConfiguration, Integer> entry = runningStubs.getEntry(mappedServiceName);
final Collection servers = new ArrayList<Server>();
if (entry != null) {
servers.add(new Server("localhost", entry.getValue()) {
@Override
public MetaInfo getMetaInfo() {
return new MetaInfo() {
@Override
public String getAppName() {
return stubMapperProperties.fromIvyNotationToId(entry.getKey().toColonSeparatedDependencyNotation());
}
@Override
public String getServerGroup() {
return null;
}
@Override
public String getServiceIdForDiscovery() {
return stubMapperProperties.fromIvyNotationToId(entry.getKey().getArtifactId());
}
@Override
public String getInstanceId() {
return stubMapperProperties.fromIvyNotationToId(entry.getKey().getArtifactId());
}
};
}
});
}
return new ServerList() {
@Override
public List<?> getInitialListOfServers() {
List combinedList = new ArrayList<>();
combinedList.addAll(servers);
combinedList.addAll(serverList.getInitialListOfServers());
return combinedList;
}
@Override
public List<?> getUpdatedListOfServers() {
List combinedList = new ArrayList<>();
combinedList.addAll(servers);
combinedList.addAll(serverList.getUpdatedListOfServers());
return combinedList;
}
};
}
}

View File

@@ -0,0 +1,94 @@
package io.codearte.accurest.stubrunner.spring.cloud.ribbon;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import io.codearte.accurest.stubrunner.RunningStubs;
import io.codearte.accurest.stubrunner.StubConfiguration;
import io.codearte.accurest.stubrunner.StubFinder;
import io.codearte.accurest.stubrunner.spring.cloud.StubMapperProperties;
import io.codearte.accurest.stubrunner.util.StringUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* Stub Runner representation of a server list
*
* @author Marcin Grzejszczak
*/
class StubRunnerRibbonServerList implements ServerList {
private final ServerList<?> serverList;
@SuppressWarnings("unchecked")
StubRunnerRibbonServerList(final StubFinder stubFinder,
final StubMapperProperties stubMapperProperties,
final IClientConfig clientConfig,
final ServerList<?> delegate) {;
String serviceName = clientConfig.getClientName();
String mappedServiceName = StringUtils
.hasText(stubMapperProperties.fromServiceIdToIvyNotation(serviceName)) ?
stubMapperProperties.fromServiceIdToIvyNotation(serviceName) : serviceName;
RunningStubs runningStubs = stubFinder.findAllRunningStubs();
final Map.Entry<StubConfiguration, Integer> entry = runningStubs.getEntry(mappedServiceName);
final Collection servers = new ArrayList<>();
if (entry != null) {
servers.add(new Server("localhost", entry.getValue()) {
@Override
public MetaInfo getMetaInfo() {
return new MetaInfo() {
@Override
public String getAppName() {
return stubMapperProperties.fromIvyNotationToId(entry.getKey().toColonSeparatedDependencyNotation());
}
@Override
public String getServerGroup() {
return null;
}
@Override
public String getServiceIdForDiscovery() {
return stubMapperProperties.fromIvyNotationToId(entry.getKey().getArtifactId());
}
@Override
public String getInstanceId() {
return stubMapperProperties.fromIvyNotationToId(entry.getKey().getArtifactId());
}
};
}
});
}
serverList = new ServerList() {
@Override
public List<?> getInitialListOfServers() {
List combinedList = new ArrayList<>();
combinedList.addAll(servers);
combinedList.addAll(delegate.getInitialListOfServers());
return combinedList;
}
@Override
public List<?> getUpdatedListOfServers() {
List combinedList = new ArrayList<>();
combinedList.addAll(servers);
combinedList.addAll(delegate.getUpdatedListOfServers());
return combinedList;
}
};
}
@Override
public List<?> getInitialListOfServers() {
return serverList.getInitialListOfServers();
}
@Override
public List<?> getUpdatedListOfServers() {
return serverList.getUpdatedListOfServers();
}
}