added sample test that make sure a users default ribbon config is used even if using autoconfig
This commit is contained in:
1
pom.xml
1
pom.xml
@@ -30,6 +30,7 @@
|
||||
<module>netflix-sidecar</module>
|
||||
<module>noweb</module>
|
||||
<module>oauth2-ribbon</module>
|
||||
<module>ribbon-default-config</module>
|
||||
<module>ribbon-eureka</module>
|
||||
<module>turbine</module>
|
||||
<module>zuul</module>
|
||||
|
||||
65
ribbon-default-config/pom.xml
Normal file
65
ribbon-default-config/pom.xml
Normal file
@@ -0,0 +1,65 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.2.3.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-sample-ribbon-default-config</artifactId>
|
||||
<version>1.0.3.BUILD-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.7</java.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<!--skip deploy (this is just a test module) -->
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<configuration>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-parent</artifactId>
|
||||
<version>1.0.3.BUILD-SNAPSHOT</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-eureka</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-ribbon</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,48 @@
|
||||
package demo;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.BestAvailableRule;
|
||||
import com.netflix.loadbalancer.ConfigurationBasedServerList;
|
||||
import com.netflix.loadbalancer.IPing;
|
||||
import com.netflix.loadbalancer.IRule;
|
||||
import com.netflix.loadbalancer.PingUrl;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import com.netflix.loadbalancer.ServerList;
|
||||
import com.netflix.loadbalancer.ServerListSubsetFilter;
|
||||
|
||||
/**
|
||||
* @author brenuart
|
||||
*
|
||||
*/
|
||||
@Configuration
|
||||
public class MyDefaultRibbonConfig {
|
||||
@Bean
|
||||
public IRule ribbonRule() {
|
||||
return new BestAvailableRule();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public IPing ribbonPing() {
|
||||
return new PingUrl();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ServerList<Server> ribbonServerList(IClientConfig config) {
|
||||
return new MyDefaultRibbonConfig.BazServiceList(config);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ServerListSubsetFilter serverListFilter() {
|
||||
ServerListSubsetFilter filter = new ServerListSubsetFilter();
|
||||
return filter;
|
||||
}
|
||||
|
||||
public static class BazServiceList extends ConfigurationBasedServerList {
|
||||
public BazServiceList(IClientConfig config) {
|
||||
super.initWithNiwsConfig(config);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package demo;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.cloud.netflix.ribbon.RibbonClients;
|
||||
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.FilterType;
|
||||
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import com.netflix.loadbalancer.ServerList;
|
||||
import com.netflix.loadbalancer.ZoneAwareLoadBalancer;
|
||||
|
||||
/**
|
||||
* @author brenuart
|
||||
*/
|
||||
@ComponentScan(
|
||||
// Exclude @Configuration classes that should be included only in sub contexts created
|
||||
// by Ribbon's SpringClientFactory.
|
||||
excludeFilters={
|
||||
@ComponentScan.Filter(type=FilterType.REGEX, pattern=".*RibbonConfig")
|
||||
}
|
||||
)
|
||||
|
||||
@SpringBootApplication
|
||||
@RibbonClients(defaultConfiguration=MyDefaultRibbonConfig.class)
|
||||
@Configuration
|
||||
public class RibbonDefaultConfigApplication {
|
||||
|
||||
@Autowired
|
||||
private SpringClientFactory clientFactory;
|
||||
|
||||
/**
|
||||
* Throws exception if the SpringClientFactory doesn't return a balancer with a server list
|
||||
* of the expected type.
|
||||
*
|
||||
*/
|
||||
@PostConstruct
|
||||
public void test() throws Exception {
|
||||
ZoneAwareLoadBalancer<Server> lb = (ZoneAwareLoadBalancer<Server>)clientFactory.getLoadBalancer("baz");
|
||||
|
||||
ServerList<Server> serverList= lb.getServerListImpl();
|
||||
if( !(serverList instanceof MyDefaultRibbonConfig.BazServiceList) ) {
|
||||
throw new Exception("wrong server list type");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
SpringApplication app = new SpringApplicationBuilder(RibbonDefaultConfigApplication.class).build();
|
||||
app.run(args);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2013-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package demo;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
|
||||
public class RibbonDefaultConfigApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
SpringApplication app = new SpringApplicationBuilder(RibbonDefaultConfigApplication.class).build();
|
||||
app.run();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user