使用jsch-spring-boot在本地访问远程云服务

来源:互联网 发布:美国历年财政支出数据 编辑:程序博客网 时间:2024/06/05 15:46

现在很多创业公司都不再自建机房,更多地选择云主机,如阿里云和腾讯云等。为了安全考虑,他们提供的关系数据库、nosql数据库等服务器都是不能直接访问的,只能通过云主机访问。

因为在本地不能访问,这样在开发和测试过程中就比较麻烦。

在Java环境中,可以使用JSch解决这个问题。本地程序通过JSch连接到云主机,通过端口转发访问云数据库等受限的服务器,相当于本地操作,很方便。

为方便使用JSch,参考Creating your own auto-configuration创建了一个spring boot自动配置项目jsch-spring-boot。

第1步:创建jsch-spring-boot自动配置项目。

如下图,一般两个子项目jsch-spring-boot-autoconfigurejsch-spring-boot-starterjsch-spring-boot-sample是测试子项目。

第2步:jsch-spring-boot自动配置实现。

一般只需要实现两个类,一个配置文件类,一个自动配置类。

@ConfigurationProperties(prefix = "spring.jsch")public class JschProperties {    /**     * proxy host     */    private String proxyHost;    /**     * the port to access proxy host      */    private int proxyPort;    /**     * the user login to the proxy host     */    private String proxyUser;    /**     * the user's password     */    private String proxyPassword;    /**     * default is no     */    private String strictHostKeyChecking = "no";    /**     * romte destination host     */    private String destHost;    /**     * romte destination port      */    private int destPort;    /**     * local port      */    private int localPort;}

自动配置类代码。

@Configuration@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)@EnableConfigurationProperties(JschProperties.class)public class JschAutoConfiguration implements InitializingBean, DisposableBean {    private final JschProperties jschProperties;    private Session session;    public JschAutoConfiguration(JschProperties jschProperties) {        this.jschProperties = jschProperties;    }    @Override    public void afterPropertiesSet() throws Exception {        JSch jsch = new JSch();        session = jsch.getSession(jschProperties.getProxyUser(), jschProperties.getProxyHost(),                jschProperties.getProxyPort());        session.setPassword(jschProperties.getProxyPassword());        session.setConfig("StrictHostKeyChecking", jschProperties.getStrictHostKeyChecking());        System.out.println("Jsch_AutoConfiguration connect to:::host:" + jschProperties.getProxyHost() + ", port : "                + jschProperties.getProxyPort() + ", user: " + jschProperties.getProxyUser());        session.connect();        System.out.println("Jsch_AutoConfiguration:::" + session.getServerVersion());// 打印SSH服务器版本信息        int assinged_port = session.setPortForwardingL(jschProperties.getLocalPort(), jschProperties.getDestHost(),                jschProperties.getDestPort());        System.out.println("Jsch_AutoConfiguration:::localhost:" + assinged_port + " -> " + jschProperties.getDestHost()                + ":" + jschProperties.getDestHost());    }    @Override    public void destroy() throws Exception {        if (session != null) {            session.disconnect();        }        System.out.println("Jsch_AutoConfiguration::: destory connection");    }}
第3步:jsch-spring-boot配置

jsch-spring-boot-autoconfigure项目src/main/resources/META-INF创建spring.factories文件,配置自动配置类位置。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\jsch.autoconfigure.JschAutoConfiguration

jsch-spring-boot-starter项目src/main/resources/META-INF创建spring.provides文件,配置jsch spring boot自动配置项目名称,如下:

provides: jsch-spring-boot-autoconfigure
第4步:jsch-spring-boot引用
https://github.com/nivance/ssh2-spring-boot.git

因为没有将jsch-spring-boot放到任何maven公共仓库,所以需要将jsch-spring-boot的jar包安装到本地repository中。

cd ssh-spring-bootmvn clean installcd jsch-spring-boot/jsch-spring-boot-starter/targetmvn install:install-file -Dfile=jsch-spring-boot-starter-1.0.0.jar \    -DgroupId=ssh2.spring.boot \    -DartifactId=jsch-spring-boot-starter \    -Dversion=1.0.0 \    -Dpackaging=jarcd ../../jsch-spring-boot-autoconfigure/targetmvn install:install-file -Dfile=jsch-spring-boot-autoconfigure-1.0.0.jar \    -DgroupId=ssh2.spring.boot \    -DartifactId=jsch-spring-boot-autoconfigure \     -Dversion=1.0.0 \    -Dpackaging=jar

然后就可以在其他项目中引用了。

<dependency>    <groupId>ssh2.spring.boot</groupId>    <artifactId>jsch-spring-boot-starter</artifactId>    <version>1.0.0</version></dependency>
第5步:jsch-spring-boot使用

以访问远程mysql服务器为例,yml配置文件内容如下:

spring:  ssh2:    proxy-host: 101.99.35.198    proxy-port: 2201    proxy-user: java    proxy-password: jdfkd199!    dest-host: 101.99.35.199    dest-port: 3306    local-port: 3306  datasource:    url: jdbc:mysql://localhost:3306/evey?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull    username: root    password: 

具体代码可参考本项目jsch-spring-boot中jsch-spring-boot-sample用例。

原创粉丝点击