Ruoyi-Cloud-服务间的调用

Ruoyi-Cloud-服务间的调用

如文档所描述的,Ruoyi-Cloud服务间调用使用的是feign,文档链接:http://doc.ruoyi.vip/ruoyi-cloud/cloud/feign.html#基本介绍

Feign 是Spring Cloud Netflix组件中的一量级Restful的 HTTP 服务客户端,实现了负载均衡和 Rest 调用的开源框架,封装了RibbonRestTemplate, 实现了WebService的面向接口编程,进一步降低了项目的耦合度。

Feign在Github中的例子很好地描述了使用Feign的便捷性,https://github.com/OpenFeign/feign

public interface GitHub {

  @RequestLine("GET /repos/{owner}/{repo}/contributors")
  List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repository);

  class Contributor {
    String login;
    int contributions;
  }
}

public class MyApp {
  public static void main(String[] args) {
    GitHub github = Feign.builder()
                         .decoder(new GsonDecoder())
                         .target(GitHub.class, "https://api.github.com");

    /* The owner and repository parameters will be used to expand the owner and repo expressions
     * defined in the RequestLine.
     *
     * the resulting uri will be https://api.github.com/repos/OpenFeign/feign/contributors
     */
    github.contributors("OpenFeign", "feign");
  }
}

上述代码实现了读取https://api.github.com/repos/OpenFeign/feign/contributors返回的结果并条目化输出。

搞清楚了上面的例子,再回到Ruoyi-Cloud,就很容易理解服务间的调用了。

com.ruoyi.system.api中定义了以下三个接口:
RemoteUserService
RemoteLogService
RemoteFileService

比如RemoteUserService中的定义:

@FeignClient(contextId = "remoteUserService", value = ServiceNameConstants.SYSTEM_SERVICE, fallbackFactory = RemoteUserFallbackFactory.class)
public interface RemoteUserService
{
    /**
     * 通过用户名查询用户信息
     *
     * @param username 用户名
     * @return 结果
     */
    @GetMapping(value = "/user/info/{username}")
    public R<LoginUser> getUserInfo(@PathVariable("username") String username);
}

在ruoyi.auth的SysLoginService中的登录,就调用了api中的RemoteUserService和remoteLogService接口:

        // 用户名不在指定范围内 错误
        if (username.length() < UserConstants.USERNAME_MIN_LENGTH
                || username.length() > UserConstants.USERNAME_MAX_LENGTH)
        {
            remoteLogService.saveLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围");
            throw new BaseException("用户名不在指定范围");
        }
        // 查询用户信息
        R<LoginUser> userResult = remoteUserService.getUserInfo(username);
posted @ 2021-06-12 22:27  一 缕 阳 光  阅读(1739)  评论(0编辑  收藏  举报