`
huangyongxing310
  • 浏览: 491954 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

Hystrix 断路器

 
阅读更多
Hystrix 断路器

由于网络原因或者自身的原因,服务并不能保证服务的100%可用,如果单个服务出现问题,调用这个服务就会出现网络延迟,此时若有大量的网络涌入,会形成任务累计,导致服务瘫痪,甚至导致服务“雪崩”。
为了解决这个问题,就出现断路器模型。
当对特定的服务的调用达到一个阀值(hystric 是5秒20次) 断路器将会被打开。
就是当服务不可用时就会返回断路器设定的值,防止长时间等待和重试。

当断路器打开后,超过一定时间后又会重新允许请求尝试访问(默认为5s)。


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



ribbon方式:
@SpringBootApplication //spring boot 开启应用
//@EnableDiscoveryClient //Discovery Service”有多种实现,比如:eureka, consul, zookeeper。
@EnableEurekaClient //只能为eureka作用
@EnableHystrix//断路器
public class Application {

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

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


package com.Service;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

//ribbon远程调用服务
@Service("callService")
public class CallService {
	final static Logger logger = LogManager.getLogger(CallService.class);

	@Autowired
    RestTemplate restTemplate;

	@HystrixCommand(fallbackMethod = "callServiceError")
	public String callService(String name) {
		//这里只是把IP和端口号变成了服务的名字(通过HTTP进行远程服务调用的)
        return restTemplate.getForObject("http://eureka-Client/eurekaClient/Test/test",String.class);
    }
	
	public String callServiceError(String name) {
        return "hi,"+name+",sorry,error!";
    }
}



feign方式:(feign是自带断路器的)

server.port=8765
#logging.pattern.level=INFO

#服务器路径
server.context-path=/feign


#
#eureka.instance.hostname=localHost
#表示是否注册自身到eureka服务器,因为当前这个应用就是eureka服务器,没必要注册自身,所以这里是false。
#eureka.client.registerWithEureka=false
#fetchRegistry表示是否从eureka服务器获取注册信息,同上,这里不需要
#eureka.client.fetchRegistry=false
#设置eureka服务器所在的地址,查询服务和注册服务都需要依赖这个地址。
#eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/


eureka.client.serviceUrl.defaultZone=http\://localhost\:8761/eurekaServer/eureka/

spring.application.name=feign


#自定义断路器(网上有说falser的,但实测应该使用true)
feign.hystrix.enabled=true



package com.Service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(value = "eureka-Client",fallback = CallServiceError.class) //服务名字
public interface CallService {
    @RequestMapping(value = "/eurekaClient/Test/test",method = RequestMethod.GET)
    String callService();
}


package com.Service;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Component
public class CallServiceError implements CallService{

	@Override
	public String callService() {
		return "sorry Error!";
	}
	
}



勘误:有人反映feign的熔断器不起作用,springcloud版本的问题,用这个:
<dependencyManagement>
   <dependencies>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-dependencies</artifactId>
         <version>Camden.SR6</version>
         <type>pom</type>
         <scope>import</scope>
      </dependency>
   </dependencies>
</dependencyManagement>






监控平台搭建Hystrix-dashboard
使用Turbine组件做集群数据汇总
https://www.cnblogs.com/yepei/p/7169127.html(Hystrix使用详解)
https://www.cnblogs.com/leeSmall/p/8847652.html(Hystrix 熔断机制(Hystrix基本配置、服务降级、HystrixDashboard服务监控、Turbine聚合监控))









https://blog.csdn.net/hry2015/article/details/78577695?utm_medium=referral(相关参数注解配置)
  • 大小: 87.8 KB
分享到:
评论

相关推荐

    Hystrix断路器.md

    断路器

    25-Spring Cloud断路器Hystrix1

    Spring Cloud 提供了断路器模式的实现,名为 Hystrix,用于防止服务间的级联故障,提高系统的容错性。断路器模式的核心思想是在调用远程服务时,通过一个中间层(即断路器)来监控调用的健康状况。当服务出现故障时...

    Spring Cloud Netfix Hystrix断路器例子

    Spring Cloud Netfix Hystrix断路器例子工程。使用Spring Cloud Netflix Hystrix以及Spring RestTemplate或Spring Cloud Netflix Feign实现断路器模式。

    spring cloud Hystrix断路器的使用(熔断器)

    "spring cloud Hystrix断路器的使用" spring cloud Hystrix断路器是Netflix开发的一种断路器模式,用于防止级联故障在微服务架构中。断路器的使用可以防止服务之间的级联故障,使得系统更加稳定和可靠。 Hystrix...

    spingcloud整合eureka、 Hystrix断路器的降级.zip

    “断路器”本身是一种开关装置,当某个服务单元发生故障监控(类似熔断保险丝),向调用方法返回一个符合预期的、可处理的备选响应(FallBack),而不是长时间的等待或者抛出调用方法无法处理的异常,这样就保证了服务...

    详解Spring Cloud Hystrix断路器实现容错和降级

    Spring Cloud Hystrix断路器实现容错和降级 Spring Cloud Hystrix断路器是一种实现容错和降级的机制,它可以在服务不可用时,对配置了断路器的方法实行降级策略,临时调用备用方法。这种机制可以避免服务雪崩效应,...

    spring cloud微服务框架demo完整可用2版(注册中心+生产者+消费者+feign负载均衡+hystrix断路器+仪表盘+gate路由网关+配置中心)

    spring cloud微服务框架demo完整可用2版 比第一版多集成了mybatis...(注册中心+生产者+消费者+feign负载均衡+hystrix断路器+仪表盘+gate路由网关+config配置中心+mybatis+oracle+mybatisPlus generator代码自动生成)

    spingcloud整合eureka、 Hystrix断路器的降级、熔断、服务监控图形.zip

    Hystrix是一个库,通过添加延迟容忍和容错逻辑,帮助你控制这些分布式服务之间的交互。Hystrix通过隔离服务之间的访问点、停止级联失败和提供回退选项来实现这一点,所有这些都可以提高系统的整体弹性。复杂分布式...

    SpringCloud——断路器(Hystrix)

    在分布式系统中,Spring Cloud Hystrix 是一个关键的组件,它作为一个断路器来防止服务雪崩。断路器模式是微服务架构中的一个重要概念,用于提高系统的容错性和稳定性。下面我们将深入探讨 Spring Cloud Hystrix 的...

    camel-hystrix-endpoint:将子端点包装到同步hystrix断路器中的骆驼端点

    骆驼hystrix端点 将子端点包装到同步hystrix断路器中的骆驼端点。 该项目可将hystrix组件轻松集成到骆驼路线中。 一个非常简单的示例可能如下所示:private class TestRoute extends RouteBuilder {@Overridepublic ...

    详解SpringCloud微服务架构之Hystrix断路器

    通过以上步骤,我们可以构建一个利用Hystrix断路器的Feign客户端,实现对故障服务的智能处理,提高系统的整体稳定性和可靠性。 总结来说,Spring Cloud Hystrix是一个强大的工具,它通过断路器模式有效地解决了...

    【微服务架构】SpringCloud之断路器(hystrix)

    Feign使得编写消费其他服务的客户端变得更简单,同时也内置了Hystrix断路器,以确保高可用性。 总之,Spring Cloud Hystrix作为微服务架构中的关键组件,通过断路器机制提高了系统的稳定性和容错性。结合Eureka ...

    断路器hystrix实现.rar

    Hystrix是Netflix开源的一款强大的断路器库,它适用于Java环境,并且广泛应用于Spring Cloud框架中。本教程将深入探讨如何使用Hystrix在微服务中实现断路器功能。 首先,让我们理解断路器的工作原理。断路器在正常...

    SpringCloud中的断路器(Hystrix)和断路器监控(Dashboard)

    本篇主要介绍的是SpringCloud中的断路器(Hystrix)和断路器指标看板(Dashboard)的相关使用知识,需要的朋友可以参考下

    springcloud hystrix 断路由

    在本文中,我们将深入探讨“Spring Cloud Hystrix 断路器”的概念及其在实际应用中的工作原理。 首先,我们来理解什么是断路器模式。断路器模式是软件设计模式的一种,用于在系统中引入故障保护机制。当服务出现...

    hystrix.zip

    3. `eureka-consumer-feign-hystrix`:这是一个服务消费者,它使用Feign客户端和Hystrix断路器来调用`eureka-producer`提供的服务。Feign是一个声明式Web服务客户端,使得编写Web服务客户端变得简单。而Hystrix则是...

Global site tag (gtag.js) - Google Analytics