Integrate local methods when calling remote services using FeignClient

background

When wrapping a user service, some functions need to call remote services, while other functions 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");
  }
}

In order to simplify the space, the exception is not 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 adding native methods to 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() defined in the UserService interface caused the compiler to report an error directly, which requires a Mapping annotation.

Try implementing both interfaces by

Another way of thinking, back to the beginning, implements two interfaces in the circuit breaker:

@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, and the remote method is invalid at all, as if it is a local method, and it goes directly to the fuse method.

IS -> HAS

Let RemoteUserService is LocalUserService since it doesn't work, 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");
  }
}

This is possible. It's a little more troublesome to code, and it's much clearer to use.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325528438&siteId=291194637