FeignClient调用远程服务整合本地
54powerman 人气:0FeignClient调用远程服务时整合本地
包装一个用户服务,一部分功能需要调用远程服务,而另一部分功能调用本地方法,如:
@FeignClient(value="USER-SERVICE") public interface RemoteUserService{ @GetMapping("getUserByUserId") public User getUserByUserId(String userId); } public interface LocalUserService{ public String getUserId(); } @Service public class LocalUserServiceImpl implements LocalUserService{ @Autowired private HttpServletRequest request; public String getUserId(){ return (String)request.getSession().getAttribute("user-id"); } }
为了简化篇幅,并没有处理异常。
当使用者使用这两个用户相关的服务,就需要自动装载两个Service:
@Autowired private LocalUserService localUserService; @Autowired private RemoteUserService remoteUserService;
能不能简化一下呢?将两个服务整合。
尝试将本地方法加入Feign接口
@FeignClient(value="USER-SERVICE",fallback=UserServiceHystrix.class) public interface UserService{ @GetMapping("getUserByUserId") public User getUserByUserId(String userId); public String getUserId(); } @Service public class UserServiceHystrix implements UserService{ @Autowired private HttpServletRequest request; public User getUserByUserId(String userId){ return null; } public String getUserId(){ return (String)request.getSession().getAttribute("user-id"); } }
Fail:
测试发现,UserService 接口中定义本地方法 getUserId() 编译器直接报错了,要求必须有Mapping注解。
尝试通过实现两个接口
换一个思路,回到最初,在熔断器中实现两个接口:
@FeignClient(value="USER-SERVICE",fallback=UserServiceHystrix.class) public interface UserService{ @GetMapping("getUserByUserId") public User getUserByUserId(String userId); } public interface LocalUserService extends RemoteUserService{ public String getUserId(); } @Service public class UserServiceHystrix implements LocalUserService,RemoteUserService{ @Autowired private HttpServletRequest request; public User getUserByUserId(String userId){ return null; } public String getUserId(){ return (String)request.getSession().getAttribute("user-id"); } }
Fail:
测试发现,本地方法正常,远程方法根本无效,好像就是一个本地方法,直接走到了熔断方法中。
IS -> HAS
让 RemoteUserService is LocalUserService 既然不行,那么就试试让 LocalUserService has RemoteUserService。
@FeignClient(value="USER-SERVICE",fallback=UserServiceHystrix.class) public interface UserService{ @GetMapping("getUserByUserId") public User getUserByUserId(String userId); } public interface LocalUserService extends RemoteUserService{ public String getUserId(); } @Service public class UserServiceImpl implements LocalUserService,RemoteUserService{ @Autowired private HttpServletRequest request; @Autowire private RemoteUserService remoteUserService; public User getUserByUserId(String userId){ return remoteUserService.getUserByUserId(userId); } public String getUserId(){ return (String)request.getSession().getAttribute("user-id"); } }
SUCCESS:
这样是可以的。编码时稍微麻烦了一点,使用时就清晰多了。
FeignClient服务之间调用
服务A需调用服务B的test方法
B服务不动
A:
最好新建一个server
@Component @FeignClient(value = "tools") public interface ServiceAFeignClient { @RequestMapping(value = "/hi") String test(); }
tools为服务B配置中的spring.application.name, hi为B中Controller的接口
然后A中代码调用新建的server的接口即可,可能会出错
com.netflix.client.ClientException: Load balancer does not have available server for client
A的application配置加
ribbon: eureka: enabled: true
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
加载全部内容