Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
晶之木
advanced-java
提交
8057def3
A
advanced-java
项目概览
晶之木
/
advanced-java
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
advanced-java
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
8057def3
编写于
1月 09, 2019
作者:
Y
yanglbme
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
docs: update hystrix-timeout
基于 timeout 机制为服务接口的调用超时提供安全保护
上级
ac26eed8
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
105 addition
and
2 deletion
+105
-2
README.md
README.md
+2
-1
docs/high-availability/hystrix-timeout.md
docs/high-availability/hystrix-timeout.md
+103
-1
docs/high-availability/images/220px-Internet_dog.jpg
docs/high-availability/images/220px-Internet_dog.jpg
+0
-0
images/220px-Internet_dog.jpg
images/220px-Internet_dog.jpg
+0
-0
未找到文件。
README.md
浏览文件 @
8057def3
...
...
@@ -102,7 +102,8 @@
### 降级
-
如何进行降级?
## 微服务架构【额外新增,待补充】
## 微服务架构
-
[
微服务架构整个章节内容属额外新增,会陆续补充完善
](
)
-
[
关于微服务架构的描述
](
/docs/micro-services/microservices-introduction.md
)
### Spring Cloud 微服务架构
\ No newline at end of file
docs/high-availability/hystrix-timeout.md
浏览文件 @
8057def3
## 基于 timeout 机制为服务接口调用超时提供安全保护
\ No newline at end of file
## 基于 timeout 机制为服务接口调用超时提供安全保护
一般来说,在调用依赖服务的接口的时候,比较常见的一个问题就是
**超时**
。超时是在一个复杂的分布式系统中,导致系统不稳定,或者系统抖动。出现大量超时,线程资源会被 hang 死,从而导致吞吐量大幅度下降,甚至服务崩溃。
你去调用各种各样的依赖服务,特别是在大公司,你甚至都不认识开发一个服务的人,你都不知道那个人的技术水平怎么样,对那个人根本不了解。
Peter Steiner 说过,"
[
On the Internet, nobody knows you're a dog
](
https://en.wikipedia.org/wiki/On_the_Internet,_nobody_knows_you%27re_a_dog
)
",也就是说在互联网的另外一头,你都不知道甚至坐着一条狗。
![
220px-Internet_dog.jpg
](
/images/220px-Internet_dog.jpg
)
像特别复杂的分布式系统,特别是在大公司里,多个团队、大型协作,你可能都不知道服务是谁的,很可能说开发服务的那个哥儿们甚至是一个实习生。依赖服务的接口性能可能很不稳定,有时候 2ms,有时候 200ms,甚至 2s,都有可能。
如果你不对各种依赖服务接口的调用做超时控制,来给你的服务提供安全保护措施,那么很可能你的服务就被各种垃圾的依赖服务的性能给拖死了。大量的接口调用很慢,大量的线程被卡死。如果你做了资源的隔离,那么也就是线程池的线程被卡死,但其实我们可以做超时控制,没必要让它们全卡死。
### TimeoutMilliseconds
在 Hystrix 中,我们可以手动设置 timeout 时长,如果一个 command 运行时间超过了设定的时长,那么就被认为是 timeout,然后 Hystrix command 标识为 timeout,同时执行 fallback 降级逻辑。
`TimeoutMilliseconds`
默认值是 1000,也就是 1000ms。
```
java
HystrixCommandProperties
.
Setter
()
..
withExecutionTimeoutInMilliseconds
(
int
)
```
### TimeoutEnabled
这个参数用于控制是否要打开 timeout 机制,默认值是 true。
```
java
HystrixCommandProperties
.
Setter
()
.
withExecutionTimeoutEnabled
(
boolean
)
```
## 实例 Demo
我们在 command 中,将超时时间设置为 500ms,然后在 run() 方法中,设置休眠时间 1s,这样一个请求过来,直接休眠 1s,结果就会因为超时而执行降级逻辑。
```
java
public
class
GetProductInfoCommand
extends
HystrixCommand
<
ProductInfo
>
{
private
Long
productId
;
private
static
final
HystrixCommandKey
KEY
=
HystrixCommandKey
.
Factory
.
asKey
(
"GetProductInfoCommand"
);
public
GetProductInfoCommand
(
Long
productId
)
{
super
(
Setter
.
withGroupKey
(
HystrixCommandGroupKey
.
Factory
.
asKey
(
"ProductInfoService"
))
.
andCommandKey
(
KEY
)
.
andThreadPoolPropertiesDefaults
(
HystrixThreadPoolProperties
.
Setter
()
.
withCoreSize
(
8
)
.
withMaxQueueSize
(
10
)
.
withQueueSizeRejectionThreshold
(
8
))
.
andCommandPropertiesDefaults
(
HystrixCommandProperties
.
Setter
()
.
withCircuitBreakerEnabled
(
true
)
.
withCircuitBreakerRequestVolumeThreshold
(
20
)
.
withCircuitBreakerErrorThresholdPercentage
(
40
)
.
withCircuitBreakerSleepWindowInMilliseconds
(
3000
)
// 设置是否打开超时,默认是true
.
withExecutionTimeoutEnabled
(
true
)
// 设置超时时间,默认1000(ms)
.
withExecutionTimeoutInMilliseconds
(
500
)
.
withFallbackIsolationSemaphoreMaxConcurrentRequests
(
30
)));
this
.
productId
=
productId
;
}
@Override
protected
ProductInfo
run
()
throws
Exception
{
System
.
out
.
println
(
"调用接口查询商品数据,productId="
+
productId
);
// 休眠1s
TimeUtils
.
sleep
(
1
);
String
url
=
"http://localhost:8081/getProductInfo?productId="
+
productId
;
String
response
=
HttpClientUtils
.
sendGetRequest
(
url
);
System
.
out
.
println
(
response
);
return
JSONObject
.
parseObject
(
response
,
ProductInfo
.
class
);
}
@Override
protected
ProductInfo
getFallback
()
{
ProductInfo
productInfo
=
new
ProductInfo
();
productInfo
.
setName
(
"降级商品"
);
return
productInfo
;
}
}
```
在测试类中,我们直接发起请求。
```
java
@SpringBootTest
@RunWith
(
SpringRunner
.
class
)
public
class
TimeoutTest
{
@Test
public
void
testTimeout
()
{
HttpClientUtils
.
sendGetRequest
(
"http://localhost:8080/getProductInfo?productId=1"
);
}
}
```
结果中可以看到,打印出了降级商品相关信息。
```
c
ProductInfo
(
id
=
null
,
name
=
降级商品
,
price
=
null
,
pictureList
=
null
,
specification
=
null
,
service
=
null
,
color
=
null
,
size
=
null
,
shopId
=
null
,
modifiedTime
=
null
,
cityId
=
null
,
cityName
=
null
,
brandId
=
null
,
brandName
=
null
)
{
"id"
:
1
,
"name"
:
"iphone7手机"
,
"price"
:
5599
,
"pictureList"
:
"a.jpg,b.jpg"
,
"specification"
:
"iphone7的规格"
,
"service"
:
"iphone7的售后服务"
,
"color"
:
"红色,白色,黑色"
,
"size"
:
"5.5"
,
"shopId"
:
1
,
"modifiedTime"
:
"2017-01-01 12:00:00"
,
"cityId"
:
1
,
"brandId"
:
1
}
```
\ No newline at end of file
docs/high-availability/images/220px-Internet_dog.jpg
0 → 100644
浏览文件 @
8057def3
18.7 KB
images/220px-Internet_dog.jpg
0 → 100644
浏览文件 @
8057def3
18.7 KB
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录