Skip to content

Commit c2ee5eb

Browse files
author
zhaiyongchao
committed
Spring Cloud构建微服务架构(三)断路器
1 parent 2b2806a commit c2ee5eb

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed

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

+72
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>
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+
}
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+
}
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+
}
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)