diff --git a/pom.xml b/pom.xml index 16a0412..5931dbe 100644 --- a/pom.xml +++ b/pom.xml @@ -52,6 +52,7 @@ zuul-config-discovery zuul-proxy zuul-proxy-eureka + tomcat-7.x diff --git a/tomcat-7.x/pom.xml b/tomcat-7.x/pom.xml new file mode 100644 index 0000000..1681aea --- /dev/null +++ b/tomcat-7.x/pom.xml @@ -0,0 +1,131 @@ + + + 4.0.0 + + org.springframework.cloud.sample + spring-cloud-sample-tomcat-7.x + ${spring-cloud.version} + jar + + + org.springframework.boot + spring-boot-starter-parent + 1.4.1.RELEASE + + + + + UTF-8 + demo.TomcatApp + 1.7 + Camden.BUILD-SNAPSHOT + 7.0.59 + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.cloud + spring-cloud-starter-zuul + + + + org.apache.tomcat + tomcat-juli + ${tomcat.version} + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + maven-deploy-plugin + + true + + + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/libs-snapshot-local + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/libs-milestone-local + + false + + + + spring-releases + Spring Releases + https://repo.spring.io/release + + false + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/libs-snapshot-local + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/libs-milestone-local + + false + + + + spring-releases + Spring Releases + https://repo.spring.io/release + + false + + + + + + \ No newline at end of file diff --git a/tomcat-7.x/src/main/java/demo/TomcatApp.java b/tomcat-7.x/src/main/java/demo/TomcatApp.java new file mode 100644 index 0000000..5089738 --- /dev/null +++ b/tomcat-7.x/src/main/java/demo/TomcatApp.java @@ -0,0 +1,48 @@ +package demo; + +import java.util.Arrays; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.ribbon.RibbonClient; +import org.springframework.cloud.netflix.zuul.EnableZuulProxy; +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.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import com.netflix.loadbalancer.BaseLoadBalancer; +import com.netflix.loadbalancer.ILoadBalancer; +import com.netflix.loadbalancer.Server; + +/** + * @author Ryan Baxter + */ +@SpringBootApplication +@EnableZuulProxy +@RestController +@RibbonClient(name = "self", configuration = TomcatApp.SelfRibbonClientConfiguration.class) +public class TomcatApp { + public static void main(String[] args) { + SpringApplication.run(TomcatApp.class, args); + } + + @RequestMapping("/good") + public String good(@RequestParam(required = false) String value) { + return "GOOD" + (value == null ? "" : " " + value) + "!"; + } + + // Load balancer with fixed server list for "hello" pointing to example.com + @Configuration + class SelfRibbonClientConfiguration { + + @Bean + public ILoadBalancer ribbonLoadBalancer() { + //because of this, it doesn't use eureka to lookup the server, + // but the classpath is tested + BaseLoadBalancer balancer = new BaseLoadBalancer(); + balancer.setServersList(Arrays.asList(new Server("localhost", 1234))); + return balancer; + } + + } +} diff --git a/tomcat-7.x/src/main/resources/application.yml b/tomcat-7.x/src/main/resources/application.yml new file mode 100644 index 0000000..fc6ff6e --- /dev/null +++ b/tomcat-7.x/src/main/resources/application.yml @@ -0,0 +1,15 @@ +zuul: + routes: + self: + path: /self/** + example: + path: /goodurl/** + url: http://localhost:1234/good + set-content-length: true + +spring: + application: + name: zuul-app +server: + port: 1234 + diff --git a/tomcat-7.x/src/test/java/demo/TomcatAppTests.java b/tomcat-7.x/src/test/java/demo/TomcatAppTests.java new file mode 100644 index 0000000..e8b97bc --- /dev/null +++ b/tomcat-7.x/src/test/java/demo/TomcatAppTests.java @@ -0,0 +1,37 @@ +package demo; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +/** + * @author Ryan Baxter + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +@RunWith(SpringRunner.class) +public class TomcatAppTests { + + @Autowired + private TestRestTemplate restTemplate; + + + @Test + public void ribbonTest() throws Exception { + ResponseEntity responseEntity = restTemplate.getForEntity("/self/good", String.class); + assertThat(responseEntity.getBody(), is("GOOD!")); + } + + @Test + public void sendResponseTest() throws Exception { + ResponseEntity responseEntity = restTemplate.getForEntity("/goodurl", String.class); + assertThat(responseEntity.getBody(), is("GOOD!")); + assertThat(responseEntity.getHeaders().get("Content-Length").get(0), is("5")); + } +}