7、Hystrix (提供者服务熔断)

 

项目Demo地址

https://github.com/mryhw/spring_cloud_stady_2019.git

避免单体调用导致全局 down 机

向调用方返回一个符合预期的、可处理的备选响应(FallBack),而不是长时间的等待或者抛出调用方无法处理的异常,会进行服务降级,进而熔断该节点服务的调用,快速返回错误的响应信息。

一般是某个服务故障或者异常时引起,类似现实生活世界的保险丝,当某个异常条件被触发,直接熔断整个服务,而不是一直等到此服务超时。

能干嘛

  • 服务降级
  • 服务熔断
  • 服务限流
  • 接近实时的监控

整合 hystrix (提供者服务熔断

  • POM

<!-- 引入自己定义的api通用包,可以使用Dept部门Entity -->
<dependency>
    <groupId>com.spcd</groupId>
    <artifactId>ms-api</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>
<!-- actuator监控信息完善 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 将微服务provider侧注册进eureka -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<!--<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>springloaded</artifactId>
</dependency>-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- hystrix -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

 

  • YML(主要修改的是红色部分)

server:
  port: 8001
# 如果使用 xml 方式 需要使用以下配置
#mybatis:
#  config-location: classpath:mybatis/mybatis.cfg.xml        # mybatis配置文件所在路径
#  type-aliases-package: com.atguigu.springcloud.entities    # 所有Entity别名类所在包
#  mapper-locations:
#    - classpath:mybatis/mapper/**/*.xml                       # mapper映射文件
spring:
  application:
    name: ms-provider
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
    url: jdbc:mysql://localhost:3306/cloudDB01?useUnicode=true&characterEncoding=utf-8&useSSL=false     # 数据库名称
    username: root
    password: 123456
    dbcp2:
      min-idle: 5                                           # 数据库连接池的最小维持连接数
      initial-size: 5                                       # 初始化连接数
      max-total: 5                                          # 最大连接数
      max-wait-millis: 200                                  # 等待连接获取的最大超时时间

eureka:
  client: #客户端注册进eureka服务列表内
    service-url:
#      defaultZone: http://localhost:7001/eureka
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    instance-id: ms-provider-hystrix-8001    # 防止 Eureka web页面报错 ,主机服务名称修改
    prefer-ip-address: true     # 访问路径可以显示IP地址,(左下角IP显示)
info:
  app.name: atguigu-microservicecloud
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$
 
  • 主启动类

@EnableCircuitBreaker   // 开启 hystrix
  • 测试

@RequestMapping("getById")
@HystrixCommand(fallbackMethod = "fallbackGetById")
public Map<String,Object> getById(@RequestParam Integer id)  {
    System.out.println(" ================ 8001 ==============");
    Map<String, Object> map = new HashMap<>();
    if(deptService.getById(id) == null) {
        throw new RuntimeException("不存在");
    }
    map.put("result", deptService.getById(id));
    map.put("server", "8001");
    return map;
}

public Map<String,Object> fallbackGetById(@RequestParam Integer id) {
    Map<String, Object> map = new HashMap<>();
    map.put("result", id + "不存在");
    map.put("server", "8001");
    return map;
}
  • postMan 调用
                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值