首页 > 编程语言 > 详细

小D课堂 - 新版本微服务springcloud+Docker教程_5-03 feign结合hystrix断路器开发实战上

时间:2019-09-02 22:59:51      阅读:91      评论:0      收藏:0      [点我收藏+]

笔记

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
技术分享图片
以上就是熔断后降级的操作



 

小D课堂 - 新版本微服务springcloud+Docker教程_5-03 feign结合hystrix断路器开发实战上

原文:https://www.cnblogs.com/wangjunwei/p/11448789.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!