您的位置:首页 > 编程语言 > Java开发

SpringCloud入门教学|第四篇:断路器(Hystrix)

2018-02-22 19:26 711 查看
产生原因

断路器介绍

在ribbon使用断路器

Feign中使用断路器

Hystrix Dashboard断路器的仪表盘

产生原因

在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。

为了解决这个问题,业界提出了断路器模型

断路器介绍

Netflix的创造了一个调用的库Hystrix实现了断路器图案。在微服务架构中,通常有多层服务调用



较低级别的服务中的服务故障可能导致用户级联故障。当对特定服务的呼叫达到一定阈值时(Hystrix中的默认值为5秒内的20次故障),电路打开,不进行通话。在错误和开路的情况下,开发人员可以提供后备



在ribbon使用断路器

对我们之前的service-ribbon的Moudle的工程代码进行相应更改。

第一步: 增加
spring-cloud-starter-hystrix


<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>


第二步: 进行更改入口类

@SpringBootApplication
@EnableHystrix //@EnableHystrix  开启断路器
@EnableDiscoveryClient  // @EnableDiscoveryClient向服务中心注册
public class ServiceRibbonApplication {

public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}

@Bean
@LoadBalanced // @LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
RestTemplate restTemplate(){
return new RestTemplate();
}

}


这里增加了
@EnableHystrix
进行开启断路器。

* 第三步:进行更改HelloService类。

@Service
class HelloService {
@Autowired
private lateinit var restTemplate: RestTemplate

@HystrixCommand(fallbackMethod = "hiError")
fun helloService(name: String): String {
return restTemplate.getForObject("http://SERVICE-HI/hello?name=$name",String::class.java)
}

fun hiError(name: String): String {
return "hi $name ,sorry,error!"
}

}


这里我们增加了
@HystrixCommand
注解,该注解对该方法创建了熔断器的功能,并制定了fallbackMethod熔断方法。这里直接返回了一个字符串。

测试

当我们启动service-ribbon工程,访问http://localhost:8764/hi?name=houshuai,浏览器显示

hi houshuai,I am from port:8769


此时关闭service-hi工程,再次访问后出现

hi ,houshuai,sorry,error!


这就说明当 service-hi 工程不可用的时候,service-ribbon调用 service-hi的API接口时,会执行快速失败,直接返回一组字符串,而不是等待响应超时,这很好的控制了容器的线程阻塞。

Feign中使用断路器

Feign是自带断路器的,在低版本的Spring Cloud中,它没有默认打开。需要在配置文件中配置打开它

# 高版本的SpringCloud 移除了这个配置
feign.hystrix.enabled=true


基于service-fegin工程进行改造。

@FeignClient(value = "service-hi",fallback = SchedualServiceHiHystric::class)
interface SchedualServiceHi {
// 这里的/hello 是你注册的Eureka中的生命接口
@RequestMapping(value = ["/hello"], method = [(RequestMethod.GET)])
fun sayHiFromClientOne(@RequestParam(value = "name") name:String): String
}


@FeignClient
中的fallback 指定了断路器启用后的指定类

* SchedualServiceHiHystric需要实现SchedualServiceHi 接口,并注入到Ioc容器中,代码如下:

@Component
class SchedualServiceHiHystric :SchedualServiceHi{
override fun sayHiFromClientOne(name: String): String {
return "sorry $name"
}

}


可以断开service-hi 试一下有没有生效

Hystrix Dashboard(断路器的仪表盘)

增加如下pom.xml的依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>


需要在程序入口类中增加
@EnableHystrixDashboard
来启动hystrixDashboard

@EnableHystrixDashboard
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceFeignApplication {

public static void main(String[] args) {
SpringApplication.run(ServiceFeignApplication.class, args);
}
}


运行后,可以访问http://localhost:8764/hystrix,来访问监控界面


源码

https://github.com/houshuai0816/SpringCloudDemo
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: