From c176368b30c5461a408635122e0c506bc549097b Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 13 Mar 2015 09:36:31 +0000 Subject: [PATCH] Add ribbon-eureka sample --- pom.xml | 5 +- ribbon-eureka/pom.xml | 106 ++++++++++++++++++ .../java/demo/HelloClientApplication.java | 30 +++++ .../src/main/resources/application.yml | 11 ++ .../java/apps/EurekaServerApplication.java | 19 ++++ .../src/test/java/apps/SimpleApplication.java | 41 +++++++ .../demo/HelloClientApplicationTests.java | 49 ++++++++ .../src/test/resources/eureka.properties | 4 + .../src/test/resources/simple.properties | 5 + 9 files changed, 268 insertions(+), 2 deletions(-) create mode 100644 ribbon-eureka/pom.xml create mode 100644 ribbon-eureka/src/main/java/demo/HelloClientApplication.java create mode 100644 ribbon-eureka/src/main/resources/application.yml create mode 100644 ribbon-eureka/src/test/java/apps/EurekaServerApplication.java create mode 100644 ribbon-eureka/src/test/java/apps/SimpleApplication.java create mode 100644 ribbon-eureka/src/test/java/demo/HelloClientApplicationTests.java create mode 100644 ribbon-eureka/src/test/resources/eureka.properties create mode 100644 ribbon-eureka/src/test/resources/simple.properties diff --git a/pom.xml b/pom.xml index 5752db4..b14d75f 100644 --- a/pom.xml +++ b/pom.xml @@ -20,13 +20,14 @@ configserver-eureka eureka-first + eureka-noweb feign feign-eureka - eureka-noweb + hystrix netflix-sidecar noweb oauth2-ribbon - hystrix + ribbon-eureka turbine zuul-config-discovery zuul-proxy diff --git a/ribbon-eureka/pom.xml b/ribbon-eureka/pom.xml new file mode 100644 index 0000000..ff2e031 --- /dev/null +++ b/ribbon-eureka/pom.xml @@ -0,0 +1,106 @@ + + + 4.0.0 + + org.springframework.cloud + spring-cloud-sample-ribbon-eureka + 1.0.1.BUILD-SNAPSHOT + jar + + spring-cloud-sample-ribbon-eureka + Demo project for Spring Cloud + + + org.springframework.cloud + spring-cloud-starter-parent + 1.0.1.BUILD-SNAPSHOT + + + + + UTF-8 + 1.7 + + + + + + + maven-deploy-plugin + + true + + + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.cloud + spring-cloud-starter-eureka + + + org.springframework.cloud + spring-cloud-starter-ribbon + + + org.springframework.cloud + spring-cloud-starter-eureka-server + test + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-loader-tools + 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/ribbon-eureka/src/main/java/demo/HelloClientApplication.java b/ribbon-eureka/src/main/java/demo/HelloClientApplication.java new file mode 100644 index 0000000..4e95ef3 --- /dev/null +++ b/ribbon-eureka/src/main/java/demo/HelloClientApplication.java @@ -0,0 +1,30 @@ +package demo; + +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.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +/** + * @author Spencer Gibb + */ +@SpringBootApplication +@EnableDiscoveryClient +@RestController +public class HelloClientApplication { + @Autowired + RestTemplate client; + + @RequestMapping("/") + public String hello() { + return client.getForObject("http://simple/", String.class); + } + + public static void main(String[] args) { + SpringApplication.run(HelloClientApplication.class, args); + } + +} \ No newline at end of file diff --git a/ribbon-eureka/src/main/resources/application.yml b/ribbon-eureka/src/main/resources/application.yml new file mode 100644 index 0000000..134d419 --- /dev/null +++ b/ribbon-eureka/src/main/resources/application.yml @@ -0,0 +1,11 @@ +spring: + application: + name: HelloClient +eureka: + instance: + leaseRenewalIntervalInSeconds: 10 + client: + registryFetchIntervalSeconds: 5 + serviceUrl: + defaultZone: http://localhost:${eureka.port:8761}/eureka/ + \ No newline at end of file diff --git a/ribbon-eureka/src/test/java/apps/EurekaServerApplication.java b/ribbon-eureka/src/test/java/apps/EurekaServerApplication.java new file mode 100644 index 0000000..1983ade --- /dev/null +++ b/ribbon-eureka/src/test/java/apps/EurekaServerApplication.java @@ -0,0 +1,19 @@ +package apps; + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; +import org.springframework.context.annotation.Configuration; + +@Configuration +@EnableAutoConfiguration +@EnableEurekaServer +public class EurekaServerApplication { + + public static void main(String[] args) { + new SpringApplicationBuilder(EurekaServerApplication.class).properties( + "spring.config.name:eureka", "logging.level.com.netflix.discovery:OFF") + .run(args); + } + +} diff --git a/ribbon-eureka/src/test/java/apps/SimpleApplication.java b/ribbon-eureka/src/test/java/apps/SimpleApplication.java new file mode 100644 index 0000000..d45b119 --- /dev/null +++ b/ribbon-eureka/src/test/java/apps/SimpleApplication.java @@ -0,0 +1,41 @@ +/* + * Copyright 2014-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 apps; + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Configuration +@EnableAutoConfiguration +@EnableDiscoveryClient +@RestController +public class SimpleApplication { + + @RequestMapping("/") + public String hello() { + return "Hello"; + } + + public static void main(String[] args) { + new SpringApplicationBuilder(SimpleApplication.class).properties( + "spring.config.name:simple").run(args); + } +} diff --git a/ribbon-eureka/src/test/java/demo/HelloClientApplicationTests.java b/ribbon-eureka/src/test/java/demo/HelloClientApplicationTests.java new file mode 100644 index 0000000..61fd95b --- /dev/null +++ b/ribbon-eureka/src/test/java/demo/HelloClientApplicationTests.java @@ -0,0 +1,49 @@ +package demo; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.IntegrationTest; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = HelloClientApplication.class) +@WebAppConfiguration +@IntegrationTest("server.port=0") +@DirtiesContext +public class HelloClientApplicationTests { + + @Value("${local.server.port}") + int port; + + @Test + public void contextLoads() throws Exception { + } + + @Test + @Ignore + /* + * Unignore this if you have the eureka and "simple" servers running in separate + * processes (Eureka singletons will prevent you from running all apps in the same + * process). You also need to pause (e.g. set debug stop) while HelloClient registers + * with Eureka (only registered services can access the registry). + */ + public void clientConnects() throws Exception { + ResponseEntity response = new TestRestTemplate().getForEntity( + "http://localhost:" + port, String.class); + assertNotNull("response was null", response); + assertEquals("Wrong status code", HttpStatus.OK, response.getStatusCode()); + assertEquals("Hello", response.getBody()); + } + +} diff --git a/ribbon-eureka/src/test/resources/eureka.properties b/ribbon-eureka/src/test/resources/eureka.properties new file mode 100644 index 0000000..a0e3d7f --- /dev/null +++ b/ribbon-eureka/src/test/resources/eureka.properties @@ -0,0 +1,4 @@ +server.port: 8761 +spring.application.name: eureka +eureka.client.registerWithEureka=false +eureka.client.fetchRegistry=false \ No newline at end of file diff --git a/ribbon-eureka/src/test/resources/simple.properties b/ribbon-eureka/src/test/resources/simple.properties new file mode 100644 index 0000000..c6a8f1f --- /dev/null +++ b/ribbon-eureka/src/test/resources/simple.properties @@ -0,0 +1,5 @@ +server.port: 8081 +spring.application.name: simple +eureka.instance.leaseRenewalIntervalInSeconds: 10 +eureka.client.registryFetchIntervalSeconds: 5 +eureka.client.serviceUrl.defaultZone: http://localhost:${eureka.port:8761}/eureka/