跟我学---四、断路器 Hystrix

Hystrix简介
Netflix Hystrix是SOA/微服务架构中提供服务隔离、熔断、降级机制的工具/框架。Netflix Hystrix是断路器的一种实现,用于高微服务架构的可用性,是防止服务出现雪崩的利器。

Hystrix具备能力:

  • 在通过网络依赖服务出现高延迟或者失败时,为系统提供保护和控制
  • 在复杂的分布式系统中防止级联失败
  • 快速失败(Fail fast)同时能快速恢复
  • 提供失败回退(Fallback)和相对优雅的服务降级机制
  • 提供实时的监控、报警和运维控制手段

Hystrix 如何解决级联故障/防止服务雪崩

  • 将请求的逻辑进行封装,相关逻辑会在独立的线程中执行
  • 有自动超时策略,如果外部请求超过阈值,Hystrix会以超时来处理
  • 会为每个依赖维护一个线程池,当线程满载,不会进行线程排队,会直接终止操作
  • 有熔断机制: 在依赖服务失效比例超过阈值时,手动或者自动地切断服务一段时间

1.搭建Eureka 服务注册中心
在idea创建SpringBoot 项目,主要依赖如下:

<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

在启动类中添加注释@EnableEurekaServer,代码如下所示:

//会为项目自动配置必须得配置类,标识该服务为注册中心
@EnableEurekaServer
@SpringBootApplication
public class EurakeServerApplication {

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

}

在application.yml 配置文件中添加一下配置,配置注册中心得端口和标识:

server:
  port: 8761
eureka:
  instance:
    hostname: standalone
    instance-id: ${spring.application.name}:${vcap.application.instance-id:${spring.application.instance-id:${random.value}}}
  client:
    register-with-eureka: false #表明该服务不会向Eureka Server注册自己得信息
    fetch-registry: false  #表明该服务不会向Eureka Server获取注册信息
    service-url:    #Eureka Server注册中心得地址,用于Client与Server交流
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

spring:
  application:
    name: eureka-service

2.搭建Euraka服务提供者
在idea创建SpringBoot 项目,主要依赖如下

<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

启动类如下:

@SpringBootApplication
public class EurakeClient01Application {

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

}

application.yml 配置如下:

server:
  port: 8762
eureka:
  instance:
    hostname: client
    instance-id: ${spring.application.name}:${vcap.application.instance-id:${spring.application.instance-id:${random.value}}}
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/  #服务注册中心访问地址

spring:
  application:
    name: hystrix-service

新建一个controller包,添加一个提供服务的接口,代码如下:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/*
 * @Author chencundeng
 * @Description //TODO
 * @Date 2019/2/23
 **/
@RequestMapping("/feign-service")
@RestController
public class FeignServiceController {

    @GetMapping("/say-hello/{name}")
    public String sayHello(@PathVariable("name") String name){
        return "hello ".concat(name).concat("!");
    }

}

3.服务消费者搭建
在idea创建SpringBoot 项目,主要依赖如下

dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

启动类如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker  //开启Hystrix
public class HystrixClientApplication {

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

}

OpenFeign 是自带Hystrix,但是默认没有打开,需要添加开启配置 , application.yml 配置如下:

server:
  port: 8763
eureka:
  instance:
    hostname: client
    instance-id: ${spring.application.name}:${vcap.application.instance-id:${spring.application.instance-id:${random.value}}}
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/  #服务注册中心地址

feign:    
  hystrix:    //打开hystrix配置
    enabled: true

spring:
  application:
    name: hystrix-client

新建一个controller包,添加一个服务消费类,代码如下:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/*
 * @Author chencundeng
 * @Description //TODO
 * @Date 2019/2/24
 **/
@RestController
public class HystrixController {

    @Autowired
    private FeignServiceClient feignServiceClient;

    @GetMapping("/say-hello/{name}")
    public String sayHello(@PathVariable("name") String name){
        return feignServiceClient.sayHello(name);
    }

}

新建Feign 接口类,通过fallback指定HystrixService为失败回滚类,代码如下:

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;


@FeignClient(value = "hystrix-service",fallback = HystrixService.class)
public interface FeignServiceClient {

    @GetMapping("/say-hello/{name}")
    public String sayHello(@PathVariable("name") String name);

}

新建hystrix失败回滚类,代码如下:

import org.springframework.stereotype.Component;


/*
 * @Author chencundeng
 * @Description //TODO
 * @Date 2019/2/24
 **/
@Component
public class HystrixService implements FeignServiceClient{

    @Override
    public String sayHello(String name) {
        return "service error";
    }
}

4.OpenFeign客户端调用
搭建好上述三个服务后,依次启动三个服务,输入地址http://127.0.0.1:8762/say-hello/feign就可以看到输出语句,断开服务提供者,重新服务看到输出 service error,表示hystrix失败回滚已生效。

5.例子源码地址如下:
https://gitee.com/mingzhishuyuan/spring-cloud.git

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值