Wrapping the created ServerList in stub runner version (#241)
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user