SpringCloud服务降级案列
服务的降级
什么是服务降级
所有的RPC技术里面服务降级是一个最为重要的话题,所谓的降级指的是当服务的提供方不可使用的时候,程序不会出现异常,而会出现本地的操作调
服务降级案例
1、目录展示

2、导入依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--eureka依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<exclusions>
<exclusion>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<exclusions>
<exclusion>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.2.3.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!--springCloud依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
3、application.yml配置文件
##应用名称 spring: application: name: order-service ##端口号 server: port: 9002 ##设置Tomcat最大线程数 tomcat: max-threads: 50 ##注册到注册中心当中去,指定注册中心地址 eureka: client: service-url: defaultZone: http://localhost:8001/eureka ##配置ribbon读取时间 ribbon: ReadTimeout: 15000 ConnectTimeout: 15000 ##开启断路器 ##feign: ##hystrix: ##enabled: true ##因为我们在members服务中线程休眠3s,服务调用默认超过1s当做调用失败,那么就会执行服务降级的类,我们可以配置服务响应时间 hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 4000
4、MemberService

5、MemberServiceFallBack

6、controller

7、启动类StartSpringCloudOrder

效果展示

使用雪崩测试器,模拟雪崩场景


当访问页面路径时,便不会出现等待加载的状况,从而返回服务降级


浙公网安备 33010602011771号