zoukankan      html  css  js  c++  java
  • 使用Hystrix实现断路器处理

    在之前的架构的基础上我们会发现,一旦级别低的服务宕了,会导致调用它的服务也挂掉,这样容易产生级联效应(雪崩效应),为了防止这种情况的出现,我引入了Hystrix来处理,先介绍ribbon使用Hystrix

    首先引入以来pom.xml:

    <!-- 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的注解@EnableCircuitBreaker

    最后在MovieController中加入如下代码

    @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);
    }

    这里解释一下@HystrixCommand(fallbackMethod = "fallbackfindUserByNameEn")是调用服务异常那就去执行fallbackfindUserByNameEn方法,当然你可能在别的博客里看到有如下的写法:

    @HystrixCommand(fallbackMethod = "fallbackfindUserByNameEn",commandProperties = {@HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")},但是官方文档里面推荐写成我上面的代码中的格式,不要写commandProperties属性
    commandProperties这段属性的意思是调用fallbackfindUserByNameEn方法和执行findUserByNameEn方法在同一个线程中执行,官方不推荐添加这一段,官方文档推荐出现运行时找不到上下文异常的时候再加上这段代码,下面是官方文档的截图

    记录一个异常,今天启动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中使用Hystrix

    参照之前博客中的movie服务中的UserInterface(使用Feign调用user服务的接口类),在这个类的注解中加入标红的代码@FeignClient(name = "xing-user" ,fallback = UserInterfaceFallback.class)//服务名,之后在UserInterface这个java类的下面加一个类

    @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

     

  • 相关阅读:
    VC连接数据库方式
    vc中有关数据类型的转换
    获得MFC窗口指针方法总结
    Windows 中绘图以及Windows 的图形设备接口(GDI )
    C#访问远程共享加锁文件夹
    C#操作注册表全攻略
    学习asp.net比较完整的流程
    sogou rank查询接口
    jquery星级插件、支持页面中多次使用
    防止网页被嵌入框架的js代码
  • 原文地址:https://www.cnblogs.com/xing-12/p/9947806.html
Copyright © 2011-2022 走看看