设为首页 收藏本站
查看: 1056|回复: 0

[经验分享] How to deploy JAVA Application on Azure Service Fabric

[复制链接]

尚未签到

发表于 2017-3-2 07:58:25 | 显示全部楼层 |阅读模式
  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.


  • Prepare JAVA Application
    I follow the Embedded Jetty Examples to create a simple file server.
    The source code is like below.


    DSC0000.gif DSC0001.gif


    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();
         }

    }
    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.)
    DSC0002.png

  • Set up Service Fabric project

    • Create Service Fabric Application in Visual Studio
      DSC0003.png


    • 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
      DSC0004.png
    • 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.
      DSC0005.png
    • 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>


  • 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 ).
  • Verify JAVA application is running as expected by browsing to http://localhost:8080
    DSC0006.png
  Notes:


  • 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.
  • 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.
      DSC0007.png   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.



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-348996-1-1.html 上篇帖子: 微信公众平台开发中-SAE不支持XStream框架的解决方案 下篇帖子: Maven 配置Tomcat
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表