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

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



     

  • 相关阅读:
    Android Studio配置 AndroidAnnotations——Hi_博客 Android App 开发笔记
    Android请求网络共通类——Hi_博客 Android App 开发笔记
    Html Agility Pack 解析Html
    Asp.Net Mvc 使用WebUploader 多图片上传
    JavaScript初探 二
    JavaScript初探 三
    JavaScript初探 四
    JavaScript初探一
    async & await 的前世今生(Updated)
    Grunt基础知识介绍
  • 原文地址:https://www.cnblogs.com/wangjunwei/p/11448789.html
Copyright © 2011-2022 走看看