更新旧式 Java Service Fabric 应用程序,以便从 Maven 提取 Java 库Update your previous Java Service Fabric application to fetch Java libraries from Maven

Service Fabric Java 二进制文件已从 Service Fabric Java SDK 移至 Maven 托管。Service Fabric Java binaries have moved from the Service Fabric Java SDK to Maven hosting. 你可以使用 mavencentral 来提取最新的 Service Fabric Java 依赖项。You can use mavencentral to fetch the latest Service Fabric Java dependencies. 本指南将帮助你更新使用 Yeoman 模板或 Eclipse 为 Service Fabric Java SDK 创建的现有 Java 应用程序,使其与基于 Maven 的内部版本兼容。This guide will help you update existing Java applications created for the Service Fabric Java SDK using either Yeoman template or Eclipse to be compatible with the Maven-based build.

先决条件Prerequisites

  1. 首先,卸载现有的 Java SDK。First, uninstall the existing Java SDK.

    sudo dpkg -r servicefabricsdkjava
    
  2. 按照此处提到的步骤安装最新的 Service Fabric CLI。Install the latest Service Fabric CLI following the steps mentioned here.

  3. 若要生成和使用 Service Fabric Java 应用程序,请确保已安装 JDK 1.8 和 Gradle。To build and work on the Service Fabric Java applications, ensure that you have JDK 1.8 and Gradle installed. 如果尚未安装,可运行以下命令来安装 JDK 1.8 (openjdk-8-jdk) 和 Gradle:If not yet installed, you can run the following to install JDK 1.8 (openjdk-8-jdk) and Gradle -

    sudo apt-get install openjdk-8-jdk-headless
    sudo apt-get install gradle
    
  4. 按照此处提到的步骤,更新应用程序的安装/卸载脚本,以便使用新的 Service Fabric CLI。Update the install/uninstall scripts of your application to use the new Service Fabric CLI following the steps mentioned here. 可以参考入门示例You can refer to our getting-started examples for reference.

提示

卸载 Service Fabric Java SDK 后,Yeoman 将无法使用。After uninstalling the Service Fabric Java SDK, Yeoman will not work. 请遵循此处提到的先决条件,以便启动和运行 Service Fabric Yeoman Java 模板生成器。Follow the Prerequisites mentioned here to have Service Fabric Yeoman Java template generator up and working.

Maven 上的 Service Fabric Java 库Service Fabric Java libraries on Maven

Service Fabric Java 库已托管在 Maven 中。Service Fabric Java libraries have been hosted in Maven. 可以在项目的 pom.xmlbuild.gradle 中添加依赖项,以便使用 mavenCentral 提供的 Service Fabric Java 库。You can add the dependencies in the pom.xml or build.gradle of your projects to use Service Fabric Java libraries from mavenCentral.

执行组件Actors

针对应用程序的 Service Fabric Reliable Actor 支持。Service Fabric Reliable Actor support for your application.

<dependency>
    <groupId>com.microsoft.servicefabric</groupId>
    <artifactId>sf-actors</artifactId>
    <version>1.0.0</version>
</dependency>
repositories {
    mavenCentral()
}
dependencies {
    compile 'com.microsoft.servicefabric:sf-actors:1.0.0'
}

服务Services

针对应用程序的 Service Fabric 无状态服务支持。Service Fabric Stateless Service support for your application.

<dependency>
    <groupId>com.microsoft.servicefabric</groupId>
    <artifactId>sf-services</artifactId>
    <version>1.0.0</version>
</dependency>
repositories {
    mavenCentral()
}
dependencies {
    compile 'com.microsoft.servicefabric:sf-services:1.0.0'
}

其他Others

传输Transport

针对 Service Fabric Java 应用程序的传输层支持。Transport layer support for Service Fabric Java application. 不需向 Reliable Actor 或 Service 应用程序显式添加此依赖项,除非在传输层编程。You do not need to explicitly add this dependency to your Reliable Actor or Service applications, unless you program at the transport layer.

<dependency>
    <groupId>com.microsoft.servicefabric</groupId>
    <artifactId>sf-transport</artifactId>
    <version>1.0.0</version>
</dependency>
repositories {
    mavenCentral()
}
dependencies {
    compile 'com.microsoft.servicefabric:sf-transport:1.0.0'
}

Fabric 支持Fabric support

针对 Service Fabric(与本机 Service Fabric 运行时通信)的系统级支持。System level support for Service Fabric, which talks to native Service Fabric runtime. 不需向 Reliable Actor 或 Service 应用程序显式添加此依赖项。You do not need to explicitly add this dependency to your Reliable Actor or Service applications. 当你包括上述其他依赖项时,会自动从 Maven 提取此依赖项。This gets fetched automatically from Maven, when you include the other dependencies above.

<dependency>
    <groupId>com.microsoft.servicefabric</groupId>
    <artifactId>sf</artifactId>
    <version>1.0.0</version>
</dependency>
repositories {
    mavenCentral()
}
dependencies {
    compile 'com.microsoft.servicefabric:sf:1.0.0'
}

迁移 Service Fabric 无状态服务Migrating Service Fabric Stateless Service

若要使用从 Maven 提取的 Service Fabric 依赖项生成现有的 Service Fabric 无状态 Java 服务,需更新该服务中的 build.gradle 文件。To be able to build your existing Service Fabric stateless Java service using Service Fabric dependencies fetched from Maven, you need to update the build.gradle file inside the Service. 以前,该文件如下所示:Previously it used to be like as follows -

dependencies {
    compile fileTree(dir: '/opt/microsoft/sdk/servicefabric/java/packages/lib', include: ['*.jar'])
    compile project(':Interface')
}
.
.
.
jar {
    manifest {
    attributes(
                'Main-Class': 'statelessservice.MyStatelessServiceHost',
                "Class-Path": configurations.compile.collect { 'lib/' + it.getName() }.join(' '))
    baseName "MyStateless"
    destinationDir = file('./../MyStatelessApplication/MyStatelessPkg/Code')
}
.
.
.
task copyDeps <<{
    copy {
        from("/opt/microsoft/sdk/servicefabric/java/packages/lib")
        into("./../MyStatelessApplication/MyStatelessPkg/Code/lib")
        include('*.jar')
    }
    copy {
        from("/opt/microsoft/sdk/servicefabric/java/packages/lib")
        into("./../MyStatelessApplication/MyStatelessPkg/Code/lib")
        include('libj*.so')
    }
}

现在,若要从 Maven 提取依赖项,更新的 build.gradle 需包含如下所示的相应部分 -Now, to fetch the dependencies from Maven, the updated build.gradle would have the corresponding parts as follows -

repositories {
        mavenCentral()
}

configurations {
    azuresf
}

dependencies {
    compile project(':Interface')
    azuresf ('com.microsoft.servicefabric:sf-services:1.0.0')
    compile fileTree(dir: 'lib', include: '*.jar')
}

task explodeDeps(type: Copy, dependsOn:configurations.azuresf) { task ->
    configurations.azuresf.filter { it.toString().contains("native") }.each{
        from zipTree(it)
    }
    configurations.azuresf.filter { !it.toString().contains("native") }.each {
        from it
    }
    into "lib"
    include "libj*.so", "*.jar"
}

compileJava.dependsOn(explodeDeps)
.
.
.
jar {
    manifest {
        def mpath = configurations.compile.collect {'lib/'+it.getName()}.join (' ')
        mpath = mpath + ' ' + configurations.azuresf.collect {'lib/'+it.getName()}.join (' ')
        attributes(
                'Main-Class': 'statelessservice.MyStatelessServiceHost',
                "Class-Path": mpath)
    baseName "MyStateless"
    destinationDir = file('./../MyStatelessApplication/MyStatelessPkg/Code')
   }
}
.
.
.
task copyDeps <<{
    copy {
        from("lib/")
        into("./../MyStatelessApplication/MyStatelessPkg/Code/lib")
        include('*')
    }
}

通常情况下,若要粗略地了解 Service Fabric 无状态 Java 服务的生成脚本的情况,可参阅入门示例中提供的任何示例。In general, to get an overall idea about how the build script would look like for a Service Fabric stateless Java service, you can refer to any sample from our getting-started examples. 这里是适用于 EchoServer 示例的 build.gradleHere is the build.gradle for the EchoServer sample.

迁移 Service Fabric Actor 服务Migrating Service Fabric Actor Service

若要使用从 Maven 提取的 Service Fabric 依赖项生成现有的 Service Fabric Actor Java 应用程序,需更新接口包和服务包中的 build.gradle 文件。To be able to build your existing Service Fabric Actor Java application using Service Fabric dependencies fetched from Maven, you need to update the build.gradle file inside the interface package and in the Service package. 如果有 TestClient 包,则也需更新该包。If you have a TestClient package, you need to update that as well. 因此,对于执行组件 Myactor,需在以下位置进行更新:So, for your actor Myactor, the following would be the places where you need to update -

./Myactor/build.gradle
./MyactorInterface/build.gradle
./MyactorTestClient/build.gradle

更新接口项目的生成脚本Updating build script for the interface project

以前,该文件如下所示:Previously it used to be like as follows -

dependencies {
    compile fileTree(dir: '/opt/microsoft/sdk/servicefabric/java/packages/lib', include: ['*.jar'])
}
.
.

现在,若要从 Maven 提取依赖项,更新的 build.gradle 需包含如下所示的相应部分 -Now, to fetch the dependencies from Maven, the updated build.gradle would have the corresponding parts as follows -

repositories {
    mavenCentral()
}

configurations {
    azuresf
}

dependencies {
    azuresf ('com.microsoft.servicefabric:sf-actors:1.0.0')
    compile fileTree(dir: 'lib', include: '*.jar')
}

task explodeDeps(type: Copy, dependsOn:configurations.azuresf) { task ->
    configurations.azuresf.filter { it.toString().contains("native") }.each{
        from zipTree(it)
    }
    configurations.azuresf.filter { !it.toString().contains("native") }.each {
        from it
    }
    into "lib"
    include "libj*.so", "*.jar"
}

compileJava.dependsOn(explodeDeps)
.
.

更新执行组件项目的生成脚本Updating build script for the actor project

以前,该文件如下所示:Previously it used to be like as follows -

dependencies {
    compile fileTree(dir: '/opt/microsoft/sdk/servicefabric/java/packages/lib', include: ['*.jar'])
    compile project(':MyactorInterface')
}
.
.
.
jar {
    manifest {
    attributes(
                'Main-Class': 'reliableactor.MyactorHost',
                "Class-Path": configurations.compile.collect { 'lib/' + it.getName() }.join(' '))
      baseName "myactor"
    destinationDir = file('./../myjavaapp/MyactorPkg/Code')
    }
}
.
.
.
task copyDeps<< {
    copy {
        from("/opt/microsoft/sdk/servicefabric/java/packages/lib")
        into("./../myjavaapp/MyactorPkg/Code/lib")
        include('*.jar')
    }
    copy {
        from("/opt/microsoft/sdk/servicefabric/java/packages/lib")
        into("./../myjavaapp/MyactorPkg/Code/lib")
        include('libj*.so')
    }
    copy {
        from("../MyactorInterface/out/lib")
        into("./../myjavaapp/MyactorPkg/Code/lib")
        include('*.jar')
    }
}

现在,若要从 Maven 提取依赖项,更新的 build.gradle 需包含如下所示的相应部分 -Now, to fetch the dependencies from Maven, the updated build.gradle would have the corresponding parts as follows -

repositories {
    mavenCentral()
}

configurations {
    azuresf
}

dependencies {
    compile project(':MyactorInterface')
    azuresf ('com.microsoft.servicefabric:sf-actors:1.0.0')
    compile fileTree(dir: 'lib', include: '*.jar')
}

task explodeDeps(type: Copy, dependsOn:configurations.azuresf) { task ->
    configurations.azuresf.filter { it.toString().contains("native") }.each{
        from zipTree(it)
    }
    configurations.azuresf.filter { !it.toString().contains("native") }.each {
        from it
    }
    into "lib"
    include "libj*.so", "*.jar"
}

compileJava.dependsOn(explodeDeps)
.
.
.
jar {
    manifest {
        def mpath = configurations.compile.collect {'lib/'+it.getName()}.join (' ')
        mpath = mpath + ' ' + configurations.azuresf.collect {'lib/'+it.getName()}.join (' ')
        attributes(
                'Main-Class': 'reliableactor.MyactorHost',
                "Class-Path": mpath)
    baseName "myactor"
    destinationDir = file('../myjavaapp/MyactorPkg/Code')}
 }
.
.
.
task copyDeps<< {
      copy {
              from("lib/")
              into("../myjavaapp/MyactorPkg/Code/lib")
              include('*')
      }
      copy {
              from("../MyactorInterface/out/lib")
              into("../myjavaapp/MyactorPkg/Code/lib")
              include('*.jar')
      }
}

更新测试客户端项目的生成脚本Updating build script for the test client project

此处的更改类似于在上一部分(即执行组件项目)中讨论的更改。Changes here are similar to the changes discussed in previous section, that is, the actor project. 以前,Gradle 脚本如下所示:Previously the Gradle script used to be like as follows -

dependencies {
    compile fileTree(dir: '/opt/microsoft/sdk/servicefabric/java/packages/lib', include: ['*.jar'])
      compile project(':MyactorInterface')
}
.
.
.
jar
{
    manifest {
    attributes(
        'Main-Class': 'reliableactor.test.MyactorTestClient',
        "Class-Path": configurations.compile.collect { 'lib/' + it.getName() }.join(' '))
    }
    baseName "myactor-test"
  destinationDir = file('out/lib')
}
.
.
.
task copyDeps<< {
        copy {
                from("/opt/microsoft/sdk/servicefabric/java/packages/lib")
                into("./out/lib/lib")
                include('*.jar')
        }
        copy {
                from("/opt/microsoft/sdk/servicefabric/java/packages/lib")
                into("./out/lib/lib")
                include('libj*.so')
        }
        copy {
                from("../MyactorInterface/out/lib")
                into("./out/lib/lib")
                include('*.jar')
        }
}

现在,若要从 Maven 提取依赖项,更新的 build.gradle 需包含如下所示的相应部分 -Now, to fetch the dependencies from Maven, the updated build.gradle would have the corresponding parts as follows -

repositories {
    mavenCentral()
}

configurations {
    azuresf
}

dependencies {
    compile project(':MyactorInterface')
    azuresf ('com.microsoft.servicefabric:sf-actors:1.0.0')
    compile fileTree(dir: 'lib', include: '*.jar')
}

task explodeDeps(type: Copy, dependsOn:configurations.azuresf) { task ->
    configurations.azuresf.filter { it.toString().contains("native") }.each{
        from zipTree(it)
    }
    configurations.azuresf.filter { !it.toString().contains("native") }.each {
        from it
    }
    into "lib"
    include "libj*.so", "*.jar"
}

compileJava.dependsOn(explodeDeps)
.
.
.
jar
{
    manifest {
        def mpath = configurations.compile.collect {'lib/'+it.getName()}.join (' ')
        mpath = mpath + ' ' + configurations.azuresf.collect {'lib/'+it.getName()}.join (' ')
    attributes(
                'Main-Class': 'reliableactor.test.MyactorTestClient',
                "Class-Path": mpath)
    baseName "myactor-test"
    destinationDir = file('./out/lib')
        }
}
.
.
.
task copyDeps<< {
        copy {
                from("lib/")
                into("./out/lib/lib")
                include('*')
        }
        copy {
                from("../MyactorInterface/out/lib")
                into("./out/lib/lib")
                include('*.jar')
        }
}

后续步骤Next steps