2017-10-10 120 views
0

我正在使用Feign客戶端,Feign客戶端錯誤處理

我有一個位置服務。所以我使用FeignClient爲我的LocationService創建了一個客戶端。

@FeignClient(url="http://localhost:9003/location/v1", name="location-service") 
public interface LocationControllerVOneClient { 

    @RequestMapping(value = "/getMultipleLocalities", method = RequestMethod.POST) 
    public Response<Map<Integer, Locality>> getMultipleLocalities(List<Integer> localityIds); 

    @RequestMapping(value = "/getMultipleCities", method = RequestMethod.POST) 
    public Response<Map<Integer, City>> getMultipleCities(List<Integer> cityIds); 

    @RequestMapping(value = "/getMultipleStates", method = RequestMethod.POST) 
    public Response<Map<Integer, State>> getMultipleStates(List<Integer> stateIds); 

    @RequestMapping(value = "/getMultipleCitiesName", method = RequestMethod.POST) 
    public Response<Map<Integer, String>> getMultipleCitiesName(MultiValueMap<String, String> formParams); 

    @RequestMapping(value = "/getMultipleStatesName", method = RequestMethod.POST) 
    public Response<Map<Integer, String>> getMultipleStatesName(MultiValueMap<String, String> formParams); 

    @RequestMapping(value = "/getMultipleLocalitiesName", method = RequestMethod.POST) 
    public Response<Map<Integer, String>> getMultipleLocalitiesName(MultiValueMap<String, String> formParams); 

} 

現在其他服務可能會通過LocationClient調用此LocationService。我想對這個Feign客戶端(LocationClient)的異常處理在一個共同的地方(即我不想讓每個調用者這樣做,這應該是LocationClient的一部分)。異常可能會被拒絕連接(如果LocationService關閉),超時等

回答

0

您可以定義一個備用客戶端時調用像超時或拒絕連接異常出現:

@FeignClient(url="http://localhost:9003/location/v1", name="location-service", fallback=LocationFallbackClient.class) 
public interface LocationControllerVOneClient { 
    ... 
} 

LocationFallbackClient必須實現LocationControllerVOneClient