天天看点

Spring Cloud(F版)断路器Hystrix

在微服务架构中,我们将系统拆分成很多服务单元,各单元间通过服务的注册和订阅方式相互依赖。由于每个单元都在不同的进程中进行,依赖通过远程调用的方式执行,这样就有可能因为网络原因或者依赖服务自身问题出现调用故障或者延迟,而这些问题会直接导致调用方的对外服务也出现延迟,若此时调用方的请求不断增加,最后就会因等待出现故障的依赖方响应形成任务积压,最终导致自身服务的瘫痪。

Hystrix是Netflix开源的一个针对分布式系统容错处理的开源组件,实现了断路器、线程隔离等一系列服务保护功能。Hystrix具备服务降级、服务熔断、线程和信号隔离、请求缓存、请求合并以及服务监控等强大的功能。

下面我们就开始整合Hystrix

——————————————————————————————————————————————————————

环境搭建

Spring Cloud(F版)断路器Hystrix

这是我搭建的一个maven多模块的测试项目,如果不知道IDEA如何创建多模块项目的可以看一下【IntelliJ IDEA创建Maven多模块项目】这篇文章。

此项目中,commons是公共类模块,存放实体类、工具类等,是一个普通的java项目,consumers是消费者,这里我创建两个模块分别是移动端和pc端这两个消费者,providers是提供者,同样也创建了用户以及订单两个提供者。

eureka-server则是服务注册中心,这个就不过多讲解了可以看之前的几篇文章。

这里提供者和消费者都需要引入commons模块的依赖

添加依赖

这里我们使用consumer-web项目添加Hystrix

<!-- 断路器 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
		</dependency>
           

添加注解

在程序主入口添加@EnableHystrix注解,启用断路器。

package com.clarezhou.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * @author clarezhou
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class ConsumerWebApplication {

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

	@Bean
	@LoadBalanced
	RestTemplate restTemplate(){
		return new RestTemplate();
	}
}
           

修改Controller

添加 @HystrixCommand注解和降级fallbackMethod

@HystrixCommand(fallbackMethod = "error")
    @RequestMapping(value="sayHello", method = RequestMethod.GET)
    public String helloConsumer(){
        return restTemplate.getForEntity("http://PROVIDER-USER/hello/sayHello",String.class).getBody();
    }


    public String error() {
        return "error";
    }
           

测试结果

不启动提供者的情况下,返回error

Spring Cloud(F版)断路器Hystrix

Feign中使用Hystrix

在前面的文章中介绍了Feign的使用方法,当时项目是consumer-mobile,这里依然在原有的代码上进行修改。

在@FeignClient配置降级

在HelloService的@FeignClient添加fallback属性

@FeignClient(name="PROVIDER-USER",fallback = HelloServiceFallback.class)
           

创建HelloServiceFallback降级回退类

package com.clarezhou.example.service.fallback;

import com.clarezhou.example.service.HelloService;
import main.java.com.clarezhou.example.entity.UserInfoEntity;
import org.springframework.stereotype.Component;

/**
 * @author clarezhou
 * @date 2019/7/31 10:17
 **/
@Component
public class HelloServiceFallback implements HelloService {

    @Override
    public UserInfoEntity getUserInfo() {
        return null;
    }

    @Override
    public String sayHelloByName(String name) {
        return "系统繁忙";
    }

    @Override
    public String sayHello1(String name) {
        return "系统繁忙";
    }

    @Override
    public String sayHello2(UserInfoEntity user) {
        return "系统繁忙";
    }
}
           

测试

在不启动提供者provider-user的情况下,在不配置feign.hystrix.enable情况下(默认为false),访问sayHelloByName会返回500的错误。

Spring Cloud(F版)断路器Hystrix

修改配置feign.hystrix.enable = true,再重新访问sayHelloByName,就能看到友好的提示了。

feign:
  hystrix:
    enabled: true
           
Spring Cloud(F版)断路器Hystrix

继续阅读