Background attempts to add a local method to the feign interface attempt to implement the two interfaces is-> has
background
Packaging a User Service, part of the function needs to invoke the remote service,? While another part of the function calls the local method, 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 space, there is no handling of exceptions.
When users use these two user-related services, they need to automatically load two service:
@Autowired
private Localuserservice localuserservice;
@Autowired
private Remoteuserservice remoteuserservice;
Can you simplify it. Consolidate two services. attempt to add a 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 () is defined in the UserService interface, and the compiler directly complains,?? Requirements must have mapping annotations. try to implement two interfaces by implementing
To change one idea, back to the beginning, implement two interfaces 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 found that the local method is normal, the remote method is simply invalid, as if it were a local method, went directly to the fuse method. Is -> has
Let Remoteuserservice is localuserservice since it is not, then try to let 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:
This is OK. The code is a little bit cumbersome, and it's much clearer when used.