针对 Spring-Feign
开发环境下,编译生成二方包下 Feign
的 FallbackFactory
。
添加 feign-help
maven 依赖到项目 模块中 pom.xml :
<dependency>
<groupId>io.github.moyada</groupId>
<artifactId>feign-help</artifactId>
<version>1.0.2-RELEASE</version>
</dependency>
public interface UserApi {
@RequestMapping(value = "user/get", method = RequestMethod.GET)
Result<UserDTO> getUser(@RequestParam("userId") Long userId);
@RequestMapping(value = "user/create", method = RequestMethod.POST)
Result<Boolean> createUser(@RequestBody UserDTO data);
@RequestMapping(value = "user/delete", method = RequestMethod.DELETE)
Result<Boolean> deleteUser(@RequestBody UserRequest data);
}
只需简单使用注解标记,经过编译过的class文件将会自动添加对应的 Fallback
, FallbackFactory
@FallbackFactoryBuild
@FeignClient(name = "user", fallbackFactory = UserRemote.FallbackFactory.class)
public interface UserRemote extends UserApi {
}
@FeignClient(
name = "user",
fallbackFactory = UserRemote.FallbackFactory.class
)
public interface UserRemote extends UserApi {
@Component
public static class FallbackFactory implements feign.hystrix.FallbackFactory {
public FallbackFactory() {
}
public UserRemote create(Throwable arg0) {
return new UserRemote.Fallback();
}
}
public static class Fallback implements UserRemote {
public Fallback() {
}
public Result<UserDTO> getUser(@RequestParam("userId") Long userId) {
return null;
}
public Result<Boolean> createUser(@RequestBody UserDTO data) {
return null;
}
public Result<Boolean> deleteUser(@RequestBody UserRequest data) {
return null;
}
}
}
moyada |
---|