Netflix Feign - Propagate Status and Exception through Microservices

后端 未结 6 804
予麋鹿
予麋鹿 2021-02-05 05:04

I\'m using Netflix Feign to call to one operation of a Microservice A to other other operation of a Microservice B which validates a code using Spring Boot.

The operation

6条回答
  •  情话喂你
    2021-02-05 05:30

    Since 2017 we've created a library that does this from annotations (making it fairly easy to, just like for requests/etc, to code this up by annotations).

    it basically allows you to code error handling as follows:

    @ErrorHandling(codeSpecific =
        {
            @ErrorCodes( codes = {401}, generate = UnAuthorizedException.class),
            @ErrorCodes( codes = {403}, generate = ForbiddenException.class),
            @ErrorCodes( codes = {404}, generate = UnknownItemException.class),
        },
        defaultException = ClassLevelDefaultException.class
    )
    interface GitHub {
    
        @ErrorHandling(codeSpecific =
            {
                @ErrorCodes( codes = {404}, generate = NonExistentRepoException.class),
                @ErrorCodes( codes = {502, 503, 504}, generate = RetryAfterCertainTimeException.class),
            },
            defaultException = FailedToGetContributorsException.class
        )
        @RequestLine("GET /repos/{owner}/{repo}/contributors")
        List contributors(@Param("owner") String owner, @Param("repo") String repo);
    }
    

    You can find it in the OpenFeign organisation: https://github.com/OpenFeign/feign-annotation-error-decoder

    disclaimer: I'm a contributor to feign and the main dev for that error decoder.

提交回复
热议问题