Spring Cloud Hystrix的使用

Spring Cloud Hystrix

熔断器(断路器)用于保护微服务不雪崩的方法

  • 当有服务调用的情况下才会出现服务器雪崩,所以Hystrix常和OpenFeign,Ribbon一起出现

在OpenFeign中使用Hystrix

编写两个模块

  • 模块1:customer-service-01:用于调用模块2的方法
  • 模块2:rent-car-service-02用于编写方法

模块1所需依赖:springwebeureka-clientopenfeign

模块2所需依赖:springwebeureka-client

模块依赖如上方所示,我就不示例创建项目了

编写(创建)方配置

即模块2:rent-car-service-02

yml:

server:
  port: 8080
spring:
  application:
    name: rent-car-service
eureka:
  client:
    service-url:
      defaultZone: eureka远程地址
  instance:
    hostname: localhost
    instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}

启动类:

开启eurekaClient服务

@SpringBootApplication
@EnableEurekaClient

编写方法

创建文件夹controller–>创建方法类RentCarController

@RestController
public class RentCarController {
    @GetMapping("rent")
    public String rent(){
        return "租车....";
    }
}

上方就配置好了方法编写模块

调用(使用)方配置

即模块2:rent-car-service-02

yml:

  • feign.hystrix.enabled:true用于开启Hystrix的
server:
  port: 8081
spring:
  application:
    name: customer-service
eureka:
  client:
    service-url:
      defaultZone: eureka远程地址
  instance:
    hostname: localhost
    instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port}
feign:
  hystrix:
    enabled: true #在cloudF版前是默认开启的

启动类:

开启eurekaClient、feignClient服务

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients

编写Feign接口

创建文件夹feign–>创建接口CustomerRentFeign

然后编写指定服务中的方法接口(方法名一致)

  • value:指定调用服务名
@FeignClient(value = "rent-car-service")
public interface CustomerRentFeign {
    @GetMapping("rent")
    public String rent();
}

编写方法

创建文件夹controller–>创建方法类CustomerController

@RestController
public class CustomerController {
    @Autowired
    private CustomerRentFeign customerRentFeign;

    @GetMapping("customerRent")
    public String CustomerRent(){
        System.out.println("客户来了");
        return customerRentFeign.rent();
    }
}

这样即可实现调用编写方的接口了~

使用熔断器(Hystrix)

解决编写方停了导致服务器报错

在调用方新增依赖

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

feign文件夹下创建hystrix文件夹并编写文件CustomeRentFeignHystrix

继承编写的feign并重写它的方法

@Component
public class CustomeRentFeignHystrix implements CustomerRentFeign {
    @Override
    public String rent() {
        return "服务器雪崩备选方案~";
    }
}

再在feign中指向备选方案

fallback:表示服务挂了指向的备选接口

@FeignClient(value = "rent-car-service",fallback = CustomeRentFeignHystrix.class)
public interface CustomerRentFeign {
    @GetMapping("rent")
    public String rent();
}

这样即可实现即使服务挂了也不会报错,也能有备选方案~✌

常用配置

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

碰磕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值