SpringCloud有五大神兽,包括:服务中心(即注册中心)、负载均衡、断路器、服务网关、分布式配置中心
在https://mp.csdn.net/mdeditor/89673212# 此博客内已经描述了注册中心和负载均衡的实现,此处就不在赘述,此文对断路器、服务网关和分布式配置中心的实现进行详细描述。
(一)断路器(Hystrix)
断路器类似于我们生活中的保险丝,在系统中使用断路器通过隔离限流、熔断、降级等策略,避免一个服务访问出现延迟时,导致所有请求阻塞,造成的系统崩掉。
- 服务提供端导入依赖包
User_service_hystrix_8002的pom.xml
<!--hystrix断路器支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
- 服务提供端提供托底函数
UserController
package cn.itsource.springcloud.controller;
import cn.itsource.springcloud.client.UserClient;
import cn.itsource.springcloud.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/provider")
public class UserController implements UserClient {
@HystrixCommand(fallbackMethod = "getUserFailBack") //出现短