Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
阿啄debugIT
transmittable-thread-local
提交
91224817
T
transmittable-thread-local
项目概览
阿啄debugIT
/
transmittable-thread-local
与 Fork 源项目一致
从无法访问的项目Fork
通知
8
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
transmittable-thread-local
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
91224817
编写于
12月 13, 2019
作者:
oldratlee
🔥
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
WIP
上级
5c6db373
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
163 addition
and
0 deletion
+163
-0
pom4ide.xml
pom4ide.xml
+12
-0
src/test/java/com/alibaba/demo/hystrix/EmployeeService.java
src/test/java/com/alibaba/demo/hystrix/EmployeeService.java
+12
-0
src/test/java/com/alibaba/demo/hystrix/EmployeeServiceImpl.java
...st/java/com/alibaba/demo/hystrix/EmployeeServiceImpl.java
+26
-0
src/test/java/com/alibaba/demo/hystrix/HystrixHook.java
src/test/java/com/alibaba/demo/hystrix/HystrixHook.java
+52
-0
src/test/java/com/alibaba/demo/hystrix/MainApplication.java
src/test/java/com/alibaba/demo/hystrix/MainApplication.java
+11
-0
src/test/java/com/alibaba/demo/hystrix/ThreadLocalUtil.java
src/test/java/com/alibaba/demo/hystrix/ThreadLocalUtil.java
+50
-0
未找到文件。
pom4ide.xml
浏览文件 @
91224817
...
...
@@ -172,6 +172,18 @@
<version>
3.5
</version>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
com.netflix.hystrix
</groupId>
<artifactId>
hystrix-core
</artifactId>
<version>
1.5.18
</version>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-hystrix
</artifactId>
<version>
1.4.7.RELEASE
</version>
<scope>
test
</scope>
</dependency>
</dependencies>
<distributionManagement>
...
...
src/test/java/com/alibaba/demo/hystrix/EmployeeService.java
0 → 100644
浏览文件 @
91224817
package
com.alibaba.demo.hystrix
;
import
org.springframework.stereotype.Component
;
@Component
public
abstract
class
EmployeeService
{
public
abstract
void
getEmployee
(
int
employeeId
);
public
void
fallbackMethod
(
int
employeeid
)
{
ThreadLocalUtil
.
addDataToThreadLocalMap
(
"ErrorResponse"
,
"Fallback response:: No employee details available temporarily"
);
}
}
src/test/java/com/alibaba/demo/hystrix/EmployeeServiceImpl.java
0 → 100644
浏览文件 @
91224817
package
com.alibaba.demo.hystrix
;
import
com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand
;
import
com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.core.ParameterizedTypeReference
;
import
org.springframework.stereotype.Service
;
@Service
public
class
EmployeeServiceImpl
extends
EmployeeService
{
@HystrixCommand
(
fallbackMethod
=
"fallbackMethod"
,
commandProperties
=
{
@HystrixProperty
(
name
=
"execution.isolation.thread.timeoutInMilliseconds"
,
value
=
"900"
),
@HystrixProperty
(
name
=
"circuitBreaker.errorThresholdPercentage"
,
value
=
"10"
)
})
public
void
getEmployee
(
int
employeeId
)
{
System
.
out
.
println
(
"Getting Employee details for "
+
employeeId
+
", threadLocalUtil : "
+
ThreadLocalUtil
.
getDataFromThreadLocalMap
(
"EMPLOYEE_ID"
));
String
response
=
restTemplate
.
exchange
(
"http://localhost:8011/findEmployeeDetails/{employeeid}"
,
HttpMethod
.
GET
,
null
,
new
ParameterizedTypeReference
<
String
>()
{
},
employeeId
).
getBody
();
ThreadLocalUtil
.
addDataToThreadLocalMap
(
"Response"
,
response
);
}
@Autowired
RestTemplate
restTemplate
;
}
src/test/java/com/alibaba/demo/hystrix/HystrixHook.java
0 → 100644
浏览文件 @
91224817
package
com.alibaba.demo.hystrix
;
import
com.netflix.hystrix.HystrixInvokable
;
import
com.netflix.hystrix.exception.HystrixRuntimeException
;
import
com.netflix.hystrix.strategy.concurrency.HystrixRequestContext
;
import
com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableDefault
;
import
com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook
;
import
java.util.concurrent.ConcurrentHashMap
;
public
class
HystrixHook
extends
HystrixCommandExecutionHook
{
private
HystrixRequestVariableDefault
<
ConcurrentHashMap
<
String
,
Object
>>
hrv
=
new
HystrixRequestVariableDefault
<>();
@Override
public
<
T
>
void
onStart
(
HystrixInvokable
<
T
>
commandInstance
)
{
HystrixRequestContext
.
initializeContext
();
getThreadLocals
();
}
@Override
public
<
T
>
void
onExecutionStart
(
HystrixInvokable
<
T
>
commandInstance
)
{
setThreadLocals
();
}
@Override
public
<
T
>
void
onFallbackStart
(
HystrixInvokable
<
T
>
commandInstance
)
{
setThreadLocals
();
}
@Override
public
<
T
>
void
onSuccess
(
HystrixInvokable
<
T
>
commandInstance
)
{
HystrixRequestContext
.
getContextForCurrentThread
().
shutdown
();
super
.
onSuccess
(
commandInstance
);
}
@Override
public
<
T
>
Exception
onError
(
HystrixInvokable
<
T
>
commandInstance
,
HystrixRuntimeException
.
FailureType
failureType
,
Exception
e
)
{
HystrixRequestContext
.
getContextForCurrentThread
().
shutdown
();
return
super
.
onError
(
commandInstance
,
failureType
,
e
);
}
private
void
getThreadLocals
()
{
hrv
.
set
(
ThreadLocalUtil
.
getThreadLocalData
());
}
private
void
setThreadLocals
()
{
ThreadLocalUtil
.
setThreadLocalData
(
hrv
.
get
());
}
}
src/test/java/com/alibaba/demo/hystrix/MainApplication.java
0 → 100644
浏览文件 @
91224817
package
com.alibaba.demo.hystrix
;
import
com.netflix.hystrix.strategy.HystrixPlugins
;
import
org.springframework.boot.SpringApplication
;
public
class
MainApplication
{
public
static
void
main
(
String
[]
args
)
{
HystrixPlugins
.
getInstance
().
registerCommandExecutionHook
(
new
HystrixHook
());
SpringApplication
.
run
(
Abc
.
class
,
args
);
}
}
src/test/java/com/alibaba/demo/hystrix/ThreadLocalUtil.java
0 → 100644
浏览文件 @
91224817
package
com.alibaba.demo.hystrix
;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
public
class
ThreadLocalUtil
{
private
static
ThreadLocal
<
ConcurrentHashMap
<
String
,
Object
>>
transmittableThreadLocal
=
new
ThreadLocal
<>();
public
static
ConcurrentHashMap
<
String
,
Object
>
getThreadLocalData
()
{
return
transmittableThreadLocal
.
get
();
}
public
static
void
setThreadLocalData
(
ConcurrentHashMap
<
String
,
Object
>
data
)
{
transmittableThreadLocal
.
set
(
data
);
}
public
static
void
addDataToThreadLocalMap
(
String
key
,
Object
value
)
{
Map
<
String
,
Object
>
existingDataMap
=
transmittableThreadLocal
.
get
();
if
(
value
!=
null
)
{
existingDataMap
.
put
(
key
,
value
);
}
}
public
static
Object
getDataFromThreadLocalMap
(
String
key
)
{
Map
<
String
,
Object
>
existingDataMap
=
transmittableThreadLocal
.
get
();
return
existingDataMap
.
get
(
key
);
}
public
static
void
clearThreadLocalDataMap
()
{
if
(
transmittableThreadLocal
!=
null
)
transmittableThreadLocal
.
remove
();
}
public
static
Object
getRequestData
(
String
key
)
{
Map
<
String
,
Object
>
existingDataMap
=
transmittableThreadLocal
.
get
();
if
(
existingDataMap
!=
null
)
{
return
existingDataMap
.
get
(
key
);
}
return
"-1"
;
}
public
static
void
initThreadLocals
()
{
transmittableThreadLocal
.
set
(
new
ConcurrentHashMap
<>());
String
requestId
=
"REQUEST_ID_"
+
System
.
currentTimeMillis
();
addDataToThreadLocalMap
(
"REQUEST_ID"
,
requestId
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录