Skip to content

Commit fcdc921

Browse files
author
YunaiV
committed
增加 Hystrix 入门示例(Hystrix + Feign)
1 parent 8aeb88f commit fcdc921

File tree

12 files changed

+172
-24
lines changed

12 files changed

+172
-24
lines changed

labx-23/labx-23-scn-hystrix-dashboard-turbine/pom.xml

+2-6
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,13 @@
4848
<artifactId>spring-boot-starter-web</artifactId>
4949
</dependency>
5050

51-
<!-- 引入 Spring Cloud Netflix Hystrix 相关依赖,将 Hystrix 作为服务保障组件,并实现对其的自动配置 -->
52-
<dependency>
53-
<groupId>org.springframework.cloud</groupId>
54-
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
55-
</dependency>
56-
51+
<!-- 实现对 Hystrix Dashboard 的自动配置 -->
5752
<dependency>
5853
<groupId>org.springframework.cloud</groupId>
5954
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
6055
</dependency>
6156

57+
<!-- 实现对 Turbine 的自动配置 -->
6258
<dependency>
6359
<groupId>org.springframework.cloud</groupId>
6460
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>

labx-23/labx-23-scn-hystrix-dashboard-turbine/src/main/java/cn/iocoder/springcloud/labx23/hystrixdemo/HystrixDashboardApplication.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import org.springframework.cloud.netflix.turbine.EnableTurbine;
77

88
@SpringBootApplication
9-
@EnableHystrixDashboard
10-
@EnableTurbine
9+
@EnableHystrixDashboard // 声明开启 Hystrix Dashboard 功能
10+
@EnableTurbine // 声明开启 Turbine 功能
1111
public class HystrixDashboardApplication {
1212

1313
public static void main(String[] args) {

labx-23/labx-23-scn-hystrix-dashboard-turbine/src/main/resources/application.yml

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ eureka:
1010
service-url:
1111
defaultZone: http://127.0.0.1:8761/eureka/ # Eureka-Server 地址
1212

13+
# Turbine 配置项,对应 TurbineProperties 配置类
1314
turbine:
14-
app-config: hystrix-demo
15-
combine-host-port: true
16-
cluster-name-expression: new String('default')
15+
app-config: hystrix-demo # 配置需要 Turbine 聚合的服务名;如果有多个,使用逗号分隔。
16+
combine-host-port: true # 服务是否以 host + port 进行区分,默认为 true。如果设置为 false,则只以 host 进行区分,这样会导致相同主机部署了相同服务的多个实例,会被认为是一个
17+
cluster-name-expression: new String('default') # 指定集群名,设置为 `default` 表示默认集群。

labx-23/labx-23-scn-hystrix-dashboard/pom.xml

+1-12
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,7 @@
4848
<artifactId>spring-boot-starter-web</artifactId>
4949
</dependency>
5050

51-
<!-- 引入 Spring Cloud Netflix Hystrix 相关依赖,将 Hystrix 作为服务保障组件,并实现对其的自动配置 -->
52-
<dependency>
53-
<groupId>org.springframework.cloud</groupId>
54-
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
55-
</dependency>
56-
57-
<!-- 实现对 Actuator 的自动化配置 -->
58-
<dependency>
59-
<groupId>org.springframework.boot</groupId>
60-
<artifactId>spring-boot-starter-actuator</artifactId>
61-
</dependency>
62-
51+
<!-- 实现对 Hystrix Dashboard 的自动配置 -->
6352
<dependency>
6453
<groupId>org.springframework.cloud</groupId>
6554
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>

labx-23/labx-23-scn-hystrix-dashboard/src/main/java/cn/iocoder/springcloud/labx23/hystrixdemo/HystrixDashboardApplication.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
66

77
@SpringBootApplication
8-
@EnableHystrixDashboard
8+
@EnableHystrixDashboard // 声明开启 Hystrix Dashboard 功能
99
public class HystrixDashboardApplication {
1010

1111
public static void main(String[] args) {
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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>labx-23</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>labx-23-scn-hystrix-feign</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+
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
19+
</properties>
20+
21+
<!--
22+
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
23+
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
24+
-->
25+
<dependencyManagement>
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-parent</artifactId>
30+
<version>${spring.boot.version}</version>
31+
<type>pom</type>
32+
<scope>import</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.cloud</groupId>
36+
<artifactId>spring-cloud-dependencies</artifactId>
37+
<version>${spring.cloud.version}</version>
38+
<type>pom</type>
39+
<scope>import</scope>
40+
</dependency>
41+
</dependencies>
42+
</dependencyManagement>
43+
44+
<dependencies>
45+
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-starter-web</artifactId>
49+
</dependency>
50+
51+
<!-- 引入 Spring Cloud Netflix Hystrix 相关依赖,将 Hystrix 作为服务保障组件,并实现对其的自动配置 -->
52+
<dependency>
53+
<groupId>org.springframework.cloud</groupId>
54+
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
55+
</dependency>
56+
57+
<!-- 引入 Spring Cloud OpenFeign 相关依赖,使用 OpenFeign 提供声明式调用,并实现对其的自动配置 -->
58+
<dependency>
59+
<groupId>org.springframework.cloud</groupId>
60+
<artifactId>spring-cloud-starter-openfeign</artifactId>
61+
</dependency>
62+
</dependencies>
63+
64+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cn.iocoder.springcloud.labx23.hystrixdemo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
6+
import org.springframework.cloud.openfeign.EnableFeignClients;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.web.client.RestTemplate;
9+
10+
@SpringBootApplication
11+
@EnableCircuitBreaker // 声明开启断路器
12+
@EnableFeignClients // 开启 Feign Client 功能
13+
public class DemoApplication {
14+
15+
@Bean
16+
public RestTemplate restTemplate() {
17+
return new RestTemplate();
18+
}
19+
20+
public static void main(String[] args) {
21+
SpringApplication.run(DemoApplication.class, args);
22+
}
23+
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cn.iocoder.springcloud.labx23.hystrixdemo.controller;
2+
3+
import cn.iocoder.springcloud.labx23.hystrixdemo.feign.UserServiceFeignClient;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RequestParam;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
@RestController
13+
@RequestMapping("/feign-demo")
14+
public class FeignDemoController {
15+
16+
private Logger logger = LoggerFactory.getLogger(getClass());
17+
18+
@Autowired
19+
private UserServiceFeignClient userServiceFeignClient;
20+
21+
@GetMapping("/get_user")
22+
public String getUser(@RequestParam("id") Integer id) {
23+
logger.info("[getUser][准备调用 user-service 获取用户({})详情]", id);
24+
return userServiceFeignClient.getUser(id);
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cn.iocoder.springcloud.labx23.hystrixdemo.fallback;
2+
3+
import cn.iocoder.springcloud.labx23.hystrixdemo.feign.UserServiceFeignClient;
4+
import feign.hystrix.FallbackFactory;
5+
import org.apache.commons.lang.exception.ExceptionUtils;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.stereotype.Component;
9+
10+
@Component
11+
public class UserServiceFeignClientFallbackFactory implements FallbackFactory<UserServiceFeignClient> {
12+
13+
private Logger logger = LoggerFactory.getLogger(getClass());
14+
15+
@Override
16+
public UserServiceFeignClient create(Throwable cause) {
17+
return new UserServiceFeignClient() {
18+
19+
@Override
20+
public String getUser(Integer id) {
21+
logger.info("[getUserFallback][id({}) exception({})]", id, ExceptionUtils.getRootCauseMessage(cause));
22+
return "mock:User:" + id;
23+
}
24+
25+
};
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cn.iocoder.springcloud.labx23.hystrixdemo.feign;
2+
3+
import cn.iocoder.springcloud.labx23.hystrixdemo.fallback.UserServiceFeignClientFallbackFactory;
4+
import org.springframework.cloud.openfeign.FeignClient;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestParam;
7+
8+
@FeignClient(name = "user-service", url = "http://127.0.0.1:18080", fallbackFactory = UserServiceFeignClientFallbackFactory.class)
9+
public interface UserServiceFeignClient {
10+
11+
@GetMapping("/user/get")
12+
String getUser(@RequestParam("id") Integer id);
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
feign:
2+
hystrix:
3+
enabled: true # 开启 Hystrix 对 Feign 的支持,默认为 false 关闭。

labx-23/pom.xml

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
<module>labx-23-scn-hystrix-demo01-cluster</module>
2323
<module>labx-23-scn-hystrix-dashboard-turbine</module>
24+
25+
<module>labx-23-scn-hystrix-feign</module>
2426
</modules>
2527

2628
</project>

0 commit comments

Comments
 (0)