AggregateX 整合了 CQRS 模式、事件溯源、事件总线等 DDD 核心概念,为开发复杂业务系统提供完整的架构支持,帮助开发团队更好地实现领域驱动设计。
提供标准的领域驱动设计分层架构,确保代码结构清晰、职责明确。
读写分离的命令查询职责分离模式,提升系统性能和扩展性。
内置强大的DDD模块代码生成器,快速搭建标准化的领域模块。
完善的事件总线和事件溯源机制,支持微服务间的异步通信。
内置安全特性,确保应用程序的安全性和可靠性。
优化的架构设计,支持高并发和大规模应用场景。
三个步骤快速上手 AggregateX DDD 框架
dependencies {
implementation 'cn.treedeep:aggregatex-ddd-framework:1.0.0'
}
<dependency>
<groupId>cn.treedeep</groupId>
<artifactId>aggregatex-ddd-framework</artifactId>
<version>1.0.0</version>
</dependency>
在主应用类上添加 @EnableAggregateX
注解:
@SpringBootApplication
@EnableAggregateX
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
快速生成标准的DDD模块结构:
# 运行代码生成器
java -cp build/libs/AggregateX-1.0.0.jar cn.treedeep.king.tools.DDDModuleGenerator
# 交互式输入
📁 请输入项目路径 (默认为当前路径 '.'):
📦 请输入模块名称,可空格带注释 (如: user 用户, order 订单): user 用户管理
生成器将自动创建: 聚合根和实体ID、命令/查询处理器、REST控制器、JPA仓储实现、标准DTO和转换器
配置应用的基础设置和数据源:
spring:
application:
name: AggregateX
profiles:
active: dev
mvc:
static-path-pattern: /**
web:
resources:
static-locations: classpath:/static/
datasource:
type: com.zaxxer.hikari.HikariDataSource
hikari:
minimum-idle: 5
maximum-pool-size: 20
auto-commit: true
idle-timeout: 3000
pool-name: DatebookHikariCP
max-lifetime: 1800000
connection-timeout: 30000
connection-test-query: SELECT 1
validation-timeout: 3000
login-timeout: 5
read-only: false
jpa:
hibernate:
ddl-auto: none
show-sql: false
open-in-view: false
data:
redis:
timeout: 60000
database: 0
lettuce:
pool:
max-active: 8
max-wait: -1ms
max-idle: 8
min-idle: 0
cache:
type: caffeine
cache-names:
- events
- snapshots
- archives
- aggregates
caffeine:
spec: maximumSize=10000,expireAfterWrite=3600s
# AggregateX DDD 框架配置
app:
event-store:
type: memory # 事件存储类型:memory/jpa
table-name: events # 事件表名称(JPA模式)
batch-size: 1000 # 批量操作大小
snapshot:
enabled: true # 是否启用快照
frequency: 100 # 快照频率(事件数)
event-bus:
type: simple # 事件总线类型:simple/rabbitmq
async: true # 是否启用异步处理
pool-size: 5 # 异步处理线程池大小
retry:
max-attempts: 3 # 最大重试次数
initial-delay: 1000 # 初始重试延迟(毫秒)
cqrs:
async:
core-pool-size: 5
max-pool-size: 10
queue-capacity: 25
thread-name-prefix: async-command-
retry:
max-attempts: 3
initial-delay: 1000
multiplier: 2.0
max-delay: 10000
validation:
fail-fast: true
validation-enabled: true
# 日志配置
logging:
config: classpath:logback-spring.xml
level:
cn.treedeep.king: debug
# 监控配置
management:
endpoints:
web:
exposure:
include: health,metrics,prometheus
server:
port: 9000
servlet:
context-path: /
spring:
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/ddd
username: root
password: 123456
jpa:
hibernate:
ddl-auto: update
show-sql: true
data:
redis:
host: localhost
port: 6379
password: 123456
mail:
host: smtp.qq.com
port: 465
protocol: smtp
username:
password:
properties:
mail:
smtp:
connectiontimeout: 5000
timeout: 3000
writetimeout: 5000
auth: true
ssl:
enable: true
app:
event-archive:
enabled: true
audit:
enabled: true
# King 平台配置
king:
work-dir: .king
tls: false
host: localhost
server-url:
oss:
aliyun:
access-key-id:
access-key-secret:
bucket: sense-sports
endpoint: oss-cn-shanghai.aliyuncs.com
chunkSize: 8192
sms:
default-code: 996900
aliyun:
access-key-id:
access-key-secret:
endpoint: dysmsapi.aliyuncs.com
sign-name: 树深技术
# API 文档配置
knife4j:
enable: true
setting:
enable-footer-custom: true
footer-custom-content: KingAdmin API Documentation
springdoc:
swagger-ui:
path: /swagger-ui.html
tags-sorter: alpha
operations-sorter: alpha
几分钟内,您就可以开始使用 AggregateX 构建您的 DDD 应用。
查看完整文档基于现代 Java 生态系统构建
看看如何使用 AggregateX 构建 DDD 应用
@AggregateRoot
public class User extends BaseAggregateRoot<UserId> {
private String username;
private String email;
private UserStatus status;
// 构造函数
public User(String username, String email) {
super(new UserId());
this.username = username;
this.email = email;
this.status = UserStatus.INACTIVE;
}
// 业务方法
public void activate() {
if (this.status == UserStatus.ACTIVE) {
throw new DomainException("用户已激活");
}
this.status = UserStatus.ACTIVE;
// 发布领域事件
registerEvent(new UserActivatedEvent(getId()));
}
}
@Component
public class CreateUserCommandHandler
implements CommandHandler<CreateUserCommand, UserId> {
private final UserRepository userRepository;
public CreateUserCommandHandler(UserRepository repository) {
this.userRepository = repository;
}
@Override
public UserId handle(CreateUserCommand command) {
// 业务验证
if (userRepository.existsByEmail(command.getEmail())) {
throw new BusinessException("邮箱已存在");
}
// 创建聚合根
User user = new User(
command.getUsername(),
command.getEmail()
);
return userRepository.save(user).getId();
}
}
@Component
public class GetUserQueryHandler
implements QueryHandler<GetUserQuery, UserDTO> {
private final UserQueryRepository queryRepository;
public GetUserQueryHandler(UserQueryRepository repository) {
this.queryRepository = repository;
}
@Override
public UserDTO handle(GetUserQuery query) {
// 查询用户数据
return queryRepository
.findById(query.getUserId())
.map(UserDTO::from)
.orElseThrow(() -> new NotFoundException(
"用户不存在: " + query.getUserId()
));
}
// 批量查询支持
public List<UserDTO> findActiveUsers() {
return queryRepository.findByStatus(UserStatus.ACTIVE);
}
}
@RestController
@RequestMapping("/api/users")
public class UserController {
private final CommandBus commandBus;
private final QueryBus queryBus;
public UserController(CommandBus commandBus,
QueryBus queryBus) {
this.commandBus = commandBus;
this.queryBus = queryBus;
}
@PostMapping
public ResponseEntity<UserId> createUser(
@RequestBody CreateUserRequest request) {
CreateUserCommand command = new CreateUserCommand(
request.getUsername(),
request.getEmail()
);
UserId userId = commandBus.send(command);
return ResponseEntity.ok(userId);
}
@GetMapping("/{id}")
public UserDTO getUser(@PathVariable String id) {
return queryBus.send(new GetUserQuery(id));
}
}
以上代码展示了标准的 DDD 模式实现:聚合根封装业务逻辑,命令处理器处理状态变更, 查询处理器优化数据读取,REST 控制器提供 API 接口。
适用于业务逻辑复杂、需要严格建模的企业级应用系统,确保业务规则的一致性和完整性。
为微服务提供标准化的DDD实现,确保服务间的一致性和可维护性,支持分布式架构。
通过CQRS和事件驱动架构,支持高并发、高性能的业务处理,适应大规模应用需求。
关于AggregateX DDD框架和树深公司的常见问题解答
AggregateX是基于领域驱动设计(DDD)的现代化Java框架,整合了CQRS、事件溯源、事件总线等核心概念,为开发复杂业务系统提供完整的架构支持。它基于Spring Boot 3.5和Java 21构建,提供高性能、可扩展的企业级解决方案。
AggregateX基于Java 21和Spring Boot 3.5,支持Spring Data JPA、Redis缓存、PostgreSQL数据库、Kafka消息队列、Docker容器化部署等主流技术栈。框架遵循MPL-2.0开源协议。
您可以通过Maven或Gradle添加依赖,使用@EnableAggregateX注解启用框架,然后利用内置的代码生成器快速创建DDD模块结构。详细的快速开始指南和代码示例请参考GitHub文档。
我们提供全方位的软硬件开发服务,包括:App开发定制、小程序开发、网站建设、硬件开发、物联网解决方案、AI技术服务、企业级系统开发、微服务架构设计等。服务覆盖医疗、教育、电商、制造、交通等多个行业。
我们是拥有ISO 9001认证的高新技术企业,拥有多项软件著作权。团队在Java企业级开发、微服务架构、云原生技术、大数据处理等领域具备丰富经验,能够为客户提供从需求分析到上线运维的全周期服务。
您可以通过以下方式联系我们:商务合作邮箱 shushen@treedeep.cn,技术咨询热线 13169919969【微信同号】,或访问我们的官网 www.treedeep.cn。我们位于广东省深圳市,欢迎实地交流合作。
AggregateX与现代企业级开发技术的完美集成
通过聚合根、实体、值对象等概念建模复杂业务领域,确保代码与业务逻辑的一致性。
命令查询职责分离,通过读写分离提升系统性能,支持复杂查询和高并发场景。
通过事件流记录状态变更,支持时间旅行、审计追踪和事件重放等高级功能。