本文共 3514 字,大约阅读时间需要 11 分钟。
当目标数据库不能直连的,需要一个服务器作为中间跳板的时候,我们需要通过SSH通道连接数据库,其使用方式如下:
引入依赖com.jcraft jsch 0.1.55
编写SSHConnection连接类
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 = 3307; 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 = "123456"; private final static String SSH_REMOTE_SERVER = "39.100.4.8"; private final static String MYSQL_REMOTE_SERVER = "39.22.22.1"; private Session sesion; //represents each ssh session public void closeSSH () { sesion.disconnect(); } public SSHConnection () throws Throwable { JSch jsch = null; 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! //by security policy, you must connect through a fowarded port sesion.setPortForwardingL(LOCAl_PORT, MYSQL_REMOTE_SERVER, REMOTE_PORT); }}
编写MyContextListener监听
import org.springframework.stereotype.Component;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.annotation.WebListener;@WebListener@Componentpublic class MyContextListener implements ServletContextListener { private SSHConnection conexionssh; public MyContextListener() { super(); } /** * @see ServletContextListener#contextInitialized(ServletContextEvent) */ public void contextInitialized(ServletContextEvent arg0) { System.out.println("Context initialized ... !"); try { conexionssh = new SSHConnection(); } catch (Throwable e) { e.printStackTrace(); // error connecting SSH server } } /** * @see ServletContextListener#contextDestroyed(ServletContextEvent) */ public void contextDestroyed(ServletContextEvent arg0) { System.out.println("Context destroyed ... !"); conexionssh.closeSSH(); // disconnect }}
更改数据库配置文件
# 服务端口和服务名称spring: datasource: url: jdbc:mysql://localhost:3307/cs?useUnicode=true&characterEncoding=UTF8&allowMultiQueries=true&useSSL=false&serverTimezone=UTC username: cs password: 987654321 driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=15000 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 filters: stat,wall,log4j useGlobalDataSourceStat: true
这样使得使用本地指定的3307端口的时候会通过跳板服务器连接目标数据库,而其它端口则不会例如3306
至此完毕转载地址:http://rcnab.baihongyu.com/