使用FeignClient调用远程服务时整合本地方法

背景

包装一个用户服务,一部分功能需要调用远程服务,而另一部分功能调用本地方法,如:

@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");
  }
}

这样是可以的。编码时稍微麻烦了一点,使用时就清晰多了。

上一篇:SpringCloud的版本


下一篇:ASP备份数据库