From 13dcebbeb022445e354e0b4cc88172ee0d582313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E7=8C=BFDD?= Date: Sat, 25 Jun 2016 22:31:15 +0800 Subject: [PATCH] =?UTF-8?q?Spring=20Cloud=E6=9E=84=E5=BB=BA=E5=BE=AE?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E6=9E=B6=E6=9E=84=EF=BC=88=E4=B8=89=EF=BC=89?= =?UTF-8?q?=E6=96=AD=E8=B7=AF=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Chapter9-1-3/compute-service/pom.xml | 61 +++++++++++++++++ .../didispace/ComputeServiceApplication.java | 15 +++++ .../com/didispace/web/ComputeController.java | 28 ++++++++ .../src/main/resources/application.properties | 6 ++ .../java/com/didispace/ApplicationTests.java | 40 +++++++++++ Chapter9-2-1/pom.xml | 66 +++++++++++++++++++ .../main/java/com/didispace/Application.java | 15 +++++ .../src/main/resources/application.properties | 0 Chapter9-2-1/src/main/resources/dubbo.xml | 23 +++++++ .../java/com/didispace/ApplicationTests.java | 40 +++++++++++ 10 files changed, 294 insertions(+) create mode 100644 Chapter9-1-3/compute-service/pom.xml create mode 100644 Chapter9-1-3/compute-service/src/main/java/com/didispace/ComputeServiceApplication.java create mode 100644 Chapter9-1-3/compute-service/src/main/java/com/didispace/web/ComputeController.java create mode 100644 Chapter9-1-3/compute-service/src/main/resources/application.properties create mode 100644 Chapter9-1-3/compute-service/src/test/java/com/didispace/ApplicationTests.java create mode 100644 Chapter9-2-1/pom.xml create mode 100644 Chapter9-2-1/src/main/java/com/didispace/Application.java create mode 100644 Chapter9-2-1/src/main/resources/application.properties create mode 100644 Chapter9-2-1/src/main/resources/dubbo.xml create mode 100644 Chapter9-2-1/src/test/java/com/didispace/ApplicationTests.java 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"))); + } + +}