使用jsch-spring-boot在本地訪問遠程雲服務

現在很多創業公司都不再自建機房,更多地選擇雲主機,如阿里雲騰訊雲等。爲了安全考慮,他們提供的關係數據庫、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-boot

mvn clean install

cd jsch-spring-boot/jsch-spring-boot-starter/target

mvn 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=jar

cd ../../jsch-spring-boot-autoconfigure/target

mvn 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-bootjsch-spring-boot-sample用例。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章