为什么我的Alpakka SFTP连接器始终无法连接?

vlf7wbxs  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(112)

我是Alpakka/Akka Streams的新手,我正在尝试设置一个流,在两个SFTP服务器之间传输数据,我的系统位于中间,下面是代码。

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.alpakka.ftp.scaladsl.Sftp
import akka.stream.alpakka.ftp.{FtpCredentials, SftpSettings}
import akka.stream.scaladsl.Keep
import net.schmizz.sshj.{DefaultConfig, SSHClient}

import java.net.InetAddress

class StreamingSftpTransport {
  implicit val system: ActorSystem = ActorSystem("dr-service")
  implicit val materializer: ActorMaterializer = ActorMaterializer()

  private val PORT = 22
  private val USER = "testsftp"
  private val CREDENTIALS = FtpCredentials.create(USER, "t3st123")
  private val BASEPATH = s"/home/$USER"
  private val FILE_NAME = "testfile"

  // Set up the source system connection
  private val SOURCE_HOSTNAME = "host1"

  private val sourceSettings = SftpSettings.apply(host = InetAddress.getByName(SOURCE_HOSTNAME))
    .withCredentials(CREDENTIALS)
    .withPort(22)

  private val sourceClient = new SSHClient(new DefaultConfig)
  private val configuredSourceClient = Sftp(sourceClient)

  // Set up the destination system connection

  private val DEST_HOSTNAME = "host2"
  private val destSettings = SftpSettings.apply(host = InetAddress.getByName(DEST_HOSTNAME))
    .withCredentials(CREDENTIALS)
    .withPort(22)

  private val destClient = new SSHClient(new DefaultConfig)
  private val configuredDestClient = Sftp(destClient)

  /**
   * Execute the stream from host1 to host2
   */
  def doTransfer(): Unit = {
    val source = configuredSourceClient.fromPath(s"$BASEPATH/$FILE_NAME", sourceSettings)
    val sink = configuredDestClient.toPath(s"$BASEPATH/$FILE_NAME", destSettings)
    val runnable = source.toMat(sink)(Keep.right).run()
  }
}

我在单元测试中用new StreamingSftpTransport.doTransfer()调用了这个函数,但是它从来没有尝试连接。我做错了什么?

7fhtutme

7fhtutme1#

正如artur在对我的问题的评论中所建议的那样,我没有阻塞未来,所以JVM在连接建立之前就退出了。
添加以下行允许建立连接

Await.result(runnable, 180 seconds)

PS:在生产中不要这样做:)

相关问题