feat: D.3/F.1/F.3任务实施 - Nacos配置中心/读写分离/索引优化
## D.3 Nacos统一配置中心 - 添加spring-cloud-starter-alibaba-nacos-config依赖 - 创建bootstrap.yaml配置文件 - DynamicConfig: @RefreshScope动态配置刷新示例 ## F.1 ShardingSphere读写分离 - 添加shardingsphere-jdbc-core依赖 - sharding-config.yaml: 读写分离配置示例 - 支持主从切换、负载均衡策略 ## F.3 数据库索引优化 - db-index-optimization.sql: 全库索引优化脚本 - 用户/角色/菜单/部门表索引 - 用款/支出/收款表索引 - 复合索引优化常用查询场景
This commit is contained in:
parent
23c8f81ebd
commit
da445a44de
137
doc/db-index-optimization.sql
Normal file
137
doc/db-index-optimization.sql
Normal file
@ -0,0 +1,137 @@
|
|||||||
|
-- =====================================================
|
||||||
|
-- 资金平台数据库索引优化SQL
|
||||||
|
-- 执行说明: 根据实际查询场景选择执行
|
||||||
|
-- =====================================================
|
||||||
|
|
||||||
|
-- 使用fund_sys数据库
|
||||||
|
USE fund_sys;
|
||||||
|
|
||||||
|
-- =====================================================
|
||||||
|
-- 用户表索引
|
||||||
|
-- =====================================================
|
||||||
|
|
||||||
|
-- 用户名唯一索引(已存在可跳过)
|
||||||
|
CREATE UNIQUE INDEX IF NOT EXISTS idx_sys_user_username ON sys_user(username);
|
||||||
|
|
||||||
|
-- 租户ID索引(多租户查询)
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sys_user_tenant_id ON sys_user(tenant_id);
|
||||||
|
|
||||||
|
-- 部门ID索引(按部门查询用户)
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sys_user_dept_id ON sys_user(dept_id);
|
||||||
|
|
||||||
|
-- 状态索引(查询启用/禁用用户)
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sys_user_status ON sys_user(status);
|
||||||
|
|
||||||
|
-- 创建时间索引(按时间范围查询)
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sys_user_created_time ON sys_user(created_time);
|
||||||
|
|
||||||
|
-- 复合索引: 租户+状态(常用查询组合)
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sys_user_tenant_status ON sys_user(tenant_id, status);
|
||||||
|
|
||||||
|
-- =====================================================
|
||||||
|
-- 角色表索引
|
||||||
|
-- =====================================================
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sys_role_tenant_id ON sys_role(tenant_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sys_role_status ON sys_role(status);
|
||||||
|
|
||||||
|
-- =====================================================
|
||||||
|
-- 菜单表索引
|
||||||
|
-- =====================================================
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sys_menu_parent_id ON sys_menu(parent_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sys_menu_type ON sys_menu(type);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sys_menu_sort ON sys_menu(sort);
|
||||||
|
|
||||||
|
-- =====================================================
|
||||||
|
-- 部门表索引
|
||||||
|
-- =====================================================
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sys_dept_parent_id ON sys_dept(parent_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sys_dept_tenant_id ON sys_dept(tenant_id);
|
||||||
|
|
||||||
|
-- =====================================================
|
||||||
|
-- 用户角色关联表索引
|
||||||
|
-- =====================================================
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sys_user_role_user_id ON sys_user_role(user_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sys_user_role_role_id ON sys_user_role(role_id);
|
||||||
|
|
||||||
|
-- =====================================================
|
||||||
|
-- 角色菜单关联表索引
|
||||||
|
-- =====================================================
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sys_role_menu_role_id ON sys_role_menu(role_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sys_role_menu_menu_id ON sys_role_menu(menu_id);
|
||||||
|
|
||||||
|
-- =====================================================
|
||||||
|
-- 操作日志表索引
|
||||||
|
-- =====================================================
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sys_log_user_id ON sys_log(user_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sys_log_tenant_id ON sys_log(tenant_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sys_log_operation ON sys_log(operation);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sys_log_created_time ON sys_log(created_time);
|
||||||
|
-- 复合索引: 租户+时间(常用日志查询)
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_sys_log_tenant_time ON sys_log(tenant_id, created_time);
|
||||||
|
|
||||||
|
|
||||||
|
-- =====================================================
|
||||||
|
-- 使用fund_req数据库(用款申请)
|
||||||
|
-- =====================================================
|
||||||
|
|
||||||
|
USE fund_req;
|
||||||
|
|
||||||
|
-- 用款申请表索引
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_fund_request_tenant_id ON fund_request(tenant_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_fund_request_user_id ON fund_request(user_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_fund_request_status ON fund_request(status);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_fund_request_created_time ON fund_request(created_time);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_fund_request_project_id ON fund_request(project_id);
|
||||||
|
-- 复合索引: 租户+状态+时间(审批列表查询)
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_fund_request_tenant_status_time ON fund_request(tenant_id, status, created_time);
|
||||||
|
|
||||||
|
|
||||||
|
-- =====================================================
|
||||||
|
-- 使用fund_exp数据库(支出管理)
|
||||||
|
-- =====================================================
|
||||||
|
|
||||||
|
USE fund_exp;
|
||||||
|
|
||||||
|
-- 支出表索引
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_fund_expense_tenant_id ON fund_expense(tenant_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_fund_expense_request_id ON fund_expense(request_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_fund_expense_status ON fund_expense(status);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_fund_expense_created_time ON fund_expense(created_time);
|
||||||
|
-- 复合索引: 租户+支付状态
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_fund_expense_tenant_status ON fund_expense(tenant_id, status);
|
||||||
|
|
||||||
|
|
||||||
|
-- =====================================================
|
||||||
|
-- 使用fund_receipt数据库(收款管理)
|
||||||
|
-- =====================================================
|
||||||
|
|
||||||
|
USE fund_receipt;
|
||||||
|
|
||||||
|
-- 收款表索引
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_fund_receipt_tenant_id ON fund_receipt(tenant_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_fund_receipt_project_id ON fund_receipt(project_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_fund_receipt_status ON fund_receipt(status);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_fund_receipt_created_time ON fund_receipt(created_time);
|
||||||
|
-- 复合索引: 租户+确认状态
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_fund_receipt_tenant_status ON fund_receipt(tenant_id, status);
|
||||||
|
|
||||||
|
|
||||||
|
-- =====================================================
|
||||||
|
-- 分析慢查询(可选)
|
||||||
|
-- =====================================================
|
||||||
|
|
||||||
|
-- 查看表的索引情况
|
||||||
|
-- SHOW INDEX FROM sys_user;
|
||||||
|
|
||||||
|
-- 分析查询执行计划
|
||||||
|
-- EXPLAIN SELECT * FROM sys_user WHERE tenant_id = 1 AND status = 1;
|
||||||
|
|
||||||
|
-- 查看慢查询日志(需要开启慢查询日志)
|
||||||
|
-- SHOW VARIABLES LIKE 'slow_query_log';
|
||||||
|
-- SHOW VARIABLES LIKE 'long_query_time';
|
||||||
@ -41,6 +41,13 @@
|
|||||||
<artifactId>mysql-connector-j</artifactId>
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- ShardingSphere 读写分离 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.shardingsphere</groupId>
|
||||||
|
<artifactId>shardingsphere-jdbc-core</artifactId>
|
||||||
|
<version>5.4.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- HikariCP -->
|
<!-- HikariCP -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.zaxxer</groupId>
|
<groupId>com.zaxxer</groupId>
|
||||||
@ -80,6 +87,31 @@
|
|||||||
<groupId>org.springframework.security</groupId>
|
<groupId>org.springframework.security</groupId>
|
||||||
<artifactId>spring-security-crypto</artifactId>
|
<artifactId>spring-security-crypto</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Nacos服务注册发现 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Nacos配置中心 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Spring Cloud Bootstrap (用于配置中心) -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-starter-bootstrap</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Logstash Logback Encoder (用于ELK) -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.logstash.logback</groupId>
|
||||||
|
<artifactId>logstash-logback-encoder</artifactId>
|
||||||
|
<version>7.4</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@ -0,0 +1,28 @@
|
|||||||
|
package com.fundplatform.sys.config;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 动态配置示例
|
||||||
|
* 通过Nacos配置中心动态刷新配置
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@RefreshScope
|
||||||
|
public class DynamicConfig {
|
||||||
|
|
||||||
|
@Value("${app.feature.switch:false}")
|
||||||
|
private Boolean featureSwitch;
|
||||||
|
|
||||||
|
@Value("${app.max-request-size:10MB}")
|
||||||
|
private String maxRequestSize;
|
||||||
|
|
||||||
|
public Boolean isFeatureEnabled() {
|
||||||
|
return featureSwitch;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMaxRequestSize() {
|
||||||
|
return maxRequestSize;
|
||||||
|
}
|
||||||
|
}
|
||||||
23
fund-sys/src/main/resources/bootstrap.yaml
Normal file
23
fund-sys/src/main/resources/bootstrap.yaml
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
spring:
|
||||||
|
application:
|
||||||
|
name: fund-sys
|
||||||
|
cloud:
|
||||||
|
nacos:
|
||||||
|
# Nacos配置中心
|
||||||
|
config:
|
||||||
|
server-addr: localhost:8848
|
||||||
|
namespace: fund-platform
|
||||||
|
group: DEFAULT_GROUP
|
||||||
|
file-extension: yaml
|
||||||
|
# 共享配置
|
||||||
|
shared-configs:
|
||||||
|
- data-id: common.yaml
|
||||||
|
group: DEFAULT_GROUP
|
||||||
|
refresh: true
|
||||||
|
# 动态刷新
|
||||||
|
refresh-enabled: true
|
||||||
|
# Nacos服务发现
|
||||||
|
discovery:
|
||||||
|
server-addr: localhost:8848
|
||||||
|
namespace: fund-platform
|
||||||
|
group: DEFAULT_GROUP
|
||||||
49
fund-sys/src/main/resources/sharding-config.yaml
Normal file
49
fund-sys/src/main/resources/sharding-config.yaml
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
# =====================================================
|
||||||
|
# ShardingSphere 读写分离配置示例
|
||||||
|
# 生产环境使用时取消注释并修改为实际的数据库地址
|
||||||
|
# =====================================================
|
||||||
|
|
||||||
|
#spring:
|
||||||
|
# datasource:
|
||||||
|
# driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver
|
||||||
|
# url: jdbc:shardingsphere:classpath:sharding-config.yaml
|
||||||
|
|
||||||
|
# =====================================================
|
||||||
|
# sharding-config.yaml (放在resources目录下)
|
||||||
|
# =====================================================
|
||||||
|
#mode:
|
||||||
|
# type: Standalone
|
||||||
|
# repository:
|
||||||
|
# type: JDBC
|
||||||
|
#
|
||||||
|
#dataSources:
|
||||||
|
# ds_master:
|
||||||
|
# dataSourceClassName: com.zaxxer.hikari.HikariDataSource
|
||||||
|
# driverClassName: com.mysql.cj.jdbc.Driver
|
||||||
|
# jdbcUrl: jdbc:mysql://master-host:3306/fund_sys?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
|
||||||
|
# username: root
|
||||||
|
# password: zjf@123456
|
||||||
|
# ds_slave_0:
|
||||||
|
# dataSourceClassName: com.zaxxer.hikari.HikariDataSource
|
||||||
|
# driverClassName: com.mysql.cj.jdbc.Driver
|
||||||
|
# jdbcUrl: jdbc:mysql://slave-host:3306/fund_sys?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
|
||||||
|
# username: root
|
||||||
|
# password: zjf@123456
|
||||||
|
#
|
||||||
|
#rules:
|
||||||
|
# - !READWRITE_SPLITTING
|
||||||
|
# dataSources:
|
||||||
|
# readwrite_ds:
|
||||||
|
# writeDataSourceName: ds_master
|
||||||
|
# readDataSourceNames:
|
||||||
|
# - ds_slave_0
|
||||||
|
# transactionalReadQueryStrategy: PRIMARY
|
||||||
|
# loadBalancerName: round_robin
|
||||||
|
# loadBalancers:
|
||||||
|
# round_robin:
|
||||||
|
# type: ROUND_ROBIN
|
||||||
|
# props:
|
||||||
|
# default: 0
|
||||||
|
#
|
||||||
|
#props:
|
||||||
|
# sql-show: true
|
||||||
Loading…
x
Reference in New Issue
Block a user