From f7975c91e8394d1a05a551ad3fdd6d9a5748ca78 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Mon, 8 Jun 2015 14:02:55 -0600 Subject: [PATCH] added sample test that make sure a users default ribbon config is used even if using autoconfig --- pom.xml | 1 + ribbon-default-config/pom.xml | 65 +++++++++++++++++++ .../main/java/demo/MyDefaultRibbonConfig.java | 48 ++++++++++++++ .../demo/RibbonDefaultConfigApplication.java | 63 ++++++++++++++++++ .../RibbonDefaultConfigApplicationTests.java | 31 +++++++++ 5 files changed, 208 insertions(+) create mode 100644 ribbon-default-config/pom.xml create mode 100644 ribbon-default-config/src/main/java/demo/MyDefaultRibbonConfig.java create mode 100644 ribbon-default-config/src/main/java/demo/RibbonDefaultConfigApplication.java create mode 100644 ribbon-default-config/src/test/java/demo/RibbonDefaultConfigApplicationTests.java diff --git a/pom.xml b/pom.xml index 0540998..60e0e5b 100644 --- a/pom.xml +++ b/pom.xml @@ -30,6 +30,7 @@ netflix-sidecar noweb oauth2-ribbon + ribbon-default-config ribbon-eureka turbine zuul diff --git a/ribbon-default-config/pom.xml b/ribbon-default-config/pom.xml new file mode 100644 index 0000000..19a864f --- /dev/null +++ b/ribbon-default-config/pom.xml @@ -0,0 +1,65 @@ + + 4.0.0 + + + + org.springframework.boot + spring-boot-starter-parent + 1.2.3.RELEASE + + + + org.springframework.cloud + spring-cloud-sample-ribbon-default-config + 1.0.3.BUILD-SNAPSHOT + jar + + + UTF-8 + 1.7 + + + + + + + maven-deploy-plugin + + true + + + + + + + + + org.springframework.cloud + spring-cloud-starter-parent + 1.0.3.BUILD-SNAPSHOT + pom + import + + + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.cloud + spring-cloud-starter-eureka + + + org.springframework.cloud + spring-cloud-starter-ribbon + + + org.springframework.boot + spring-boot-starter-test + test + + + diff --git a/ribbon-default-config/src/main/java/demo/MyDefaultRibbonConfig.java b/ribbon-default-config/src/main/java/demo/MyDefaultRibbonConfig.java new file mode 100644 index 0000000..9237c10 --- /dev/null +++ b/ribbon-default-config/src/main/java/demo/MyDefaultRibbonConfig.java @@ -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 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); + } + } +} diff --git a/ribbon-default-config/src/main/java/demo/RibbonDefaultConfigApplication.java b/ribbon-default-config/src/main/java/demo/RibbonDefaultConfigApplication.java new file mode 100644 index 0000000..1e16309 --- /dev/null +++ b/ribbon-default-config/src/main/java/demo/RibbonDefaultConfigApplication.java @@ -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 lb = (ZoneAwareLoadBalancer)clientFactory.getLoadBalancer("baz"); + + ServerList 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); + } +} + diff --git a/ribbon-default-config/src/test/java/demo/RibbonDefaultConfigApplicationTests.java b/ribbon-default-config/src/test/java/demo/RibbonDefaultConfigApplicationTests.java new file mode 100644 index 0000000..be56252 --- /dev/null +++ b/ribbon-default-config/src/test/java/demo/RibbonDefaultConfigApplicationTests.java @@ -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(); + } +} +