background
To wrap a user service, some functions need to call remote services, while others need to call local methods, such as:
@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"); } }
To simplify the length, no exceptions were handled.
When users use these two user related services, they need to automatically load two services:
@Autowired private LocalUserService localUserService; @Autowired private RemoteUserService remoteUserService;
Can you simplify it? Integrate the two services.
Try to add the local method to the Feign interface
@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:
The test found that the local method getUserId() compiler defined in the UserService interface directly reported an error, requiring a Mapping annotation.
Try to implement two interfaces
In another way, back to the beginning, two interfaces are implemented in the fuse:
@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:
The test shows that the local method is normal, and the remote method is not effective at all. It seems that it is just a local method that goes directly to the fusing method.
IS -> HAS
Since RemoteUserService is LocalUserService is not available, try 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"); } }
This is OK. It's a little bit troublesome when coding, and it's much clearer when using.