Skip to content

Commit dbbcd00

Browse files
author
YunaiV
committed
增加 Hystrix 入门示例(Hystrix + Dubbo)
1 parent fcdc921 commit dbbcd00

File tree

14 files changed

+327
-6
lines changed

14 files changed

+327
-6
lines changed

labx-07/labx-07-sca-dubbo-demo01/labx-07-sca-dubbo-demo01-provider/pom.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
</properties>
2121

2222
<!--
23-
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
24-
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
25-
-->
23+
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
24+
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
25+
-->
2626
<dependencyManagement>
2727
<dependencies>
2828
<dependency>

labx-07/labx-07-sca-dubbo-demo02/labx-07-sca-dubbo-demo02-provider-rest/pom.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
</properties>
2222

2323
<!--
24-
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
25-
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
26-
-->
24+
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
25+
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
26+
-->
2727
<dependencyManagement>
2828
<dependencies>
2929
<dependency>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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-scn-hystrix-dubbo-demo</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-dubbo-demo-application</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+
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
20+
</properties>
21+
22+
<!--
23+
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
24+
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
25+
-->
26+
<dependencyManagement>
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-parent</artifactId>
31+
<version>${spring.boot.version}</version>
32+
<type>pom</type>
33+
<scope>import</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.springframework.cloud</groupId>
37+
<artifactId>spring-cloud-dependencies</artifactId>
38+
<version>${spring.cloud.version}</version>
39+
<type>pom</type>
40+
<scope>import</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>com.alibaba.cloud</groupId>
44+
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
45+
<version>${spring.cloud.alibaba.version}</version>
46+
<type>pom</type>
47+
<scope>import</scope>
48+
</dependency>
49+
</dependencies>
50+
</dependencyManagement>
51+
52+
<dependencies>
53+
<!-- 引入用户服务 API 包 -->
54+
<dependency>
55+
<groupId>cn.iocoder.springboot.labs</groupId>
56+
<artifactId>labx-23-scn-hystrix-dubbo-demo-user-service-api</artifactId>
57+
<version>1.0-SNAPSHOT</version>
58+
</dependency>
59+
60+
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
61+
<dependency>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-starter-web</artifactId>
64+
</dependency>
65+
66+
<!-- 引入 Spring Cloud Alibaba Nacos Discovery 相关依赖,将 Nacos 作为注册中心,并实现对其的自动配置 -->
67+
<dependency>
68+
<groupId>com.alibaba.cloud</groupId>
69+
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
70+
</dependency>
71+
72+
<!-- 引入 Spring Cloud Alibaba Dubbo 相关依赖,实现呢 Dubbo 进行远程调用,并实现对其的自动配置 -->
73+
<dependency>
74+
<groupId>com.alibaba.cloud</groupId>
75+
<artifactId>spring-cloud-starter-dubbo</artifactId>
76+
</dependency>
77+
78+
<!-- 引入 Spring Cloud Netflix Hystrix 相关依赖,将 Hystrix 作为服务保障组件,并实现对其的自动配置 -->
79+
<dependency>
80+
<groupId>org.springframework.cloud</groupId>
81+
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
82+
</dependency>
83+
</dependencies>
84+
85+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cn.iocoder.springcloud.labx23.demo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
6+
7+
@SpringBootApplication
8+
@EnableCircuitBreaker // 声明开启断路器
9+
public class DemoApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(DemoApplication.class, args);
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cn.iocoder.springcloud.labx23.demo.controller;
2+
3+
import cn.iocoder.springcloud.labx23.userservice.api.UserService;
4+
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
5+
import org.apache.commons.lang.exception.ExceptionUtils;
6+
import org.apache.dubbo.config.annotation.Reference;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RequestParam;
12+
import org.springframework.web.bind.annotation.RestController;
13+
14+
@RestController
15+
@RequestMapping("/demo")
16+
public class DemoController {
17+
18+
private Logger logger = LoggerFactory.getLogger(getClass());
19+
20+
@Reference(protocol = "dubbo", version = "1.0.0")
21+
private UserService userService;
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 userService.getUser(id);
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
spring:
2+
application:
3+
name: demo-consumer
4+
cloud:
5+
# Nacos 作为注册中心的配置项
6+
nacos:
7+
discovery:
8+
server-addr: 127.0.0.1:8848
9+
10+
# Dubbo 配置项,对应 DubboConfigurationProperties 类
11+
dubbo:
12+
# Dubbo 服务注册中心配置,对应 RegistryConfig 类
13+
registry:
14+
address: spring-cloud://127.0.0.1:8848 # 指定 Dubbo 服务注册中心的地址
15+
# Spring Cloud Alibaba Dubbo 专属配置项,对应 DubboCloudProperties 类
16+
cloud:
17+
subscribed-services: user-service # 设置订阅的应用列表,默认为 * 订阅所有应用。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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-scn-hystrix-dubbo-demo</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-dubbo-demo-user-service-api</artifactId>
13+
14+
15+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package cn.iocoder.springcloud.labx23.userservice.api;
2+
3+
public interface UserService {
4+
5+
String getUser(Integer id);
6+
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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-scn-hystrix-dubbo-demo</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-dubbo-demo-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+
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
19+
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
20+
</properties>
21+
22+
<!--
23+
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
24+
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
25+
-->
26+
<dependencyManagement>
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-parent</artifactId>
31+
<version>${spring.boot.version}</version>
32+
<type>pom</type>
33+
<scope>import</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.springframework.cloud</groupId>
37+
<artifactId>spring-cloud-dependencies</artifactId>
38+
<version>${spring.cloud.version}</version>
39+
<type>pom</type>
40+
<scope>import</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>com.alibaba.cloud</groupId>
44+
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
45+
<version>${spring.cloud.alibaba.version}</version>
46+
<type>pom</type>
47+
<scope>import</scope>
48+
</dependency>
49+
</dependencies>
50+
</dependencyManagement>
51+
52+
<dependencies>
53+
<!-- 引入用户服务 API 包 -->
54+
<dependency>
55+
<groupId>cn.iocoder.springboot.labs</groupId>
56+
<artifactId>labx-23-scn-hystrix-dubbo-demo-user-service-api</artifactId>
57+
<version>1.0-SNAPSHOT</version>
58+
</dependency>
59+
60+
<!-- 引入 Spring Cloud Alibaba Nacos Discovery 相关依赖,将 Nacos 作为注册中心,并实现对其的自动配置 -->
61+
<dependency>
62+
<groupId>com.alibaba.cloud</groupId>
63+
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
64+
</dependency>
65+
66+
<!-- 引入 Spring Cloud Alibaba Dubbo 相关依赖,实现呢 Dubbo 进行远程调用,并实现对其的自动配置 -->
67+
<dependency>
68+
<groupId>com.alibaba.cloud</groupId>
69+
<artifactId>spring-cloud-starter-dubbo</artifactId>
70+
</dependency>
71+
</dependencies>
72+
73+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.iocoder.springcloud.labx23.userservice;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class UserServiceApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(UserServiceApplication.class);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.iocoder.springcloud.labx23.userservice.service;
2+
3+
import cn.iocoder.springcloud.labx23.userservice.api.UserService;
4+
5+
@org.apache.dubbo.config.annotation.Service(protocol = "dubbo", version = "1.0.0")
6+
public class UserServiceImpl implements UserService {
7+
8+
@Override
9+
public String getUser(Integer id) {
10+
return "User:" + id;
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
spring:
2+
application:
3+
name: user-service
4+
cloud:
5+
# Nacos 作为注册中心的配置项
6+
nacos:
7+
discovery:
8+
server-addr: 127.0.0.1:8848 # Nacos 服务器地址
9+
10+
# Dubbo 配置项,对应 DubboConfigurationProperties 类
11+
dubbo:
12+
scan:
13+
base-packages: cn.iocoder.springcloud.labx23.userservice.service # 指定 Dubbo 服务实现类的扫描基准包
14+
# Dubbo 服务暴露的协议配置,对应 ProtocolConfig Map
15+
protocols:
16+
dubbo:
17+
name: dubbo # 协议名称
18+
port: -1 # 协议端口,-1 表示自增端口,从 20880 开始
19+
# Dubbo 服务注册中心配置,对应 RegistryConfig 类
20+
registry:
21+
address: spring-cloud://127.0.0.1:8848 # 指定 Dubbo 服务注册中心的地址
22+
# Spring Cloud Alibaba Dubbo 专属配置项,对应 DubboCloudProperties 类
23+
cloud:
24+
subscribed-services: '' # 设置订阅的应用列表,默认为 * 订阅所有应用。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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-dubbo-demo</artifactId>
13+
<packaging>pom</packaging>
14+
15+
<modules>
16+
<module>labx-23-scn-hystrix-dubbo-demo-user-service</module>
17+
<module>labx-23-scn-hystrix-dubbo-demo-user-service-api</module>
18+
<module>labx-23-scn-hystrix-dubbo-demo-application</module>
19+
</modules>
20+
21+
22+
</project>

labx-23/pom.xml

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
<module>labx-23-scn-hystrix-dashboard-turbine</module>
2424

2525
<module>labx-23-scn-hystrix-feign</module>
26+
27+
<module>labx-23-scn-hystrix-dubbo-demo</module>
2628
</modules>
2729

2830
</project>

0 commit comments

Comments
 (0)