Skip to content

Commit cf2457f

Browse files
committed
feat(hystrix):feign集成hystrix进行服务降级配置
1 parent 11f246e commit cf2457f

File tree

8 files changed

+77
-3
lines changed

8 files changed

+77
-3
lines changed

api-gateway/src/main/resources/application.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ zuul.ignoredServices='*'
1515
zuul.routes.api-c.path=/feign/**
1616
zuul.routes.api-c.serviceId=feign-server
1717

18+
#请求连接的超时时间
1819
#ribbon.ConnectTimeout=6000
20+
#请求处理的超时时间
1921
#ribbon.ReadTimeout=6000

eureka-order/src/main/java/com/coderqian/eurekaorder/controller/TestController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.swagger.annotations.ApiOperation;
55
import org.springframework.web.bind.annotation.RequestMapping;
66
import org.springframework.web.bind.annotation.RequestMethod;
7+
import org.springframework.web.bind.annotation.RequestParam;
78
import org.springframework.web.bind.annotation.RestController;
89

910
/**
@@ -19,7 +20,7 @@ public class TestController {
1920

2021
@ApiOperation(value = "返回用户输入的结果", notes = "返回用户输入的结果")
2122
@RequestMapping(value = "/result", method = RequestMethod.GET)
22-
public String test(String text) {
23+
public String test(@RequestParam(value = "text") String text) {
2324
return text;
2425
}
2526
}

feign-server/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@
3737
<artifactId>spring-cloud-starter-feign</artifactId>
3838
</dependency>
3939

40+
<dependency>
41+
<groupId>org.springframework.cloud</groupId>
42+
<artifactId>spring-cloud-starter-hystrix</artifactId>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>org.springframework.cloud</groupId>
47+
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
48+
</dependency>
49+
4050
<dependency>
4151
<groupId>org.springframework.cloud</groupId>
4252
<artifactId>spring-cloud-starter-eureka</artifactId>

feign-server/src/main/java/com/coderqian/feginserver/FeignServerApplication.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
56
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
67
import org.springframework.cloud.netflix.feign.EnableFeignClients;
8+
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
79

810

911
@SpringBootApplication
1012
@EnableEurekaClient
1113
@EnableFeignClients
14+
@EnableHystrixDashboard
15+
@EnableCircuitBreaker
1216
public class FeignServerApplication {
1317

1418
public static void main(String[] args) {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.coderqian.feginserver.configuration;
2+
3+
import com.netflix.hystrix.HystrixCommand;
4+
import feign.Feign;
5+
import feign.hystrix.HystrixFeign;
6+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
7+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.context.annotation.Scope;
11+
12+
/**
13+
* @author qianliqing
14+
* @date 2018-10-16 下午5:29
15+
16+
*/
17+
18+
//@Configuration
19+
@ConditionalOnClass({HystrixCommand.class, HystrixFeign.class})
20+
public class FeignServerConfiguration {
21+
22+
@Bean
23+
@Scope("prototype")
24+
@ConditionalOnProperty(name = "feign.hystrix.enabled", matchIfMissing = true)
25+
public Feign.Builder feignBuilder() {
26+
return Feign.builder();
27+
}
28+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.coderqian.feginserver.configuration.fallback;
2+
3+
import com.coderqian.feginserver.service.TestCustomerService;
4+
import org.springframework.stereotype.Component;
5+
6+
/**
7+
* @author qianliqing
8+
* @date 2018-10-16 下午5:08
9+
10+
*/
11+
12+
@Component
13+
public class HystrixClientFallback implements TestCustomerService {
14+
15+
@Override
16+
public String testCustomer(String text) {
17+
return "失败:" + text;
18+
}
19+
}

feign-server/src/main/java/com/coderqian/feginserver/service/TestCustomerService.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.coderqian.feginserver.service;
22

3+
import com.coderqian.feginserver.configuration.fallback.HystrixClientFallback;
34
import org.springframework.cloud.netflix.feign.FeignClient;
5+
import org.springframework.stereotype.Service;
46
import org.springframework.web.bind.annotation.RequestMapping;
57
import org.springframework.web.bind.annotation.RequestMethod;
68
import org.springframework.web.bind.annotation.RequestParam;
@@ -11,7 +13,8 @@
1113
1214
*/
1315

14-
@FeignClient(value = "eureka-customer")
16+
@FeignClient(value = "eureka-customer", fallback = HystrixClientFallback.class)
17+
@Service
1518
public interface TestCustomerService {
1619

1720
@RequestMapping(value = "/test/result", method = RequestMethod.GET)
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
spring.application.name=feign-server
22
server.port=8765
3-
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka
3+
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka
4+
5+
feign.hystrix.enabled=true
6+
7+
#请求处理的超时时间
8+
#ribbon.ReadTimeout=120000
9+
#请求连接的超时时间
10+
#ribbon.ConnectTimeout=30000

0 commit comments

Comments
 (0)