Skip to content

Commit 13dcebb

Browse files
dyc87112zhaiyongchao
authored and
zhaiyongchao
committed
Spring Cloud构建微服务架构(三)断路器
1 parent c2ee5eb commit 13dcebb

File tree

10 files changed

+294
-0
lines changed

10 files changed

+294
-0
lines changed

Chapter9-1-3/compute-service/pom.xml

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.didispace</groupId>
7+
<artifactId>compute-service</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>compute-service</name>
12+
<description>Spring Cloud project</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.3.5.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.cloud</groupId>
29+
<artifactId>spring-cloud-starter-eureka</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-test</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
38+
</dependencies>
39+
40+
<dependencyManagement>
41+
<dependencies>
42+
<dependency>
43+
<groupId>org.springframework.cloud</groupId>
44+
<artifactId>spring-cloud-dependencies</artifactId>
45+
<version>Brixton.RELEASE</version>
46+
<type>pom</type>
47+
<scope>import</scope>
48+
</dependency>
49+
</dependencies>
50+
</dependencyManagement>
51+
52+
<build>
53+
<plugins>
54+
<plugin>
55+
<groupId>org.springframework.boot</groupId>
56+
<artifactId>spring-boot-maven-plugin</artifactId>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
61+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.didispace;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
import org.springframework.boot.builder.SpringApplicationBuilder;
5+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
6+
7+
@EnableDiscoveryClient
8+
@SpringBootApplication
9+
public class ComputeServiceApplication {
10+
11+
public static void main(String[] args) {
12+
new SpringApplicationBuilder(ComputeServiceApplication.class).web(true).run(args);
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.didispace.web;
2+
3+
import org.apache.log4j.Logger;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.cloud.client.ServiceInstance;
6+
import org.springframework.cloud.client.discovery.DiscoveryClient;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RequestMethod;
9+
import org.springframework.web.bind.annotation.RequestParam;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
@RestController
13+
public class ComputeController {
14+
15+
private final Logger logger = Logger.getLogger(getClass());
16+
17+
@Autowired
18+
private DiscoveryClient client;
19+
20+
@RequestMapping(value = "/add" ,method = RequestMethod.GET)
21+
public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
22+
ServiceInstance instance = client.getLocalServiceInstance();
23+
Integer r = a + b;
24+
logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
25+
return r;
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
spring.application.name=compute-service
2+
3+
server.port=2222
4+
5+
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.didispace;
2+
3+
import com.didispace.web.ComputeController;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.boot.test.SpringApplicationConfiguration;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.mock.web.MockServletContext;
10+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
11+
import org.springframework.test.context.web.WebAppConfiguration;
12+
import org.springframework.test.web.servlet.MockMvc;
13+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
14+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
15+
16+
import static org.hamcrest.Matchers.equalTo;
17+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
18+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
19+
20+
21+
@RunWith(SpringJUnit4ClassRunner.class)
22+
@SpringApplicationConfiguration(classes = MockServletContext.class)
23+
@WebAppConfiguration
24+
public class ApplicationTests {
25+
26+
private MockMvc mvc;
27+
28+
@Before
29+
public void setUp() throws Exception {
30+
mvc = MockMvcBuilders.standaloneSetup(new ComputeController()).build();
31+
}
32+
33+
@Test
34+
public void getHello() throws Exception {
35+
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
36+
.andExpect(status().isOk())
37+
.andExpect(content().string(equalTo("Hello World")));
38+
}
39+
40+
}

Chapter9-2-1/pom.xml

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.didispace</groupId>
7+
<artifactId>Chapter9-2-1</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>Chapter9-2-1</name>
12+
<description>Spring Boot project</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.3.2.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-test</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-web</artifactId>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>com.alibaba</groupId>
45+
<artifactId>dubbo</artifactId>
46+
<version>2.4.9</version>
47+
<exclusions>
48+
<exclusion>
49+
<artifactId>spring</artifactId>
50+
<groupId>org.springframework</groupId>
51+
</exclusion>
52+
</exclusions>
53+
</dependency>
54+
55+
</dependencies>
56+
57+
<build>
58+
<plugins>
59+
<plugin>
60+
<groupId>org.springframework.boot</groupId>
61+
<artifactId>spring-boot-maven-plugin</artifactId>
62+
</plugin>
63+
</plugins>
64+
</build>
65+
66+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.didispace;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.ImportResource;
6+
7+
@SpringBootApplication
8+
@ImportResource({"classpath:dubbo.xml"})
9+
public class Application {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(Application.class, args);
13+
}
14+
15+
}

Chapter9-2-1/src/main/resources/application.properties

Whitespace-only changes.
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
6+
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
7+
8+
<!-- 提供方应用信息,用于计算依赖关系 -->
9+
<dubbo:application name="hello-world-app" />
10+
11+
<!-- 使用multicast广播注册中心暴露服务地址 -->
12+
<dubbo:registry address="multicast://224.5.6.7:1234" />
13+
14+
<!-- 用dubbo协议在20880端口暴露服务 -->
15+
<dubbo:protocol name="dubbo" port="20880" />
16+
17+
<!-- 声明需要暴露的服务接口 -->
18+
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />
19+
20+
<!-- 和本地bean一样实现服务 -->
21+
<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />
22+
23+
</beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.didispace;
2+
3+
import com.didispace.web.HelloController;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.boot.test.SpringApplicationConfiguration;
8+
import org.springframework.http.MediaType;
9+
import org.springframework.mock.web.MockServletContext;
10+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
11+
import org.springframework.test.context.web.WebAppConfiguration;
12+
import org.springframework.test.web.servlet.MockMvc;
13+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
14+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
15+
16+
import static org.hamcrest.Matchers.equalTo;
17+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
18+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
19+
20+
21+
@RunWith(SpringJUnit4ClassRunner.class)
22+
@SpringApplicationConfiguration(classes = MockServletContext.class)
23+
@WebAppConfiguration
24+
public class ApplicationTests {
25+
26+
private MockMvc mvc;
27+
28+
@Before
29+
public void setUp() throws Exception {
30+
mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
31+
}
32+
33+
@Test
34+
public void getHello() throws Exception {
35+
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
36+
.andExpect(status().isOk())
37+
.andExpect(content().string(equalTo("Hello World")));
38+
}
39+
40+
}

0 commit comments

Comments
 (0)