基于Spring Boot/Cloud的企业级多子模块基础类库集合
UW-Base 是一套基于Spring Boot 3.x和Spring Cloud 2025.x构建的企业级多子模块基础类库集合。该项目旨在为微服务架构提供完整、稳定、高性能的基础设施支持,涵盖了从数据访问、缓存管理、认证授权到任务调度、日志收集等全方位的功能模块。
本项目采用模块化设计,各子模块既可独立使用,也可协同工作,为构建大规模分布式系统提供坚实的基础支撑。
UW-Base 提供了以下核心能力:
| 功能领域 | 核心能力 | 对应模块 |
|---|---|---|
| 数据访问 | 多数据源支持、分库分表、JPA风格CRUD、SQL映射 | uw-dao |
| 缓存管理 | 本地缓存(Caffeine)、分布式缓存(Redis)、融合缓存、全局锁、计数器 | uw-cache |
| 认证授权 | Token管理、权限控制、访问限速、操作日志、多因素认证 | uw-auth-service, uw-auth-client, uw-mfa |
| 任务调度 | 定时任务、队列任务、RPC调用、流量控制、任务监控 | uw-task |
| 日志收集 | Elasticsearch日志写入、Logback集成、日志查询 | uw-log-es, uw-logback-es |
| HTTP通信 | HTTP客户端、JSON/XML序列化、接口日志 | uw-httpclient |
| AI集成 | Chat对话、Embedding向量、RAG检索、MCP协议、翻译 | uw-ai |
| OAuth2登录 | 多平台OAuth2登录、扫码登录、账号绑定 | uw-oauth2-client |
| 通用工具 | 响应封装、加密解密、日期处理、JSON工具 | uw-common, uw-common-app |
通用工具类库,提供项目基础支撑能力。
主要功能:
ResponseData<T>:统一响应数据封装,支持函数式链式调用BitConfigUtils:位运算配置工具,支持32/64位开关存储DateUtils:灵活的日期处理工具JsonUtils:JSON序列化/反序列化工具AESUtils/RSAUtils/DigestUtils:加密解密与签名工具SnowflakeIdGenerator:分布式雪花ID生成器IpMatchUtils:IP匹配工具,支持CIDR格式ByteArrayUtils:字节数组操作工具技术栈:Java 21, Jackson
使用场景:所有需要统一响应格式和基础工具的项目
基于Web应用的公共类库,提供后台应用通用功能。
主要功能:
技术栈:Spring Boot, uw-dao, uw-auth-service
使用场景:后台管理系统、需要数据审计和配置管理的应用
数据库访问层类库,比Hibernate更高效,比MyBatis更简单。
主要功能:
技术栈:Java 21, JDBC, Kryo
使用场景:需要高性能数据访问、分库分表、复杂SQL查询的业务系统
基于Caffeine和Redis的融合缓存类库。
主要功能:
技术栈:Caffeine, Redis, Lettuce, Kryo
使用场景:高并发缓存、分布式锁、计数器、延迟队列等场景
性能数据(MBP16 M2Max, 20线程):
认证授权服务的服务端模块,提供完整的权限管理能力。
主要功能:
技术栈:Spring Boot, Spring Cloud, Redis
使用场景:需要完整权限管理的微服务应用
认证授权的客户端模块,用于内部RPC调用的鉴权。
主要功能:
技术栈:Spring Boot, uw-httpclient
使用场景:微服务间的内部RPC调用
多因素认证(MFA)类库,融合IP限制、验证码、设备码认证。
主要功能:
MfaFusionHelper:融合MFA帮助类,统一处理认证流程技术栈:Redis, Java 2D
使用场景:登录认证、敏感操作验证、防暴力破解
分布式任务框架,支持定时任务和队列任务。
主要功能:
技术栈:Spring Boot, RabbitMQ, Redis
使用场景:分布式定时任务、异步队列处理、RPC调用
Elasticsearch日志客户端,支持日志的写入和查询。
主要功能:
技术栈:Elasticsearch, uw-httpclient
使用场景:日志集中存储与分析
Logback的Elasticsearch Appender,直接将日志发送到ES。
主要功能:
技术栈:Logback, Elasticsearch
使用场景:替代传统ELK架构中的Logstash环节
针对接口业务设计的HTTP客户端类库。
主要功能:
技术栈:OkHttp, Jackson
使用场景:第三方接口对接、微服务HTTP调用
基于AI技术的类库,集成多种AI能力。
主要功能:
技术栈:Spring AI, OpenAI/Claude等模型API
使用场景:智能客服、知识库问答、内容生成
轻量级OAuth2客户端库,支持多平台第三方登录。
主要功能:
技术栈:Java 21, uw-httpclient
使用场景:C端用户登录、第三方账号集成
网关客户端,提供服务网关相关功能支持。
基于SSE(Server-Sent Events)技术的Web通知库。
短链接客户端,提供短链接生成与解析功能。
MyDB服务客户端,用于访问MyDB数据服务。
| 组件 | 版本要求 | 说明 |
|---|---|---|
| JDK | 21+ | 必须使用Java 21或更高版本 |
| Maven | 3.8+ | 项目构建工具 |
| Spring Boot | 3.5.6 | 基础框架版本 |
| Spring Cloud | 2025.0.0 | 微服务框架版本 |
| Spring Cloud Alibaba | 2023.0.1.2 | 阿里云微服务组件 |
| 组件 | 版本 | 用途 |
|---|---|---|
| MySQL | 8.0+ | 数据存储 |
| Redis | 6.0+ | 缓存、分布式锁 |
| RabbitMQ | 3.8+ | 任务队列 |
| Elasticsearch | 8.x | 日志存储与搜索 |
在项目的 pom.xml 中添加UW-Base的依赖管理:
<parent>
<groupId>com.umtone</groupId>
<artifactId>uw-base</artifactId>
<version>2026.0301.0002</version>
<relativePath/>
</parent>
根据需求引入具体模块:
<dependencies>
<!-- 基础工具 -->
<dependency>
<groupId>com.umtone</groupId>
<artifactId>uw-common</artifactId>
</dependency>
<!-- 数据访问 -->
<dependency>
<groupId>com.umtone</groupId>
<artifactId>uw-dao</artifactId>
</dependency>
<!-- 缓存 -->
<dependency>
<groupId>com.umtone</groupId>
<artifactId>uw-cache</artifactId>
</dependency>
<!-- 认证授权 -->
<dependency>
<groupId>com.umtone</groupId>
<artifactId>uw-auth-service</artifactId>
</dependency>
</dependencies>
# 数据源配置(uw-dao)
uw:
dao:
conn-pool:
default:
driver: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: password
min-conn: 5
max-conn: 20
# 缓存配置(uw-cache)
cache:
redis:
host: localhost
port: 6379
password:
database: 0
# 认证配置(uw-auth-service)
auth:
service:
app-label: "我的应用"
auth-center-host: http://uw-auth-center
auth-entry-point: "/api/*"
@RestController
public class UserController {
@GetMapping("/user/{id}")
public ResponseData<User> getUser(@PathVariable Long id) {
User user = userService.findById(id);
if (user == null) {
return ResponseData.errorCode(UserResponseCode.USER_NOT_FOUND);
}
return ResponseData.success(user);
}
}
@Service
public class UserService {
private final DaoManager dao = DaoManager.getInstance();
public ResponseData<User> findById(Long id) {
return dao.load(User.class, id)
.map(ResponseData::success)
.orElse(ResponseData.errorCode(UserResponseCode.USER_NOT_FOUND));
}
public ResponseData<DataList<User>> listUsers(UserQueryParam param) {
return dao.list(User.class, param);
}
}
@Component
public class UserCache {
@PostConstruct
public void init() {
FusionCache.Config config = new FusionCache.Config(
User.class,
1000, // 本地缓存最大数量
86400_000L // 缓存过期时间(毫秒)
);
FusionCache.config(config, new CacheDataLoader<Long, User>() {
@Override
public User load(Long userId) {
return userDao.findById(userId).orElse(null);
}
});
}
public User getUser(Long userId) {
return FusionCache.get(User.class, userId);
}
}
各模块的详细API文档请参考各子模块的README.md文件:
我们欢迎社区贡献!如果您想为UW-Base做出贡献,请遵循以下步骤:
git checkout -b feature/AmazingFeature)git commit -m 'Add some AmazingFeature')git push origin feature/AmazingFeature)本项目采用 Apache License 2.0 开源许可证。
Copyright 2025 UW-Base Contributors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Star ⭐ 本项目如果它对您有帮助!