zoukankan      html  css  js  c++  java
  • How to deploy JAVA Application on Azure Service Fabric

    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 Examples to create a simple file server.
      The source code is like below.
       1 package org.eclipse.jetty.embedded;
       2 
       3 import org.eclipse.jetty.server.Handler;
       4 import org.eclipse.jetty.server.Server;
       5 import org.eclipse.jetty.server.handler.DefaultHandler;
       6 import org.eclipse.jetty.server.handler.HandlerList;
       7 import org.eclipse.jetty.server.handler.ResourceHandler;
       8 
       9 public class FileServer {
      10 
      11     public static void main(String[] args) throws Exception{
      12         // TODO Auto-generated method stub
      13         // Create a basic Jetty server object that will listen on port 8080.  Note that if you set this to port 0
      14         // then a randomly available port will be assigned that you can either look in the logs for the port,
      15         // or programmatically obtain it for use in test cases.
      16         Server server = new Server(8080);
      17         
      18         // Create the ResourceHandler. It is the object that will actually handle the request for a given file. It is
      19         // a Jetty Handler object so it is suitable for chaining with other handlers as you will see in other examples.
      20         ResourceHandler resource_handler = new ResourceHandler();
      21         
      22         // Configure the ResourceHandler. Setting the resource base indicates where the files should be served out of.
      23         // In this example it is the current directory but it can be configured to anything that the jvm has access to.
      24         resource_handler.setDirectoriesListed(true);
      25         resource_handler.setWelcomeFiles(new String[]{ "index.html" });
      26         resource_handler.setResourceBase(".");
      27         
      28         // Add the ResourceHandler to the server.
      29         HandlerList handlers = new HandlerList();
      30         handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
      31         server.setHandler(handlers);
      32         
      33         // Start things up! By using the server.join() the server thread will join with the current thread.
      34         // See "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()" for more details.
      35         server.start();
      36         server.join();
      37     }
      38 
      39 }
      View Code

      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.)

    2. Set up Service Fabric project
      • Create Service Fabric Application in Visual Studio


      • 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
      • 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.
      • 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_111injava.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

    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.

        The command line is:

        "C:SfDevClusterData\_App\_Node_0JettyAppType_App1JettyGuestAppPkg.Code.1.0.0jre1.8.0_111injava.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.
  • 相关阅读:
    kingbase8d常见问题
    kibana-7.9.1安装
    ElasticSearch-7.9.1
    在线分析-jstack
    Wireshark常用过滤条件
    shiro登录认证过程
    查看mysql库中所有表的大小和记录数
    linux设置定时任务crontab
    POI 使用常见问题
    Java源码之String-构造方法
  • 原文地址:https://www.cnblogs.com/xingzhou/p/6040526.html
Copyright © 2011-2022 走看看