Merge branch 'master' into feat/auth_dev_v1
This commit is contained in:
@@ -29,7 +29,7 @@ public class DynamicDataSourceConfig {
|
||||
targetDataSources.put(key, builder.build());
|
||||
});
|
||||
|
||||
DynamicRoutingDataSource routing = new DynamicRoutingDataSource();
|
||||
DynamicRoutingDataSource routing = new DynamicRoutingDataSource(props);
|
||||
routing.setDefaultTargetDataSource(targetDataSources.get(props.getPrimary()));
|
||||
routing.setTargetDataSources(targetDataSources);
|
||||
return routing;
|
||||
|
||||
@@ -12,5 +12,12 @@ public class DataSourceProperty {
|
||||
private String username;
|
||||
private String password;
|
||||
private String driverClassName = "com.mysql.cj.jdbc.Driver";
|
||||
|
||||
private Boolean sshConnect;
|
||||
private String sshHost;
|
||||
private Integer sshPort = 22;
|
||||
private String sshUser;
|
||||
private String sshPassword;
|
||||
private Integer localPort; // 本地转发端口
|
||||
private String remoteHost; // 远端数据库 host
|
||||
private Integer remotePort; // 远端数据库 port
|
||||
}
|
||||
|
||||
@@ -1,11 +1,37 @@
|
||||
package com.xiang.xservice.mysql.service;
|
||||
|
||||
import com.xiang.xservice.basic.utils.SSHManager;
|
||||
import com.xiang.xservice.mysql.config.DynamicDataSourceContext;
|
||||
import com.xiang.xservice.mysql.entity.DataSourceProperty;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
|
||||
import com.xiang.xservice.mysql.entity.DynamicDataSourceProperties;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class DynamicRoutingDataSource extends AbstractRoutingDataSource {
|
||||
|
||||
private final DynamicDataSourceProperties dynamicDataSourceProperties;
|
||||
|
||||
@Override
|
||||
protected Object determineCurrentLookupKey() {
|
||||
return DynamicDataSourceContext.get();
|
||||
String key = DynamicDataSourceContext.get();
|
||||
DataSourceProperty dataSourceProperty = dynamicDataSourceProperties.getDatasource().get(key);
|
||||
if (Boolean.TRUE.equals(dataSourceProperty.getSshConnect())) {
|
||||
try {
|
||||
SSHManager.createTunnel(
|
||||
key,
|
||||
dataSourceProperty.getSshHost(),
|
||||
dataSourceProperty.getSshPort(),
|
||||
dataSourceProperty.getSshUser(),
|
||||
dataSourceProperty.getSshPassword(),
|
||||
dataSourceProperty.getLocalPort(),
|
||||
dataSourceProperty.getRemoteHost(),
|
||||
dataSourceProperty.getRemotePort()
|
||||
);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to establish SSH tunnel for " + key, e);
|
||||
}
|
||||
}
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user