Spring-cloud-netflix: Hystrix fallbacks with Feign.

Created on 10 Jan 2016  Â·  15Comments  Â·  Source: spring-cloud/spring-cloud-netflix

https://github.com/Netflix/feign/issues/298 ,I like this

How about a fallback implementation of the interface?

public interface FooClient {
      void doSomething(String withArg);
}

public class FooFallback implements FooClient {
      public void doSomething(String withArg){
            System.out.println(withArg);
       }
}

@FeignClient(name="foo-client", fallback=FooFallback.class)       
public interface FeignFooClient extends FooClient {

    @Override
    @RequestMapping(method = RequestMethod.GET, value = "/something/{withArg}")
    void doSomething(String withArg);

}  
enhancement help wanted

Most helpful comment

@miguelfgar TestFallback must be a spring bean. Please open a new issue otherwise.

All 15 comments

Depends on #727

Which version of spring-cloud-starter-parent dependencies contains this?

apply plugin: "io.spring.dependency-management"

dependencyManagement {
  imports {
    mavenBom "org.springframework.cloud:spring-cloud-starter-parent:${springCloudStarterVersion}"
  }
}

dependencies {
  compile("org.springframework.cloud:spring-cloud-starter-feign")
  compile("org.springframework.boot:spring-boot-starter-data-mongodb")
  compile("org.projectlombok:lombok")
}

I doesn't exist yet, that is what this issue will track. The code you pasted in the example above doesn't exist yet, it has to be built on https://github.com/Netflix/feign/issues/298.

Thanks,which version can I use this feature ?

Only in SNAPSHOTS at the moment.

https://repo.spring.io/snapshot/org/springframework/cloud/spring-cloud-starter-parent/Brixton.BUILD-SNAPSHOT/

or like :
mavenBom "org.springframework.cloud:spring-cloud-starter-parent:1.1.0.BUILD-SNAPSHOT"

this version contains it?

I will continue to focus on the issues and test it in my development environment.

Hi @spencergibb, @Dreampie,

I don't know where to find the snapshot either... I want to use the feature "fallback" in the @FeingClient annotation that was added in this commit (from 15th Jan 2016):

https://github.com/spring-cloud/spring-cloud-netflix/commit/f00f7619960dd2a7ff23a054ebb6b9bb5cdb55a1

However I don't see an updated snaphot in Spring Snapshots repository.
Is it necessary and additional snapshot repo or how can I use it?

Thanks for your help!

This is the snapshot repo it is in is:

<repository>
  <id>spring-snapshots</id>
  <name>Spring Snapshots</name>
  <url>http://repo.spring.io/libs-snapshot-local</url>
  <snapshots>
     <enabled>true</enabled>
  </snapshots>
</repository>

Hi @spencergibb,

Thanks so much for your help, I added the proper Maven repo to my config.

In order to use "fallback" field in @FeignClient I thought to use the SNAPSHOT just for the artifact that contains @FeignClient (spring-cloud-core-netflix) however I had some other errors in cascade so I guess that currently the way to use spring-cloud-core-netflix SNAPSHOT to use the "fallback" feingclient feature is directly adding the Brixton.BUILD-SNAPSHOT BOM. Is this the best way? can you please confirm?

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-parent</artifactId>
            <version>Brixton.BUILD-SNAPSHOT</version> 
            <type>pom</type>
            <scope>import</scope>
        </dependency>           

On the other hand, I get the following exception with a simple example I have implemented:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.appverse.server.showcases.oauth.userservice.services.presentation.UserServiceImpl$CarClient org.appverse.server.showcases.oauth.userservice.services.presentation.UserServiceImpl.client; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl$CarClient': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: No fallback instance of type class org.appverse.server.showcases.oauth.userservice.services.presentation.TestFallback found for feign client car-service

Is this the correct way to implement it?

(Imports omitted)

@RestController
@RequestMapping(value = "/api")
public class UserServiceImpl {

@Autowired
CarClient client;


private List<UserVO> users = Arrays.asList(
        new UserVO(1, "user1", "User1 Surname", "1", "[email protected]"),
        new UserVO(2, "user2", "User2 Surname", "2", "[email protected]"),
        new UserVO(3, "user3", "User3 Surname", "3", "[email protected]"));

/**
 * Return all users
 * 
 * @return
 */
@RequestMapping(value="/user", method = RequestMethod.GET, headers = "Accept=application/json")
public List<UserVO> getUsers() {
    List<CarVO> cars = client.getAllCars();
    for (UserVO user: users){
        for(CarVO car: cars){
            if (user.getId() == car.getOwnerId()){
                user.setCar(car.getBrand() + " " + car.getModel());
                break;
            }               
        }           
    }       
    return users;
}

/**
 * Return user associated with an specific user name
 * 
 * @param userName
 * @return
 */
@RequestMapping(value = "/user/{userName}", method = RequestMethod.GET, headers = "Accept=application/json")
public UserVO getUserByUserName(@PathVariable("userName") String userName) {
    UserVO userDtoToReturn = null;
    for (UserVO currentUser : users) {
        if (currentUser.getUserName().equalsIgnoreCase(userName)) {
            userDtoToReturn = currentUser;
            break;
        }
    }

    return userDtoToReturn;
}

@FeignClient(name = "car-service", fallback=TestFallback.class, configuration = FeignClientConfiguration.class)
interface CarClient {       

    @RequestMapping(value = "car-service/api/car", method = GET)
    List<CarVO> getAllCars();

}
}

(Imports omitted)

public class TestFallback implements CarClient {

    @Override
    public List<CarVO> getAllCars() {
        // TODO: implement fallback
        return null;
    }   
}

Thank you!

@miguelfgar TestFallback must be a spring bean. Please open a new issue otherwise.

Thanks @spencergibb, that makes sense. I have added it as a spring bean and now my application (microservice) starts.

However, using Brixton.BUILD-SNAPSHOT spring-cloud-starter-parent (because I need "fallback" feature in @FeignClient - just available currently as snapshot) when I try all my system (with ZuulProxy, Eureka, Config Server, etc) I always get a "Forwarding exception" (timeout) in ZuulProxy when forwarding to my application (microservice). This worked fine before with Brixton.M4. In fact I have commented out (removed) the "fallback" feature in @FeignClient and gone back to Brixton.M4 and everything works fine again without making any other changes - ZuulProxy is again forwarding correctly to my application.

With Brixton.BUILD-SNAPSHOT I have checked that my application is correctly registered in Eureka and showing status "Up", no exceptions in any application, all seems to be OK. The only thing is that for some reason ZuulProxy tries to forward (route) to the application and this is failing.
Any clue? Maybe some configuration or properties that have changed from Brixton.M4 to Brixton.BUILD-SNAPSHOT? Something that should be done now different or maybe a bug?

(Instead of adding Brixton.BUILD-SNAPSHOT I tried also to keep Brixton.M4 spring-cloud-starter-parent and selectively override the required libraries to snapshot, for instance spring-cloud-netflix-core.1.1.0-BUILD-SNAPSHOT which contains @FeignClient, adding new spring-cloud-netflix-eureka-client that has been splitted now in its own module, and so on... However, I had some other things failing on application start up so I thought that at least all the netflix dependent modules needed to be aligned and so I tried directly to use the Brixton.BUILD-SNAPSHOT spring-cloud-starter-parent which should ensure all dependencies are properly aligned).

Thanks @spencergibb for your time and help :-)

@miguelfgar I don't know how many times I need to ask

Please open a new issue

@spencergibb , how can we access any exception from FiegnClient in the fallback class?

@satishgummadelli it's not currently possible.

@spencergibb, Thanks for the information

Was this page helpful?
0 / 5 - 0 ratings