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);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM