Spring Cloud Feign 之Fallback

timg.jpg

遷移到CSDN

Spring Cloud Feign 之Fallback

環(huán)境信息: java 1.8、Spring boot 1.5.10.RELEASE、spring cloud-Edgware.SR3、maven 3.3+

在網(wǎng)絡(luò)請(qǐng)求時(shí),可能會(huì)出現(xiàn)異常請(qǐng)求,如果還想再異常情況下使系統(tǒng)可用,那么就需要容錯(cuò)處理,比如:網(wǎng)絡(luò)請(qǐng)求超時(shí)時(shí)給用戶提示“稍后重試”或使用本地快照數(shù)據(jù)等等。

Spring Cloud Feign就是通過Fallback實(shí)現(xiàn)的,有兩種方式:

1、@FeignClient.fallback = UserFeignFallback.class指定一個(gè)實(shí)現(xiàn)Feign接口的實(shí)現(xiàn)類。

2、@FeignClient.fallbackFactory = UserFeignFactory.class指定一個(gè)實(shí)現(xiàn)FallbackFactory<T>工廠接口類

因?yàn)?code>Fallback是通過Hystrix實(shí)現(xiàn)的, 所以需要開啟Hystrix,spring boot application.properties文件配置feign.hystrix.enabled=true,這樣就開啟了Fallback

Fallback-實(shí)現(xiàn)Feign接口

UserFeignFallback回調(diào)實(shí)現(xiàn),由spring創(chuàng)建使用@Component(其他的注冊(cè)也可以)注解

HystrixTargeter.targetWithFallback方法實(shí)現(xiàn)了@FeignClient.fallback處理邏輯,通過源碼可以知道UserFeignFallback回調(diào)類是從Spring容器中獲取的,所以UserFeignFallback由spring創(chuàng)建。

UserFeign配置:

package com.example.feign;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@FeignClient(name = "user",url = "${user.url}",fallback = UserFeignFallback.class
        /*fallbackFactory = UserFeignFactory.class*/)
public interface UserFeign {

    @PostMapping
    void save(User user);

    @GetMapping("/{id}")
    User getUserByID(@PathVariable("id") String id);

    @GetMapping
    List<User> findAll();
}

UserFeignFallback類:

package com.example.feign;

import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UserFeignFallback implements UserFeign {

    @Override
    public void save(User user) {

    }

    @Override
    public User getUserByID(String id) {
        User user = new User();
        user.setId("100");
        user.setName("fallback 回調(diào)用戶");
        return user;
    }

    @Override
    public List<User> findAll() {
        return null;
    }
}

為了模擬回調(diào)失敗服務(wù)提供方,拋出500錯(cuò)誤。

 @GetMapping("/{id}")
    public User getUserByID(@PathVariable("id") String id) {

//        return userMap.get(id);
        throw new RuntimeException("服務(wù)端測(cè)試異常!");
    }

運(yùn)行單元測(cè)試UserFeignTest.getUserByID控制臺(tái)輸出結(jié)果:

2018-08-18 11:47:59.800  INFO 8660 --- [ hystrix-user-1] com.example.feign.UserFeign              : [UserFeign#getUserByID] ---> GET http://localhost:8080/user/1 HTTP/1.1
2018-08-18 11:47:59.800  INFO 8660 --- [ hystrix-user-1] com.example.feign.UserFeign              : [UserFeign#getUserByID] ---> END HTTP (0-byte body)
2018-08-18 11:47:59.828  INFO 8660 --- [ hystrix-user-1] com.example.feign.UserFeign              : [UserFeign#getUserByID] <--- HTTP/1.1 500 (27ms)
2018-08-18 11:47:59.828  INFO 8660 --- [ hystrix-user-1] com.example.feign.UserFeign              : [UserFeign#getUserByID] connection: close
2018-08-18 11:47:59.828  INFO 8660 --- [ hystrix-user-1] com.example.feign.UserFeign              : [UserFeign#getUserByID] content-type: application/json;charset=UTF-8
2018-08-18 11:47:59.828  INFO 8660 --- [ hystrix-user-1] com.example.feign.UserFeign              : [UserFeign#getUserByID] date: Sat, 18 Aug 2018 03:47:59 GMT
2018-08-18 11:47:59.828  INFO 8660 --- [ hystrix-user-1] com.example.feign.UserFeign              : [UserFeign#getUserByID] transfer-encoding: chunked
2018-08-18 11:47:59.828  INFO 8660 --- [ hystrix-user-1] com.example.feign.UserFeign              : [UserFeign#getUserByID] 
2018-08-18 11:47:59.829  INFO 8660 --- [ hystrix-user-1] com.example.feign.UserFeign              : [UserFeign#getUserByID] {"timestamp":1534564079825,"status":500,"error":"Internal Server Error","exception":"java.lang.RuntimeException","message":"服務(wù)端測(cè)試異常!","path":"/user/1"}
2018-08-18 11:47:59.829  INFO 8660 --- [ hystrix-user-1] com.example.feign.UserFeign              : [UserFeign#getUserByID] <--- END HTTP (167-byte body)
User{id='100', name='fallback 回調(diào)用戶'}

服務(wù)提供方拋出的500錯(cuò)誤代碼,但是客戶端程序還可以正常運(yùn)行輸出了UserFeignFallback.getUserByID方法返回的結(jié)果。

FallbackFactory<T>工廠

上面的實(shí)現(xiàn)方式簡(jiǎn)單,但是獲取不到HTTP請(qǐng)求錯(cuò)誤狀態(tài)碼和信息 ,這時(shí)就可以使用工廠模式來實(shí)現(xiàn)Fallback

同樣工廠實(shí)現(xiàn)類也要交由spring管理,同時(shí)結(jié)合UserFeignFallback使用,這里需要注意的create方法返回值類型一定要實(shí)現(xiàn)Feign接口,否則會(huì)報(bào)錯(cuò)。

UserFeignFactory只做了打印異常處理:

package com.example.feign;

import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

@Component
public class UserFeignFactory implements FallbackFactory<UserFeign> {

    private final UserFeignFallback userFeignFallback;

    public UserFeignFactory(UserFeignFallback userFeignFallback) {
        this.userFeignFallback = userFeignFallback;
    }

    @Override
    public UserFeign create(Throwable cause) {
        //打印下異常
        cause.printStackTrace();
        return userFeignFallback;
    }
}

UserFeign:

package com.example.feign;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@FeignClient(name = "user", url = "${user.url}",
/*fallback = UserFeignFallback.class*/
        fallbackFactory = UserFeignFactory.class)
public interface UserFeign {

    @PostMapping
    void save(User user);

    @GetMapping("/{id}")
    User getUserByID(@PathVariable("id") String id);

    @GetMapping
    List<User> findAll();
}

運(yùn)行單元測(cè)試UserFeignTest.getUserByID可以看到控制臺(tái)打印的異常feign.FeignException更多信息省略。

ErrorDecoder接口處理請(qǐng)求錯(cuò)誤信息,默認(rèn)實(shí)現(xiàn)ErrorDecoder.Default拋出FeignException異常

FeignException.status 方法返回HTTP狀態(tài)碼,FallbackFactory.create默認(rèn)情況下可以強(qiáng)制轉(zhuǎn)換成FeignException異常這樣就可以獲取到HTTP狀態(tài)碼了。

自定義ErrorDecoder

第一種:application.properties

全局配置,通過application.properties配置文件

feign.client.default-config=my-config
feign.client.config.my-config.error-decoder=com.example.feign.MyErrorDecoder

錯(cuò)誤解碼實(shí)現(xiàn)類MyErrorDecoder

package com.example.feign;

import feign.Response;
import feign.codec.ErrorDecoder;

public class MyErrorDecoder implements ErrorDecoder {

    @Override
    public Exception decode(String methodKey, Response response) {
        return new MyFeignException(methodKey,response);
    }
}

自定義異常MyFeignException

package com.example.feign;

import feign.Response;

public class MyFeignException extends RuntimeException {
    private final String methodKey;
    private Response response;


    MyFeignException(String methodKey, Response response) {
        this.methodKey = methodKey;
        this.response = response;
    }


    public Response getResponse() {
        return response;
    }

    public String getMethodKey() {
        return methodKey;
    }
}

第二種:@EnableFeignClients

全局配置,@EnableFeignClients.defaultConfiguration注解

package com.example;

import com.example.feign.FeignClientsConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

/**
 * 啟動(dòng)類
 *
 * @author: sunshaoping
 * @date: Create by in 上午10:47 2018/8/7
 */
@EnableFeignClients(
        defaultConfiguration = FeignClientsConfig.class
)
@SpringBootApplication
public class FeignApplication {

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


}

第三種:@FeignClient

作用范圍是Feign接口,優(yōu)先級(jí)要高于上面兩種,@FeignClient.configuration 注解

package com.example.feign;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@FeignClient(name = "user", url = "${user.url}",
/*fallback = UserFeignFallback.class*/
        decode404 = true,
        fallbackFactory = UserFeignFactory.class,
        configuration = FeignClientsConfig.class
)
public interface UserFeign {

    @PostMapping
    void save(User user);

    @GetMapping("/{id}")
    User getUserByID(@PathVariable("id") String id);

    @GetMapping
    List<User> findAll();
}

總結(jié)

本章節(jié)講了如下內(nèi)容

Spring Cloud Feign HTTP請(qǐng)求異常Fallback容錯(cuò)機(jī)制,它是基于Hystrix實(shí)現(xiàn)的,所以要通過配置參數(shù)feign.hystrix.enabled=true開啟該功能,及其兩種實(shí)現(xiàn)方式。

Fallback工廠方式引出了ErrorDecoder錯(cuò)誤解碼自定義處理,有三種方式,可根據(jù)實(shí)際請(qǐng)求選擇,舉一反三其他自定義配置也可以通過這種方式實(shí)現(xiàn)如:Decoder、Encoder、Logger(第二、三章有介紹)。

如果開啟的Hystrix就不要用feign的超時(shí)配置了,單位是毫秒

feign.client.config.defalut.connect-timeout=10000

defalut是默認(rèn)配置名稱,可以使用feign.client.default-config替換自定義名稱

feign.client.default-config=my-config
feign.client.config.my-config.connect-timeout=10000

請(qǐng)使用如下屬性配置超時(shí)時(shí)間,單位毫秒

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=20000

樣例地址 spring-cloud-feign 分支 Spring-Cloud-Feign-之fallback

寫在最后

Spring Cloud Feign 系列持續(xù)更新中。。。。。歡迎關(guān)注

如發(fā)現(xiàn)哪些知識(shí)點(diǎn)有誤或是沒有看懂,請(qǐng)?jiān)谠u(píng)論區(qū)提出,博主及時(shí)改正。

歡迎轉(zhuǎn)載請(qǐng)注明出處。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容