From 91224817306b83bc6627e3e76573a78470f4b957 Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Fri, 13 Dec 2019 16:26:37 +0800 Subject: [PATCH] WIP --- pom4ide.xml | 12 +++++ .../alibaba/demo/hystrix/EmployeeService.java | 12 +++++ .../demo/hystrix/EmployeeServiceImpl.java | 26 ++++++++++ .../com/alibaba/demo/hystrix/HystrixHook.java | 52 +++++++++++++++++++ .../alibaba/demo/hystrix/MainApplication.java | 11 ++++ .../alibaba/demo/hystrix/ThreadLocalUtil.java | 50 ++++++++++++++++++ 6 files changed, 163 insertions(+) create mode 100644 src/test/java/com/alibaba/demo/hystrix/EmployeeService.java create mode 100644 src/test/java/com/alibaba/demo/hystrix/EmployeeServiceImpl.java create mode 100644 src/test/java/com/alibaba/demo/hystrix/HystrixHook.java create mode 100644 src/test/java/com/alibaba/demo/hystrix/MainApplication.java create mode 100644 src/test/java/com/alibaba/demo/hystrix/ThreadLocalUtil.java diff --git a/pom4ide.xml b/pom4ide.xml index c0d83568..17a1bbb9 100644 --- a/pom4ide.xml +++ b/pom4ide.xml @@ -172,6 +172,18 @@ 3.5 test + + com.netflix.hystrix + hystrix-core + 1.5.18 + test + + + org.springframework.cloud + spring-cloud-starter-hystrix + 1.4.7.RELEASE + test + diff --git a/src/test/java/com/alibaba/demo/hystrix/EmployeeService.java b/src/test/java/com/alibaba/demo/hystrix/EmployeeService.java new file mode 100644 index 00000000..1579f342 --- /dev/null +++ b/src/test/java/com/alibaba/demo/hystrix/EmployeeService.java @@ -0,0 +1,12 @@ +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"); + } +} diff --git a/src/test/java/com/alibaba/demo/hystrix/EmployeeServiceImpl.java b/src/test/java/com/alibaba/demo/hystrix/EmployeeServiceImpl.java new file mode 100644 index 00000000..d59a6544 --- /dev/null +++ b/src/test/java/com/alibaba/demo/hystrix/EmployeeServiceImpl.java @@ -0,0 +1,26 @@ +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() { + }, employeeId).getBody(); + + ThreadLocalUtil.addDataToThreadLocalMap("Response", response); + } + + @Autowired + RestTemplate restTemplate; +} diff --git a/src/test/java/com/alibaba/demo/hystrix/HystrixHook.java b/src/test/java/com/alibaba/demo/hystrix/HystrixHook.java new file mode 100644 index 00000000..d51e2a26 --- /dev/null +++ b/src/test/java/com/alibaba/demo/hystrix/HystrixHook.java @@ -0,0 +1,52 @@ +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> hrv = new HystrixRequestVariableDefault<>(); + + @Override + public void onStart(HystrixInvokable commandInstance) { + HystrixRequestContext.initializeContext(); + getThreadLocals(); + } + + @Override + public void onExecutionStart(HystrixInvokable commandInstance) { + setThreadLocals(); + } + + + @Override + public void onFallbackStart(HystrixInvokable commandInstance) { + setThreadLocals(); + } + + + @Override + public void onSuccess(HystrixInvokable commandInstance) { + HystrixRequestContext.getContextForCurrentThread().shutdown(); + super.onSuccess(commandInstance); + } + + @Override + public Exception onError(HystrixInvokable 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()); + } +} diff --git a/src/test/java/com/alibaba/demo/hystrix/MainApplication.java b/src/test/java/com/alibaba/demo/hystrix/MainApplication.java new file mode 100644 index 00000000..00ab3ba6 --- /dev/null +++ b/src/test/java/com/alibaba/demo/hystrix/MainApplication.java @@ -0,0 +1,11 @@ +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); + } +} diff --git a/src/test/java/com/alibaba/demo/hystrix/ThreadLocalUtil.java b/src/test/java/com/alibaba/demo/hystrix/ThreadLocalUtil.java new file mode 100644 index 00000000..17be5c3d --- /dev/null +++ b/src/test/java/com/alibaba/demo/hystrix/ThreadLocalUtil.java @@ -0,0 +1,50 @@ +package com.alibaba.demo.hystrix; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class ThreadLocalUtil { + + private static ThreadLocal> transmittableThreadLocal = new ThreadLocal<>(); + + public static ConcurrentHashMap getThreadLocalData() { + return transmittableThreadLocal.get(); + } + + public static void setThreadLocalData(ConcurrentHashMap data) { + transmittableThreadLocal.set(data); + } + + public static void addDataToThreadLocalMap(String key, Object value) { + Map existingDataMap = transmittableThreadLocal.get(); + if (value != null) { + existingDataMap.put(key, value); + } + } + + public static Object getDataFromThreadLocalMap(String key) { + Map existingDataMap = transmittableThreadLocal.get(); + return existingDataMap.get(key); + } + + public static void clearThreadLocalDataMap() { + if (transmittableThreadLocal != null) + transmittableThreadLocal.remove(); + } + + public static Object getRequestData(String key) { + Map 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); + } +} -- GitLab