第112天学习打卡(SpringCloud Feign:使用接口方式调用服务 Hystrix Dashboard) (6)

修改springcloud-consumer-dept-feign的application.yml

server: port: 80 # 开启降级feign.hystrix feign: hystrix: enabled: true # Eureka配置 eureka: client: register-with-eureka: false # 不向Eureka 注册自己 service-url: defaultZone: :7001/eureka/,:7002/eureka/,:7003/eureka/

服务降级在客户端 服务熔断在服务端

**测试:先启动7001 然后启动8001(springcloud-proviser-sept-8001) ,80(springcloud-consumer-dept-feign),然后在把8001停掉 **

测试结果:

image-20210517095137227

Hystrix: Dashboard 流监控 springcloud-consumer-hystrix-dashboard

导入依赖:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 "> <parent> <artifactId>springcloud</artifactId> <groupId>com.kuang</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-consumer-hystrix-dashboard</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <!--实体类+web--> <dependencies> <!-- hystrix依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> <version>1.4.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!-- Ribbon--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!-- Eureka的依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.4.6.RELEASE</version> </dependency> <dependency> <groupId>com.kuang</groupId> <artifactId>springcloud-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--热部署--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>

application.yml

server: port: 9001

DeptConsumerDashboard_9001.java

package com.kuang.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; @SpringBootApplication @EnableHystrixDashboard //开启监控页面 public class DeptConsumerDashboard_9001 { public static void main(String[] args){ SpringApplication.run(DeptConsumerDashboard_9001.class,args); } }

springclou-provider-dept-hystrix-8001 修改 DeptProviderHystrix.java

package com.kuang.springcloud; //启动类 import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; 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.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; @SpringBootApplication @EnableEurekaClient//在服务启动后自动注册到Eureka中 @EnableDiscoveryClient //服务发现 @EnableCircuitBreaker //添加对熔断的支持 public class DeptProviderHystrix_8001 { public static void main(String[] args){ SpringApplication.run(DeptProviderHystrix_8001.class,args); } //增加一个Servlet @Bean public ServletRegistrationBean hystrixMetricsStreamServlet(){ ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet()); registrationBean.addUrlMappings("/actuator/hystrix.stream");//访问这个页面就可以被监控了 return registrationBean; } }

先启动7001 端口 再启动9001端口 然后启动DeptProviderHystrix_8001端口

测试结果图

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwdgzf.html