springcloud hystrix+openfeign服务降级案例


前言

随着springcloud的使用越来越多,最近使用了服务降级功能,写出来作为自己的记录以及分享。

一、环境准备

1.本次环境springcloud版本为

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
            </dependency>

springboot版本为

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

前置准备
1.一个producer服务,一个comsumer服务,一个eureka服务。
这个就不展示了,其他博客教程很多,这里主要演示服务降级相关内容。

二、使用步骤

服务降级可以分为两种,一个是客户端降级,一个是服务端降级。
如果服务端配置了降级,那么就是服务端的降级生效,否则是客户端生效,下面两种情况都会做演示。

1.客户端降级

1.1 引入hystrix包,其实不引入也可以,openfeign已经引入了这个包了。

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

1.2 开启hystrix
在这里插入图片描述
1.3 配置文件application.yml加上配置

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

1.4 调用openfeign的接口写上FallbackFactory参数,这里的sayhello就是我们要调用的远程服务方法。

@FeignClient(value = "eureka-service-hi",fallbackFactory = RemoteInterfaceImpl.class)
@Component
public interface RemoteInterface {

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String sayHello();
}

1.5 重写降级方法,如果远程服务sayhello故障则进入下面的sayhello方法
返回fuwujiangji.

package com.my.comsumer.service.impl;

import com.my.comsumer.service.RemoteInterface;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

@Component
public class RemoteInterfaceImpl implements FallbackFactory<RemoteInterface> {


    @Override
    public RemoteInterface create(Throwable throwable) {
        RemoteInterface remoteInterface = new RemoteInterface() {
            @Override
            public String sayHello() {
                return "fuwujiangji";
            }
        };
        return remoteInterface;
    }
}

1.6 测试结果。
消费端的control如下:

@RestController
public class Control {

    @Autowired
    private RemoteInterface remoteInterface;

    @RequestMapping(value = "/comsumer",method = RequestMethod.GET)
    public String comsumer(){
        String s = "xxxx"+remoteInterface.sayHello();

        return s;
    }
}

可以看到返回了xxxx加上fuwujiangji 符合预期结果。
在这里插入图片描述

2.服务端降级

2.1 服务端pom加上依赖

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

2.2 启动类加上注解

@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
public class ProducerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProducerApplication.class, args);
    }

}

2.3 服务接口加上降级方法。


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Control {

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    @HystrixCommand(fallbackMethod="aaa")
    public String sayHello(){

        String result = System.currentTimeMillis()+"hello";
        throw new NullPointerException();
    }

    public String aaa(){
        return "produce fuwu jiangji";
    }

}

2.4 测试结果
在服务端的接口我们手动抛出异常,现在是两边都加上了降级配置,我们测试下哪个会先生效。
在这里插入图片描述

可以看到返回了produce fuwu jiangji 说明如果两边都配置的情况是服务侧的会生效。

总结

这里仅仅是springcloud的降级演示,其实生成端除了现在的这种降级方式,还可以针对整个类或者全局做降级处理,这里就不展开了,感兴趣的小伙伴,自行查阅资料。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Hystrix是一个开源的熔断器框架,它能够帮助开发者有效地处理服务依赖中的延迟和故障。熔断器的主要目的是在出现故障时提供一种优雅的降级机制,以避免整个系统的崩溃。 熔断和降级Hystrix中两个重要的概念。 熔断(Circuit Breaker)指的是在服务出现故障或错误率过高时,自动地切换到指定的备用服务或返回事先定义好的错误结果,起到保护系统免受故障传播的影响的作用。当服务不可用或响应时间过长时,熔断器会打开,拒绝后续请求的访问,并尝试通过执行降级逻辑来快速响应客户端。一旦后续请求不再出现故障,熔断器将会进入半开状态,允许少量的请求通过以检测服务是否恢复正常。 降级(Degradation)指的是在系统资源不足或者高访问量时,服务降级会关闭一些不重要的功能,以保证系统核心功能的可用性和稳定性。降级可以通过阻止非必要的调用、减少资源的消耗以及返回默认值或缓存结果来实现。降级需要提前定义好一些备用的逻辑,一旦系统资源紧张,就可以立即启用降级逻辑来保障系统的可用性。 总而言之,熔断和降级都是为了保护系统免受故障的影响。熔断主要是针对服务故障和错误率过高的情况,通过切换到备用服务或返回错误结果来保护系统。降级主要是在系统资源紧张或高访问量的情况下,关闭一些不重要的功能来保证核心功能的可用性和稳定性。两者都是通过提前定义备用逻辑来保障系统的正常运行。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值