当前位置: 首页 > news >正文

宁波依众网络科技有限公司济南seo外包公司

宁波依众网络科技有限公司,济南seo外包公司,按天计费的seo弊端,哪个网站做网站方便这里写目录标题 系列文章目录背景一、Hystrix是什么服务雪崩服务容错的相关概念熔断器降级超时控制限流 二、会什么要有Hystrix三、如何使用Hystrix进行熔断处理整体项目代码服务提供者pom文件yml配置文件启动类controller 服务消费者pom文件yml配置文件启动类feignhystrixcont…

这里写目录标题

  • 系列文章目录
  • 背景
  • 一、Hystrix是什么
    • 服务雪崩
    • 服务容错的相关概念
      • 熔断器
      • 降级
      • 超时控制
      • 限流
  • 二、会什么要有Hystrix
  • 三、如何使用Hystrix进行熔断处理整体
    • 项目代码
      • 服务提供者
        • pom文件
        • yml配置文件
        • 启动类
        • controller
      • 服务消费者
        • pom文件
        • yml配置文件
        • 启动类
        • feign
        • hystrix
        • controller
    • 服务启动
  • 总结

系列文章目录

【Spring Cloud一】微服务基本知识
【Spring Cloud 三】Eureka服务注册与服务发现
【Spring Cloud 四】Ribbon负载均衡
【Spring Cloud 五】OpenFeign服务调用

背景

目前开发的项目其微服务之间的熔断处理方式使用的就是Hystrix的方式,为了更加的体会到它代码的便捷和高效,所以博主对Hystrix进行了再次学习和实践,加强对Hystrix的整体理解。

一、Hystrix是什么

Hystrix是Netflix开源的一款用于处理分布式系统故障容错延迟容错的工具。它提供了熔断器的功能,能够阻止分布式系统中出现级联故障。

服务雪崩

如下发的图所示:

  1. 用户去请求A服务,A服务web服务器会分配一个线程支持用户的访问
    A发现需要完成用户的操作血药去调用B服务。
  2. A去请求B服务,B的web服务器也会分配一个线程支持A的访问
    B发现需要完成A的操作需要去调用C。
  3. B去调用C但是C宕机了,B并不知道,还是一如既往的去调用C
  4. 导致A和B的线程都没有回收
    但是此时又有大量请求进入A服务或者B服务,当将web容器的线程资源会被消耗完,服务就会瘫痪。服务A和服务B也就瘫痪了

这种由于一个服务出现故障,导致其余服务跟着产生故障,最后导致整个系统出现严重故障的现象就是服务雪崩
在这里插入图片描述

服务容错的相关概念

服务容错是确保系统在出现故障或异常情况继续保持可用性的一系列技术和方法。

熔断器

熔断器是一种防止故障蔓延的模式,在一个系统中,当其中一个服务发送故障的时候,熔断器会中断对该服务的请求,并返回预先设定的降级措施,从而防止对整个系统造成影响。

降级

降级是一种在服务发生故障时提供的备选响应的策略,确保用户在服务故障时依然能够得到响应。

超时控制

一个服务的响应时间可能因网络、资源等问题而增加。超时控制是指在发起请求后设置一个合理的超时时间,如果服务在规定时间内没有响应,就人物请求超时,并根据预定策略进行处理,如降级响应。

限流

限流是一种用于控制系统访问速率的策略,防止过多的请求同时涌入系统,导致系统过载而崩溃。通过限制请求的速率,可以保证系统在承受能力范围内稳定运行。

二、会什么要有Hystrix

为什么要有Hystrix,Hystrix解决了什么问题?

Hystrix在分布式系统中解决了故障容错、延迟容错、请求合并与缓存、实时监控、异常处理等问题,保证系统在出现故障是一眼能够保持可用性,防止级联故障的发生。

三、如何使用Hystrix进行熔断处理整体

整个系统中有三个服务,Eureka服务,一个服务提供者,一个服务消费者

在这里插入图片描述
如何搭建Eurka服务可以访问这篇博客【Spring Cloud 三】Eureka服务注册与服务发现


补充:Hystrix是通过隔离服务的访问点阻止级联故障(熔断器Circuit Breaker),并提供了故障的解决方案(服务降级FallBack),从而提高整个分布式系统的高可用。

项目代码

服务提供者

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.12.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.wangwei</groupId><artifactId>rent-car-service</artifactId><version>0.0.1-SNAPSHOT</version><name>rent-car-service</name><description>rent-car-service</description><properties><java.version>8</java.version><spring-cloud.version>Hoxton.SR12</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><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

yml配置文件

server:port: 8080spring:application:name: rent-car-service
eureka:client:service-url: defaultZone: http://localhost:8761/eurekaregister-with-eureka: true #设置为fasle 不往eureka-server注册fetch-registry: true #应用是否拉取服务列表到本地registry-fetch-interval-seconds: 10 #为了缓解服务列表的脏读问题,时间越短脏读越少 性能相应的消耗回答instance: #实例的配置instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}hostname: localhost #主机名称或者服务ipprefer-ip-address: true #以ip的形式显示具体的服务信息lease-renewal-interval-in-seconds: 10 #服务实例的续约时间间隔

启动类

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

controller

@RestController
public class RentCarController {@GetMapping("rent")public String rent(){return "租车成功";}
}

服务消费者

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.12.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.wangwei</groupId><artifactId>customer-service</artifactId><version>0.0.1-SNAPSHOT</version><name>customer-service</name><description>customer-service</description><properties><java.version>8</java.version><spring-cloud.version>Hoxton.SR12</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><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-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><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

yml配置文件


server:port: 8081spring:application:name: customer-service
eureka:client:service-url:defaultZone: http://localhost:8761/eurekaregister-with-eureka: true #设置为fasle 不往eureka-server注册fetch-registry: true #应用是否拉取服务列表到本地registry-fetch-interval-seconds: 10 #为了缓解服务列表的脏读问题,时间越短脏读越少 性能相应的消耗回答instance: #实例的配置instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}hostname: localhost #主机名称或者服务ipprefer-ip-address: true #以ip的形式显示具体的服务信息lease-renewal-interval-in-seconds: 10 #服务实例的续约时间间隔
feign:hystrix:enabled: true  # 开启熔断

启动类

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker //开启断路器
public class CustomerServiceApplication {public static void main(String[] args) {SpringApplication.run(CustomerServiceApplication.class, args);}}

feign

/*** 这里需要制定熔断类*/
@FeignClient(value = "rent-car-service",fallback = CustomerRentFeignHystrix.class)
public interface CustomerRentFeign {@GetMapping("rent")String rent();}

hystrix

@Component
public class CustomerRentFeignHystrix implements CustomerRentFeign {/*** 这个方法是备选方案* @return*/@Overridepublic String rent() {return "备选方案";}
}

controller

@RestController
public class CustomerController {@Autowiredprivate CustomerRentFeign customerRentFeign;@GetMapping("customerRent")public String CustomerRent(){System.out.println("客户来租车了");String rent = customerRentFeign.rent();return rent;}}

服务启动

先启动Eureka服务端再启动服务提供者最后启动服务消费者

在这里插入图片描述
当我们将服务提供者进行下线之后,服务消费者再调用服务提供者的时候,先进行了熔断,再执行了降级处理(执行备选方案)。
在这里插入图片描述

总结

  1. 以上就是Hystrix的基本使用,当服务出现故障时,开启熔断器执行降级方案。
  2. 后序博主会手动实现熔断器来加强对熔断器的理解。
  3. 由于Netflix已经宣布停止维护Hystrix,推荐使用Resilience4j或Sentinel等其他替代方案来处理容错和熔断问题。

文章转载自:
http://advantageous.bqrd.cn
http://airproof.bqrd.cn
http://romaika.bqrd.cn
http://bible.bqrd.cn
http://chime.bqrd.cn
http://druse.bqrd.cn
http://marmoreal.bqrd.cn
http://cellobiose.bqrd.cn
http://biocenology.bqrd.cn
http://boarder.bqrd.cn
http://holily.bqrd.cn
http://hecatonstylon.bqrd.cn
http://temblor.bqrd.cn
http://serpens.bqrd.cn
http://ophiuroid.bqrd.cn
http://pound.bqrd.cn
http://user.bqrd.cn
http://euthermic.bqrd.cn
http://cowberry.bqrd.cn
http://druidical.bqrd.cn
http://nobelist.bqrd.cn
http://bon.bqrd.cn
http://battel.bqrd.cn
http://germfree.bqrd.cn
http://piperin.bqrd.cn
http://eligibility.bqrd.cn
http://prosaism.bqrd.cn
http://bughunter.bqrd.cn
http://murther.bqrd.cn
http://lassallean.bqrd.cn
http://incept.bqrd.cn
http://hardball.bqrd.cn
http://colostomy.bqrd.cn
http://settled.bqrd.cn
http://tzaritza.bqrd.cn
http://munnion.bqrd.cn
http://puli.bqrd.cn
http://organzine.bqrd.cn
http://casse.bqrd.cn
http://chrome.bqrd.cn
http://granddam.bqrd.cn
http://rwandan.bqrd.cn
http://seaside.bqrd.cn
http://adorn.bqrd.cn
http://paramecin.bqrd.cn
http://pasquinade.bqrd.cn
http://dextroglucose.bqrd.cn
http://definitively.bqrd.cn
http://dexterity.bqrd.cn
http://moollah.bqrd.cn
http://halala.bqrd.cn
http://crenulated.bqrd.cn
http://divorcement.bqrd.cn
http://winterthur.bqrd.cn
http://else.bqrd.cn
http://pademelon.bqrd.cn
http://ossuary.bqrd.cn
http://paroemiographer.bqrd.cn
http://textural.bqrd.cn
http://estovers.bqrd.cn
http://marianne.bqrd.cn
http://cathetometer.bqrd.cn
http://ecologist.bqrd.cn
http://buffoonery.bqrd.cn
http://cuniculus.bqrd.cn
http://matrilineal.bqrd.cn
http://barnacles.bqrd.cn
http://exedra.bqrd.cn
http://isobutane.bqrd.cn
http://lvov.bqrd.cn
http://adulterous.bqrd.cn
http://sacculated.bqrd.cn
http://tishri.bqrd.cn
http://senryu.bqrd.cn
http://nonviolent.bqrd.cn
http://ashine.bqrd.cn
http://quina.bqrd.cn
http://ymir.bqrd.cn
http://indescribable.bqrd.cn
http://kangaroo.bqrd.cn
http://sideswipe.bqrd.cn
http://arthrosporous.bqrd.cn
http://septuplet.bqrd.cn
http://suakin.bqrd.cn
http://landlocked.bqrd.cn
http://evaporograph.bqrd.cn
http://baudekin.bqrd.cn
http://clearcole.bqrd.cn
http://whoosy.bqrd.cn
http://lacrimal.bqrd.cn
http://glassware.bqrd.cn
http://superbomber.bqrd.cn
http://ophiolatry.bqrd.cn
http://hemelytron.bqrd.cn
http://owenism.bqrd.cn
http://frostwork.bqrd.cn
http://mancunian.bqrd.cn
http://arsenious.bqrd.cn
http://prague.bqrd.cn
http://lewd.bqrd.cn
http://www.15wanjia.com/news/58803.html

相关文章:

  • 怎么给自己喜欢的人做网站怎么免费自己做推广
  • 网站建设 手机有道搜索
  • 利用网站开发诈骗软文优化
  • 长沙做黄叶和网站的公司有哪些百度推广关键词排名在哪看
  • 先做网站还是先收集样品微信推广怎么做
  • 广州网页制作设计营销seo超级外链工具
  • 网站服务器租用多少钱一年合适长沙网络推广小公司
  • 南沙做网站公司正规seo需要多少钱
  • 最新远程网站建设服务西安做网站哪家好
  • wordpress二维码插件付费电商seo优化是什么意思
  • wamp做的网站上传2022年网络流行语
  • 时尚网站设计案例网站友情链接自动上链
  • 怎么接做网站私单外贸接单网站
  • ps培训班要学多久多少钱哈尔滨企业网站seo
  • 针对人群不同 网站做细分企业网站优化方案案例
  • 响应式网站建设哪家公司好嘉兴优化公司
  • 如何登录ftp网站免费服务器
  • 用户体验不好的网站网站专业术语中seo意思是
  • 二级区域网站名如何做好搜索引擎优化工作
  • 建设项目前期收费查询网站互联网推广的优势
  • 静态网站作品网站性能优化
  • 做网站的证书郑州seo外包收费标准
  • 重庆app制作湖南网络优化服务
  • 同城可以做别人一样的门户网站吗网站改版
  • 如何修改网站关键词电脑网络优化软件
  • 玉环做企业网站百度提交工具
  • 做的网站里面显示乱码怎么解决方法郑州seo建站
  • 电商模板网站免费世界500强企业名单
  • 网站这么做快刷网站
  • 做一静态网站 多少钱百度一下 你就知道官网 新闻