1.依赖包
<!--SSH-->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
2.连接信息
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.util.Properties;
public class SSHConnection {
private final static String S_PATH_FILE_KNOWN_HOSTS = "localhost";
private final static int LOCAl_PORT = 3315;
private final static int REMOTE_PORT = 3306;
private final static int SSH_REMOTE_PORT = 22;
private final static String SSH_USER = "root";
private final static String SSH_PASSWORD = "123";
private final static String SSH_REMOTE_SERVER = "10.245.243.245";
private final static String MYSQL_REMOTE_SERVER = "10.245.240.2";
private Session sesion;
public void closeSSH() {
sesion.disconnect();
}
public SSHConnection() throws Throwable {
JSch jsch;
jsch = new JSch();
jsch.setKnownHosts(S_PATH_FILE_KNOWN_HOSTS);
//jsch.addIdentity(S_PATH_FILE_PRIVATE_KEY);
sesion = jsch.getSession(SSH_USER, SSH_REMOTE_SERVER, SSH_REMOTE_PORT);
sesion.setPassword(SSH_PASSWORD);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
sesion.setConfig(config);
sesion.connect(); //ssh connection established!
//ssh -Nfg -L 3315:10.245.240.2:3306 root@10.245.243.245
sesion.setPortForwardingL(LOCAl_PORT, MYSQL_REMOTE_SERVER, REMOTE_PORT);
}
}
3.监听启动程序,打通隧道
import com.cetc.datasyncserver.config.SSHConnection;
import org.springframework.stereotype.Component;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* @author tom
* @create 2001-07-15 10:20
* @description
*/
@WebListener
@Component
public class SSHContextListener implements ServletContextListener {
private SSHConnection sshConnection;
public SSHContextListener() {
super();
}
/**
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
@Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("Context initialized ... !");
try {
sshConnection = new SSHConnection();
System.out.println("------>>>>>>>>>sshConnection:" + sshConnection);
} catch (Throwable e) {
e.printStackTrace(); // error connecting SSH server
}
}
/**
* @see ServletContextListener#contextDestroyed(ServletContextEvent)
*/
@Override
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("Context destroyed ... !");
sshConnection.closeSSH(); // disconnect
}
}
4.数据库配置
spring:
datasource:
## ssh 本地数据库
username: read_only
password: 123456
url: jdbc:mysql://localhost:3315/xa_maas?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500