没看源码之前,觉得很离谱,为啥只能限制数据库为mysql,按道理来说,nacos用了JdbcTemplate,可以适配很多数据库才是
最近看了nacos的源码,发现其中有很多硬编码,才明白原因
nacos的数据源获取都是通过com.alibaba.nacos.config.server.service.datasource.DynamicDataSource来获取的
在获取数据源时,根据配置判断你到底是使用内置的本地数据库还是外部的数据库(mysql)
public synchronized DataSourceService getDataSource() {
try {
// Embedded storage is used by default in stand-alone mode
// In cluster mode, external databases are used by default
// 根据System.getProperty("nacos.standalone")来判断你到底是不是standalone模式
// standalone模式,使用内置数据库
if (PropertyUtil.isEmbeddedStorage()) {
if (localDataSourceService == null) {
localDataSourceService = new LocalDataSourceServiceImpl();
localDataSourceService.init();
}
return localDataSourceService;
} else {
// 如果不是standalone,直接创建外部的数据源
if (basicDataSourceService == null) {
basicDataSourceService = new ExternalDataSourceServiceImpl();
basicDataSourceService.init();
}
return basicDataSourceService;
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
外部数据源com.alibaba.nacos.config.server.service.datasource.ExternalDataSourceServiceImpl.init()
@Override
public void init() {
queryTimeout = ConvertUtils.toInt(System.getProperty("QUERYTIMEOUT"), 3);
jt = new JdbcTemplate();
// Set the maximum number of records to prevent memory expansion
jt.setMaxRows(50000);
jt.setQueryTimeout(queryTimeout);
testMasterJT = new JdbcTemplate();
testMasterJT.setQueryTimeout(queryTimeout);
testMasterWritableJT = new JdbcTemplate();
// Prevent the login interface from being too long because the main library is not available
testMasterWritableJT.setQueryTimeout(1);
// Database health check
testJtList = new ArrayList<JdbcTemplate>();
isHealthList = new ArrayList<Boolean>();
tm = new DataSourceTransactionManager();
tjt = new TransactionTemplate(tm);
// Transaction timeout needs to be distinguished from ordinary operations.
tjt.setTimeout(TRANSACTION_QUERY_TIMEOUT);
// 判断到底是是不是用外部数据库
// 这个可以在com.alibaba.nacos.config.server.utils.PropertyUtil#loadSetting中看到
// setUseExternalDB("mysql".equalsIgnoreCase(getString("spring.datasource.platform", "")));
// 好家伙,直接判断配置的是不是mysql,是mysql那就是外部数据库,进行reload,不是,那就不管了
if (PropertyUtil.isUseExternalDB()) {
try {
reload();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(DB_LOAD_ERROR_MSG);
}
if (this.dataSourceList.size() > DB_MASTER_SELECT_THRESHOLD) {
ConfigExecutor.scheduleConfigTask(new SelectMasterTask(), 10, 10, TimeUnit.SECONDS);
}
ConfigExecutor.scheduleConfigTask(new CheckDbHealthTask(), 10, 10, TimeUnit.SECONDS);
}
}
在com.alibaba.nacos.config.server.service.datasource.ExternalDataSourceServiceImpl#reload中,我们可以看到
@Override
public synchronized void reload() throws IOException {
try {
// 根据配置文件,构建数据源集合
dataSourceList = new ExternalDataSourceProperties()
.build(EnvUtil.getEnvironment(), (dataSource) -> {
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setQueryTimeout(queryTimeout);
jdbcTemplate.setDataSource(dataSource);
testJtList.add(jdbcTemplate);
isHealthList.add(Boolean.TRUE);
});
new SelectMasterTask().run();
new CheckDbHealthTask().run();
} catch (RuntimeException e) {
FATAL_LOG.error(DB_LOAD_ERROR_MSG, e);
throw new IOException(e);
}
}
在com.alibaba.nacos.config.server.service.datasource.ExternalDataSourceProperties#build中
List<HikariDataSource> build(Environment environment, Callback<HikariDataSource> callback) {
List<HikariDataSource> dataSources = new ArrayList<>();
// 把胚子信息绑定到当前的ExternalDataSourceProperties对象,赋值操作
// 因为外面是直接new出来的,需要对属性根据文件进行赋值
Binder.get(environment).bind("db", Bindable.ofInstance(this));
Preconditions.checkArgument(Objects.nonNull(num), "db.num is null");
Preconditions.checkArgument(CollectionUtils.isNotEmpty(user), "db.user or db.user.[index] is null");
Preconditions.checkArgument(CollectionUtils.isNotEmpty(password), "db.password or db.password.[index] is null");
// 可以配置多个数据库
for (int index = 0; index < num; index++) {
int currentSize = index + 1;
Preconditions.checkArgument(url.size() >= currentSize, "db.url.%s is null", index);
// 拿到spring.datasource.xxx一堆,这个针对所有的数据源都适用
DataSourcePoolProperties poolProperties = DataSourcePoolProperties.build(environment);
// 为每一个数据源进行单独的url,user,password进行替换
poolProperties.setDriverClassName(JDBC_DRIVER_NAME);
poolProperties.setJdbcUrl(url.get(index).trim());
poolProperties.setUsername(getOrDefault(user, index, user.get(0)).trim());
poolProperties.setPassword(getOrDefault(password, index, password.get(0)).trim());
HikariDataSource ds = poolProperties.getDataSource();
ds.setConnectionTestQuery(TEST_QUERY);
dataSources.add(ds);
callback.accept(ds);
}
Preconditions.checkArgument(CollectionUtils.isNotEmpty(dataSources), "no datasource available");
return dataSources;
}
这个整体还行,但是为啥JDBC_DRIVER_NAME是硬编码呢,代码中清晰看到
private static final String JDBC_DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
到这已经一目了然,代码中硬编码了mysql,driver也没法改,所以根本没法更换数据库驱动,有点骚,而且com.mysql.cj.jdbc.Driver是mysql8的驱动,对mysql版本是有要求的
再看其他部分,也可以发现大量的硬编码,例如com.alibaba.nacos.config.server.auth.ExternalUserPersistServiceImpl
public class ExternalUserPersistServiceImpl implements UserPersistService {
@Autowired
private ExternalStoragePersistServiceImpl persistService;
private JdbcTemplate jt;
@PostConstruct
protected void init() {
jt = persistService.getJdbcTemplate();
}
/**
* Execute create user operation.
*
* @param username username string value.
* @param password password string value.
*/
public void createUser(String username, String password) {
String sql = "INSERT into users (username, password, enabled) VALUES (?, ?, ?)";
try {
jt.update(sql, username, password, true);
} catch (CannotGetJdbcConnectionException e) {
LogUtil.FATAL_LOG.error("[db-error] " + e.toString(), e);
throw e;
}
}
/**
* Execute delete user operation.
*
* @param username username string value.
*/
public void deleteUser(String username) {
String sql = "DELETE from users WHERE username=?";
try {
jt.update(sql, username);
} catch (CannotGetJdbcConnectionException e) {
LogUtil.FATAL_LOG.error("[db-error] " + e.toString(), e);
throw e;
}
}
/**
* Execute update user password operation.
*
* @param username username string value.
* @param password password string value.
*/
public void updateUserPassword(String username, String password) {
try {
jt.update("UPDATE users SET password = ? WHERE username=?", password, username);
} catch (CannotGetJdbcConnectionException e) {
LogUtil.FATAL_LOG.error("[db-error] " + e.toString(), e);
throw e;
}
}
/**
* Execute find user by username operation.
*
* @param username username string value.
* @return User model.
*/
public User findUserByUsername(String username) {
String sql = "SELECT username,password FROM users WHERE username=? ";
try {
return this.jt.queryForObject(sql, new Object[] {username}, USER_ROW_MAPPER);
} catch (CannotGetJdbcConnectionException e) {
LogUtil.FATAL_LOG.error("[db-error] " + e.toString(), e);
throw e;
} catch (EmptyResultDataAccessException e) {
return null;
} catch (Exception e) {
LogUtil.FATAL_LOG.error("[db-other-error]" + e.getMessage(), e);
throw new RuntimeException(e);
}
}
public Page<User> getUsers(int pageNo, int pageSize) {
PaginationHelper<User> helper = persistService.createPaginationHelper();
String sqlCountRows = "select count(*) from users where ";
String sqlFetchRows = "select username,password from users where ";
String where = " 1=1 ";
try {
Page<User> pageInfo = helper
.fetchPage(sqlCountRows + where, sqlFetchRows + where, new ArrayList<String>().toArray(), pageNo,
pageSize, USER_ROW_MAPPER);
if (pageInfo == null) {
pageInfo = new Page<>();
pageInfo.setTotalCount(0);
pageInfo.setPageItems(new ArrayList<>());
}
return pageInfo;
} catch (CannotGetJdbcConnectionException e) {
LogUtil.FATAL_LOG.error("[db-error] " + e.toString(), e);
throw e;
}
}
@Override
public List<String> findUserLikeUsername(String username) {
String sql = "SELECT username FROM users WHERE username like '%' ? '%'";
List<String> users = this.jt.queryForList(sql, new String[]{username}, String.class);
return users;
}
}
几乎所有的sql都是硬编码....所以要改造成其他数据库工作量还是非常大的