虽然Service Fabric的Java支持版本尚未正式发布,可是Service Fabric自己的服务管理、部署、升级等功能是很是好用的,那么Java的开发者能够如何利用上Service Fabric的这个功能呢?答案很简单,其实只要利用Service Fabric的Deploy Guest Executable就行了,也就是说Service Fabric是容许部署一个或者多个独立运行的exe,而且运行在Service Fabric的节点中的。详细的信息能够参看官方的这个文档:html
https://azure.microsoft.com/zh-cn/documentation/articles/service-fabric-deploy-existing-app/java
下面我就拿部署Java的Jetty作个样例:web
首先咱们用Visual Studio建立一个Service Fabric的Application浏览器
在向导的界面上选择咱们先将主要参数留空,将Working Folder的选项从默认的Work改为CodeBasetomcat
建立好工程后,咱们在jettyonjavaPkg的目录下建立一个名字为Code的目录,而且将事先下载好的Java的SDK和Jetty拷入Code目录下app
文件拷贝完成后,就能够编辑ServiceManifest.xml了。主要是要修改EntryPoint这一节负载均衡
<EntryPoint> <ExeHost> <Program>java\bin\java.exe</Program> <Arguments>-Djetty.port=8080 -Djetty.base=..\..\jetty -jar ..\..\jetty\start.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>
其中Program里面咱们会调用Java.exe文件,而后在Arguments里面填入Jetty的相关参数,这里要注意的是Program里面不能使用批处理文件,由于Service Fabric的服务监控须要跟踪Exe的进程ID,以便当进程出问题时可以及时切换到其余节点里去。若是你的Program里面指定的是批处理文件.bat的话,虽然这个批处理会执行,可是因为拿不到进程ID,因此Service Fabric会不断的去Call这个批处理的,这也是为何我选择Jetty而不是常见的Tomcat容器的缘由。webapp
编辑好了EntryPoint了,由于jetty是要提供Web服务的,咱们须要告诉Service Fabric,这个Application提供的是什么端口服务,因此咱们还须要编辑Resources节jsp
<Resources> <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="tomcatTypeEndpoint" Protocol="http" Port="8080" Type="Input" /> </Endpoints> </Resources>
在Endpoint的设置里面能够看到发布的8080 http端口跟前面一节的参数里面启动Jetty的参数8080是一致的。测试
作完这些设置,咱们再在jetty的webapps的Root里面放入一个index.jsp,以便验证下咱们的jetty是否是发布在Service Fabric上的
这个jsp的代码比较简单,就是用来显示下Java版本啥的
<!DOCTYPE html> <html> <head> <title>Microsoft Service Fabric on Java - Welcome</title> </head> <body bgcolor="#00abec" > <div id="feature"> <div id="content"> <h2>Service Fabric on Java,Node Address is:<%out.print(request.getLocalAddr());%></h2> <br/> <table width="750" frame="below"> <tr bgcolor="rgb(13,188,242)"> <th align="left" width="200"> Java Property </th> <th align="left" width="500"> Value </th> </tr> <%@ page import="java.util.*" %> <% ArrayList<String> mainPageProps = new ArrayList<String>(); mainPageProps.add("java.version"); mainPageProps.add("java.vendor"); mainPageProps.add("os.arch"); mainPageProps.add("catalina.base"); mainPageProps.add("jetty.base"); mainPageProps.add("user.timezone"); for(String name : mainPageProps) { String value = System.getProperty(name); if(value != null) { out.print("<tr><td>" + name); out.print("</td><td>" + value ); out.print("</td></tr>"); } } %> </table> </font> </div> </div> </body> </html>
准备好了,咱们就能够在VS上面按F5,将程序部署到本地的集群里面进行测试了。
这时候打开浏览器访问http://localhost:8080/index.jsp 这样咱们就能够看到咱们刚才准备好的jsp文件了
从上面的截图,能够看出来Jetty是运行在Node_4上的,咱们试试从Service Fabric Explorer里把节点4重启一下,
用浏览器访问http://localhost:19080
这时候,咱们会发现Service Fabric的群集很快帮咱们吧应用迁移到了Node_0
这样咱们很容易就体验到了Service Fabric帮咱们管理服务的健康,实现应用快速迁移的功能。固然Service Fabric还支持服务自己的负载均衡的,可是因为本机无法两个应用同时发布8080端口,咱们只能等Java版本的Service Fabric Service SDK发布或者将应用部署到云上面才能体验了。