SpringCloud:断路器(Hystrix)

在微服务架构中,我们将业务拆分成一个个的服务,服务与服务之间可以相互调用(RPC)。为了保证其高可用,单个服务又必须集群部署。由于网络原因或者自身的原因,服务并不能保证服务的100%可用,如果单个服务出现问题,调用这个服务就会出现网络延迟,此时若有大量的网络涌入,会形成任务累计,导致服务瘫痪,甚至导致服务“雪崩”。
为了解决这个问题,就出现断路器模型。

断路器简介

Netflix已经创建了一个名为Hystrix的库来实现断路器模式。 在微服务架构中,多层服务调用是非常常见的。
这里写图片描述
较底层的服务如果出现故障,会导致连锁故障。当对特定的服务的调用达到一个阀值(hystrix 是5秒20次) 断路器将会被打开。
这里写图片描述
断路打开后,可用避免连锁故障,fallback方法可以直接返回一个固定值。

准备工作

基于上一节的工程,启动eureka-server 工程;启动service-hi工程,它的端口为8762;

在ribbon使用断路器

改造serice-ribbon 工程的代码:
在pox.xml文件中加入:

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

在程序的入口类加@EnableHystrix:

@EnableDiscoveryClient//向服务中心注册
@SpringBootApplication
@EnableHystrix
public class RobbinApplication {

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

    //向程序的ioc注入一个bean: restTemplate;
    //并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

改造HelloService类,加上@HystrixCommand,并指定fallbackMethod方法。

@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError")
    public String hiService(String name) {
        return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
    }

    public String hiError(String name) {
        return "hi,"+name+",sorry,error!";
    }
}

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

hi wh,I am from port:8762

此时关闭 service-hi ,工程,当我们再访问http://localhost:8764/hi?name=wh,浏览器会显示:

hi ,wh,sorry,error!

这就证明断路器起作用了。

Circuit Breaker: Hystrix Dashboard (断路器:hystrix 仪表盘)

基于service-ribbon 改造下:
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:

@EnableDiscoveryClient//向服务中心注册
@SpringBootApplication
@EnableHystrix
@EnableHystrixDashboard//开启hystrix 仪表盘
public class RobbinApplication {

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

    //向程序的ioc注入一个bean: restTemplate;
    //并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

打开浏览器:访问http://localhost:8764/hystrix,界面如下:
这里写图片描述

输入stream后,点击monitor stream,进入下一个界面,访问:http://localhost:8764/hi?name=wh
这里写图片描述

转载:
http://blog.csdn.net/forezp/article/details/69934399

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值