栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springcloud hystrix+openfeign服务降级案例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springcloud hystrix+openfeign服务降级案例


前言

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

一、环境准备

1.本次环境springcloud版本为

            
                org.springframework.cloud
                spring-cloud-dependencies
                Greenwich.RELEASE
            

springboot版本为

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.5.RELEASE
         
    

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

二、使用步骤

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

1.客户端降级

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

        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
        

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 {


    @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加上依赖

        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
        

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/924498.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号