RUOYI框架在实际项目中的应用三:Ruoyi微服务版本-RuoYi-Cloud

如需观看Ruoyi框架的整体介绍,请移步:RUOYI框架在实际项目中的应用一:ruoyi简介

一、Ruoyi微服务版本-Ruoyi微服务版本

1、官方资料

1:代码地址:https://gitee.com/y_project/RuoYi-Cloud.git

2:文档介绍地址:http://doc.ruoyi.vip/ruoyi-cloud/

3:演示地址:http://vue.ruoyi.vip/login

注:如果是大体量公司项目,需要使用微服务管理。就选择这个。

2、框架简介

RuoYi-Cloud 是一个 Java EE 分布式微服务架构平台,基于经典技术组合(Spring Boot、Spring Cloud & Alibaba、Vue、Element),内置模块如:部门管理、角色用户、菜单及按钮授权、数据权限、系统参数、日志管理、代码生成等。在线定时任务配置;支持集群,支持多数据源。

1:技术选型

1、系统环境

  • Java EE 8
  • Servlet 3.0
  • Apache Maven 3

2、主框架

  • Spring Boot 2.3.x
  • Spring Cloud Hoxton.SR9
  • Spring Framework 5.2.x
  • Spring Security 5.2.x

3、持久层

  • Apache MyBatis 3.5.x
  • Hibernate Validation 6.0.x
  • Alibaba Druid 1.2.x

4、视图层

  • Vue 2.6.x
  • Axios 0.21.0
  • Element 2.14.x

2:官方推荐软件环境版本

JDK >= 1.8 (推荐1.8版本)

Mysql >= 5.7.0 (推荐5.7版本)

Redis >= 3.0

Maven >= 3.0

Node >= 12

nacos >= 2.0.4 (ruoyi-cloud < 3.0 需要下载nacos >= 1.4.x版本)

sentinel >= 1.6.0

二、框架搭建

1:下载代码

https://gitee.com/y_project/RuoYi-Cloud.git

2:环境配置

在Ruoyi-cloud中需要用的如下中间件:

以上三个中间件在此不再赘述,如果公司有就用,没有的话,就自己搭建

3:配置框架数据库表

注:

  • 创建数据库ry-cloud并导入数据脚本ry_2021xxxx.sql(必须),quartz.sql(可选)
  • 创建数据库ry-config并导入数据脚本ry_config_2021xxxx.sql(如果有nacos就不用执行了)
  • 创建数据库ry-seata并导入数据脚本ry_seata_2021xxxx.sql(如果不涉及分布式事务,就不需要了)

4:修改配置

1:修改各模块下src/main/resources/bootstrap.yml

如果配置全部放在nacos下,那么就修改bootstrap.yml 的nacos配置

这里给出一个配置实例

yml 复制代码
# Tomcat
server:
  port: 9201

# Spring
spring: 
  application:
    # 应用名称
    name: ruoyi-system
  profiles:
    # 环境配置
    active: dev
  cloud:
    nacos:
      discovery:
        # 服务注册地址
        server-addr: 127.0.0.1:8848
        namespace: ruoyi_test
        group: DEFAULT_GROUP
        username: nacos
        password: nacos_2024@
      config:
        # 配置中心地址
        server-addr: ${spring.cloud.nacos.discovery.server-addr}
        # 配置文件格式
        file-extension: yml
        # 共享配置
        shared-configs:
          - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
        namespace: ${spring.cloud.nacos.discovery.namespace}
        group: ${spring.cloud.nacos.discovery.group}
        username: ${spring.cloud.nacos.discovery.username}
        password: ${spring.cloud.nacos.discovery.password}

注:

1:如果对nacos不了破的,推荐看:Nacos-01-Nacos的简介和安装

2:nacos中默认的各模块配置文件,在程序的ry_config_2021xxxx.sql 的config_info中,大家可自行获取

5:启动后端

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

  • RuoYiGatewayApplication (网关模块 必须)
  • RuoYiAuthApplication (认证模块 必须)
  • RuoYiSystemApplication (系统模块 必须)
  • RuoYiMonitorApplication (监控中心 可选)
  • RuoYiGenApplication (代码生成 可选)
  • RuoYiJobApplication (定时任务 可选)
  • RuoYFileApplication (文件服务 可选)

6:启动前段

sh 复制代码
# 进入项目目录
cd ruoyi-ui

# 安装依赖
npm install

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

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

打开浏览器,输入:(http://localhost:80 (opens new window)) 默认账户/密码 admin/admin123)

若能正确展示登录页面,并能成功登录,菜单及页面展示正常,则表明环境搭建成功

三、代码模块说明

1:后端结构

sh 复制代码
com.ruoyi     
├── ruoyi-ui              // 前端框架 [80]
├── ruoyi-gateway         // 网关模块 [8080]
├── ruoyi-auth            // 认证中心 [9200]
├── ruoyi-api             // 接口模块
│       └── ruoyi-api-system                          // 系统接口
├── ruoyi-common          // 通用模块
│       └── ruoyi-common-core                         // 核心模块
│       └── ruoyi-common-datascope                    // 权限范围
│       └── ruoyi-common-datasource                   // 多数据源
│       └── ruoyi-common-log                          // 日志记录
│       └── ruoyi-common-redis                        // 缓存服务
│       └── ruoyi-common-seata                        // 分布式事务
│       └── ruoyi-common-security                     // 安全模块
│       └── ruoyi-common-sensitive                    // 数据脱敏
│       └── ruoyi-common-swagger                      // 系统接口
├── ruoyi-modules         // 业务模块
│       └── ruoyi-system                              // 系统模块 [9201]
│       └── ruoyi-gen                                 // 代码生成 [9202]
│       └── ruoyi-job                                 // 定时任务 [9203]
│       └── ruoyi-file                                // 文件服务 [9300]
├── ruoyi-visual          // 图形化管理模块
│       └── ruoyi-visual-monitor                      // 监控中心 [9100]
├──pom.xml                // 公共依赖

2:前端结构

sh 复制代码
com.ruoyi     
├── ruoyi-ui              // 前端框架 [80]
├── ruoyi-gateway         // 网关模块 [8080]
├── ruoyi-auth            // 认证中心 [9200]
├── ruoyi-api             // 接口模块
│       └── ruoyi-api-system                          // 系统接口
├── ruoyi-common          // 通用模块
│       └── ruoyi-common-core                         // 核心模块
│       └── ruoyi-common-datascope                    // 权限范围
│       └── ruoyi-common-datasource                   // 多数据源
│       └── ruoyi-common-log                          // 日志记录
│       └── ruoyi-common-redis                        // 缓存服务
│       └── ruoyi-common-seata                        // 分布式事务
│       └── ruoyi-common-security                     // 安全模块
│       └── ruoyi-common-sensitive                    // 数据脱敏
│       └── ruoyi-common-swagger                      // 系统接口
├── ruoyi-modules         // 业务模块
│       └── ruoyi-system                              // 系统模块 [9201]
│       └── ruoyi-gen                                 // 代码生成 [9202]
│       └── ruoyi-job                                 // 定时任务 [9203]
│       └── ruoyi-file                                // 文件服务 [9300]
├── ruoyi-visual          // 图形化管理模块
│       └── ruoyi-visual-monitor                      // 监控中心 [9100]
├──pom.xml                // 公共依赖

3:实际使用过程

ruoyi的cloud框架很不错,但是很多情况下没什么用,或者用不到,那么最精简的情况是

sh 复制代码
com.ruoyi     
├── ruoyi-ui              // 前端框架 [80]
├── ruoyi-gateway         // 网关模块 [8080]
├── ruoyi-auth            // 认证中心 [9200]
├── ruoyi-api             // 接口模块
│       └── ruoyi-api-system                          // 系统接口
├── ruoyi-common          // 通用模块
│       └── ruoyi-common-core                         // 核心模块
│       └── ruoyi-common-datascope                    // 权限范围
│       └── ruoyi-common-datasource                   // 多数据源
│       └── ruoyi-common-log                          // 日志记录
│       └── ruoyi-common-redis                        // 缓存服务
│       └── ruoyi-common-security                     // 安全模块
│       └── ruoyi-common-swagger                      // 系统接口
├── ruoyi-modules         // 业务模块
│       └── ruoyi-system                              // 系统模块 [9201]
│       └── ruoyi-gen                                 // 代码生成 [9202]
│       └── ruoyi-job                                 // 定时任务 [9203]
│       └── ruoyi-file                                // 文件服务 [9300]
├──pom.xml                // 公共依赖

4:Ruoyi-cloud框架默认nacos配置

在我们启动ruoyi-cloud后,我们需要在系统中配置nacos的默认配置文件,比如

上边nacos中默认的各模块配置文件,在程序的ry_config_2021xxxx.sql 的config_info中,大家可自行获取

注:如果我们使用ruoyi-cloud自带的nacos数据库,那么打开nacos就会出现上边的配置。如果使用的是其他的nacos,大家可以从这里进行对应配置的获取。

四、功能模块说明

1:新建子模块

Maven多模块下新建子模块流程案例。

1、在ruoyi-modules下新建业务模块目录,例如:ruoyi-test。

2、在ruoyi-test业务模块下新建pom.xml文件以及src\main\java,src\main\resources目录

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>com.ruoyi</groupId>
        <artifactId>ruoyi-modules</artifactId>
        <version>x.x.x</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
	
    <artifactId>ruoyi-modules-test</artifactId>

    <description>
        ruoyi-modules-test系统模块
    </description>
	
    <dependencies>
    	
    	<!-- SpringCloud Alibaba Nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        
        <!-- SpringCloud Alibaba Nacos Config -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        
    	<!-- SpringCloud Alibaba Sentinel -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        
    	<!-- SpringBoot Actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
		
        <!-- Mysql Connector -->
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
        </dependency>
        
        <!-- Ruoyi Common Security -->
        <dependency>
            <groupId>com.ruoyi</groupId>
            <artifactId>ruoyi-common-security</artifactId>
        </dependency>
        
        <!-- Ruoyi Common Swagger -->
        <dependency>
            <groupId>com.ruoyi</groupId>
            <artifactId>ruoyi-common-swagger</artifactId>
        </dependency>
		
		<!-- RuoYi Common Log -->
        <dependency>
            <groupId>com.ruoyi</groupId>
            <artifactId>ruoyi-common-log</artifactId>
        </dependency>
        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
   
</project>

3、在ruoyi-modules目录下pom.xml模块节点modules添加业务模块

xml 复制代码
<module>ruoyi-test</module>

4、src/main/resources添加bootstrap.yml文件

yml 复制代码
# Tomcat
server:
  port: 9301

# Spring
spring: 
  application:
    # 应用名称
    name: ruoyi-test
  profiles:
    # 环境配置
    active: dev
  cloud:
    nacos:
      discovery:
        # 服务注册地址
        server-addr: 127.0.0.1:8848
      config:
        # 配置中心地址
        server-addr: 127.0.0.1:8848
        # 配置文件格式
        file-extension: yml
        # 共享配置
        shared-configs:
          - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}

5、com.ruoyi.test包下添加主启动类

java 复制代码
package com.ruoyi.test;

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import com.ruoyi.common.security.annotation.EnableCustomConfig;
import com.ruoyi.common.security.annotation.EnableRyFeignClients;
import com.ruoyi.common.swagger.annotation.EnableCustomSwagger2;

/**
 * 测试模块
 * 
 * @author ruoyi
 */
@EnableCustomConfig
@EnableCustomSwagger2
@EnableRyFeignClients
@SpringCloudApplication
public class RuoYiTestApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(RuoYiTestApplication.class, args);
        System.out.println("(♥◠‿◠)ノ゙  测试模块启动成功   ლ(´ڡ`ლ)゙  \n" +
                " .-------.       ____     __        \n" +
                " |  _ _   \\      \\   \\   /  /    \n" +
                " | ( ' )  |       \\  _. /  '       \n" +
                " |(_ o _) /        _( )_ .'         \n" +
                " | (_,_).' __  ___(_ o _)'          \n" +
                " |  |\\ \\  |  ||   |(_,_)'         \n" +
                " |  | \\ `'   /|   `-'  /           \n" +
                " |  |  \\    /  \\      /           \n" +
                " ''-'   `'-'    `-..-'              ");
    }
}

2:系统接口-springDoc冒烟测试

  • ruoyi-cloud版本<3.6.4 时,使用的是swagger 作为系统接口文档展示,
  • ruoyi-cloud版本>3.6.4 时,改用了SpringDoc作为系统接口文档展示。

3:ruoyi-cloud版本<3.6.4 时,使用的是swagger

swagger的基础使用,请看:swagger-springboot详解

1>:使用ruoyi框架后自己创建swagger

1):添加依赖
xml 复制代码
添加依赖

<!-- SpringBoot Web -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Swagger -->
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger2</artifactId>
	<version>${swagger.fox.version}</version>
</dependency>

<!-- Swagger UI -->
<dependency>
	<groupId>io.springfox</groupId>
	<artifactId>springfox-swagger-ui</artifactId>
	<version>${swagger.fox.version}</version>
2):在application.yml添加服务配置
yml 复制代码
swagger:
  title: 系统模块接口文档
  license: Powered By ruoyi
  licenseUrl: https://ruoyi.vip
3):在Application启动类加入注解@SpringBootApplication和@EnableSwagger2。
java 复制代码
@EnableSwagger2
@SpringBootApplication
public class RuoYiSwaggerApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(RuoYiSwaggerApplication.class, args);
        System.out.println("(♥◠‿◠)ノ゙  Swagger启动成功   ლ(´ڡ`ლ)゙  \n" +
                " .-------.       ____     __        \n" +
                " |  _ _   \\      \\   \\   /  /    \n" +
                " | ( ' )  |       \\  _. /  '       \n" +
                " |(_ o _) /        _( )_ .'         \n" +
                " | (_,_).' __  ___(_ o _)'          \n" +
                " |  |\\ \\  |  ||   |(_,_)'         \n" +
                " |  | \\ `'   /|   `-'  /           \n" +
                " |  |  \\    /  \\      /           \n" +
                " ''-'   `'-'    `-..-'              ");
    }
}

2>:项目中存在ruoyi-common-swagger模块,可忽略上边三个步骤,可以直接依赖后使用。

1)、业务模块添加依赖
xml 复制代码
<!-- ruoyi common swagger -->
<dependency>
	<groupId>com.ruoyi</groupId>
	<artifactId>ruoyi-common-swagger</artifactId>
</dependency>
2):增加配置文件参数
yml 复制代码
# swagger配置
swagger:
  title: 系统模块接口文档
  license: Powered By ruoyi
  licenseUrl: https://ruoyi.vip
3):在Application启动类加入系统接口注解@EnableCustomSwagger2
java 复制代码
@EnableCustomConfig
@EnableCustomSwagger2
@EnableRyFeignClients
@SpringCloudApplication
public class RuoYiSystemApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(RuoYiSystemApplication.class, args);
        System.out.println("(♥◠‿◠)ノ゙  系统模块启动成功   ლ(´ڡ`ლ)゙  \n" +
                " .-------.       ____     __        \n" +
                " |  _ _   \\      \\   \\   /  /    \n" +
                " | ( ' )  |       \\  _. /  '       \n" +
                " |(_ o _) /        _( )_ .'         \n" +
                " | (_,_).' __  ___(_ o _)'          \n" +
                " |  |\\ \\  |  ||   |(_,_)'         \n" +
                " |  | \\ `'   /|   `-'  /           \n" +
                " |  |  \\    /  \\      /           \n" +
                " ''-'   `'-'    `-..-'              ");
    }
}
4):测试验证

访问http://{ip}:{port}/swagger-ui/index.html地址,出现如下图表示成功。

4:ruoyi-cloud版本>3.6.4 时,改用了SpringDoc

1>:使用ruoyi框架后自己创建swagger

1)、添加依赖
xml 复制代码
<!-- RuoYi Common Swagger -->
<dependency>
	<groupId>com.ruoyi</groupId>
	<artifactId>ruoyi-common-swagger</artifactId>
</dependency>
2)、在application.yml添加服务配置
yml 复制代码
server:
  port: 6666

spring:
  application:
    name: ruoyi-xxxx

# springdoc配置
springdoc:
  # 通过网关使用地址
  # gatewayUrl: http://localhost:8080/${spring.application.name}
  # 单独运行使用本机
  gatewayUrl: http://localhost:6666
  api-docs:
    # 是否开启接口文档
    enabled: true
  info:
    # 标题
    title: '测试接口文档'
    # 描述
    description: '测试接口描述'
    # 作者信息
    contact:
      name: RuoYi
      url: https://ruoyi.vip
3)、在Application启动类加入注解@SpringBootApplication。
java 复制代码
@SpringBootApplication
public class RuoYiSwaggerApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(RuoYiSwaggerApplication.class, args);
        System.out.println("(♥◠‿◠)ノ゙  Swagger启动成功   ლ(´ڡ`ლ)゙  \n" +
                " .-------.       ____     __        \n" +
                " |  _ _   \\      \\   \\   /  /    \n" +
                " | ( ' )  |       \\  _. /  '       \n" +
                " |(_ o _) /        _( )_ .'         \n" +
                " | (_,_).' __  ___(_ o _)'          \n" +
                " |  |\\ \\  |  ||   |(_,_)'         \n" +
                " |  | \\ `'   /|   `-'  /           \n" +
                " |  |  \\    /  \\      /           \n" +
                " ''-'   `'-'    `-..-'              ");
    }
}

2>:项目中存在ruoyi-common-swagger模块,可忽略上边三个步骤,可以直接依赖后使用。

1)、业务模块添加依赖
yml 复制代码
<!-- ruoyi common swagger -->
<dependency>
	<groupId>com.ruoyi</groupId>
	<artifactId>ruoyi-common-swagger</artifactId>
</dependency>
2)、在ruoyi-xxxx-dev.yml添加接口配置
yml 复制代码
springdoc方式,ruoyi-cloud版本 > 3.6.4 使用
# springdoc配置  
springdoc:
  # 通过网关使用地址
  # gatewayUrl: http://localhost:8080/${spring.application.name}
  # 单独运行使用本机
  gatewayUrl: http://localhost:6666
  api-docs:
    # 是否开启接口文档
    enabled: true
  info:
    # 标题
    title: '测试接口文档'
    # 描述
    description: '测试接口描述'
    # 作者信息
    contact:
      name: RuoYi
      url: https://ruoyi.vip
3):测试验证

访问http://{ip}:{port}/swagger-ui/index.html地址

5:其他拓展功能

除了ruoyi内置的很多功能以外,如果我们想对系统代码进行中间技术框架替换,如knife4j替换swagger,使用minio,使用es等,我们都可以从容的增加和使用,在ruoyi网站中也有类似的文档指导,大家可以参考:

6:如何更换项目包路径

我们使用过程中还有很多问题,比如因为ruoyi默认的是com.ruoyi路径,我们实际使用过程中要改成公司名,比如com.baidu。此时ruoyi也为我们提供了很多常用工具。


相关推荐
码银7 分钟前
事务管理:确保数据一致性与业务完整性
java
程序猿大波10 分钟前
基于Java,SpringBoot,Vue,HTML家政服务预约系统设计
java·vue.js·spring boot
小徐Chao努力27 分钟前
【解析】ReentrantLock锁、Syschronized锁面试点解析
java·面试·职场和发展·synchronized·
V少年37 分钟前
深入浅出Java线程池
android·java
顾林海43 分钟前
深度解析Hashtable工作原理
android·java·面试
只恨天高1 小时前
SpringBoot异常处理之注解@ExceptionHander和@ControllerAdvice
java·spring boot·spring
都叫我大帅哥1 小时前
Java中的Collection必知必会
java
都叫我大帅哥1 小时前
代码界的「跨界婚姻」:桥接模式的鹊桥艺术
java·后端·设计模式
汤姆yu1 小时前
Spring 中的 @Cacheable 缓存注解
java·spring·缓存
老码识土1 小时前
kotlin 协程源代码泛读:引子
java