diff --git a/springcloud-service-feign/src/main/java/com/kwan/springcloud/api/ComputeClientHystrix.java b/springcloud-service-feign/src/main/java/com/kwan/springcloud/api/ComputeClientHystrix.java index d291e050d4d10306113f5b81233911e43ba3e98a..03e4811844fcce954cd12f7097fe43698df4e938 100644 --- a/springcloud-service-feign/src/main/java/com/kwan/springcloud/api/ComputeClientHystrix.java +++ b/springcloud-service-feign/src/main/java/com/kwan/springcloud/api/ComputeClientHystrix.java @@ -5,6 +5,13 @@ import org.springframework.stereotype.Component; import java.io.Serializable; +/** + * feign的降级 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/1/14 16:47 + */ @Component public class ComputeClientHystrix implements ComputeClient { @Override diff --git a/springcloud-service-goods-9100/src/main/resources/application.yaml b/springcloud-service-goods-9100/src/main/resources/application.yaml index a09fc37460308ddecf1e131636caa1d676cc1460..297ced16622fdfea8a20cbc80e9641272c3059d3 100644 --- a/springcloud-service-goods-9100/src/main/resources/application.yaml +++ b/springcloud-service-goods-9100/src/main/resources/application.yaml @@ -29,7 +29,7 @@ management: web: base-path: /actuator #修改访问路径 2.0之前默认是/ 2.0默认是 /actuator 可以通过这个属性值修改 exposure: - include: * + include: "*" health: show-details: always #显示健康具体信息 默认不会显示详细信息 diff --git a/springcloud-service-goods-9200/src/main/resources/application.yaml b/springcloud-service-goods-9200/src/main/resources/application.yaml index 3c2010e1ef710f8ba2cd2a8cfd0223a0f6c2c45b..5dff84d6f18a53dbb979309a4cf0a6ee23c1da57 100644 --- a/springcloud-service-goods-9200/src/main/resources/application.yaml +++ b/springcloud-service-goods-9200/src/main/resources/application.yaml @@ -28,8 +28,8 @@ management: endpoints: web: base-path: /actuator #修改访问路径 2.0之前默认是/ 2.0默认是 /actuator 可以通过这个属性值修改 - health: - show-details: always #显示健康具体信息 默认不会显示详细信息 + exposure: + include: "*" #mybatis-plus配置 mybatis-plus: diff --git a/springcloud-service-goods-9300/src/main/resources/application.yaml b/springcloud-service-goods-9300/src/main/resources/application.yaml index 872152e74dc3df71475cdb3b9adea098c1d1f28d..c176c3584d4e7d6ca5af404c6fc6a2a14db85682 100644 --- a/springcloud-service-goods-9300/src/main/resources/application.yaml +++ b/springcloud-service-goods-9300/src/main/resources/application.yaml @@ -28,8 +28,8 @@ management: endpoints: web: base-path: /actuator #修改访问路径 2.0之前默认是/ 2.0默认是 /actuator 可以通过这个属性值修改 - health: - show-details: always #显示健康具体信息 默认不会显示详细信息 + exposure: + include: "*" #mybatis-plus配置 mybatis-plus: diff --git a/springcloud-service-portal/src/main/java/com/kwan/springcloud/controller/PortalController.java b/springcloud-service-portal/src/main/java/com/kwan/springcloud/controller/PortalController.java index 6fac257939697cc3271bb3d508f536b26f458259..d5f1b2bf3fd554bb937ea4aca3d884b23379edc7 100644 --- a/springcloud-service-portal/src/main/java/com/kwan/springcloud/controller/PortalController.java +++ b/springcloud-service-portal/src/main/java/com/kwan/springcloud/controller/PortalController.java @@ -35,6 +35,16 @@ public class PortalController { return portalService.hystrix(id); } + + + @GetMapping(value = "/limit/{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE) + public Result limit(@PathVariable String id) { + return portalService.limit(id); + } + + + + /** * 通过主键查询单条数据 * diff --git a/springcloud-service-portal/src/main/java/com/kwan/springcloud/service/PortalService.java b/springcloud-service-portal/src/main/java/com/kwan/springcloud/service/PortalService.java index 49e49d58127b946d818d8eaf92037d80e7e1e74e..785ded241508535e2aa74402a1b8de81a40a70af 100644 --- a/springcloud-service-portal/src/main/java/com/kwan/springcloud/service/PortalService.java +++ b/springcloud-service-portal/src/main/java/com/kwan/springcloud/service/PortalService.java @@ -2,6 +2,7 @@ package com.kwan.springcloud.service; import com.kwan.springcloud.response.Result; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; +import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -26,26 +27,46 @@ public class PortalService { //调用远程的controller return restTemplate.getForEntity(GOODS_URL_2 + id, Result.class).getBody(); } - @HystrixCommand(fallbackMethod = "addServiceFallback") - public Result hystrix(String id) { + @HystrixCommand( + fallbackMethod = "addServiceFallback", + commandProperties = { + @HystrixProperty(name = "execution.timeout.enabled", value = "true"), + @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "6000")} + ) + public Result hystrix(String id) { //1·服务超时,会降级 - try { - Thread.sleep(2000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - //2.服务异常,会降级 - String str = null; - if (str == null) { - throw new RuntimeException("服务异常了."); - } +// try { +// Thread.sleep(6000); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// //2.服务异常,会降级 +// String str = null; +// if (str == null) { +// throw new RuntimeException("服务异常了."); +// } return restTemplate.getForEntity(GOODS_URL_2 + id, Result.class).getBody(); } + public Result addServiceFallback(String id) { log.info("失败id={}", id); // return Result.error("请求超时 服务不可用 请求失败"); return Result.error("请求异常 服务不可用 请求失败"); } + + @HystrixCommand( + fallbackMethod = "addServiceFallback", + threadPoolKey = "goods", + threadPoolProperties = { + @HystrixProperty(name = "coreSize", value = "2"), + @HystrixProperty(name = "maxQueueSize", value = "1"), + @HystrixProperty(name = "keepAliveTimeMinutes", value = "2"), + @HystrixProperty(name = "queueSizeRejectionThreshold", value = "100") + } + ) + public Result limit(String id) { + return restTemplate.getForEntity(GOODS_URL_2 + id, Result.class).getBody(); + } } diff --git a/springcloud-service-portal/src/main/resources/application.yaml b/springcloud-service-portal/src/main/resources/application.yaml index 21cf8a7b49b92e261d910209e3e404542dda9fe7..33c96658ef73c4cf9f056be70c63cd16d66456a0 100644 --- a/springcloud-service-portal/src/main/resources/application.yaml +++ b/springcloud-service-portal/src/main/resources/application.yaml @@ -12,14 +12,23 @@ eureka: service-url: defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/ +#开启feign开始hystrix的支持 +feign: + hystrix: + enabled: true - -feign.hystrix.enabled=true - hystrix.command.default.execution.timeout.enabled=true - hystrix.command.default.execution.isolation.thread.timeoutInMi - 1liseconds=5000 - 如果hystrix.command.default.execution.timeout.enabled为true,则会有两 - 个执行方法超时的配置,一个就是ribbon的ReadTimeout,一个就是熔断器 - I - hystrix的timeoutInMilliseconds,此时谁的值小谁生效; - 如果hystrix.command.default.execution.timeout.enabled为false,则熔断 \ No newline at end of file +hystrix: + command: + default: + execution: + isolation: + thread: + timeoutInMilliseconds: 5000 #hystrix超时时间 + timeout: + enabled: true #开启hystrix超时管理 +ribbon: + ReadTimeout: 2000 #请求超时时间 + http: + client: + enabled: true #开启ribbon超时管理 + ConnectTimeout: 2000 #连接超时时间 \ No newline at end of file