From c3ce13501e3fbc0028b2ebf75514fabec0c45a06 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 9 Feb 2015 13:31:48 +0000 Subject: [PATCH] Add 2 projects with minimal dependencies One for Spring Config with no webapp (noweb) and one for Eureka (or noop discovery) with no web dependencies at all. --- eureka-noweb/pom.xml | 102 ++++++++++++++++++ .../demo/StandaloneClientApplication.java | 41 +++++++ .../src/main/resources/application.yml | 6 ++ .../NoopStandaloneClientApplicationTests.java | 29 +++++ .../StandaloneClientApplicationTests.java | 29 +++++ .../java/demo/HelloClientApplication.java | 15 +-- .../test/java/demo/NoWebApplicationTests.java | 29 +++++ noweb/pom.xml | 88 +++++++++++++++ .../src/main/java/demo/NotWebApplication.java | 16 +++ noweb/src/main/resources/application.yml | 6 ++ .../demo/DiscoveryNotWebApplicationTests.java | 58 ++++++++++ .../java/demo/NotWebApplicationTests.java | 18 ++++ pom.xml | 2 + 13 files changed, 432 insertions(+), 7 deletions(-) create mode 100644 eureka-noweb/pom.xml create mode 100644 eureka-noweb/src/main/java/demo/StandaloneClientApplication.java create mode 100644 eureka-noweb/src/main/resources/application.yml create mode 100644 eureka-noweb/src/test/java/demo/NoopStandaloneClientApplicationTests.java create mode 100644 eureka-noweb/src/test/java/demo/StandaloneClientApplicationTests.java create mode 100644 feign-eureka/src/test/java/demo/NoWebApplicationTests.java create mode 100644 noweb/pom.xml create mode 100644 noweb/src/main/java/demo/NotWebApplication.java create mode 100644 noweb/src/main/resources/application.yml create mode 100644 noweb/src/test/java/demo/DiscoveryNotWebApplicationTests.java create mode 100644 noweb/src/test/java/demo/NotWebApplicationTests.java diff --git a/eureka-noweb/pom.xml b/eureka-noweb/pom.xml new file mode 100644 index 0000000..46d3160 --- /dev/null +++ b/eureka-noweb/pom.xml @@ -0,0 +1,102 @@ + + + 4.0.0 + + org.springframework.cloud + spring-cloud-sample-eureka-noweb + 1.0.0.BUILD-SNAPSHOT + jar + + eureka-noweb + Demo project for Spring Cloud + + + org.springframework.cloud + spring-cloud-starter-parent + 1.0.0.BUILD-SNAPSHOT + + + + + UTF-8 + 1.7 + + + + + + + maven-deploy-plugin + + true + + + + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.cloud + spring-cloud-starter-eureka + + + org.springframework + spring-web + + + org.springframework.cloud + spring-cloud-config-client + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + diff --git a/eureka-noweb/src/main/java/demo/StandaloneClientApplication.java b/eureka-noweb/src/main/java/demo/StandaloneClientApplication.java new file mode 100644 index 0000000..a400b87 --- /dev/null +++ b/eureka-noweb/src/main/java/demo/StandaloneClientApplication.java @@ -0,0 +1,41 @@ +package demo; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +/** + * @author Spencer Gibb + * @author Dave Syer + */ +@SpringBootApplication +@EnableDiscoveryClient +public class StandaloneClientApplication implements CommandLineRunner { + + private static Log logger = LogFactory.getLog(StandaloneClientApplication.class); + + @Autowired + private DiscoveryClient client; + + @Override + public void run(String... args) throws Exception { + List services = new ArrayList(); + for(ServiceInstance instance : client.getAllInstances()) { + services.add(instance.getServiceId()); + } + logger.info("Services: " + services ); + } + + public static void main(String[] args) { + SpringApplication.run(StandaloneClientApplication.class, args); + } +} diff --git a/eureka-noweb/src/main/resources/application.yml b/eureka-noweb/src/main/resources/application.yml new file mode 100644 index 0000000..54d5e12 --- /dev/null +++ b/eureka-noweb/src/main/resources/application.yml @@ -0,0 +1,6 @@ +spring: + application: + name: noweb + +server: + port: 7788 diff --git a/eureka-noweb/src/test/java/demo/NoopStandaloneClientApplicationTests.java b/eureka-noweb/src/test/java/demo/NoopStandaloneClientApplicationTests.java new file mode 100644 index 0000000..e1c825a --- /dev/null +++ b/eureka-noweb/src/test/java/demo/NoopStandaloneClientApplicationTests.java @@ -0,0 +1,29 @@ +package demo; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.IntegrationTest; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClient; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = StandaloneClientApplication.class) +@IntegrationTest({"eureka.client.enabled=false"}) +@DirtiesContext +public class NoopStandaloneClientApplicationTests { + + @Autowired + private DiscoveryClient discoveryClient; + + @Test + public void testDiscoveryClientIsNoop() { + assertTrue("discoveryClient is wrong instance type", discoveryClient instanceof NoopDiscoveryClient); + } + +} diff --git a/eureka-noweb/src/test/java/demo/StandaloneClientApplicationTests.java b/eureka-noweb/src/test/java/demo/StandaloneClientApplicationTests.java new file mode 100644 index 0000000..986b6bf --- /dev/null +++ b/eureka-noweb/src/test/java/demo/StandaloneClientApplicationTests.java @@ -0,0 +1,29 @@ +package demo; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.IntegrationTest; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = StandaloneClientApplication.class) +@IntegrationTest +@DirtiesContext +public class StandaloneClientApplicationTests { + + @Autowired + private DiscoveryClient discoveryClient; + + @Test + public void testDiscoveryClientIsEureka() { + assertTrue("discoveryClient is wrong instance type", discoveryClient instanceof EurekaDiscoveryClient); + } + +} diff --git a/feign-eureka/src/main/java/demo/HelloClientApplication.java b/feign-eureka/src/main/java/demo/HelloClientApplication.java index dbb8530..3f6bb60 100644 --- a/feign-eureka/src/main/java/demo/HelloClientApplication.java +++ b/feign-eureka/src/main/java/demo/HelloClientApplication.java @@ -1,23 +1,24 @@ package demo; -import com.netflix.client.config.IClientConfig; -import com.netflix.loadbalancer.*; +import static org.springframework.web.bind.annotation.RequestMethod.GET; + +import java.util.Arrays; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; -import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; +import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.cloud.netflix.ribbon.RibbonClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import java.util.Arrays; -import java.util.List; - -import static org.springframework.web.bind.annotation.RequestMethod.*; +import com.netflix.loadbalancer.BaseLoadBalancer; +import com.netflix.loadbalancer.ILoadBalancer; +import com.netflix.loadbalancer.Server; /** * @author Spencer Gibb diff --git a/feign-eureka/src/test/java/demo/NoWebApplicationTests.java b/feign-eureka/src/test/java/demo/NoWebApplicationTests.java new file mode 100644 index 0000000..46657da --- /dev/null +++ b/feign-eureka/src/test/java/demo/NoWebApplicationTests.java @@ -0,0 +1,29 @@ +package demo; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.IntegrationTest; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClient; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = HelloClientApplication.class) +@IntegrationTest("eureka.client.enabled=false") +@DirtiesContext +public class NoWebApplicationTests { + + @Autowired + private DiscoveryClient discoveryClient; + + @Test + public void testDiscoveryClientIsNoop() { + assertTrue("discoveryClient is wrong instance type", discoveryClient instanceof NoopDiscoveryClient); + } + +} diff --git a/noweb/pom.xml b/noweb/pom.xml new file mode 100644 index 0000000..bdfe5ef --- /dev/null +++ b/noweb/pom.xml @@ -0,0 +1,88 @@ + + + 4.0.0 + + org.springframework.cloud + spring-cloud-sample-noweb + 1.0.0.BUILD-SNAPSHOT + jar + + noweb + Demo project for Spring Cloud + + + org.springframework.cloud + spring-cloud-starter-parent + 1.0.0.BUILD-SNAPSHOT + + + + + UTF-8 + 1.7 + + + + + + + maven-deploy-plugin + + true + + + + + + + + org.springframework.cloud + spring-cloud-starter + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + diff --git a/noweb/src/main/java/demo/NotWebApplication.java b/noweb/src/main/java/demo/NotWebApplication.java new file mode 100644 index 0000000..e7efbb8 --- /dev/null +++ b/noweb/src/main/java/demo/NotWebApplication.java @@ -0,0 +1,16 @@ +package demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @author Spencer Gibb + * @author Dave Syer + */ +@SpringBootApplication +public class NotWebApplication { + + public static void main(String[] args) { + SpringApplication.run(NotWebApplication.class, args); + } +} diff --git a/noweb/src/main/resources/application.yml b/noweb/src/main/resources/application.yml new file mode 100644 index 0000000..553b44a --- /dev/null +++ b/noweb/src/main/resources/application.yml @@ -0,0 +1,6 @@ +spring: + application: + name: standalone + +server: + port: 7777 diff --git a/noweb/src/test/java/demo/DiscoveryNotWebApplicationTests.java b/noweb/src/test/java/demo/DiscoveryNotWebApplicationTests.java new file mode 100644 index 0000000..7563ebe --- /dev/null +++ b/noweb/src/test/java/demo/DiscoveryNotWebApplicationTests.java @@ -0,0 +1,58 @@ +package demo; + +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClient; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import demo.DiscoveryNotWebApplicationTests.NoDiscoveryApplication; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = NoDiscoveryApplication.class) +public class DiscoveryNotWebApplicationTests { + + @Autowired + private DiscoveryClient discoveryClient; + + @Test + public void testDiscoveryClientIsNoop() { + assertTrue("discoveryClient is wrong instance type", + discoveryClient instanceof NoopDiscoveryClient); + } + + /* + * This works because the NoopDiscoveryClient is installed if there is no @EnableDiscoveryClient + * *and* there is no implementation on the classpath + */ + @SpringBootApplication + protected static class NoDiscoveryApplication implements CommandLineRunner { + + private static Log logger = LogFactory.getLog(NotWebApplication.class); + + @Autowired + private DiscoveryClient client; + + @Override + public void run(String... args) throws Exception { + List services = new ArrayList(); + for (ServiceInstance instance : client.getAllInstances()) { + services.add(instance.getServiceId()); + } + logger.info("Services: " + services); + } + } + +} diff --git a/noweb/src/test/java/demo/NotWebApplicationTests.java b/noweb/src/test/java/demo/NotWebApplicationTests.java new file mode 100644 index 0000000..d5aad37 --- /dev/null +++ b/noweb/src/test/java/demo/NotWebApplicationTests.java @@ -0,0 +1,18 @@ +package demo; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = NotWebApplication.class) +@DirtiesContext +public class NotWebApplicationTests { + + @Test + public void testContextLoads() { + } + +} diff --git a/pom.xml b/pom.xml index 4c78cc0..7c8d37f 100644 --- a/pom.xml +++ b/pom.xml @@ -20,6 +20,8 @@ feign feign-eureka + eureka-noweb + noweb hystrix turbine zuul-proxy