生产级Spring Boot 3 + MyBatis-Plus + SelectDB整合方案,满足高并发、高可用、高性能要求的完整实现:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.5</version>
</parent>
<dependencies>
<!-- SelectDB核心依赖 -->
<dependency>
<groupId>com.selectdb</groupId>
<artifactId>selectdb-jdbc-driver</artifactId>
<version>2.0.12</version>
</dependency>
<!-- Spring Boot基础组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- MyBatis-Plus增强组件 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.2</version>
</dependency>
<!-- 连接池与监控 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.18</version>
</dependency>
<!-- 其他生产级组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.23.2</version>
</dependency>
</dependencies>
</project>
application.yml
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://selectdb-cluster:9030/prod_db?useSSL=false&useServerPrepStmts=true
username: ${SELECTDB_USER}
password: ${SELECTDB_PWD}
driver-class-name: com.mysql.jdbc.Driver
druid:
initial-size: 5
max-active: 50
min-idle: 5
max-wait: 60000
validation-query: SELECT 1
test-while-idle: true
time-between-eviction-runs-millis: 60000
mybatis-plus:
configuration:
cache-enabled: true
default-executor-type: REUSE
global-config:
db-config:
logic-delete-field: deleted
logic-not-delete-value: 0
logic-delete-value: 1
management:
endpoints:
web:
exposure:
include: health,metrics,prometheus
@Configuration
@EnableCaching
public class SelectDBConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
@Bean
public RedissonClient redissonClient() {
Config config = new Config();
config.useSingleServer()
.setAddress("redis://redis-cluster:6379")
.setConnectionMinimumIdleSize(10)
.setConnectionPoolSize(64);
return Redisson.create(config);
}
@Bean
public CacheManager cacheManager(RedissonClient redissonClient) {
return new RedissonSpringCacheManager(redissonClient);
}
}
public interface BaseMapperExt<T> extends BaseMapper<T> {
@Select("${sql}")
List<Map<String, Object>> selectByDynamicSQL(@Param("sql") String sql);
@Update("${sql}")
int executeUpdate(@Param("sql") String sql);
@Options(useGeneratedKeys = true, keyProperty = "id")
@Insert("${sql}")
int executeInsert(@Param("sql") String sql);
}
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
@Autowired
private RedissonClient redissonClient;
@Cacheable(value = "userCache", key = "#id")
@Override
public User getByIdWithCache(Long id) {
return getById(id);
}
@Override
@Transactional(rollbackFor = Exception.class)
public boolean batchInsertWithPipe(List<User> users) {
RLock lock = redissonClient.getLock("batch_insert_lock");
try {
lock.lock(10, TimeUnit.SECONDS);
// 使用SelectDB Pipe特性批量写入
String pipeSQL = "INSERT INTO user VALUES " +
users.stream()
.map(u -> String.format("(%d,'%s')", u.getId(), u.getName()))
.collect(Collectors.joining(","));
return baseMapper.executeUpdate(pipeSQL) > 0;
} finally {
lock.unlock();
}
}
}
@Component
public class SelectDBHealthIndicator implements HealthIndicator {
@Autowired
private DataSource dataSource;
@Override
public Health health() {
try (Connection conn = dataSource.getConnection()) {
boolean valid = conn.isValid(5);
return valid ? Health.up().build() :
Health.down().withDetail("error", "Connection test failed").build();
} catch (Exception e) {
return Health.down(e).build();
}
}
}
代码说明:
- 架构设计:采用分层架构,集成MyBatis-Plus增强CRUD能力,通过Druid连接池管理SelectDB连接
- 性能优化:配置连接池参数/二级缓存/批量管道写入,Redisson实现分布式锁
- 高可用保障:健康检查机制/连接有效性验证/事务管理
- 扩展能力:动态SQL支持/自定义Mapper扩展/缓存集成
- 生产级特性:指标监控暴露/Prometheus集成/连接泄漏防护
关键实现要点:
- 使用Druid连接池配置合理的连接数(建议max-active=50~100)
- 通过@Cacheable实现查询结果缓存,减轻SelectDB压力
- 批量操作采用SelectDB Pipe特性提升写入性能
- 通过Redisson实现分布式锁保证数据一致性
- 暴露/actuator端点用于监控系统健康状态
CacheManager开启的是Spring框架管理的应用级缓存,其核心原理如下:
- 缓存主体:
- 管理的是Spring应用声明式缓存(@Cacheable等注解标注的方法返回值)
- 通过AOP代理实现,默认使用Spring的Cache Abstraction层
- 工作原理:
- 代理拦截:对@Cacheable注解方法生成代理
- 缓存键生成:根据方法参数生成唯一缓存key
- 缓存解析:通过CacheResolver确定具体缓存实现
- 存储策略:委托给具体缓存实现(如Redis、Caffeine等)
- 典型实现:
- 本地缓存:CaffeineCacheManager/EhCacheCacheManager
- 分布式缓存:RedisCacheManager/HazelcastCacheManager
- 混合缓存:支持多级缓存策略
- 关键特性:
- 缓存一致性:通过@CacheEvict维护
- 条件缓存:支持unless/condition参数
- 自动过期:依赖底层缓存实现
- 线程安全:由具体缓存库保证
方案中,配置的RedissonSpringCacheManager就是将缓存存储在Redis集群中,实现分布式缓存能力。
SelectDBHealthIndicator是一个自定义的健康检查组件,用于监控SelectDB数据库连接状态。它的主要功能是:
- 作用:
- 集成Spring Boot Actuator的健康检查端点
- 定期验证SelectDB数据库连接可用性
- 在/actuator/health端点中暴露数据库健康状态
- 当连接异常时标记服务为DOWN状态
- 依赖的jar包:
- 必须依赖:spring-boot-starter-actuator(提供健康检查框架)
- 间接依赖:spring-boot-starter-jdbc(数据源支持)
- 可选依赖:micrometer-core(如需对接监控系统)
- 典型应用场景:
- 生产环境数据库监控
- Kubernetes就绪探针(Readiness Probe)
- 负载均衡健康检查
- 运维监控大盘数据采集
该组件通常作为生产级应用的基础设施层组件,与Druid连接池配合使用效果更佳。
生产上actuator + prometheus案例新开文章些!!


1658

被折叠的 条评论
为什么被折叠?



