SpringCloud微服务实战之断路器Hystrix

在微服务架构中,一个服务单元往往依赖其他很多的服务单元,若其中一个发生故障,很容易导致服务阻塞。针对这些问题,Spring Cloud Hystrix实现了断路器、线程隔离等一系列功能。

Spring Cloud Hystrix是基于Netflix的开源架构Hystrix来实现的,该框架的目标在于通过控制那些访问远程系统、服务和第三方库的节点,从而对延迟和故障提供更大的容错能力。Hystrix具备服务降级、服务熔断、线程和信号隔离、请求缓存、请求合并以及服务监控等强大功能。

一、修改SpringCloud-Ribbon项目
1、在pom中添加hystrix依赖

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

2、修改主类,添加@EnableCircuitBreaker注解开启断路器功能

@EnableCircuitBreaker
@EnableDiscoveryClient
@SpringBootApplication
public class App {

    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

}

3、新增HelloService,通过注解@HystrixCommand,利用fallback属性来指定服务降级处理逻辑的方法。

@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod="helloFallback")
    public String helloConsumer(){

        ResponseEntity<String> entity = restTemplate.getForEntity("http://helloservice/hello", String.class);

        return entity.getBody();
    }

    public String helloFallback(){

        return "Error,I'm wrong!";
    }
}

4、改造ConsumerController,通过调用helloservice来调用hello服务。

    @Autowired
    HelloService helloService;
    @RequestMapping(value="/ribbonconsumer",method=RequestMethod.GET)
    public String helloConsumer(){
        return helloService.helloConsumer();
    }

二、启动SpringCloud-EurekaServer项目,通过http://localhost:8888/访问
三、改造SpringCloud-Service项目,完成后启动项目

利用Thread的sleep模拟延迟阻塞,hystrix默认超时时间为2000ms,超过这个时间,服务就会认为是阻塞。

@RestController
public class HelloServiceController {

    @Autowired
    private DiscoveryClient client;

    @RequestMapping(value="/hello",method=RequestMethod.GET)
    public String greeting(){
        ServiceInstance si = client.getLocalServiceInstance();
        try {
            int sleepTime = new Random().nextInt(3000);
            System.out.println("Thread Sleep =====>"+sleepTime);
            Thread.sleep(sleepTime);
        } catch (InterruptedException e) {

            e.printStackTrace();
        }
        return "hello,"+si.getPort();
    }
}

四、启动SpringCloud-Ribbon项目测试
成功调用:
这里写图片描述
这里写图片描述


阻塞,调用Fallback函数
这里写图片描述
这里写图片描述

五、Hystrix仪表盘

1、将hystrix仪表盘整合在SpringCloud-Ribbon中,在pom中添加Hystrix仪表盘依赖。

 <!-- add hystrix dashboard depend -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

2、主类添加注解@EnableHystrixDashboard开启hystrix仪表盘功能。
3、启动SpringCloud-Ribbon测试
这里写图片描述


输入本地single Hystrix地址:
这里写图片描述

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值