zoukankan      html  css  js  c++  java
  • 小D课堂

    笔记

    3、Feign结合Hystrix断路器开发实战《上》
        简介:讲解SpringCloud整合断路器的使用,用户服务异常情况

        1、加入依赖
        
        注意:网上新旧版本问题,所以要以官网为主,不然部分注解会丢失
        最新版本 2.0

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            </dependency>


        2、增加注解
            启动类里面增加注解
            @EnableCircuitBreaker

            注解越来越多-》 SpringCloudApplication注解

        3、API接口编码实战
          熔断-》降级

            1)最外层api使用,好比异常处理(网络异常,参数或者内部调用问题)
                api方法上增加 @HystrixCommand(fallbackMethod = "saveOrderFail")
                
                编写fallback方法实现,方法签名一定要和api方法签名一致(注意点!!!)



            

        补充: 修改maven仓库地址
        pom.xml中修改

        <repositories>
            <repository>
                <id>nexus-aliyun</id>
                <name>Nexus aliyun</name>
                <layout>default</layout>
                <url>http://maven.aliyun.com/nexus/content/groups/public</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
                <releases>
                    <enabled>true</enabled>
                </releases>
            </repository>
        </repositories>

    开始

    spring cloud整合官方文档
    http://cloud.spring.io/spring-cloud-netflix/single/spring-cloud-netflix.html#_circuit_breaker_hystrix_clients
    搜索关键字
    How to Include Hystrix

     

     <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            </dependency>



    先加入依赖。在order service里面加入依赖

    maven如果下载比较慢的话,百度一下换成阿里云的maven地址



    只作用于当前项目的阿里云地址

    <repositories>
            <repository>
                <id>nexus-aliyun</id>
                <name>Nexus aliyun</name>
                <layout>default</layout>
                <url>http://maven.aliyun.com/nexus/content/groups/public</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
                <releases>
                    <enabled>true</enabled>
                </releases>
            </repository>
        </repositories>


    复制到最下面

    启动类里面加注解

    @EnableCircuitBreaker

    下单Controller修改


    SpringCloudApplication这一个注解包含上面那几个注解。所以可以用这一个注解替换上面的注解

    编码


    上面的方法为了和下面的保持一致,所以这里也用Map包装起来

    启动应用。orderService和ProductService
    注解中心看到两个服务已经有了。

    这是正常的情况下 请求到的数据

    如果某一天服务挂了。把ProductService这个服务停掉。
    再去访问OrderService

    以上就是熔断后降级的操作



     

  • 相关阅读:
    更改discuz!3.4注册后用户所在用户组
    APACHE服务器500错误解决方法
    有关redis笔记
    真正免费!!!爱客追剧神器【珍藏】
    discuz 论坛如何设置一个邀请码重复使用不过期,真正管理员专用
    discuz3.4设置会员免回复查看隐藏帖
    BigDecimal的用法详解(保留两位小数,四舍五入,数字格式化,科学计数法转数字,数字里的逗号处理)
    tinyproxy轻量代理服务器安装
    人物-企业家-实业家、发明家:松下幸之助
    图书-励志:《你的梦想一定能实现》
  • 原文地址:https://www.cnblogs.com/wangjunwei/p/11448789.html
Copyright © 2011-2022 走看看