原理

在微服务架构中,服务之间形成调用链路,链路中的任何一个服务提供者都可能面临着相应超时、宕机等不可用的情况,在高并发的情况下,这种情况会随着并发量的上升恶化,形成“雪崩效应”,而断路器hystrix正是用来解决这一个问题的组件。需要JAVA Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码 一零三八七七四六二六

断路器基本原理为:

正常情况下,断路器关闭,服务消费者正常请求微服务

一段事件内,失败率达到一定阈值(比如50%失败,或者失败了50次),断路器将断开,此时不再请求服务提供者,而是只是快速失败的方法(断路方法)

断路器打开一段时间,自动进入“半开”状态,此时,断路器可允许一个请求方法服务提供者,如果请求调用成功,则关闭断路器,否则继续保持断路器打开状态。

断路器hystrix是保证了局部发生的错误,不会扩展到整个系统,从而保证系统的即使出现局部问题也不会造成系统雪崩。

配置/使用

下面讲解在restTemplate和feign中断路器的配置和使用步骤

restTemplate+ribbon整合Hystrix

引入hystrix依赖

  <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId></dependency>
复制代码

虽然Eureka依赖了ribbon,ribbon依赖了hystrix-core,但还是要引用了上面的maven依赖,因为下面用到的@HystrixCommand注解用到了hystrix-javanica包

启动类加上@EnableCircuitBreaker注解(@EnableHystrix也可以)

修改HelloWorldController的入口请求方法

@GetMapping("/message")@HystrixCommand(fallbackMethod = "getMessageFallback")public HelloworldMessage getMessage() {HelloMessage hello = getMessageFromHelloService();WorldMessage world = getMessageFromWorldService();HelloworldMessage helloworld = new HelloworldMessage();helloworld.setHello(hello);helloworld.setWord(world);log.debug("Result helloworld message:{}", helloworld);return helloworld;}/*** 断路方法* @return*/public HelloworldMessage getMessageFallback(){HelloMessage helloMessage=new HelloMessage();helloMessage.setName("hello");helloMessage.setMessage("error occurs");WorldMessage worldMessage=new WorldMessage();worldMessage.setMessage("world error occurs");HelloworldMessage helloworldMessage=new HelloworldMessage();helloworldMessage.setHello(helloMessage);helloworldMessage.setWord(worldMessage);return helloworldMessage;}
复制代码

通过@HystrixCommand注解的fallbackMethod指向断路方法,该方法会在调用hello服务或者world服务失败时被调用。

@HystrixCommand 注解还可以配置超时事件等其他属性。

测试

1)依次启动eureka server:discovery/trace/hello/world/helloword项目

2)在浏览器输入地址http:\localhost:8020/message,则返回正确的结果

3)停止hello项目,再次输入上述地址,则执行断路器中的方法。

feign下整合Hystrix

feign禁用Hystrix

在Spring Cloud中,只要Hystrix在项目的classpath中,Feign就会用断路器包裹Feign客户端的所有方法,如果要禁用Hystrix则可以通过自定义feign的配置来解决。

@Configuration
public class FeignConfiguration{@Bean@Scope("prototype")public Feign Builder feignBuilder(){return Feign.builder();}
}
复制代码

要禁用Hystrix的接口引用该配置即可

@FeignClient(name="hello",configuration=FeignConfiguration.class)
public interface HelloService{......
}
复制代码

feign使用Hystrix

启用Hystrix

默认情况下feign已经整合了Hystrix,在配置文件中开启即可(本人用的的Dalston.SR2版本的Spring Cloud,需要在配置文件开启)

feign:hystrix:enabled: true
复制代码

接口指定回退类

在HelloService中修改FeignClient类,指定fallback的类

package com.example.helloworldfeign.service;import com.example.helloworldfeign.model.HelloMessage;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;/*** @author billjiang 475572229@qq.com* @create 17-8-23*/
@FeignClient(value="hello",fallback = HelloServiceFallback.class)
public interface HelloService {@GetMapping("/message")HelloMessage hello();}复制代码

实现了接口fallback的类的实现:

package com.example.helloworldfeign.service;import com.example.helloworldfeign.model.HelloMessage;
import org.springframework.stereotype.Component;/*** @author billjiang 475572229@qq.com* @create 17-8-28*/
@Component
public class HelloServiceFallback implements HelloService {@Overridepublic HelloMessage hello() {HelloMessage helloMessage=new HelloMessage();helloMessage.setName("hello");helloMessage.setMessage("error occurs");return helloMessage;}
}
复制代码

world项目同上

测试

1)依次启动eureka server:discovery/trace/hello/world/helloword-feing项目

2)在浏览器输入地址http:\localhost:8030/message,则返回正确的结果

3)停止hello项目,再次输入上述地址,则执行断路器中的方法。

查看断路器错误日志

如果要查看详细的断路器的日志,可以通过注解@FeignClient的fallbackFactory来实现,如下代码所示:

import com.example.helloworldfeign.model.HelloMessage;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;/*** @author billjiang 475572229@qq.com* @create 17-8-23*/
@FeignClient(value="hello",fallbackFactory = HelloServiceFallbackFactory.class)
public interface HelloService {@GetMapping("/message")HelloMessage hello();}HelloServiceFallbackFactory类:
package com.example.helloworldfeign.service;import com.example.helloworldfeign.model.HelloMessage;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;/*** @author billjiang 475572229@qq.com* @create 17-8-28*/
@Component
public class HelloServiceFallbackFactory implements FallbackFactory<HelloService> {private final static Logger LOGGER= LoggerFactory.getLogger(HelloServiceFallbackFactory.class);@Overridepublic HelloService create(Throwable throwable) {return new HelloService() {@Overridepublic HelloMessage hello() {//print the errorLOGGER.error("fallback ,the result is:",throwable);HelloMessage helloMessage=new HelloMessage();helloMessage.setName("hello");helloMessage.setMessage("error occurs");return helloMessage;}};}
}
复制代码

这样会在控制台把具体导致熔断的信息输出,以便跟踪错误。

java B2B2C springmvc mybatis电子商务平台源码

转载于:https://juejin.im/post/5ca56f13e51d450caa3569e1

java B2B2C Springcloud电子商城系统-断路器(Hystrix)相关推荐

  1. java B2B2C Springcloud电子商城系统-通过消息队列传输zipkin日志

    一.zipkin服务端配置 需要JAVA Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码 一零三八七七四六二六 1.引入依赖 //===========消息队列方式的依 ...

  2. java B2B2C Springcloud电子商城系统-Spring Cloud常见问题与总结(四)

    一.Spring Cloud定位问题思路总结 Spring Cloud进入Camden时代后,已经比较稳定.一般来说,问题都不是Spring Cloud本身的Bug导致.建议按照如下步骤进行定位.需要 ...

  3. java B2B2C Springcloud电子商城系统-Ribbon设计原理

    Ribbon 是netflix 公司开源的基于客户端的负载均衡组件,是Spring Cloud大家庭中非常重要的一个模块:Ribbon应该也是整个大家庭中相对而言比较复杂的模块,直接影响到服务调度的质 ...

  4. java B2B2C Springcloud电子商城系统--------负载均衡(Load Balance)

    负载均衡(Load Balance) 由于目前现有网络的各个核心部分随着业务量的提高,访问量和数据流量的快速增长,其处理能力和计算强度也相应地增大,使得单一的服务器设备根本无法承担.在此情况下,如果扔 ...

  5. java B2B2C Springboot电子商城系统

    大型企业分布式互联网电子商务平台,推出PC+微信+APP+云服务的云商平台系统,其中包括B2B.B2C.C2C.O2O.新零售.直播电商等子平台. 需要JAVA Spring Cloud大型企业分布式 ...

  6. java B2B2C Springboot电子商城系统-路由网关(zuul)

    一.Zuul简介 Zuul的主要功能是路由转发和过滤器.路由功能是微服务的一部分,比如/api/user转发到到user服务,/api/shop转发到到shop服务.zuul默认和Ribbon结合实现 ...

  7. java B2B2C Springboot电子商城系统 (六) 分布式配置中心(Spring Cloud Config)

    2019独角兽企业重金招聘Python工程师标准>>> 一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring ...

  8. java毕业生设计电子商城系统计算机源码+系统+mysql+调试部署+lw

    java毕业生设计电子商城系统计算机源码+系统+mysql+调试部署+lw java毕业生设计电子商城系统计算机源码+系统+mysql+调试部署+lw 本源码技术栈: 项目架构:B/S架构 开发语言: ...

  9. java b2b2c shop 多用户商城系统源码- eureka集群整合hystrix框架

    继之前项目继续整合hystrix框架,hystrix框架为Netflix的模块,是一个容错框架.当用户访问服务调用者的时候,如果服务提供者出现异常导致无法正常返回出现请求超时的情况,而服务调用者并不知 ...

最新文章

  1. asp.net 包含动态创建控件的容器如果要切换显示/隐藏不要用 Visible 属性
  2. 如何在PySide中使用qrc资源文件
  3. linux连接redis 命令_在Docker中使用Redis
  4. sql php修改mysql结构_sql怎么修改表内容
  5. 《Linux多线程服务端编程——使用muduo C++网络库》读书笔记
  6. 微软开源AI诊断工具Error Analysis
  7. 基础知识很扎实 - 但是面试就是做不出来, 怎么办? (长, 慎入)
  8. mysql有table函数吗_mysql_alter_table函数流程的部分修改和注解
  9. html5-css的使用强制优先级
  10. L3-006 迎风一刀斩 (30 分)-PAT 团体程序设计天梯赛 GPLT
  11. mybatis增加数据后,获取增加数据的主键Id
  12. cacti 监控添加插件
  13. Ruby中的concat()方法
  14. WordPress自动采集发布文章02-软件批量伪原创
  15. QT学习笔记(四)——常用ui控件以及自定义ui控件的使用
  16. Flutter开发之——Future<dynamic> is not a subtype of type () void
  17. 僵尸网络_僵尸网络钓鱼
  18. 敬业签:备忘录在手机哪里能找到?
  19. eclipse远程调试Java程序
  20. 又是一年推gal季(牛客OI周赛5-提高组)

热门文章

  1. mysql5.5 二进制安装
  2. C#中String与byte[]的相互转换
  3. Struts2 分割字符串标签s:generator
  4. Reactor实例解析
  5. SpringMVC @RequestBody 接收Json数组对象
  6. c/c++文件I/O函数学习--不断补充
  7. ViewStub must have a valid layoutResource
  8. linux 的一个防火墙策略
  9. gaokao--抓取高校基本信息
  10. Repeater控件的使用