From 73476c3059dc17f05a63df752a40447157bd09c0 Mon Sep 17 00:00:00 2001 From: qinyingjie Date: Sat, 14 Jan 2023 16:23:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E6=B7=BB=E5=8A=A0=E5=A4=87=E6=B3=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 1 + .../kwan/springcloud/FeignApplication.java | 10 ++++ .../springcloud/api/ComputeClientHystrix.java | 1 - .../springcloud/service/FeignService.java | 28 ++--------- .../src/main/resources/application.yaml | 6 ++- springcloud-service-hystrix-dashboard/pom.xml | 49 +++++++++++++++++++ .../HystrixDashboardApplication.java | 22 +++++++++ .../src/main/resources/application.yaml | 7 +++ .../HystrixDashboardApplicationTests.java | 13 +++++ springcloud-service-portal/pom.xml | 11 +++++ .../kwan/springcloud/PortalApplication.java | 13 ++++- .../controller/PortalController.java | 13 +++++ .../springcloud/service/PortalService.java | 27 ++++++++-- .../src/main/resources/application.yaml | 14 +++++- 14 files changed, 181 insertions(+), 34 deletions(-) create mode 100644 springcloud-service-hystrix-dashboard/pom.xml create mode 100644 springcloud-service-hystrix-dashboard/src/main/java/com/kwan/springcloud/HystrixDashboardApplication.java create mode 100644 springcloud-service-hystrix-dashboard/src/main/resources/application.yaml create mode 100644 springcloud-service-hystrix-dashboard/src/test/java/com/kwan/springcloud/HystrixDashboardApplicationTests.java diff --git a/pom.xml b/pom.xml index 7ea2a29..ac8bf57 100644 --- a/pom.xml +++ b/pom.xml @@ -35,6 +35,7 @@ springcloud-service-feign springcloud-service-zuul springcloud-service-consul + springcloud-service-hystrix-dashboard springcloud-service-eureka diff --git a/springcloud-service-feign/src/main/java/com/kwan/springcloud/FeignApplication.java b/springcloud-service-feign/src/main/java/com/kwan/springcloud/FeignApplication.java index a70cd57..c31b391 100644 --- a/springcloud-service-feign/src/main/java/com/kwan/springcloud/FeignApplication.java +++ b/springcloud-service-feign/src/main/java/com/kwan/springcloud/FeignApplication.java @@ -6,6 +6,16 @@ import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; + + +/** + * + * + * http://localhost:8081/feign/1 + *@version : 2.2.0 + *@author : qinyingjie + *@date : 2023/1/14 16:13 + */ @EnableCircuitBreaker @EnableFeignClients @EnableEurekaClient 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 a70fc7e..d291e05 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 @@ -7,7 +7,6 @@ import java.io.Serializable; @Component public class ComputeClientHystrix implements ComputeClient { - @Override public Result selectOne(Serializable id) { return Result.error("feign方式请求 服务不可用 请求失败"); diff --git a/springcloud-service-feign/src/main/java/com/kwan/springcloud/service/FeignService.java b/springcloud-service-feign/src/main/java/com/kwan/springcloud/service/FeignService.java index 37072a0..cf35f76 100644 --- a/springcloud-service-feign/src/main/java/com/kwan/springcloud/service/FeignService.java +++ b/springcloud-service-feign/src/main/java/com/kwan/springcloud/service/FeignService.java @@ -1,40 +1,20 @@ package com.kwan.springcloud.service; +import com.kwan.springcloud.api.ComputeClient; import com.kwan.springcloud.response.Result; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import org.springframework.web.client.RestTemplate; @Slf4j @Service public class FeignService { - /** - * http直接访问 - */ - private static final String GOODS_URL = "http://localhost:9100/kwanGoodsInfo/"; - - - /** - * eureka访问 - */ - private static final String GOODS_URL_2 = "http://goods-service/kwanGoodsInfo/"; - - @Autowired - RestTemplate restTemplate; + private ComputeClient computeClient; - // @HystrixCommand(fallbackMethod = "addServiceFallback") - public Result addService(String id) { - //调用远程的controller - return restTemplate.getForEntity(GOODS_URL_2 + id, Result.class).getBody(); - } - - public Result addServiceFallback(String id) { - log.info("失败id={}", id); - return Result.error("ribbon方式请求 服务不可用 请求失败"); + public Result addService(String id) { + return computeClient.selectOne(id); } - } diff --git a/springcloud-service-feign/src/main/resources/application.yaml b/springcloud-service-feign/src/main/resources/application.yaml index c3ded33..345c353 100644 --- a/springcloud-service-feign/src/main/resources/application.yaml +++ b/springcloud-service-feign/src/main/resources/application.yaml @@ -10,4 +10,8 @@ spring: eureka: client: service-url: - defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/ \ No newline at end of file + defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/ + +feign: + hystrix: + enabled: true \ No newline at end of file diff --git a/springcloud-service-hystrix-dashboard/pom.xml b/springcloud-service-hystrix-dashboard/pom.xml new file mode 100644 index 0000000..25d4e34 --- /dev/null +++ b/springcloud-service-hystrix-dashboard/pom.xml @@ -0,0 +1,49 @@ + + + 4.0.0 + + com.kwan.springcloud + springcloud-service-parent + 1.0.0 + + springcloud-service-hystrix-dashboard + 0.0.1-SNAPSHOT + springcloud-service-hystrix-dashboard + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.cloud + spring-cloud-starter-netflix-hystrix-dashboard + + + + org.springframework.boot + spring-boot-devtools + true + + + org.springframework.boot + spring-boot-starter-actuator + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/springcloud-service-hystrix-dashboard/src/main/java/com/kwan/springcloud/HystrixDashboardApplication.java b/springcloud-service-hystrix-dashboard/src/main/java/com/kwan/springcloud/HystrixDashboardApplication.java new file mode 100644 index 0000000..722e420 --- /dev/null +++ b/springcloud-service-hystrix-dashboard/src/main/java/com/kwan/springcloud/HystrixDashboardApplication.java @@ -0,0 +1,22 @@ +package com.kwan.springcloud; + + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; +/** + * + * http://localhost:9909/hystrix + *@version : 2.2.0 + *@author : qinyingjie + *@date : 2023/1/14 15:33 + */ +@EnableHystrixDashboard +@SpringBootApplication +public class HystrixDashboardApplication { + + public static void main(String[] args) { + + SpringApplication.run(HystrixDashboardApplication.class, args); + } +} diff --git a/springcloud-service-hystrix-dashboard/src/main/resources/application.yaml b/springcloud-service-hystrix-dashboard/src/main/resources/application.yaml new file mode 100644 index 0000000..b921836 --- /dev/null +++ b/springcloud-service-hystrix-dashboard/src/main/resources/application.yaml @@ -0,0 +1,7 @@ +#端口号 +server: + port: 9909 + +spring: + application: + name: hystrix-dashboard-service #服务名称 \ No newline at end of file diff --git a/springcloud-service-hystrix-dashboard/src/test/java/com/kwan/springcloud/HystrixDashboardApplicationTests.java b/springcloud-service-hystrix-dashboard/src/test/java/com/kwan/springcloud/HystrixDashboardApplicationTests.java new file mode 100644 index 0000000..c4f4a37 --- /dev/null +++ b/springcloud-service-hystrix-dashboard/src/test/java/com/kwan/springcloud/HystrixDashboardApplicationTests.java @@ -0,0 +1,13 @@ +package com.kwan.springcloud; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class HystrixDashboardApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/springcloud-service-portal/pom.xml b/springcloud-service-portal/pom.xml index 9fb6d11..9fb6186 100644 --- a/springcloud-service-portal/pom.xml +++ b/springcloud-service-portal/pom.xml @@ -29,6 +29,17 @@ org.springframework.cloud spring-cloud-starter-netflix-eureka-client + + org.springframework.cloud + spring-cloud-openfeign-core + + + + org.springframework.cloud + spring-cloud-starter-hystrix + 1.4.7.RELEASE + + diff --git a/springcloud-service-portal/src/main/java/com/kwan/springcloud/PortalApplication.java b/springcloud-service-portal/src/main/java/com/kwan/springcloud/PortalApplication.java index d278f2f..63e16ab 100644 --- a/springcloud-service-portal/src/main/java/com/kwan/springcloud/PortalApplication.java +++ b/springcloud-service-portal/src/main/java/com/kwan/springcloud/PortalApplication.java @@ -3,8 +3,17 @@ package com.kwan.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; - - +import org.springframework.cloud.netflix.hystrix.EnableHystrix; +import org.springframework.cloud.openfeign.EnableFeignClients; +/** + * + * http://localhost:8080/portal/hystrix/1 + *@version : 2.2.0 + *@author : qinyingjie + *@date : 2023/1/14 16:07 + */ +@EnableFeignClients +@EnableHystrix @EnableEurekaClient @SpringBootApplication public class PortalApplication { 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 0f05405..6fac257 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 @@ -24,6 +24,17 @@ public class PortalController { @Autowired private PortalService portalService; + /** + * 通过主键查询单条数据 + * + * @param id 主键 + * @return 单条数据 + */ + @GetMapping(value = "/hystrix/{id}", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE) + public Result hystrix(@PathVariable String id) { + return portalService.hystrix(id); + } + /** * 通过主键查询单条数据 * @@ -40,4 +51,6 @@ public class PortalController { public Result test() { return Result.ok("portal 访问成功"); } + + } \ No newline at end of file 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 4f7f62a..49e49d5 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 @@ -1,6 +1,7 @@ package com.kwan.springcloud.service; import com.kwan.springcloud.response.Result; +import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -9,26 +10,42 @@ import org.springframework.web.client.RestTemplate; @Slf4j @Service public class PortalService { - /** * http直接访问 */ private static final String GOODS_URL = "http://localhost:9100/kwanGoodsInfo/"; - - /** * eureka访问 */ private static final String GOODS_URL_2 = "http://goods-service/kwanGoodsInfo/"; - @Autowired RestTemplate restTemplate; - public Result addService(String id) { //调用远程的controller return restTemplate.getForEntity(GOODS_URL_2 + id, Result.class).getBody(); } + @HystrixCommand(fallbackMethod = "addServiceFallback") + 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("服务异常了."); + } + 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("请求异常 服务不可用 请求失败"); + } } diff --git a/springcloud-service-portal/src/main/resources/application.yaml b/springcloud-service-portal/src/main/resources/application.yaml index bde245a..21cf8a7 100644 --- a/springcloud-service-portal/src/main/resources/application.yaml +++ b/springcloud-service-portal/src/main/resources/application.yaml @@ -10,4 +10,16 @@ spring: eureka: client: service-url: - defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/ \ No newline at end of file + defaultZone: http://eureka8767:8767/eureka/,http://eureka8768:8768/eureka/,http://eureka8769:8769/eureka/ + + + +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 -- GitLab