How to deploy JAVA Application on Azure Service Fabric

时间:2023-03-08 20:50:59

At this moment, Azure Service Fabric does not support JAVA application natively (but it's on the support roadmap). However, we can host the JAVA application as a Guest Executable for the time being.

In this article, I will walk you through how to deploy JAVA application on Azure Service Fabric in details.

  1. Prepare JAVA Application
    I follow the Embedded Jetty Examplesto create a simple file server.
    The source code is like below.
     package org.eclipse.jetty.embedded;
    
     import org.eclipse.jetty.server.Handler;
    import org.eclipse.jetty.server.Server;
    import org.eclipse.jetty.server.handler.DefaultHandler;
    import org.eclipse.jetty.server.handler.HandlerList;
    import org.eclipse.jetty.server.handler.ResourceHandler; public class FileServer { public static void main(String[] args) throws Exception{
    // TODO Auto-generated method stub
    // Create a basic Jetty server object that will listen on port 8080. Note that if you set this to port 0
    // then a randomly available port will be assigned that you can either look in the logs for the port,
    // or programmatically obtain it for use in test cases.
    Server server = new Server(8080); // Create the ResourceHandler. It is the object that will actually handle the request for a given file. It is
    // a Jetty Handler object so it is suitable for chaining with other handlers as you will see in other examples.
    ResourceHandler resource_handler = new ResourceHandler(); // Configure the ResourceHandler. Setting the resource base indicates where the files should be served out of.
    // In this example it is the current directory but it can be configured to anything that the jvm has access to.
    resource_handler.setDirectoriesListed(true);
    resource_handler.setWelcomeFiles(new String[]{ "index.html" });
    resource_handler.setResourceBase("."); // Add the ResourceHandler to the server.
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
    server.setHandler(handlers); // Start things up! By using the server.join() the server thread will join with the current thread.
    // See "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()" for more details.
    server.start();
    server.join();
    } }

    Then you need to Export this application to a runnable JAR file. (Note: Choose "Package required libraries into generated JAR" as the Library handing option. In my first environment, I choose the first option which runs into issue.)
    How to deploy JAVA Application on Azure Service Fabric

  2. Set up Service Fabric project
    • Create Service Fabric Application in Visual Studio
      How to deploy JAVA Application on Azure Service Fabric

    • Select Guest Executable as the Template;
      Choose the folder where you exported JAR file as the Code Package Folder
      Choose "Copy folder contents to project" as Code Package Behavior
      Choose CodeBase as Working Folder
      How to deploy JAVA Application on Azure Service Fabric
    • The Application package file structure is shown as below.
      |-- ApplicationPackageRoot
      |-- JettyGuestAppPkg
      |-- Code
      |-- FileServer8080.jar
      |-- Config
      |-- Settings.xml
      |-- Data
      |-- ServiceManifest.xml
      |-- ApplicationManifest.xml
    • Copy JDK to the code base folder. The package file structure looks as below, where jre1.8.0_111 is JAVA runtime.
      How to deploy JAVA Application on Azure Service Fabric
    • Configure ServiceManifest.xml to set the program entry point and http listening port. (Note: Be careful about the JAR file path. It is relative to java.exe)
      <EntryPoint>
      <ExeHost>
      <Program>jre1.8.0_111\bin\java.exe</Program>
      <Arguments>-jar ..\..\FileServer8080.jar</Arguments>
      <WorkingFolder>CodeBase</WorkingFolder>
      <!-- Uncomment to log console output (both stdout and stderr) to one of the
      service's working directories. -->
      <!-- <ConsoleRedirection FileRetentionCount="5" FileMaxSizeInKb="2048"/> -->
      </ExeHost>
      </EntryPoint>
      <Endpoints>
      <!-- This endpoint is used by the communication listener to obtain the port on which to
      listen. Please note that if your service is partitioned, this port is shared with
      replicas of different partitions that are placed in your code. -->
      <Endpoint Name="JettyGuestAppTypeEndpoint" Protocol="http" Port="8080" Type="Input"/>
      </Endpoints>
  3. It's ready to deploy to Azure Service Farbic now. After deploy completed, you can check the application status via Service Fabric Explorer (http://localhost:19080/Explorer/index.html ).
  4. Verify JAVA application is running as expected by browsing to http://localhost:8080
    How to deploy JAVA Application on Azure Service Fabric

Notes:

  1. How the JAVA Application is running on Local Service Fabric Cluster?
    ServiceFabricLocalClusterManager.exe creates a new process called java.exe under its own process tree to hold the JAVA application.
  2. During the process to export the JAVA application I choose "Extract required libraries into generated JAR" as the Library handing option, and the application does not run as expected. In this case, how can we tell the issue is with Service Fabric or with your own JAVA application?
    • I use Process Monitor to check how the java.exe is launched.
      How to deploy JAVA Application on Azure Service Fabric

      The command line is:

      "C:\SfDevCluster\Data\_App\_Node_0\JettyAppType_App1\JettyGuestAppPkg.Code.1.0.0\jre1.8.0_111\bin\java.exe" -jar ..\..\FileServer.jar
    • Running the command above in Windows Command Window, and the issue happens there as well. Then I am pretty sure the issue is with JAVA code, and has nothing to do with Service Farbic.