diff --git a/Chapter9-1-5/eureka-server/pom.xml b/Chapter9-1-5/eureka-server/pom.xml new file mode 100644 index 0000000..3f01c31 --- /dev/null +++ b/Chapter9-1-5/eureka-server/pom.xml @@ -0,0 +1,62 @@ + + + 4.0.0 + + com.didispace + eureka-server + 1.0.0 + jar + + eureka-server + 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-eureka-server + + + + + + + + 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/eureka-server/src/main/java/com/didispace/Application.java b/Chapter9-1-5/eureka-server/src/main/java/com/didispace/Application.java new file mode 100644 index 0000000..414607d --- /dev/null +++ b/Chapter9-1-5/eureka-server/src/main/java/com/didispace/Application.java @@ -0,0 +1,15 @@ +package com.didispace; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; + +@EnableEurekaServer +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + new SpringApplicationBuilder(Application.class).web(true).run(args); + } + +} diff --git a/Chapter9-1-5/eureka-server/src/main/resources/application.properties b/Chapter9-1-5/eureka-server/src/main/resources/application.properties new file mode 100644 index 0000000..8dad723 --- /dev/null +++ b/Chapter9-1-5/eureka-server/src/main/resources/application.properties @@ -0,0 +1,6 @@ +server.port=1111 +#eureka.instance.hostname=localhost + +eureka.client.register-with-eureka=false +eureka.client.fetch-registry=false +eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/ \ No newline at end of file diff --git a/Chapter9-1-5/runable/eureka-server-1.0.0.jar b/Chapter9-1-5/runable/eureka-server-1.0.0.jar new file mode 100644 index 0000000..c0931b5 Binary files /dev/null and b/Chapter9-1-5/runable/eureka-server-1.0.0.jar differ diff --git a/Chapter9-1-5/runable/service-A-1.0.0.jar b/Chapter9-1-5/runable/service-A-1.0.0.jar new file mode 100644 index 0000000..12e6ea0 Binary files /dev/null and b/Chapter9-1-5/runable/service-A-1.0.0.jar differ diff --git a/Chapter9-1-5/runable/service-B-1.0.0.jar b/Chapter9-1-5/runable/service-B-1.0.0.jar new file mode 100644 index 0000000..64ce4c3 Binary files /dev/null and b/Chapter9-1-5/runable/service-B-1.0.0.jar differ diff --git a/Chapter9-1-5/service-A/pom.xml b/Chapter9-1-5/service-A/pom.xml new file mode 100644 index 0000000..d970101 --- /dev/null +++ b/Chapter9-1-5/service-A/pom.xml @@ -0,0 +1,61 @@ + + + 4.0.0 + + com.didispace + service-A + 1.0.0 + jar + + service-A + Spring Cloud project + + + org.springframework.boot + spring-boot-starter-parent + 1.3.5.RELEASE + + + + + UTF-8 + 1.8 + + + + + org.springframework.cloud + spring-cloud-starter-eureka + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + 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/service-A/src/main/java/com/didispace/ComputeServiceApplication.java b/Chapter9-1-5/service-A/src/main/java/com/didispace/ComputeServiceApplication.java new file mode 100644 index 0000000..cc30635 --- /dev/null +++ b/Chapter9-1-5/service-A/src/main/java/com/didispace/ComputeServiceApplication.java @@ -0,0 +1,15 @@ +package com.didispace; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +@EnableDiscoveryClient +@SpringBootApplication +public class ComputeServiceApplication { + + public static void main(String[] args) { + new SpringApplicationBuilder(ComputeServiceApplication.class).web(true).run(args); + } + +} 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 new file mode 100644 index 0000000..06dff1f --- /dev/null +++ b/Chapter9-1-5/service-A/src/main/java/com/didispace/web/ComputeController.java @@ -0,0 +1,28 @@ +package com.didispace.web; + +import org.apache.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class ComputeController { + + private final Logger logger = Logger.getLogger(getClass()); + + @Autowired + private DiscoveryClient client; + + @RequestMapping(value = "/add" ,method = RequestMethod.GET) + public Integer 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; + } + +} \ No newline at end of file diff --git a/Chapter9-1-5/service-A/src/main/resources/application.properties b/Chapter9-1-5/service-A/src/main/resources/application.properties new file mode 100644 index 0000000..c212856 --- /dev/null +++ b/Chapter9-1-5/service-A/src/main/resources/application.properties @@ -0,0 +1,6 @@ +spring.application.name=service-A + +server.port=2222 + +eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ + diff --git a/Chapter9-1-5/service-A/src/test/java/com/didispace/ApplicationTests.java b/Chapter9-1-5/service-A/src/test/java/com/didispace/ApplicationTests.java new file mode 100644 index 0000000..08cfadd --- /dev/null +++ b/Chapter9-1-5/service-A/src/test/java/com/didispace/ApplicationTests.java @@ -0,0 +1,37 @@ +package com.didispace; + +import com.didispace.web.ComputeController; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.http.MediaType; +import org.springframework.mock.web.MockServletContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import static org.hamcrest.Matchers.equalTo; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = MockServletContext.class) +@WebAppConfiguration +public class ApplicationTests { + + private MockMvc mvc; + + @Before + public void setUp() throws Exception { + mvc = MockMvcBuilders.standaloneSetup(new ComputeController()).build(); + } + + @Test + public void getHello() throws Exception { + } + +} diff --git a/Chapter9-1-5/service-B/pom.xml b/Chapter9-1-5/service-B/pom.xml new file mode 100644 index 0000000..3420bdf --- /dev/null +++ b/Chapter9-1-5/service-B/pom.xml @@ -0,0 +1,61 @@ + + + 4.0.0 + + com.didispace + service-B + 1.0.0 + jar + + service-B + Spring Cloud project + + + org.springframework.boot + spring-boot-starter-parent + 1.3.5.RELEASE + + + + + UTF-8 + 1.8 + + + + + org.springframework.cloud + spring-cloud-starter-eureka + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + 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/service-B/src/main/java/com/didispace/ComputeServiceApplication.java b/Chapter9-1-5/service-B/src/main/java/com/didispace/ComputeServiceApplication.java new file mode 100644 index 0000000..cc30635 --- /dev/null +++ b/Chapter9-1-5/service-B/src/main/java/com/didispace/ComputeServiceApplication.java @@ -0,0 +1,15 @@ +package com.didispace; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +@EnableDiscoveryClient +@SpringBootApplication +public class ComputeServiceApplication { + + public static void main(String[] args) { + new SpringApplicationBuilder(ComputeServiceApplication.class).web(true).run(args); + } + +} 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 new file mode 100644 index 0000000..06dff1f --- /dev/null +++ b/Chapter9-1-5/service-B/src/main/java/com/didispace/web/ComputeController.java @@ -0,0 +1,28 @@ +package com.didispace.web; + +import org.apache.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class ComputeController { + + private final Logger logger = Logger.getLogger(getClass()); + + @Autowired + private DiscoveryClient client; + + @RequestMapping(value = "/add" ,method = RequestMethod.GET) + public Integer 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; + } + +} \ No newline at end of file diff --git a/Chapter9-1-5/service-B/src/main/resources/application.properties b/Chapter9-1-5/service-B/src/main/resources/application.properties new file mode 100644 index 0000000..a906902 --- /dev/null +++ b/Chapter9-1-5/service-B/src/main/resources/application.properties @@ -0,0 +1,6 @@ +spring.application.name=service-B + +server.port=3333 + +eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ + diff --git a/Chapter9-1-5/service-B/src/test/java/com/didispace/ApplicationTests.java b/Chapter9-1-5/service-B/src/test/java/com/didispace/ApplicationTests.java new file mode 100644 index 0000000..14124ac --- /dev/null +++ b/Chapter9-1-5/service-B/src/test/java/com/didispace/ApplicationTests.java @@ -0,0 +1,31 @@ +package com.didispace; + +import com.didispace.web.ComputeController; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.mock.web.MockServletContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = MockServletContext.class) +@WebAppConfiguration +public class ApplicationTests { + + private MockMvc mvc; + + @Before + public void setUp() throws Exception { + mvc = MockMvcBuilders.standaloneSetup(new ComputeController()).build(); + } + + @Test + public void getHello() throws Exception { + } + +}