在以前的架构的基础上咱们会发现,一旦级别低的服务宕了,会致使调用它的服务也挂掉,这样容易产生级联效应(雪崩效应),为了防止这种状况的出现,我引入了Hystrix来处理,先介绍ribbon使用Hystrixjava
首先引入以来pom.xml:git
<!-- Hystrix --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>com.netflix.hystrix</groupId> <artifactId>hystrix-javanica</artifactId> </dependency> <!-- spring boot低版本使用上面的,高版本使用下面注释的内容,由于最新的hystrix隶属于netfix下 --> <!-- <dependency> --> <!-- <groupId>org.springframework.cloud</groupId> --> <!-- <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> --> <!-- </dependency> --> <!-- <dependency> --> <!-- <groupId>org.springframework.cloud</groupId> --> <!-- <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> --> <!-- </dependency> -->
接着在启动类的上面加入Hystrix的注解@EnableCircuitBreakergithub
最后在MovieController中加入以下代码spring
@HystrixCommand(fallbackMethod = "fallbackfindUserByNameEn") //ribbon使用Hystrix
@ApiOperation(value = "查询用户ByName", notes = "查询用户By中文名")//方法说明
@ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = Movie.class)})//响应数听说明,能够有多个
@ApiImplicitParam(name = "name", value = "用户名", paramType = "path", required = true, dataType = "String")
@GetMapping(value = "/findUserByName/{name}",produces = { "application/json;charset=UTF-8" })
public User findUserByName(@PathVariable String name) {
return this.restTemplate.getForObject("http://xing-user/user/findByName/"+name, User.class);
}json
这里解释一下@HystrixCommand(fallbackMethod = "fallbackfindUserByNameEn")是调用服务异常那就去执行fallbackfindUserByNameEn方法,固然你可能在别的博客里看到有以下的写法:服务器
@HystrixCommand(fallbackMethod = "fallbackfindUserByNameEn",commandProperties = {)@HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")},可是官方文档里面推荐写成我上面的代码中的格式,不要写commandProperties属性,
findUserByNameEn方法在同一个线程中执行,官方不推荐添加这一段,官方文档推荐出现运行时找不到上下文异常的时候再加上这段代码,下面是官方文档的截图commandProperties这段属性的意思是调用fallbackfindUserByNameEn方法和执行
记录一个异常,今天启动movie1的时候出现网络
org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'org.springframework.cloud.netflix.eureka.EurekaDiscoveryClientConfiguration': Singleton bean creation not allowed while the singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)的异常,找了好久才发现是我开了IDEA,IDEA中我启动了一个项目,致使了端口占用,关闭IDEA中的项目就行。架构
下面咱们介绍Feign中使用Hystrixapp
参照以前博客中的movie服务中的UserInterface(使用Feign调用user服务的接口类),在这个类的注解中加入标红的代码@FeignClient(name = "xing-user" ,fallback = UserInterfaceFallback.class)//服务名,以后在UserInterface这个java类的下面加一个类ide
@Component class UserInterfaceFallback implements UserInterface { @Override public User findByNameEn(String nameEn) { User user = new User(); user.setName(""); user.setNameEn(""); user.setId(0); return user; }
固然UserInterfaceFallback这个类也能够是单独写成一个java文件,没有非要写在UserInterface类同一个java文件中。测试成功能够实现断路功能。若是想在Feign中禁用Hystrix能够在yml中加入这个配置便可feign.hystrix.enabled=false
注意:这里提醒一点必定要加@Component这个注解,我看官方文档里面好像没有加,不加的话会有以下异常:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.xing.movie.FeignInteface.UserInterface':
FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: No fallback instance of type class com.xing.movie.FeignInteface.UserInterfaceFallback found for feign client xing-user
使用Hystrix Dashboard
Hystrix Dashboard,它主要用来实时监控Hystrix的各项指标信息。经过Hystrix Dashboard反馈的实时信息,能够帮助咱们快速发现系统中存在的问题,下面我把它引入到个人项目中,使用很简单只要两步就行
第一步: 在pom.xml文件中加入Dashboard的依赖
<!-- Hystrxi dashboard的依赖,实时监控Hystrix的各项指标反馈实时信息 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency>
第二步: 在启动类中加入@EnableHystrixDashboard注解就好了,以后启动你的项目,访问http://127.0.0.1:8081/hystrix/会看到下面这个界面
经过Hystrix Dashboard主页面的文字介绍,咱们能够知道,Hystrix Dashboard共支持三种不一样的监控方式
默认的集群监控:经过URL:http://turbine-hostname:port/turbine.stream开启,实现对默认集群的监控。
指定的集群监控:经过URL:http://turbine-hostname:port/turbine.stream?cluster=[clusterName]开启,实现对clusterName集群的监控。
单体应用的监控:经过URL:http://hystrix-app:port/hystrix.stream开启,实现对具体某个服务实例的监控。(我这里输入的是个人服务实例)
Delay:控制服务器上轮询监控信息的延迟时间,默认为2000毫秒,能够经过配置该属性来下降客户端的网络和CPU消耗。
Title:该参数能够展现合适的标题。
这个界面就能够看到你个人服务调用的成功和失败的状况了
源码地址:https://github.com/OnlyXingxing/SpringCloud