服务熔断和服务降级

服务熔断和服务降级

服务熔断:是发生在服务端的,当某个服务超时或者异常时,就引起熔断,类似于现实生活中的保险丝;
服务降级:是发生在客户端的,从整体网站请求负载考虑,当某个服务熔断或者关闭后,服务将不再调用

这次我们使用Hystrix来实现服务熔断和服务降级。

服务熔断实现步骤:

导入相关的pom依赖

在服务端导入依赖

   <!--加入熔断hystrix的依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

        <!--引入springcloud服务-->
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>
        <!--完善监控信息-->
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>com.jxau</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </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-test</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>

添加相应注解

在controller层的方法上添加相应的注解

    @GetMapping("/dept/get/{id}")
    @HystrixCommand(fallbackMethod = "doGetHystrix")
    public Dept doGet(@PathVariable("id") Long id){

        Dept dept= deptService.queryById(id);
        if(dept==null){
            throw new RuntimeException("不好意思,服务已降级,服务器关闭,你无法继续访问"); // 降级操作
        }
        return dept;
    }
    
public Dept doGetHystrix(@PathVariable("id") Long id){

       Dept dept=new Dept();
       dept.setDeptno(id);
       dept.setDname("id=>"+id+"不存在该用户,或者信息无法查询~");
       dept.setDb_source("no this database in MySQL");
       return dept;
    }

@HystrixCommand(fallbackMethod = "doGetHystrix")fallbackMethod 属性表示的是熔断后调用的方法,用来化解异常,doGetHystrix(@PathVariable("id") Long id)可以说是熔断处理方法。

服务降级实现步骤

服务降级主要在服务消费端实现,比如:当双十一来临时,淘宝就会关停用户注册的相关服务器,暂停相关服务,以保证购物的服务端足够支撑如此大规模的并发。

导入相关的pom依赖

在api客户端导入依赖:

  <!--引入Feign依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

编写服务降级后处理的方法

方法需要实现FallbackFactory接口,并重写父类接口的方法。

public class DeptClientServiceFallbackFactory implements FallbackFactory {

    @Override
    public DeptClientService create(Throwable throwable) {
        return new DeptClientService() {


            @PostMapping("/dept/add")
            public Boolean doAddDept(Dept dept){
                return null;
            }

            @GetMapping("/dept/get/{id}")
            public Dept doGet(@PathVariable("id") Long id){
                Dept dept=new Dept();
                dept.setDeptno(id);
                dept.setDname("id=>"+id+"不存在该用户,或者信息无法查询~");
                dept.setDb_source("no this database in MySQL");
                return dept;
            }

            @GetMapping("/dept/list")
            public List<Dept> doGetAll(){
            return null;
            }
        };
    }
}

在服务接口添加相应的注解
fallbackFactory

@Component
@FeignClient(fallbackFactory=DeptClientServiceFallbackFactory.class)
public interface DeptClientService {

    @PostMapping("/dept/add")
    public Boolean doAddDept(Dept dept);

    @GetMapping("/dept/get/{id}")
    public Dept doGet(@PathVariable("id") Long id);

    @GetMapping("/dept/list")
    public List<Dept> doGetAll();
}

更改配置文件

在服务调用方开启相应服务降级

# 开启服务降级
feign:
  hystrix:
    enabled: true

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值