Skip to content

Commit 329cfab

Browse files
author
翟永超
committed
Spring Cloud构建微服务架构(三)断路器
1 parent 19bd09c commit 329cfab

File tree

17 files changed

+515
-0
lines changed

17 files changed

+515
-0
lines changed

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

Lines changed: 61 additions & 0 deletions
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>
Lines changed: 15 additions & 0 deletions
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+
}
Lines changed: 28 additions & 0 deletions
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+
}
Lines changed: 6 additions & 0 deletions
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+
Lines changed: 40 additions & 0 deletions
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+
}

Chapter1-1-3/eureka-feign/pom.xml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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>eureka-feign</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>eureka-feign</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-hystrix</artifactId>-->
30+
<!--</dependency>-->
31+
<dependency>
32+
<groupId>org.springframework.cloud</groupId>
33+
<artifactId>spring-cloud-starter-feign</artifactId>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.springframework.cloud</groupId>
37+
<artifactId>spring-cloud-starter-eureka</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-web</artifactId>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-test</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
</dependencies>
50+
51+
<dependencyManagement>
52+
<dependencies>
53+
<dependency>
54+
<groupId>org.springframework.cloud</groupId>
55+
<artifactId>spring-cloud-dependencies</artifactId>
56+
<version>Brixton.RELEASE</version>
57+
<type>pom</type>
58+
<scope>import</scope>
59+
</dependency>
60+
</dependencies>
61+
</dependencyManagement>
62+
63+
<build>
64+
<plugins>
65+
<plugin>
66+
<groupId>org.springframework.boot</groupId>
67+
<artifactId>spring-boot-maven-plugin</artifactId>
68+
</plugin>
69+
</plugins>
70+
</build>
71+
72+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.didispace;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
6+
import org.springframework.cloud.netflix.feign.EnableFeignClients;
7+
8+
@SpringBootApplication
9+
@EnableDiscoveryClient
10+
@EnableFeignClients
11+
public class FeignApplication {
12+
13+
public static void main(String[] args) {
14+
SpringApplication.run(FeignApplication.class, args);
15+
}
16+
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.didispace.service;
2+
3+
import org.springframework.cloud.netflix.feign.FeignClient;
4+
import org.springframework.web.bind.annotation.RequestMapping;
5+
import org.springframework.web.bind.annotation.RequestMethod;
6+
import org.springframework.web.bind.annotation.RequestParam;
7+
8+
@FeignClient(value = "compute-service", fallback = ComputeClientHystrix.class)
9+
public interface ComputeClient {
10+
11+
@RequestMapping(method = RequestMethod.GET, value = "/add")
12+
Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.didispace.service;
2+
3+
import org.springframework.stereotype.Component;
4+
import org.springframework.web.bind.annotation.RequestParam;
5+
6+
@Component
7+
public class ComputeClientHystrix implements ComputeClient {
8+
9+
@Override
10+
public Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b) {
11+
return -9999;
12+
}
13+
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.didispace.web;
2+
3+
import com.didispace.service.ComputeClient;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.web.bind.annotation.RequestMapping;
6+
import org.springframework.web.bind.annotation.RequestMethod;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@RestController
10+
public class ConsumerController {
11+
12+
@Autowired
13+
ComputeClient computeClient;
14+
15+
@RequestMapping(value = "/add", method = RequestMethod.GET)
16+
public Integer add() {
17+
return computeClient.add(10, 20);
18+
}
19+
20+
}

0 commit comments

Comments
 (0)