Skip to content

Commit e706a80

Browse files
author
quding
committed
add dubbo熔断
1 parent d0b9ef8 commit e706a80

File tree

9 files changed

+220
-2
lines changed

9 files changed

+220
-2
lines changed

Dubbo-Demo/Service-Consumer/pom.xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<artifactId>Dubbo-Demo</artifactId>
88
<version>1.0.0</version>
99
</parent>
10+
<packaging>war</packaging>
1011
<modelVersion>4.0.0</modelVersion>
1112
<artifactId>Service-Consumer</artifactId>
1213

@@ -18,11 +19,50 @@
1819
<version>1.0.0</version>
1920
</dependency>
2021

22+
<dependency>
23+
<groupId>com.netflix.hystrix</groupId>
24+
<artifactId>hystrix-javanica</artifactId>
25+
<version>1.5.12</version>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>com.fasterxml.jackson.core</groupId>
30+
<artifactId>jackson-annotations</artifactId>
31+
<version>2.8.1</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.fasterxml.jackson.core</groupId>
35+
<artifactId>jackson-core</artifactId>
36+
<version>2.8.6</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>com.fasterxml.jackson.core</groupId>
40+
<artifactId>jackson-databind</artifactId>
41+
<version>2.8.6</version>
42+
</dependency>
43+
2144
<dependency>
2245
<groupId>org.springframework</groupId>
2346
<artifactId>spring-context</artifactId>
2447
<version>4.3.2.RELEASE</version>
2548
</dependency>
49+
<dependency>
50+
<groupId>org.springframework</groupId>
51+
<artifactId>spring-webmvc</artifactId>
52+
<version>4.3.2.RELEASE</version>
53+
</dependency>
54+
55+
<dependency>
56+
<groupId>javax.servlet</groupId>
57+
<artifactId>jstl</artifactId>
58+
<version>1.2</version>
59+
<scope>runtime</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.springframework</groupId>
63+
<artifactId>spring-web</artifactId>
64+
<version>4.3.2.RELEASE</version>
65+
</dependency>
2666
<dependency>
2767
<groupId>org.springframework</groupId>
2868
<artifactId>spring-test</artifactId>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cn.mrdear.consumer.controller;
2+
3+
import org.springframework.web.bind.annotation.GetMapping;
4+
import org.springframework.web.bind.annotation.PathVariable;
5+
import org.springframework.web.bind.annotation.RestController;
6+
7+
import cn.mrdear.client.dto.UserDTO;
8+
import cn.mrdear.consumer.manager.UserManager;
9+
10+
import javax.annotation.Resource;
11+
12+
/**
13+
* @author Niu Li
14+
* @since 2017/6/14
15+
*/
16+
@RestController
17+
public class UserController {
18+
19+
@Resource
20+
private UserManager userManager;
21+
22+
@GetMapping("/user/{id}")
23+
public UserDTO getUserById(@PathVariable Long id) {
24+
UserDTO userDTO = userManager.findById(id);
25+
return userDTO;
26+
}
27+
}

Dubbo-Demo/Service-Consumer/src/main/java/cn/mrdear/consumer/manager/UserManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ public interface UserManager {
1212

1313
UserDTO findById(Long id);
1414

15+
UserDTO findByIdBack(Long id);
16+
1517
}

Dubbo-Demo/Service-Consumer/src/main/java/cn/mrdear/consumer/manager/impl/UserManagerImpl.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cn.mrdear.consumer.manager.impl;
22

3+
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
4+
35
import org.springframework.stereotype.Service;
46

57
import cn.mrdear.client.dto.UserDTO;
@@ -8,6 +10,8 @@
810

911
import javax.annotation.Resource;
1012

13+
import java.util.Objects;
14+
1115
/**
1216
* manager调用远程RPC组装数据,此处调试只是再加一层封装
1317
* @author Niu Li
@@ -19,9 +23,29 @@ public class UserManagerImpl implements UserManager {
1923
@Resource
2024
private IUserService userService;
2125

22-
2326
@Override
27+
@HystrixCommand(groupKey = "user", fallbackMethod = "findByIdBack")
2428
public UserDTO findById(Long id) {
29+
if (Objects.equals(id, 1L)) {
30+
try {
31+
Thread.sleep(1000000);
32+
} catch (InterruptedException e) {
33+
// do nothing
34+
}
35+
}
36+
if (Objects.equals(id, 2L)) {
37+
throw new RuntimeException("熔断测试");
38+
}
2539
return userService.findById(id);
2640
}
41+
42+
public UserDTO findByIdBack(Long id) {
43+
System.err.println("findByIdBack");
44+
UserDTO userDTO = new UserDTO();
45+
userDTO.setAge(1);
46+
userDTO.setUsername("备用用户");
47+
return userDTO;
48+
}
49+
50+
2751
}

Dubbo-Demo/Service-Consumer/src/main/resources/applicationContext.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
xmlns:context="http://www.springframework.org/schema/context"
55
xmlns:task="http://www.springframework.org/schema/task"
66
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
7-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
7+
xmlns:aop="http://www.springframework.org/schema/aop"
8+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
89

910
<!-- 启用注解 -->
1011
<context:annotation-config/>
@@ -13,6 +14,11 @@
1314

1415
<context:component-scan base-package="cn.mrdear.consumer"/>
1516

17+
<!--断路器配置-->
18+
<aop:aspectj-autoproxy/>
19+
<bean id="hystrixAspect"
20+
class="com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect"/>
21+
1622
<!-- 提供方应用信息,用于计算依赖关系 -->
1723
<dubbo:application name="user_consumer" />
1824

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
4+
xmlns:contents="http://www.springframework.org/schema/context"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
6+
7+
<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
8+
<contents:component-scan base-package="cn.mrdear.consumer.controller"/>
9+
10+
<!-- 启用默认配置 -->
11+
<mvc:annotation-driven/>
12+
13+
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀(如果最后一个还是表示文件夹,则最后的斜杠不要漏了) 使用JSP-->
14+
<!-- 默认的视图解析器 在上边的解析错误时使用 (默认使用html)- -->
15+
<bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
16+
<!--可以使用jstl标签-->
17+
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
18+
<property name="prefix" value="/WEB-INF/view/"/><!--设置JSP文件的目录位置-->
19+
<property name="suffix" value=".jsp"/>
20+
</bean>
21+
</beans>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<%--
2+
Created by IntelliJ IDEA.
3+
User: niuli
4+
Date: 2017/4/19
5+
Time: 09:45
6+
To change this template use File | Settings | File Templates.
7+
--%>
8+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
9+
<html>
10+
<head>
11+
<title>Title</title>
12+
</head>
13+
<body>
14+
<h1>Error 页面</h1>
15+
</body>
16+
</html>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
5+
version="3.1">
6+
7+
<!--配置获取项目的根路径,java类中使用System.getProperty("web.root")-->
8+
<context-param>
9+
<param-name>webAppRootKey</param-name>
10+
<param-value>web.root</param-value>
11+
</context-param>
12+
<context-param>
13+
<param-name>contextConfigLocation</param-name>
14+
<param-value>classpath:applicationContext.xml</param-value>
15+
</context-param>
16+
17+
<!--配置spring编码-->
18+
<!-- 配置字符编码过滤器,编码格式设为UTF-8,避免中文乱码 -->
19+
<filter>
20+
<filter-name>encodingFilter</filter-name>
21+
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
22+
<init-param>
23+
<param-name>encoding</param-name>
24+
<param-value>UTF-8</param-value> <!--设置你想用的字符集,我这里用的是UTF-8-->
25+
</init-param>
26+
<!--强制使用该编码-->
27+
<init-param>
28+
<param-name>forceEncoding</param-name>
29+
<param-value>true</param-value>
30+
</init-param>
31+
</filter>
32+
<filter-mapping>
33+
<filter-name>encodingFilter</filter-name>
34+
<url-pattern>/*</url-pattern> <!--设置你想过滤的页面或者是Servlet,根据自己的需要配置-->
35+
</filter-mapping>
36+
37+
<listener>
38+
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
39+
</listener>
40+
<listener>
41+
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
42+
</listener>
43+
<!-- 主要负责处理由 JavaBeans Introspector的使用而引起的缓冲泄露。spring中对它的描述如下:它是一个在web应用关闭的时候,清除JavaBeans Introspector的监听器.web.xml中注册这个listener.可以保证在web 应用关闭的时候释放与掉这个web 应用相关的class loader 和由它管理的类如果你使用了JavaBeans Introspector来分析应用中的类,Introspector 缓冲中会保留这些类的引用.结果在你的应用关闭的时候,这些类以及web 应用相关的class loader没有被垃圾回收.不幸的是,清除Introspector的唯一方式是刷新整个缓冲. -->
44+
<listener>
45+
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
46+
</listener>
47+
48+
49+
<!--配置springMVC-->
50+
<servlet>
51+
<servlet-name>dispatcher</servlet-name>
52+
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
53+
<init-param>
54+
<param-name>contextConfigLocation</param-name>
55+
<!--配置文件的路径-->
56+
<param-value>classpath:dispatcher-servlet.xml</param-value>
57+
</init-param>
58+
<load-on-startup>1</load-on-startup>
59+
</servlet>
60+
<servlet-mapping>
61+
<servlet-name>dispatcher</servlet-name>
62+
<url-pattern>/</url-pattern>
63+
</servlet-mapping>
64+
65+
</web-app>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<%--
2+
Created by IntelliJ IDEA.
3+
User: NL
4+
Date: 2017/1/23
5+
Time: 9:09
6+
To change this template use File | Settings | File Templates.
7+
--%>
8+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
9+
<html>
10+
<head>
11+
<title></title>
12+
</head>
13+
<body>
14+
<h2>Hello World!</h2>
15+
<h3>请访问/user/{id}测试</h3>
16+
</body>
17+
</html>

0 commit comments

Comments
 (0)