Add ribbon-eureka sample

This commit is contained in:
Dave Syer
2015-03-13 09:36:31 +00:00
parent 3a24d6a64d
commit c176368b30
9 changed files with 268 additions and 2 deletions

View File

@@ -20,13 +20,14 @@
<modules>
<module>configserver-eureka</module>
<module>eureka-first</module>
<module>eureka-noweb</module>
<module>feign</module>
<module>feign-eureka</module>
<module>eureka-noweb</module>
<module>hystrix</module>
<module>netflix-sidecar</module>
<module>noweb</module>
<module>oauth2-ribbon</module>
<module>hystrix</module>
<module>ribbon-eureka</module>
<module>turbine</module>
<module>zuul-config-discovery</module>
<module>zuul-proxy</module>

106
ribbon-eureka/pom.xml Normal file
View File

@@ -0,0 +1,106 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sample-ribbon-eureka</artifactId>
<version>1.0.1.BUILD-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-cloud-sample-ribbon-eureka</name>
<description>Demo project for Spring Cloud</description>
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>1.0.1.BUILD-SNAPSHOT</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<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>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-loader-tools</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>

View File

@@ -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);
}
}

View File

@@ -0,0 +1,11 @@
spring:
application:
name: HelloClient
eureka:
instance:
leaseRenewalIntervalInSeconds: 10
client:
registryFetchIntervalSeconds: 5
serviceUrl:
defaultZone: http://localhost:${eureka.port:8761}/eureka/

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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<String> 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());
}
}

View File

@@ -0,0 +1,4 @@
server.port: 8761
spring.application.name: eureka
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false

View File

@@ -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/