diff --git a/Chapter9-1-5/api-gateway/pom.xml b/Chapter9-1-5/api-gateway/pom.xml new file mode 100644 index 0000000..1a80d78 --- /dev/null +++ b/Chapter9-1-5/api-gateway/pom.xml @@ -0,0 +1,72 @@ + + + 4.0.0 + + com.didispace + api-gateway + 1.0.0 + jar + + api-gateway + Spring Cloud project + + + org.springframework.boot + spring-boot-starter-parent + 1.3.5.RELEASE + + + + + UTF-8 + 1.8 + + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.cloud + spring-cloud-starter-zuul + + + + org.springframework.cloud + spring-cloud-starter-eureka + + + + org.springframework.boot + spring-boot-starter-web + + + + + + + + org.springframework.cloud + spring-cloud-dependencies + Brixton.RELEASE + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/Chapter9-1-5/api-gateway/src/main/java/com/didispace/Application.java b/Chapter9-1-5/api-gateway/src/main/java/com/didispace/Application.java new file mode 100644 index 0000000..225959c --- /dev/null +++ b/Chapter9-1-5/api-gateway/src/main/java/com/didispace/Application.java @@ -0,0 +1,15 @@ +package com.didispace; + +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.client.SpringCloudApplication; +import org.springframework.cloud.netflix.zuul.EnableZuulProxy; + +@EnableZuulProxy +@SpringCloudApplication +public class Application { + + public static void main(String[] args) { + new SpringApplicationBuilder(Application.class).web(true).run(args); + } + +} diff --git a/Chapter9-1-5/api-gateway/src/main/resources/application.properties b/Chapter9-1-5/api-gateway/src/main/resources/application.properties new file mode 100644 index 0000000..64abddd --- /dev/null +++ b/Chapter9-1-5/api-gateway/src/main/resources/application.properties @@ -0,0 +1,17 @@ +spring.application.name=api-gateway +server.port=5555 + +zuul.ignoredServices="*" + +# routes to serviceId +zuul.routes.api-a.path=/api-a/** +zuul.routes.api-a.serviceId=service-A + +zuul.routes.api-b.path=/api-b/** +zuul.routes.api-b.serviceId=service-B + +# routes to url +zuul.routes.api-a-url.path=/api-a-url/** +zuul.routes.api-a-url.url=http://localhost:2222/ + +eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ \ No newline at end of file diff --git a/Chapter9-1-5/service-A/src/main/java/com/didispace/web/ComputeController.java b/Chapter9-1-5/service-A/src/main/java/com/didispace/web/ComputeController.java index 06dff1f..6024f1c 100644 --- a/Chapter9-1-5/service-A/src/main/java/com/didispace/web/ComputeController.java +++ b/Chapter9-1-5/service-A/src/main/java/com/didispace/web/ComputeController.java @@ -18,11 +18,11 @@ public class ComputeController { private DiscoveryClient client; @RequestMapping(value = "/add" ,method = RequestMethod.GET) - public Integer add(@RequestParam Integer a, @RequestParam Integer b) { + public String add(@RequestParam Integer a, @RequestParam Integer b) { ServiceInstance instance = client.getLocalServiceInstance(); Integer r = a + b; logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r); - return r; + return "From Service-A, Result is " + r; } } \ No newline at end of file diff --git a/Chapter9-1-5/service-B/src/main/java/com/didispace/web/ComputeController.java b/Chapter9-1-5/service-B/src/main/java/com/didispace/web/ComputeController.java index 06dff1f..31af894 100644 --- a/Chapter9-1-5/service-B/src/main/java/com/didispace/web/ComputeController.java +++ b/Chapter9-1-5/service-B/src/main/java/com/didispace/web/ComputeController.java @@ -18,11 +18,11 @@ public class ComputeController { private DiscoveryClient client; @RequestMapping(value = "/add" ,method = RequestMethod.GET) - public Integer add(@RequestParam Integer a, @RequestParam Integer b) { + public String add(@RequestParam Integer a, @RequestParam Integer b) { ServiceInstance instance = client.getLocalServiceInstance(); Integer r = a + b; logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r); - return r; + return "From Service-B, Result is " + r; } } \ No newline at end of file