Hystrix:

Hystrix-服务熔断

Hystrix-服务熔断

Hystrix-服务熔断

服务熔断:

Hystrix-服务熔断

服务提供者添加Hystrix功能:

Hystrix-服务熔断

导入hystrix依赖:

<!--Hystrix依赖-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
    <version>1.4.6.RELEASE</version>
</dependency>

controller:

添加发生异常跳转的备选方法,使用注解@HystrixCommand(fallbackMethod = "hystrixQueryById")指定跳转的备选方法

@GetMapping("/dept/get/{id}")
@HystrixCommand(fallbackMethod = "hystrixQueryById")  // 指定备选方法
public Dept queryById(@PathVariable("id") Long id) {
    // 存在异常需要捕获异常
    Dept dept = deptService.queryById(id);
    if (dept == null) {
        throw new RuntimeException("id=>" + id + "不存在该用户,或者信息无法找到");
    }
    return dept;
}

// 备选方法
public Dept hystrixQueryById(@PathVariable("id") Long id) {
    return new Dept()
        .setDeptno(id)
        .setDname("id=>"+id+"没有对应信息,null--@Hystrix")
        .setDb_source("no this database in MySQL");
}

启动类:

注解@EnableCircuitBreaker开启对熔断的支持

@SpringBootApplication
@MapperScan("com.stt.springcloud.dao")
@EnableEurekaClient
@EnableDiscoveryClient
@EnableCircuitBreaker  // 添加对熔断支持
public class DeptProviderHystrix_8001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptProviderHystrix_8001.class, args);
    }
}

Hystrix-服务熔断

服务提供者的application.yml配置文件:

添加prefer-ip-address为true,显示服务的id地址

# 服务提供者:eureka
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
# 添加hystrix配置
  instance:
    instance-id: springcloud-provider-hystrix-dept8001  # 修改eureka上的默认描述信息
    prefer-ip-address: true # true ,可以显示服务的id地址

Hystrix-服务熔断

相关文章:

  • 2021-11-16
  • 2021-05-25
  • 2022-03-10
  • 2022-12-23
  • 2021-07-20
  • 2021-06-15
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-03
  • 2021-06-30
  • 2021-05-28
  • 2022-12-23
  • 2021-08-31
  • 2021-11-09
相关资源
相似解决方案