Skip to content

Commit f8949f6

Browse files
author
YunaiV
committed
增加 Hystrix 入门文章
1 parent 93992e8 commit f8949f6

File tree

9 files changed

+233
-2
lines changed

9 files changed

+233
-2
lines changed

lab-57/lab-57-hystrix-demo01/pom.xml

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>lab-57</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-57-hystrix-demo01</artifactId>
13+
14+
<properties>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
18+
</properties>
19+
20+
<!--
21+
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
22+
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
23+
-->
24+
<dependencyManagement>
25+
<dependencies>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-parent</artifactId>
29+
<version>${spring.boot.version}</version>
30+
<type>pom</type>
31+
<scope>import</scope>
32+
</dependency>
33+
</dependencies>
34+
</dependencyManagement>
35+
36+
<dependencies>
37+
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-web</artifactId>
41+
</dependency>
42+
43+
<!-- 引入 Hystrix 依赖 -->
44+
<dependency>
45+
<groupId>com.netflix.hystrix</groupId>
46+
<artifactId>hystrix-core</artifactId>
47+
<version>1.5.18</version>
48+
</dependency>
49+
<dependency>
50+
<groupId>com.netflix.hystrix</groupId>
51+
<artifactId>hystrix-javanica</artifactId>
52+
<version>1.5.18</version>
53+
</dependency>
54+
55+
</dependencies>
56+
57+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cn.iocoder.springboot.lab57.hystrixdemo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.web.client.RestTemplate;
7+
8+
@SpringBootApplication
9+
public class DemoApplication {
10+
11+
@Bean
12+
public RestTemplate restTemplate() {
13+
return new RestTemplate();
14+
}
15+
16+
public static void main(String[] args) {
17+
SpringApplication.run(DemoApplication.class, args);
18+
}
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cn.iocoder.springboot.lab57.hystrixdemo.config;
2+
3+
import com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.annotation.EnableAspectJAutoProxy;
7+
8+
@Configuration
9+
@EnableAspectJAutoProxy
10+
public class HystrixConfig {
11+
12+
@Bean
13+
public HystrixCommandAspect hystrixCommandAspect() {
14+
return new HystrixCommandAspect();
15+
}
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cn.iocoder.springboot.lab57.hystrixdemo.controller;
2+
3+
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
4+
import org.apache.commons.lang.exception.ExceptionUtils;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestParam;
11+
import org.springframework.web.bind.annotation.RestController;
12+
import org.springframework.web.client.RestTemplate;
13+
14+
@RestController
15+
@RequestMapping("/demo")
16+
public class DemoController {
17+
18+
private Logger logger = LoggerFactory.getLogger(getClass());
19+
20+
@Autowired
21+
private RestTemplate restTemplate;
22+
23+
@GetMapping("/get_user")
24+
@HystrixCommand(fallbackMethod = "getUserFallback")
25+
public String getUser(@RequestParam("id") Integer id) {
26+
logger.info("[getUser][准备调用 user-service 获取用户({})详情]", id);
27+
return restTemplate.getForEntity("http://127.0.0.1:18080/user/get?id=" + id, String.class).getBody();
28+
}
29+
30+
public String getUserFallback(Integer id, Throwable throwable) {
31+
logger.info("[getUserFallback][id({}) exception({})]", id, ExceptionUtils.getRootCauseMessage(throwable));
32+
return "mock:User:" + id;
33+
}
34+
35+
}

lab-57/lab-57-user-service/pom.xml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>lab-57</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-57-user-service</artifactId>
13+
14+
<properties>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
18+
</properties>
19+
20+
<!--
21+
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
22+
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
23+
-->
24+
<dependencyManagement>
25+
<dependencies>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-parent</artifactId>
29+
<version>${spring.boot.version}</version>
30+
<type>pom</type>
31+
<scope>import</scope>
32+
</dependency>
33+
</dependencies>
34+
</dependencyManagement>
35+
36+
<dependencies>
37+
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-web</artifactId>
41+
</dependency>
42+
</dependencies>
43+
44+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package cn.iocoder.springboot.lab57.userservice;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestParam;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
import java.util.List;
11+
import java.util.stream.Collectors;
12+
13+
@SpringBootApplication
14+
public class UserServiceApplication {
15+
16+
@RestController
17+
@RequestMapping("/user")
18+
public class UserController {
19+
20+
@GetMapping("/get")
21+
public String get(@RequestParam("id") Integer id) {
22+
return "User:" + id;
23+
}
24+
25+
@GetMapping("/batch_get")
26+
public List<String> batchGet(@RequestParam("ids") List<Integer> ids) {
27+
return ids.stream().map(id -> "User:" + id).collect(Collectors.toList());
28+
}
29+
30+
}
31+
32+
public static void main(String[] args) {
33+
// 设置端口
34+
System.setProperty("server.port", "18080");
35+
36+
// 应用启动
37+
SpringApplication.run(UserServiceApplication.class, args);
38+
}
39+
40+
}

lab-57/pom.xml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>labs-parent</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-57</artifactId>
13+
<packaging>pom</packaging>
14+
<modules>
15+
<module>lab-57-user-service</module>
16+
<module>lab-57-hystrix-demo01</module>
17+
</modules>
18+
19+
</project>

labx-23/labx-23-user-service/pom.xml

-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
<maven.compiler.target>1.8</maven.compiler.target>
1616
<maven.compiler.source>1.8</maven.compiler.source>
1717
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
18-
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
1918
</properties>
2019

2120
<!--
@@ -42,5 +41,4 @@
4241
</dependency>
4342
</dependencies>
4443

45-
4644
</project>

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
<!-- <module>labx-21</module>-->
9393
<!-- <module>labx-22</module>-->
9494
<!-- <module>labx-23</module>-->
95+
<module>lab-57</module>
9596
</modules>
9697

9798
</project>

0 commit comments

Comments
 (0)