diff --git a/Chapter9-1-3/compute-service/pom.xml b/Chapter9-1-3/compute-service/pom.xml new file mode 100644 index 0000000..04ef83d --- /dev/null +++ b/Chapter9-1-3/compute-service/pom.xml @@ -0,0 +1,61 @@ + + + 4.0.0 + + com.didispace + compute-service + 1.0.0 + jar + + compute-service + 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-3/compute-service/src/main/java/com/didispace/ComputeServiceApplication.java b/Chapter9-1-3/compute-service/src/main/java/com/didispace/ComputeServiceApplication.java new file mode 100644 index 0000000..cc30635 --- /dev/null +++ b/Chapter9-1-3/compute-service/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-3/compute-service/src/main/java/com/didispace/web/ComputeController.java b/Chapter9-1-3/compute-service/src/main/java/com/didispace/web/ComputeController.java new file mode 100644 index 0000000..06dff1f --- /dev/null +++ b/Chapter9-1-3/compute-service/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-3/compute-service/src/main/resources/application.properties b/Chapter9-1-3/compute-service/src/main/resources/application.properties new file mode 100644 index 0000000..c14fe52 --- /dev/null +++ b/Chapter9-1-3/compute-service/src/main/resources/application.properties @@ -0,0 +1,6 @@ +spring.application.name=compute-service + +server.port=2222 + +eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ + diff --git a/Chapter9-1-3/compute-service/src/test/java/com/didispace/ApplicationTests.java b/Chapter9-1-3/compute-service/src/test/java/com/didispace/ApplicationTests.java new file mode 100644 index 0000000..a2f4280 --- /dev/null +++ b/Chapter9-1-3/compute-service/src/test/java/com/didispace/ApplicationTests.java @@ -0,0 +1,40 @@ +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 { + mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string(equalTo("Hello World"))); + } + +} diff --git a/Chapter9-2-1/pom.xml b/Chapter9-2-1/pom.xml new file mode 100644 index 0000000..1c3d697 --- /dev/null +++ b/Chapter9-2-1/pom.xml @@ -0,0 +1,66 @@ + + + 4.0.0 + + com.didispace + Chapter9-2-1 + 1.0.0 + jar + + Chapter9-2-1 + Spring Boot project + + + org.springframework.boot + spring-boot-starter-parent + 1.3.2.RELEASE + + + + + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-starter-web + + + + com.alibaba + dubbo + 2.4.9 + + + spring + org.springframework + + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/Chapter9-2-1/src/main/java/com/didispace/Application.java b/Chapter9-2-1/src/main/java/com/didispace/Application.java new file mode 100644 index 0000000..8281fca --- /dev/null +++ b/Chapter9-2-1/src/main/java/com/didispace/Application.java @@ -0,0 +1,15 @@ +package com.didispace; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ImportResource; + +@SpringBootApplication +@ImportResource({"classpath:dubbo.xml"}) +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/Chapter9-2-1/src/main/resources/application.properties b/Chapter9-2-1/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29 diff --git a/Chapter9-2-1/src/main/resources/dubbo.xml b/Chapter9-2-1/src/main/resources/dubbo.xml new file mode 100644 index 0000000..931723f --- /dev/null +++ b/Chapter9-2-1/src/main/resources/dubbo.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Chapter9-2-1/src/test/java/com/didispace/ApplicationTests.java b/Chapter9-2-1/src/test/java/com/didispace/ApplicationTests.java new file mode 100644 index 0000000..5d70be9 --- /dev/null +++ b/Chapter9-2-1/src/test/java/com/didispace/ApplicationTests.java @@ -0,0 +1,40 @@ +package com.didispace; + +import com.didispace.web.HelloController; +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 HelloController()).build(); + } + + @Test + public void getHello() throws Exception { + mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().string(equalTo("Hello World"))); + } + +}