Skip to content

Commit 4570e5f

Browse files
authored
Merge pull request #1 from coderqianlq/dev
:octocat: feat(Hystrix熔断器): 初步继承Hystrix熔断器
2 parents 2c9099a + d7aa5ef commit 4570e5f

File tree

11 files changed

+74
-12
lines changed

11 files changed

+74
-12
lines changed

dubbo-api/pom.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
<dependencies>
2626
<dependency>
2727
<groupId>org.springframework.boot</groupId>
28-
<artifactId>spring-boot-starter</artifactId>
28+
<artifactId>spring-boot-starter-web</artifactId>
2929
</dependency>
3030

3131
<dependency>
32-
<groupId>org.springframework.boot</groupId>
33-
<artifactId>spring-boot-starter-web</artifactId>
32+
<groupId>io.swagger</groupId>
33+
<artifactId>swagger-annotations</artifactId>
34+
<version>1.5.20</version>
3435
</dependency>
3536
</dependencies>
3637

dubbo-api/src/main/java/com/coderqian/dubboapi/service/TestService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* @author qianliqing
55
* @date 2018/11/29 1:55 PM
6-
* email: qianliqing@hyperchain.com
6+
* email: qianlq0824@gmail.com
77
*/
88

99
public interface TestService {

dubbo-consumer/pom.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2121
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
2222
<java.version>1.8</java.version>
23+
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
2324
</properties>
2425

2526
<dependencies>
@@ -34,8 +35,26 @@
3435
<artifactId>spring-boot-starter-web</artifactId>
3536
</dependency>
3637

38+
<dependency>
39+
<groupId>org.springframework.cloud</groupId>
40+
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
41+
<version>2.0.1.RELEASE</version>
42+
</dependency>
3743
</dependencies>
3844

45+
46+
<dependencyManagement>
47+
<dependencies>
48+
<dependency>
49+
<groupId>org.springframework.cloud</groupId>
50+
<artifactId>spring-cloud-dependencies</artifactId>
51+
<version>${spring-cloud.version}</version>
52+
<type>pom</type>
53+
<scope>import</scope>
54+
</dependency>
55+
</dependencies>
56+
</dependencyManagement>
57+
3958
<build>
4059
<plugins>
4160
<plugin>
@@ -45,5 +64,4 @@
4564
</plugins>
4665
</build>
4766

48-
4967
</project>

dubbo-consumer/src/main/java/com/coderqian/dubboconsumer/controller/TestController.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
import com.alibaba.dubbo.config.annotation.Reference;
44
import com.coderqian.dubboapi.service.TestService;
5+
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
56
import org.springframework.web.bind.annotation.RequestMapping;
67
import org.springframework.web.bind.annotation.RequestMethod;
78
import org.springframework.web.bind.annotation.RestController;
89

910
/**
1011
* @author qianliqing
1112
* @date 2018/11/29 1:52 PM
12-
* email: qianliqing@hyperchain.com
13+
* email: qianlq0824@gmail.com
1314
*/
1415

1516
@RestController
@@ -19,8 +20,13 @@ public class TestController {
1920
@Reference
2021
private TestService testService;
2122

23+
@HystrixCommand(fallbackMethod = "testError")
2224
@RequestMapping(value = "/hello", method = RequestMethod.GET)
2325
public String test(String text) {
2426
return testService.test(text);
2527
}
28+
29+
private String testError(String text) {
30+
return "失败" + text;
31+
}
2632
}

dubbo-consumer/src/main/resources/application.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ dubbo.application.id=dubbo-customer
77
dubbo.application.name=dubbo-customer
88
# 注册中心
99
dubbo.registry.address=zookeeper://127.0.0.1:2181
10+
dubbo.registry.timeout=60000
11+
dubbo.registry.group=dubbo
12+
dubbo.registry.check=false
1013

1114
# 生产者提供的协议id
1215
dubbo.protocol.id=dubbo-provider

dubbo-consumer/src/main/resources/dubbo/dubbo-customer.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
99
<dubbo:application name="dubbo-customer"/>
1010

1111
<!-- 注册中心配置,使用zookeeper注册中心暴露服务地址 -->
12-
<dubbo:registry address="zookeeper://127.0.0.1:2181" timeout="60000" group="dubbo"/>
12+
<dubbo:registry address="zookeeper://127.0.0.1:2181" timeout="60000" group="dubbo" check="false"/>
1313

1414
<!-- 关闭服务消费方所有服务的启动检查。dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成 -->
1515
<dubbo:consumer check="false"/>

dubbo-provider/pom.xml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2121
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
2222
<java.version>1.8</java.version>
23+
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
2324
</properties>
2425

2526
<dependencies>
@@ -34,6 +35,13 @@
3435
<artifactId>spring-boot-starter-actuator</artifactId>
3536
</dependency>
3637

38+
<!-- Hystrix依赖 -->
39+
<dependency>
40+
<groupId>org.springframework.cloud</groupId>
41+
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
42+
<version>2.0.1.RELEASE</version>
43+
</dependency>
44+
3745
<dependency>
3846
<groupId>org.apache.zookeeper</groupId>
3947
<artifactId>zookeeper</artifactId>
@@ -71,6 +79,18 @@
7179
</dependency>
7280
</dependencies>
7381

82+
<dependencyManagement>
83+
<dependencies>
84+
<dependency>
85+
<groupId>org.springframework.cloud</groupId>
86+
<artifactId>spring-cloud-dependencies</artifactId>
87+
<version>${spring-cloud.version}</version>
88+
<type>pom</type>
89+
<scope>import</scope>
90+
</dependency>
91+
</dependencies>
92+
</dependencyManagement>
93+
7494
<build>
7595
<plugins>
7696
<plugin>
@@ -80,5 +100,4 @@
80100
</plugins>
81101
</build>
82102

83-
84103
</project>

dubbo-provider/src/main/java/com/coderqian/dubboprovider/DubboProviderApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
44
import org.springframework.boot.SpringApplication;
55
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
67

78
@EnableDubbo
9+
@EnableHystrix
810
@SpringBootApplication
911
//@ImportResource({"classpath:dubbo/dubbo-provider.xml"})
1012
public class DubboProviderApplication {

dubbo-provider/src/main/java/com/coderqian/dubboprovider/service/impl/TestServiceImpl.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,25 @@
22

33
import com.alibaba.dubbo.config.annotation.Service;
44
import com.coderqian.dubboapi.service.TestService;
5+
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
6+
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
7+
import io.swagger.annotations.ApiParam;
58

69
/**
710
* @author qianliqing
811
* @date 2018/11/29 12:53 PM
9-
* email: qianliqing@hyperchain.com
12+
* email: qianlq0824@gmail.com
1013
*/
1114

1215
@Service
1316
public class TestServiceImpl implements TestService {
1417

18+
@HystrixCommand(commandProperties = {
19+
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "3"),
20+
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
21+
})
1522
@Override
16-
public String test(String text) {
23+
public String test(@ApiParam(value = "测试数据", required = true) String text) {
1724
return text;
1825
}
1926
}

dubbo-provider/src/main/resources/application.properties

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@ dubbo.application.id=dubbo-provider
77
dubbo.application.name=dubbo-provider
88
# 注册中心
99
dubbo.registry.address=zookeeper://127.0.0.1:2181
10+
dubbo.registry.timeout=60000
11+
dubbo.registry.group=dubbo
12+
dubbo.registry.check=false
13+
1014
dubbo.server=true
1115
# 生产者暴露给消费者协议
1216
dubbo.protocol.name=dubbo
1317
# 生产者暴露给消费者端口
14-
dubbo.protocol.port=20880
18+
dubbo.protocol.port=20880
19+
20+
logging.config=classpath:log4j2.xml

0 commit comments

Comments
 (0)