From f8949f61d690053759bc70bbc1927a3d00da3210 Mon Sep 17 00:00:00 2001
From: YunaiV <>
Date: Fri, 15 May 2020 08:17:55 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20Hystrix=20=E5=85=A5?=
=?UTF-8?q?=E9=97=A8=E6=96=87=E7=AB=A0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
lab-57/lab-57-hystrix-demo01/pom.xml | 57 +++++++++++++++++++
.../lab57/hystrixdemo/DemoApplication.java | 20 +++++++
.../hystrixdemo/config/HystrixConfig.java | 17 ++++++
.../controller/DemoController.java | 35 ++++++++++++
lab-57/lab-57-user-service/pom.xml | 44 ++++++++++++++
.../userservice/UserServiceApplication.java | 40 +++++++++++++
lab-57/pom.xml | 19 +++++++
labx-23/labx-23-user-service/pom.xml | 2 -
pom.xml | 1 +
9 files changed, 233 insertions(+), 2 deletions(-)
create mode 100644 lab-57/lab-57-hystrix-demo01/pom.xml
create mode 100644 lab-57/lab-57-hystrix-demo01/src/main/java/cn/iocoder/springboot/lab57/hystrixdemo/DemoApplication.java
create mode 100644 lab-57/lab-57-hystrix-demo01/src/main/java/cn/iocoder/springboot/lab57/hystrixdemo/config/HystrixConfig.java
create mode 100644 lab-57/lab-57-hystrix-demo01/src/main/java/cn/iocoder/springboot/lab57/hystrixdemo/controller/DemoController.java
create mode 100644 lab-57/lab-57-user-service/pom.xml
create mode 100644 lab-57/lab-57-user-service/src/main/java/cn/iocoder/springboot/lab57/userservice/UserServiceApplication.java
create mode 100644 lab-57/pom.xml
diff --git a/lab-57/lab-57-hystrix-demo01/pom.xml b/lab-57/lab-57-hystrix-demo01/pom.xml
new file mode 100644
index 00000000..d07626bc
--- /dev/null
+++ b/lab-57/lab-57-hystrix-demo01/pom.xml
@@ -0,0 +1,57 @@
+
+
+
+ lab-57
+ cn.iocoder.springboot.labs
+ 1.0-SNAPSHOT
+
+ 4.0.0
+
+ lab-57-hystrix-demo01
+
+
+ 1.8
+ 1.8
+ 2.2.4.RELEASE
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ ${spring.boot.version}
+ pom
+ import
+
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+
+ com.netflix.hystrix
+ hystrix-core
+ 1.5.18
+
+
+ com.netflix.hystrix
+ hystrix-javanica
+ 1.5.18
+
+
+
+
+
diff --git a/lab-57/lab-57-hystrix-demo01/src/main/java/cn/iocoder/springboot/lab57/hystrixdemo/DemoApplication.java b/lab-57/lab-57-hystrix-demo01/src/main/java/cn/iocoder/springboot/lab57/hystrixdemo/DemoApplication.java
new file mode 100644
index 00000000..f7d9d309
--- /dev/null
+++ b/lab-57/lab-57-hystrix-demo01/src/main/java/cn/iocoder/springboot/lab57/hystrixdemo/DemoApplication.java
@@ -0,0 +1,20 @@
+package cn.iocoder.springboot.lab57.hystrixdemo;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
+import org.springframework.web.client.RestTemplate;
+
+@SpringBootApplication
+public class DemoApplication {
+
+ @Bean
+ public RestTemplate restTemplate() {
+ return new RestTemplate();
+ }
+
+ public static void main(String[] args) {
+ SpringApplication.run(DemoApplication.class, args);
+ }
+
+}
diff --git a/lab-57/lab-57-hystrix-demo01/src/main/java/cn/iocoder/springboot/lab57/hystrixdemo/config/HystrixConfig.java b/lab-57/lab-57-hystrix-demo01/src/main/java/cn/iocoder/springboot/lab57/hystrixdemo/config/HystrixConfig.java
new file mode 100644
index 00000000..9558e570
--- /dev/null
+++ b/lab-57/lab-57-hystrix-demo01/src/main/java/cn/iocoder/springboot/lab57/hystrixdemo/config/HystrixConfig.java
@@ -0,0 +1,17 @@
+package cn.iocoder.springboot.lab57.hystrixdemo.config;
+
+import com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.EnableAspectJAutoProxy;
+
+@Configuration
+@EnableAspectJAutoProxy
+public class HystrixConfig {
+
+ @Bean
+ public HystrixCommandAspect hystrixCommandAspect() {
+ return new HystrixCommandAspect();
+ }
+
+}
diff --git a/lab-57/lab-57-hystrix-demo01/src/main/java/cn/iocoder/springboot/lab57/hystrixdemo/controller/DemoController.java b/lab-57/lab-57-hystrix-demo01/src/main/java/cn/iocoder/springboot/lab57/hystrixdemo/controller/DemoController.java
new file mode 100644
index 00000000..a9af2c8a
--- /dev/null
+++ b/lab-57/lab-57-hystrix-demo01/src/main/java/cn/iocoder/springboot/lab57/hystrixdemo/controller/DemoController.java
@@ -0,0 +1,35 @@
+package cn.iocoder.springboot.lab57.hystrixdemo.controller;
+
+import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
+import org.apache.commons.lang.exception.ExceptionUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.client.RestTemplate;
+
+@RestController
+@RequestMapping("/demo")
+public class DemoController {
+
+ private Logger logger = LoggerFactory.getLogger(getClass());
+
+ @Autowired
+ private RestTemplate restTemplate;
+
+ @GetMapping("/get_user")
+ @HystrixCommand(fallbackMethod = "getUserFallback")
+ public String getUser(@RequestParam("id") Integer id) {
+ logger.info("[getUser][准备调用 user-service 获取用户({})详情]", id);
+ return restTemplate.getForEntity("http://127.0.0.1:18080/user/get?id=" + id, String.class).getBody();
+ }
+
+ public String getUserFallback(Integer id, Throwable throwable) {
+ logger.info("[getUserFallback][id({}) exception({})]", id, ExceptionUtils.getRootCauseMessage(throwable));
+ return "mock:User:" + id;
+ }
+
+}
diff --git a/lab-57/lab-57-user-service/pom.xml b/lab-57/lab-57-user-service/pom.xml
new file mode 100644
index 00000000..508a1975
--- /dev/null
+++ b/lab-57/lab-57-user-service/pom.xml
@@ -0,0 +1,44 @@
+
+
+
+ lab-57
+ cn.iocoder.springboot.labs
+ 1.0-SNAPSHOT
+
+ 4.0.0
+
+ lab-57-user-service
+
+
+ 1.8
+ 1.8
+ 2.2.4.RELEASE
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ ${spring.boot.version}
+ pom
+ import
+
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+
diff --git a/lab-57/lab-57-user-service/src/main/java/cn/iocoder/springboot/lab57/userservice/UserServiceApplication.java b/lab-57/lab-57-user-service/src/main/java/cn/iocoder/springboot/lab57/userservice/UserServiceApplication.java
new file mode 100644
index 00000000..18047b20
--- /dev/null
+++ b/lab-57/lab-57-user-service/src/main/java/cn/iocoder/springboot/lab57/userservice/UserServiceApplication.java
@@ -0,0 +1,40 @@
+package cn.iocoder.springboot.lab57.userservice;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+@SpringBootApplication
+public class UserServiceApplication {
+
+ @RestController
+ @RequestMapping("/user")
+ public class UserController {
+
+ @GetMapping("/get")
+ public String get(@RequestParam("id") Integer id) {
+ return "User:" + id;
+ }
+
+ @GetMapping("/batch_get")
+ public List batchGet(@RequestParam("ids") List ids) {
+ return ids.stream().map(id -> "User:" + id).collect(Collectors.toList());
+ }
+
+ }
+
+ public static void main(String[] args) {
+ // 设置端口
+ System.setProperty("server.port", "18080");
+
+ // 应用启动
+ SpringApplication.run(UserServiceApplication.class, args);
+ }
+
+}
diff --git a/lab-57/pom.xml b/lab-57/pom.xml
new file mode 100644
index 00000000..b2655be7
--- /dev/null
+++ b/lab-57/pom.xml
@@ -0,0 +1,19 @@
+
+
+
+ labs-parent
+ cn.iocoder.springboot.labs
+ 1.0-SNAPSHOT
+
+ 4.0.0
+
+ lab-57
+ pom
+
+ lab-57-user-service
+ lab-57-hystrix-demo01
+
+
+
diff --git a/labx-23/labx-23-user-service/pom.xml b/labx-23/labx-23-user-service/pom.xml
index aa096b03..b855f862 100644
--- a/labx-23/labx-23-user-service/pom.xml
+++ b/labx-23/labx-23-user-service/pom.xml
@@ -15,7 +15,6 @@
1.8
1.8
2.2.4.RELEASE
- Hoxton.SR1
+ lab-57