原文:http://www.javashuo.com/article/p-dquylkqj-kg.htmlhtml
参考 SpringCloud2.0 Feign 服务发现 基础教程(五)spring
调用 5260一、52603 服务是正常的,可是调用52602服务的时候,出现了阻塞等待,并最终返回了红框内的错误信息。浏览器
假如这是正式的生产环境,访问量很大的状况下,那么就会有不少请求阻塞。这会形成【服务消费者】服务器内存消耗陡增,致使应用崩溃。若是有其余应用须要【服务消费者】返回资源信息,那么调用【服务消费者】的应用也会出现阻塞。这就致使了连锁反应,也就是所谓的雪崩。服务器
因此,为了不这种悲剧发生。顺应而生的出现了熔断保护机制。即:访问不通时,要及时做出响应,而不是等待至超时。网络
1
2
3
4
|
<
dependency
>
<
groupId
>org.springframework.cloud</
groupId
>
<
artifactId
>spring-cloud-starter-netflix-hystrix</
artifactId
>
</
dependency
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package
com.miniooc.eurekafeign;
import
org.springframework.boot.SpringApplication;
import
org.springframework.boot.autoconfigure.SpringBootApplication;
import
org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import
org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import
org.springframework.cloud.netflix.hystrix.EnableHystrix;
import
org.springframework.cloud.openfeign.EnableFeignClients;
/**
* EurekaFeignApplication
* 应用程序启动类,程序入口
*
* @author 宋陆
* @version 1.0.0
*/
@EnableHystrix
// Feign默认是开启,这个注解能够不加的
@EnableDiscoveryClient
// 启用 Eureka 服务发现
@EnableFeignClients
// 启用 Feign
@SpringBootApplication
public
class
EurekaFeignApplication {
public
static
void
main(String[] args) {
SpringApplication.run(EurekaFeignApplication.
class
, args);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package
com.miniooc.eurekafeign.service;
import
org.springframework.cloud.openfeign.FeignClient;
import
org.springframework.stereotype.Service;
import
org.springframework.web.bind.annotation.RequestMapping;
/**
* EurekaFeignService
* 服务消费者,调用服务提供者提供的服务,实现业务
*
* @author 宋陆
* @version 1.0.0
*/
@FeignClient
(value =
"EUREKA-CLIENT"
, fallback = EurekaFeignServiceFailure.
class
)
// 调用的服务的名称
public
interface
EurekaFeignService {
@RequestMapping
(value =
"/info"
)
String getInfo();
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package
com.miniooc.eurekafeign.service;
import
org.springframework.stereotype.Service;
/**
* EurekaFeignServiceFailure
* 服务消费者,调用服务提供者提供的服务失败,回调处理类
*
* @author 宋陆
* @version 1.0.0
*/
@Service
public
class
EurekaFeignServiceFailure
implements
EurekaFeignService {
@Override
public
String getInfo() {
String message =
"网络繁忙,请稍后再试-_-。PS:服务消费者本身提供的信息"
;
return
message;
}
}
|
1
2
3
|
feign:
hystrix:
enabled:
true
|
调用 5260一、52603 服务是正常的,调用52602服务的时候,没有出现阻塞等待,而是返回了【服务消费者】本身提供的返回信息app
【服务消费者】加入熔断机制结束ide