美文网首页
Hystrix服务容错处理实践

Hystrix服务容错处理实践

作者: 扎你p屁 | 来源:发表于2019-01-08 23:54 被阅读0次

过程笔记:在微服务调用过程中,为了避免因个别服务提供者出错而出现的级联错误(上层服务消费者线程堵塞),采取hystrix工具进行容错处理,并借助hystrixdashboard工具监控服务消费者的运行状况。

服务提供者侧(user-service):

在某个controller接口中,加入大延时,模拟接口失效,并启动工程。

服务消费者侧(api-gateway):

1、加入hystrix和actuator依赖,前者提供hystrix工具库,后者提供url的访问接口。

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

2、approperties文件中加入以下配置。其中management.server.port表示服务消费者的actuator信息端口,原有的actuator信息端口就是工程的server.port,因为工程需要将actuator信息端口暴露给外部,所以原有方案会将工程端口暴露给外部,会遗留安全问题,所以才将工程端口与actuator信息端口分开;第二条配置表示,在actuator信息端口中暴露hystrix的监控信息。

management.server.port=8023
management.endpoints.web.exposure.include=hystrix.stream

3、继续在approperties文件中添加hystrix配置,这里不做详细说明,具体参数作用可以去hystrix的Github项目中查找,hystrix参数官方文档链接

hystrix.threadpool.default.coreSize=5
hystrix.threadpool.default.maxQueueSize=1
hystrix.threadpool.default.maximumSize=10
hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize=true

hystrix.command.default.circuitBreaker.errorThresholdPercentage=10
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=10000

3、在服务提供者加入时延的特定接口所对应的服务消费者方法所在repository类上添加注解,并在方法上添加注解,添加降级方法。

@Repository
@DefaultProperties(groupKey="userDao",
commandProperties={@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="2000")},
threadPoolProperties={@HystrixProperty(name="coreSize",value="10")
,@HystrixProperty(name="maxQueueSize",value="1000")},
threadPoolKey="userDao")
public class UserDao {
    public User getUserByTokenFb(String token){
        return new User();
    }
    @HystrixCommand(fallbackMethod="getUserByTokenFb")
    public User getUserByToken(String token) {}
}

4、服务消费者启动类添加注解

@EnableCircuitBreaker
public class ApiGatewayApplication {}

5、启动工程

dashboard侧(hystrix-dashboard):

1、添加hystrix、hystrix-dashboard、actuator依赖

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

2、approperties文件添加端口信息

server.port=9097

3、启动类加注解

@SpringBootApplication
@EnableHystrixDashboard
public class HystrixdashboardApplication {
    public static void main(String[] args) {
        SpringApplication.run(HystrixdashboardApplication.class, args);
    }
}

4、启动dashboard工程之后,访问http://127.0.0.1:9097/hystrix,之后输入监控地址
http://127.0.0.1:8023/actuator/hyxtrix.stream,即可看到监控内容。

相关文章

  • Hystrix服务容错处理实践

    过程笔记:在微服务调用过程中,为了避免因个别服务提供者出错而出现的级联错误(上层服务消费者线程堵塞),采取hyst...

  • Spring Cloud(四)服务容错保护

    服务容错保护 Spring Cloud Hystrix Spring Cloud Hystrix是基于Netfli...

  • Hystrix

    Hystrix Hystrix 是用于处理延迟和容错的开源库; Hystrix 主要用于避免级联故障,提高系统弹性...

  • Hystrix 服务容错处理:什么是Hystrix

    在微服务架构中存在多个可直接调用的服务,这些服务若在调用时出现故障会导致连锁效应,也就是可能会让整个系统得不可用,...

  • Hystrix监控的配置详解

    在微服务架构中,hystrix处理容错外,还有实时监控功能,在服务发生调用时,会将每秒请求数、成功请求数等运行指标...

  • Hystrix入门

    Hystrix能做什么? 1. 延时和容错:防止级联失败/服务优雅降级/快速失败,快速恢复/断路由/超时处理 2....

  • Hystrix——服务容错

    1 Hystrix 简介 在微服务架构中,微服务之间通过网络进行通信,存在相互依赖,当其中一个服务不可用时,有可能...

  • Hystrix仪表盘——Hystrix dashboard

    在之前的教程服务容错保护——Spring Cloud Hystrix中有说到,hystrix会监控所有托管在hys...

  • Spring Cloud构建微服务架构:服务容错保护(Hystr

    前言 在前两篇《Spring Cloud构建微服务架构:服务容错保护(Hystrix服务降级)》和《Spring ...

  • SpringCloud之Hystrix、Gateway、Conf

    1. SpringCloud Hystrix(服务熔断与降级组件 / 服务容错与保护组件) 示例(服务端降级) 示...

网友评论

      本文标题:Hystrix服务容错处理实践

      本文链接:https://www.haomeiwen.com/subject/sdfirqtx.html