SpringBoot的SSH连接远程数据库和使用数据源

1、编写连接类

package com.xxx.xxx;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.util.Properties;

public class SSHConnection {

// 需要了可以打开
//private final static String S_PATH_FILE_PRIVATE_KEY = "/Users/xxx/.ssh/id_rsa";
// 需要了可以打开
//private final static String S_PATH_FILE_KNOWN_HOSTS = "/Users/xxx/.ssh/id_rsa/.ssh/known_hosts";

// 自定义的中转接口,需要和数据源接口设置一样
private final static int LOCAl_PORT = 5555;
// 服务端的数据库端口
private final static int REMOTE_PORT = 3306; 

// 服务器端SSH端口 默认是22
private final static int SSH_REMOTE_PORT = 22;
// SSH用户名
private final static String SSH_USER = "xxxxxxx";
// SSH使用密码
private final static String SSH_PASSWORD = "xxxxxxx";
// 连接到哪个服务端的SSH
private final static String SSH_REMOTE_SERVER = "xxx.xxx.xxx";
// 服务端的本地mysql服务
private final static String MYSQL_REMOTE_SERVER = "127.0.0.1"; 

private Session sesion; //represents each ssh session
// 测试连接
public static void main(String[] args) throws Throwable {
    System.out.println(new SSHConnection());
}
public SSHConnection () throws Throwable
{

        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!
        //  设置转发    
        sesion.setPortForwardingL(LOCAl_PORT, MYSQL_REMOTE_SERVER, REMOTE_PORT);
       
        System.out.println("SSHConnection--运行OK");
}
}
public void closeSSH ()
{
    sesion.disconnect();
}

2、编写监听类

package com.xxx.xxx;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class SSHWebListener implements ServletContextListener {

    private SSHConnection conexionssh;

    public SSHWebListener() {
        super();
    }
    public void contextInitialized(ServletContextEvent arg0) {
        System.out.println("SSHWebListener initialized ... !");
        try {
            conexionssh = new SSHConnection();
        } catch (Throwable e) {
            e.printStackTrace(); 
        }
    }
    public void contextDestroyed(ServletContextEvent arg0) {
        System.out.println("SSHWebListener destroyed ... !");
        conexionssh.closeSSH(); 
    }
}

3、数据源配置

#数据库数据源
// 端口需要和SSH设置的转发端口一致
// 因为是转发,所以本地SSH仅仅是转发作用,host还是服务端的本地地址
spring.druid.url=jdbc:mysql://127.0.0.1:5555/project?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
# 服务端数据库的username和password
spring.druid.username=xxx
spring.druid.password=xxxxxx
spring.druid.maxActive=20
spring.druid.minIdle=3
spring.druid.maxWait=10000
#解决mysql 8小时的断开问题
spring.druid.validationQuery=SELECT 'x'
#空闲连接检查时间间隔
spring.druid.timeBetweenEvictionRunsMillis=60000
#空闲连接最小空闲时间
spring.druid.minEvictableIdleTimeMillis=300000
spring.druid.borrowConnectionTimeout=30000

4、注意,启动了但是没有打印如下

SSHWebListener initialized ... !
SSHConnection--运行OK
需要添加注释@ServletComponentScan

@SpringBootApplication
@ServletComponentScan
public class UserserviceApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserserviceApplication.class, args);
    }
}

5、看看逻辑

屏幕快照 2019-04-21 下午9.16.15.png

6、启动服务吧

你可能感兴趣的:(SpringBoot的SSH连接远程数据库和使用数据源)