zoukankan      html  css  js  c++  java
  • 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);
    
  • 相关阅读:
    drf 之 JWT认证 什么是集群以及分布式 什么是正向代理,什么是反向代理
    drf 之自定制过滤器 分页器(三种)如何使用(重点) 全局异常 封装Response对象 自动生成接口文档
    课堂练习之“寻找最长单词链”
    《人月神话》读书笔记(三)
    用户体验
    第十四周进度报告
    课堂练习之“寻找水王”
    《人月神话》读书笔记(二)
    第二阶段冲刺(十)
    第二阶段冲刺(九)
  • 原文地址:https://www.cnblogs.com/ShineTan/p/14879192.html
Copyright © 2011-2022 走看看