spring-cloud系列之——OpenFeign+Hystrix(断路器)

一、为什么要使用OpenFeign

作为Spring Cloud的子项目之一,Spring Cloud OpenFeign以将OpenFeign集成到Spring Boot应用中的方式,为微服务架构下服务之间的调用提供了解决方案。首先,利用了OpenFeign的声明式方式定义Web服务客户端;其次还更进一步,通过集成Ribbon或Eureka实现负载均衡的HTTP客户端。

二、为什么要使用Hystrix(断路器)

当处理极端的情况下服务器,服务出现不可用断路器就自动切换到断路实现方法中,实现快速失败。防止出现大量超时连接保护程序

创建spring-cloud-zookeeper-server

三、创建spring-cloud-zookeeper-client

  • openFeing部分

    • 通过注册中心自动路由到对应服务(spring-cloud-zookeeper-server),然后调用接口
    • 启用:@EnableFeignClients
    • 编写接口:@FeignClient(name = “spring-cloud-zookeeper-server”)
      @FeignClient(name = "spring-cloud-zookeeper")
      public interface MyFeignServer {
          @GetMapping(value = "/hi")
          @ResponseBody
          String hi();
      }
      
  • hystrix部分

    • 当远程服务不可用时,程序自动切换到快速失败,调用fallback中对应实现的方法,以响应客户端
    • 启用Hystrix:
      • @EnableHystrix
      • yml配置文件
        #feign客户端配置
        feign:
          hystrix:
            #设置feign开启hystrix(服务保护)
            enabled: true
        
      • feign中配置失败时断路的实现
        @FeignClient(name = "spring-cloud-zookeeper", fallback = MyHystrix.class)
        
  • 完整代码

  • pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>commons-logging</artifactId>
                    <groupId>commons-logging</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
  • bootstrap.yml
server:
  port: 8081
spring:
  application:
    name: spring-cloud-zookeeper-client
  cloud:
    zookeeper:
      connect-string: 192.168.199.214:2181
      
#feign客户端配置
feign:
  hystrix:
    #设置feign开启hystrix(服务保护)
    enabled: true
    
#hystrix配置
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            #feign整合hystrix 光设置Hystrix 超时没用的要配合ribbon超时
            timeoutInMilliseconds: 3000
      circuitBreaker:
        #默认20 ,熔断的阈值,如何user服务报错满足3次,熔断器就会打开,就算order之后请求正确的数据也不行。
        requestVolumeThreshold: 3
        #默认5S , 等5S之后熔断器会处于半开状态,然后下一次请求的正确和错误讲决定熔断器是否真的关闭和是否继续打开
        sleepWindowInMilliseconds: 8000
  • 工程启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableFeignClients
public class ZookeeperClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZookeeperClientApplication.class, args);
    }
}
  • feign server接口
@FeignClient(name = "spring-cloud-zookeeper-server", fallback = MyHystrix.class)
public interface MyFeignServer {

    @GetMapping(value = "/hi")
    @ResponseBody
    String hi();
}
  • feign hystirx 实现类
@Component
public class MyHystrix implements MyFeignServer {
    @Override
    public String hi() {
        return null;
    }
}
  • controller 调用远程服务 /hi
curl -X GET http://localhost:8081/hello
Hello World!

四、遇到的问题

  • spring-cloud-zookeeper-client无法调用远程服务/hi
    • 原因:开始使用的zookeeper 版本为:zookeeper-3.4.6,导致的
    • 解决:更换为:3.5就可以了
  • hystrix没有生效,将feign hystrix 当作实现类调用
    • 原因:没有开启hystrix
    • 解决:
      • @EnableHystrix
      • yml配置文件
        #feign客户端配置
        feign:
          hystrix:
            #设置feign开启hystrix(服务保护)
            enabled: true
        

关于我

  • 关注不迷路,点赞走一波~ 转载请标注~
  • 公众号
    在这里插入图片描述
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值