Consolidating local methods when using Feignclient to invoke remote services

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.