完善熔断、监控
diff --git a/samples/client/pom.xml b/samples/client/pom.xml
index a13d23a..5bc48d9 100644
--- a/samples/client/pom.xml
+++ b/samples/client/pom.xml
@@ -21,10 +21,10 @@
</dependency>
<!-- 微服务 健康监控 -->
- <!-- <dependency>
+ <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
- </dependency> -->
+ </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
@@ -32,7 +32,7 @@
</dependency>
- <dependency>
+ <!-- <dependency>
<groupId>com.supwisdom.infras</groupId>
<artifactId>infras-mvc</artifactId>
</dependency>
@@ -47,7 +47,6 @@
<artifactId>infras-i18n</artifactId>
</dependency>
- <!--
<dependency>
<groupId>com.supwisdom.infras</groupId>
<artifactId>infras-lang</artifactId>
@@ -96,6 +95,10 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.springframework.cloud</groupId>
+ <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
+ </dependency>
<!-- Test things -->
diff --git a/samples/client/src/main/java/com/supwisdom/leaveschool/client/ClientApplication.java b/samples/client/src/main/java/com/supwisdom/leaveschool/client/ClientApplication.java
index b09fe05..54b83cc 100644
--- a/samples/client/src/main/java/com/supwisdom/leaveschool/client/ClientApplication.java
+++ b/samples/client/src/main/java/com/supwisdom/leaveschool/client/ClientApplication.java
@@ -2,16 +2,35 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
+import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
+import org.springframework.context.annotation.Bean;
+
+import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
@SpringBootApplication
@EnableCircuitBreaker
-@EnableFeignClients
+@EnableFeignClients(value = "com.supwisdom.leaveschool.client.service")
+@EnableHystrixDashboard
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
+
+
+ @Bean
+ public ServletRegistrationBean<HystrixMetricsStreamServlet> hystrixMetricsStreamServlet() {
+ HystrixMetricsStreamServlet hystrixMetricsStreamServlet = new HystrixMetricsStreamServlet();
+
+ ServletRegistrationBean<HystrixMetricsStreamServlet> servletRegistrationBean = new ServletRegistrationBean<HystrixMetricsStreamServlet>(hystrixMetricsStreamServlet);
+ servletRegistrationBean.setName("hystrixMetricsStreamServlet");
+ servletRegistrationBean.setLoadOnStartup(1);
+ servletRegistrationBean.addUrlMappings("/hystrix.stream");
+
+ return servletRegistrationBean;
+ }
}
diff --git a/samples/client/src/main/resources/application.yml b/samples/client/src/main/resources/application.yml
index 550499b..9c58458 100755
--- a/samples/client/src/main/resources/application.yml
+++ b/samples/client/src/main/resources/application.yml
@@ -20,8 +20,8 @@
client:
config:
default:
- connectTimeout: 60000
- readTimeout: 60000
+ connectTimeout: 5000
+ readTimeout: 5000
loggerLevel: full
hystrix:
enabled: true
@@ -34,6 +34,6 @@
enabled: true
isolation:
thread:
- timeoutInMilliseconds: 60000
+ timeoutInMilliseconds: 5000
gateway.api.url: http://localhost:5555/api
diff --git a/samples/gateway/pom.xml b/samples/gateway/pom.xml
index 93aebcc..611dd9a 100644
--- a/samples/gateway/pom.xml
+++ b/samples/gateway/pom.xml
@@ -21,10 +21,10 @@
</dependency>
<!-- 微服务 健康监控 -->
- <!-- <dependency>
+ <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
- </dependency> -->
+ </dependency>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
@@ -36,6 +36,20 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
+
+ <dependency>
+ <groupId>org.springframework.cloud</groupId>
+ <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
+ </dependency>
+ <!-- <dependency>
+ <groupId>org.springframework.cloud</groupId>
+ <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
+ </dependency> -->
+
+ <!-- <dependency>
+ <groupId>io.micrometer</groupId>
+ <artifactId>micrometer-registry-prometheus</artifactId>
+ </dependency> -->
<!-- Test things -->
diff --git a/samples/gateway/src/main/java/com/supwisdom/leaveschool/gateway/GatewayApplication.java b/samples/gateway/src/main/java/com/supwisdom/leaveschool/gateway/GatewayApplication.java
index 39b0c99..089a50c 100644
--- a/samples/gateway/src/main/java/com/supwisdom/leaveschool/gateway/GatewayApplication.java
+++ b/samples/gateway/src/main/java/com/supwisdom/leaveschool/gateway/GatewayApplication.java
@@ -10,4 +10,17 @@
SpringApplication.run(GatewayApplication.class, args);
}
+//
+// @Bean
+// public ServletRegistrationBean<HystrixMetricsStreamServlet> hystrixMetricsStreamServlet() {
+// HystrixMetricsStreamServlet hystrixMetricsStreamServlet = new HystrixMetricsStreamServlet();
+//
+// ServletRegistrationBean<HystrixMetricsStreamServlet> servletRegistrationBean = new ServletRegistrationBean<HystrixMetricsStreamServlet>(hystrixMetricsStreamServlet);
+// servletRegistrationBean.setName("hystrixMetricsStreamServlet");
+// servletRegistrationBean.setLoadOnStartup(1);
+// servletRegistrationBean.addUrlMappings("/hystrix.stream");
+//
+// return servletRegistrationBean;
+// }
+//
}
diff --git a/samples/gateway/src/main/java/com/supwisdom/leaveschool/gateway/controller/error/ErrorController.java b/samples/gateway/src/main/java/com/supwisdom/leaveschool/gateway/controller/error/ErrorController.java
new file mode 100644
index 0000000..77cb767
--- /dev/null
+++ b/samples/gateway/src/main/java/com/supwisdom/leaveschool/gateway/controller/error/ErrorController.java
@@ -0,0 +1,27 @@
+package com.supwisdom.leaveschool.gateway.controller.error;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpStatus;
+import org.springframework.util.MimeTypeUtils;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseStatus;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+public class ErrorController {
+
+ private static final Logger logger = LoggerFactory.getLogger(ErrorController.class);
+
+ @RequestMapping(value = "/error", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
+ @ResponseStatus(value = HttpStatus.BAD_GATEWAY)
+ public Map<String, Object> error() {
+ logger.debug("error");
+ Map<String, Object> result = new HashMap<String, Object>();
+ result.put("error", "Bad Gateway");
+ return result;
+ }
+}
diff --git a/samples/gateway/src/main/resources/application.yml b/samples/gateway/src/main/resources/application.yml
index f472001..35065a6 100755
--- a/samples/gateway/src/main/resources/application.yml
+++ b/samples/gateway/src/main/resources/application.yml
@@ -6,7 +6,7 @@
level:
root: INFO
org.springframework.web: INFO
- org.springframework.cloud.gateway: INFO
+ org.springframework.cloud.gateway: TRACE
com.supwisdom.infras.security: DEBUG
com.supwisdom.leaveschool: DEBUG
@@ -23,5 +23,21 @@
- Path=/api/sample-user/**
filters:
- RewritePath=/(?<prefix>.*)/sample-user/(?<suffix>.*), /$\{prefix}/$\{suffix}
+ - name: Hystrix
+ args:
+ name: default
+ fallbackUri: forward:/error
+ metrics:
+ enabled: true
+
+hystrix:
+ command:
+ default:
+ execution:
+ timeout:
+ enabled: true
+ isolation:
+ thread:
+ timeoutInMilliseconds: 5000
management.endpoints.web.exposure.include: '*'
diff --git a/samples/user/src/main/java/com/supwisdom/leaveschool/user/controller/api/admin/ApiAdminUserController.java b/samples/user/src/main/java/com/supwisdom/leaveschool/user/controller/api/admin/ApiAdminUserController.java
index f8adf36..ee17ba3 100644
--- a/samples/user/src/main/java/com/supwisdom/leaveschool/user/controller/api/admin/ApiAdminUserController.java
+++ b/samples/user/src/main/java/com/supwisdom/leaveschool/user/controller/api/admin/ApiAdminUserController.java
@@ -3,6 +3,8 @@
import java.util.HashMap;
import java.util.Map;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@@ -13,6 +15,7 @@
@RequestMapping("/api/admin/users")
public class ApiAdminUserController {
+ private static final Logger logger = LoggerFactory.getLogger(ApiAdminUserController.class);
/**
* curl -i -s -X GET -H 'Accept:application/json' 'http://localhost:10010/api/admin/users/greeting/abc'
@@ -22,8 +25,8 @@
*/
@GetMapping(path = "/greeting/{name}", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
public Map<String, Object> greeting(@PathVariable("name") String name) {
- System.out.println(name);
- Map<String, Object> result = new HashMap<>();
+ logger.debug(name);
+ Map<String, Object> result = new HashMap<String, Object>();
result.put("message", "Good " + name);
return result;
}
diff --git a/samples/user/src/main/resources/application.yml b/samples/user/src/main/resources/application.yml
index f8a64c2..d7c4020 100755
--- a/samples/user/src/main/resources/application.yml
+++ b/samples/user/src/main/resources/application.yml
@@ -5,7 +5,7 @@
logging:
level:
root: INFO
- org.springframework.web: INFO
+ org.springframework.web: TRACE
com.supwisdom.infras.security: DEBUG
com.supwisdom.leaveschool: DEBUG