美文网首页
ruoyi-cloud (一) 启动项目,熟悉接口URL的组成和

ruoyi-cloud (一) 启动项目,熟悉接口URL的组成和

作者: 尹楷楷 | 来源:发表于2023-10-18 15:45 被阅读0次

http://doc.ruoyi.vip/ruoyi-cloud/
http://doc.ruoyi.vip/ruoyi-cloud/document/hjbs.html#%E5%90%8E%E7%AB%AF%E8%BF%90%E8%A1%8C

启动ruoyi-cloud基本步骤

1、创建数据库ry-cloud并导入数据脚本ry_2021xxxx.sql(必须),quartz.sql(可选)
创建数据库ry-config并导入数据脚本ry_config_2021xxxx.sql(必须)
2、配置nacos持久化,修改conf/application.properties文件,增加支持mysql数据源配置

# db mysql
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://localhost:3306/ry-config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user=root
db.password=password

3、单节点启动nacos

sh startup.cmd -m standalone

4、启动本地redis

5、打开运行基础模块(启动没有先后顺序)

  • RuoYiGatewayApplication (网关模块 必须)8080 端口,这个是微服务内部网关。所有子服务的访问都要经过它的转发!
  • RuoYiAuthApplication (认证模块 必须)9200端口
  • RuoYiSystemApplication (系统模块 必须)9201端口
  • RuoYiMonitorApplication (监控中心 可选)9100端口
  • RuoYiGenApplication (代码生成 可选)9202端口
  • RuoYiJobApplication (定时任务 可选)9203端口
  • RuoYFileApplication (文件服务 可选)9300端口

6、前端
先安装node完了之后执行

# 进入项目目录
cd ruoyi-ui

# 安装依赖
npm install

# 强烈建议不要用直接使用 cnpm 安装,会有各种诡异的 bug,可以通过重新指定 registry 来解决 npm 安装速度慢的问题。
npm install --registry=https://registry.npmmirror.com

# 本地开发 启动项目
npm run dev

7、前端启动报错:前端项目使用( npm run dev ) 运行vue项目时,出现错误:Error: error:0308010C:digital envelope routines::unsupported

解决:更改项目环境变量来解决
在 package.json 的 scripts 中新增:SET NODE_OPTIONS=--openssl-legacy-provider
添加前代码:

      "scripts": {
        "dev": "vue-cli-service serve",
        "build:prod": "vue-cli-service build",
        "build:stage": "vue-cli-service build --mode staging",
        "preview": "node build/index.js --preview",
        "lint": "eslint --ext .js,.vue src",
        "test:unit": "jest --clearCache && vue-cli-service test:unit",
        "test:ci": "npm run lint && npm run test:unit",
        "svgo": "svgo -f src/icons/svg --config=src/icons/svgo.yml"
      },

添加后代码:

"scripts": {
        "dev": "set NODE_OPTIONS=--openssl-legacy-provider & vue-cli-service serve",
        "build:prod": "vue-cli-service build",
        "build:stage": "vue-cli-service build --mode staging",
        "preview": "node build/index.js --preview",
        "lint": "eslint --ext .js,.vue src",
        "test:unit": "jest --clearCache && vue-cli-service test:unit",
        "test:ci": "npm run lint && npm run test:unit",
        "svgo": "svgo -f src/icons/svg --config=src/icons/svgo.yml"
      },

前端启动好了访问:
http://localhost/

ruoyi的api路径对应关系

ruoyi的路径由几个部分组成

http://localhost/环境/serviceName/functionName

  • 环境可以是dev-api、prod-api、stage-api (因为前端防止跨域问题做了层代理,这个是前端代理到80端口后多出来的路径)
  • serviceName 可以是auth、code、schedule、system、file(这个定义在getway的路由配置中) ruoyi-gateway-dev.yml,当然需要到nacos中看
  • functionName 就是 对应的requestMapping的路径了

举例子

1、登录:http://localhost/dev-api/auth/login 原路径:http://localhost:8080/auth/login

  • dev-api 环境
  • auth 服务
  • /login 接口

2、代码生成的一个接口
http://localhost/dev-api/code/gen/list 原路径:http://localhost:8080/system/code/gen/list

  • dev-api 环境
  • code服务
  • /gen/list 接口

3、我自己在system中使用代码生成器写的一个接口
http://localhost/dev-api/system/book/list 原路径:http://localhost:8080/system/book/list

  • dev-api 环境
  • system服务
  • /book/list 接口

配置不需要token的路由

对于http://localhost/dev-api/system/book/list接口我们可以在网关配置中关掉token校验:
网关配置:

# 安全配置
security:
  # 验证码
  captcha:
    enabled: true
    type: math
  # 防止XSS攻击
  xss:
    enabled: true
    excludeUrls:
      - /system/notice
  # 不校验白名单
  ignore:
    whites:
      - /auth/logout
      - /auth/login
      - /auth/register
      - /*/v2/api-docs
      - /csrf
      - /system/book/list #注意这个!!

再请求就ok了 http://localhost/dev-api/system/book/list

取消权限注解

在进行接口开发时不仅先需要开放token校验。还需要开放权限校验。不开放的后果:

{
    "msg": "没有访问权限,请联系管理员授权",
    "code": 403
}
    /**
     * 注释RequiresPermissions
     */
//    @RequiresPermissions("system:book:list")
    @GetMapping("/list")
    public TableDataInfo list(Book book)
    {
        startPage();
        List<Book> list = bookService.selectBookList(book);
        return getDataTable(list);
    }

打印sql日志

ruoyi默认没有打印sql。我们最好开启它已方便调试哈

在system的nacos config中改写配置文件:
ruoyi-system-dev.yml

# mybatis配置
mybatis:
    # 搜索指定包别名
    typeAliasesPackage: com.ruoyi.system
    # 配置mapper的扫描,找到所有的mapper.xml映射文件
    mapperLocations: classpath:mapper/**/*.xml
    # 添加这个
    configuration:
      log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

相关文章

网友评论

      本文标题:ruoyi-cloud (一) 启动项目,熟悉接口URL的组成和

      本文链接:https://www.haomeiwen.com/subject/xxccidtx.html