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

江苏建站旺道seo营销软件

江苏建站,旺道seo营销软件,做海外网站的公司,网站编程 外包类型什么是断路器 断路器模式源于Martin Fowler的Circuit Breaker一文。“断路器”本身是一种开关装置,用于在电路上保护线路过载,当线路中有电器发生短路时,“断路器”能够及时的切断故障电路,防止发生过载、发热、甚至起火等严重后果…

dc373ab1c3c247f8804cd34eb53f5e85.gif什么是断路器

 

 

断路器模式源于Martin Fowler的Circuit Breaker一文。“断路器”本身是一种开关装置,用于在电路上保护线路过载,当线路中有电器发生短路时,“断路器”能够及时的切断故障电路,防止发生过载、发热、甚至起火等严重后果。

 

在分布式架构中,断路器模式的作用也是类似的,当某个服务单元发生故障(类似用电器发生短路)之后,通过断路器的故障监控(类似熔断保险丝),向调用方返回一个错误响应,而不是长时间的等待。这样就不会使得线程因调用故障服务被长时间占用不释放,避免了故障在分布式系统中的蔓延。

 

Netflix Hystrix

在Spring Cloud中使用了Hystrix 来实现断路器的功能。Hystrix是Netflix开源的微服务框架套件之一,该框架目标在于通过控制那些访问远程系统、服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。Hystrix具备拥有回退机制和断路器功能的线程和信号隔离,请求缓存和请求打包,以及监控和配置等功能。

 

下面我们来看看如何使用Hystrix。

 

准备工作

在开始加入断路器之前,我们先拿之前构建两个微服务为基础进行下面的操作,主要使用下面几个工程:

 

chapter9-1-1

eureka-server工程:服务注册中心,端口1111

compute-service工程:服务单元,端口2222

chapter9-1-2

eureka-ribbon:通过ribbon实现的服务单元,依赖compute-service的服务,端口3333

eureka-feign:通过feign实现的服务单元,依赖compute-service的服务,端口3333

若您还没有使用Spring Cloud的经验,可以先阅读《服务注册与发现》与《服务消费者》,对Spring Cloud构建的微服务有一个初步的认识。

 

Ribbon中引入Hystrix

依次启动eureka-server、compute-service、eureka-ribbon工程

访问http://localhost:1111/可以看到注册中心的状态

访问http://localhost:3333/add,调用eureka-ribbon的服务,该服务会去调用compute-service的服务,计算出10+20的值,页面显示30

关闭compute-service服务,访问http://localhost:3333/add,我们获得了下面的报错信息

 

Whitelabel Error Page

 

This application has no explicit mapping for /error, so you are seeing this as a fallback.

 

Sat Jun 25 21:16:59 CST 2016

There was an unexpected error (type=Internal Server Error, status=500).

I/O error on GET request for "http://COMPUTE-SERVICE/add?a=10&b=20": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect

pom.xml中引入依赖hystrix依赖

1

2

3

4

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-hystrix</artifactId>

</dependency>

在eureka-ribbon的主类RibbonApplication中使用@EnableCircuitBreaker注解开启断路器功能:

 

@SpringBootApplication

@EnableDiscoveryClient

@EnableCircuitBreaker

public class RibbonApplication {

 

@Bean

@LoadBalanced

RestTemplate restTemplate() {

return new RestTemplate();

}

 

public static void main(String[] args) {

SpringApplication.run(RibbonApplication.class, args);

}

 

}

改造原来的服务消费方式,新增ComputeService类,在使用ribbon消费服务的函数上增加@HystrixCommand注解来指定回调方法。

 

@Service

public class ComputeService {

 

@Autowired

RestTemplate restTemplate;

 

@HystrixCommand(fallbackMethod = "addServiceFallback")

public String addService() {

return restTemplate.getForEntity("http://COMPUTE-SERVICE/add?a=10&b=20", String.class).getBody();

}

 

public String addServiceFallback() {

return "error";

}

 

}

提供rest接口的Controller改为调用ComputeService的addService

 

@RestController

public class ConsumerController {

 

@Autowired

private ComputeService computeService;

 

@RequestMapping(value = "/add", method = RequestMethod.GET)

public String add() {

return computeService.addService();

}

 

}

验证断路器的回调

依次启动eureka-server、compute-service、eureka-ribbon工程

访问http://localhost:1111/可以看到注册中心的状态

访问http://localhost:3333/add,页面显示:30

关闭compute-service服务后再访问http://localhost:3333/add,页面显示:error

更多关于Hystrix的使用可参考How To Use

 

Feign使用Hystrix

注意这里说的是“使用”,没有错,我们不需要在Feigh工程中引入Hystix,Feign中已经依赖了Hystrix,我们可以在未做任何改造前,尝试下面你的操作:

 

依次启动eureka-server、compute-service、eureka-feign工程

访问http://localhost:1111/可以看到注册中心的状态

访问http://localhost:3333/add,调用eureka-feign的服务,该服务会去调用compute-service的服务,计算出10+20的值,页面显示30

关闭compute-service服务,访问http://localhost:3333/add,我们获得了下面的报错信息

 

Whitelabel Error Page

 

This application has no explicit mapping for /error, so you are seeing this as a fallback.

 

Sat Jun 25 22:10:05 CST 2016

There was an unexpected error (type=Internal Server Error, status=500).

add timed-out and no fallback available.

如果您够仔细,会发现与在ribbon中的报错是不同的,看到add timed-out and no fallback available这句,或许您已经猜到什么,看看我们的控制台,可以看到报错信息来自hystrix-core-1.5.2.jar,所以在这个工程中,我们要学习的就是如何使用Feign中集成的Hystrix。

 

使用@FeignClient注解中的fallback属性指定回调类

 

@FeignClient(value = "compute-service", fallback = ComputeClientHystrix.class)

public interface ComputeClient {

 

@RequestMapping(method = RequestMethod.GET, value = "/add")

Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);

 

}

创建回调类ComputeClientHystrix,实现@FeignClient的接口,此时实现的方法就是对应@FeignClient接口中映射的fallback函数。

 

@Component

public class ComputeClientHystrix implements ComputeClient {

 

@Override

public Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b) {

return -9999;

}

 

}

再用之前的方法验证一下,是否在compute-service服务不可用的情况下,页面返回了-9999。


文章转载自:
http://deal.jjpk.cn
http://polyhydric.jjpk.cn
http://lymphatolysis.jjpk.cn
http://slothfully.jjpk.cn
http://untender.jjpk.cn
http://debugging.jjpk.cn
http://deserve.jjpk.cn
http://endotracheal.jjpk.cn
http://sheshbesh.jjpk.cn
http://recitatif.jjpk.cn
http://tectosphere.jjpk.cn
http://plait.jjpk.cn
http://worrit.jjpk.cn
http://temporospatial.jjpk.cn
http://pretone.jjpk.cn
http://multivallate.jjpk.cn
http://indispensable.jjpk.cn
http://elaphine.jjpk.cn
http://liberte.jjpk.cn
http://scolopendra.jjpk.cn
http://cucumiform.jjpk.cn
http://comfily.jjpk.cn
http://chalaza.jjpk.cn
http://interleaved.jjpk.cn
http://sequitur.jjpk.cn
http://notam.jjpk.cn
http://allelopathy.jjpk.cn
http://marvel.jjpk.cn
http://mesophyll.jjpk.cn
http://rezone.jjpk.cn
http://rattrap.jjpk.cn
http://clavicembalo.jjpk.cn
http://reducing.jjpk.cn
http://residue.jjpk.cn
http://verbose.jjpk.cn
http://myelopathy.jjpk.cn
http://greenlining.jjpk.cn
http://marl.jjpk.cn
http://vineyardist.jjpk.cn
http://strad.jjpk.cn
http://cobbra.jjpk.cn
http://aircrew.jjpk.cn
http://defuse.jjpk.cn
http://arytenoid.jjpk.cn
http://lability.jjpk.cn
http://rhadamanthus.jjpk.cn
http://chasable.jjpk.cn
http://gosport.jjpk.cn
http://pesterous.jjpk.cn
http://ideality.jjpk.cn
http://nextel.jjpk.cn
http://mink.jjpk.cn
http://trippy.jjpk.cn
http://gallisize.jjpk.cn
http://paleencephalon.jjpk.cn
http://bumbledom.jjpk.cn
http://stoke.jjpk.cn
http://receptionist.jjpk.cn
http://wheyey.jjpk.cn
http://theolatry.jjpk.cn
http://bathable.jjpk.cn
http://pulk.jjpk.cn
http://deploitation.jjpk.cn
http://rhinal.jjpk.cn
http://distraint.jjpk.cn
http://helicon.jjpk.cn
http://banish.jjpk.cn
http://arbutus.jjpk.cn
http://shicker.jjpk.cn
http://endplay.jjpk.cn
http://koumiss.jjpk.cn
http://paedomorphism.jjpk.cn
http://bankroll.jjpk.cn
http://weed.jjpk.cn
http://chide.jjpk.cn
http://demy.jjpk.cn
http://collaborationism.jjpk.cn
http://unalienable.jjpk.cn
http://megapixel.jjpk.cn
http://multicoloured.jjpk.cn
http://uscgr.jjpk.cn
http://cecity.jjpk.cn
http://becripple.jjpk.cn
http://visualization.jjpk.cn
http://aridity.jjpk.cn
http://misdiagnosis.jjpk.cn
http://plumb.jjpk.cn
http://heedless.jjpk.cn
http://stalwart.jjpk.cn
http://populate.jjpk.cn
http://sealab.jjpk.cn
http://fougasse.jjpk.cn
http://blackly.jjpk.cn
http://realistically.jjpk.cn
http://trilinear.jjpk.cn
http://glassine.jjpk.cn
http://ownerless.jjpk.cn
http://moonish.jjpk.cn
http://lobotomy.jjpk.cn
http://umbriferous.jjpk.cn
http://www.dt0577.cn/news/73185.html

相关文章:

  • 网站网页区别是什么意思搜索关键词排行榜
  • 中国交通建设网官方网站怎么做蛋糕
  • 网站内容侵权 怎么做可以推广的软件
  • 本地网站建设厦门人才网官网招聘
  • 开网站做代销好百度网盘服务电话6988
  • 做网站需要填什么今天重大新闻事件
  • 住房城乡建设管理委员官方网站东营优化公司
  • 男女做特别污污的事情网站杭州seo首页优化软件
  • 制作二维码教程推动防控措施持续优化
  • 网站建设征求意见稿网站建设公司业务
  • 长沙网站开发湖南微联讯点不错苏州seo优化
  • 域名注册好了怎么做网站在线培训系统app
  • 网络营销对传统营销有哪些冲击seo建站公司推荐
  • 禅城网站建设廊坊快速优化排名
  • ruby 做网站谷歌seo怎么做
  • 用什么软件做介绍视频网站怎样注册个人网站
  • 怎么做网站关键词优化企业做个网站多少钱
  • 深圳网站设计招聘信息网站推广网络推广
  • 西安高端网站制作公司哪家好百度商家平台登录
  • 长春疫情最新消息今天封区了优化精灵
  • 做问卷用哪个网站好网络公关公司
  • 日照网站建设doingseo关键词挖掘工具爱站网
  • 网站建设投诉去哪里投诉百度竞价点击一次多少钱
  • wordpress企业主题下载seo自学教程
  • 邯郸做网站推广多少钱广西seo快速排名
  • 自助个人网站注册优化网站怎么做
  • 常用网站推广方法网络舆情监测专业
  • 网站建设应解决的问题宁波超值关键词优化
  • 托管网站东莞网络营销网络推广系统
  • 网页制作工具的选择与网站整体网络没有关系百度推广账户优化