Hystrix整合Feign


Feign整合Hystix实现服务降级。

user-api

user-api 就是一个提供接口的项目

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

//面向接口编程
public interface UserApi {

    /**
     * 查看当前服务状态
     * @return
     */
    @GetMapping("/alive")
    String alive();
}


user-consumer

user-consumer 是一个消费者

配置文件 application.yml

feign:
  hystrix:
    enabled: true

接口

import com.quintin.userapi.UserApi;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Map;

@FeignClient(name="user-provider",fallback = UserProviderBack.class)
public interface ComsumerApi extends UserApi {

}

UserProviderBack.java

import org.springframework.stereotype.Component;
import java.util.Map;

@Component
public class UserProviderBack implements ComsumerApi{

    @Override
    public String alive() {
        System.out.println("降级");
        return "降级";
    }
}

MainController.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;

@RestController
public class MainController {

    @Autowired
    ComsumerApi api;

    @GetMapping("/alive")
    public String alive(){
        System.out.println("api = alive");
        return api.alive();
    }
}


user-provider

user-provider 是消费者

UserController.java

import com.quintin.userapi.UserApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
public class UserController implements UserApi {

    @Value("${server.port}")
    String port;

    private AtomicInteger count = new AtomicInteger();

    @Override
    public String alive(){
        System.out.println("alive");
        int i = count.getAndIncrement();
        System.out.println("port:"+ port + "、第 " + i + " 次调用");
        return "port : " + port;
    }
}


分别启动 provider 和 consumer 后,正常调用 alive 接口:

http://localhost:8088/alive

localhost:8088

调用结果:

https://pic4.zhimg.com/80/v2-850b9f561101f655e969fd511885d6c5_720w.png

停止 provider 项目,再次请求:

https://pic4.zhimg.com/80/v2-e25607e731121d4f17c9b371fcb36f30_720w.png

服务成功降级

使用fallbackFactory检查具体错误

@FeignClient(name="user-provider",fallbackFactory = UserProviderBackFactory.class)
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpServerErrorException;
import java.util.Map;

@Component
public class UserProviderBackFactory implements FallbackFactory<ComsumerApi> {
    @Override
    public ComsumerApi create(Throwable cause) {
        return new ComsumerApi() {
            @Override
            public Map<Integer, String> getMap(Integer id) {
                return null;
            }

            @Override
            public Map<Integer, String> getMap2(Integer id, String name) {
                return null;
            }

            @Override
            public Map<Integer, String> getMap3(Map<String, Object> map) {
                return null;
            }

            @Override
            public Map<Integer, String> postMap(Map<String, Object> map) {
                return null;
            }

            @Override
            public String alive() {
//                // TODO Auto-generated method stub
//                System.out.println(throwable.getLocalizedMessage());
//                throwable.printStackTrace();
//                return ToStringBuilder.reflectionToString(throwable);

                // 针对不同异常返回不同响应
                System.out.println(cause);
                if(cause instanceof HttpServerErrorException.InternalServerError) {
                    System.out.println("InternalServerError");
                    return "远程服务报错";
                }else if(cause instanceof RuntimeException) {
                    return "请求时异常:" + cause;
                }else {
                    return "都算不上";
                }
            }

            @Override
            public String getById(Integer id) {
                return null;
            }
        };
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值