Java 如何将jar文件部署到azure?
我使用grizzly和GoogleGuice创建了一个RESTFul服务。我可以在命令行上访问它:Java 如何将jar文件部署到azure?,java,azure,cloud,grizzly,Java,Azure,Cloud,Grizzly,我使用grizzly和GoogleGuice创建了一个RESTFul服务。我可以在命令行上访问它: c:\Java\src\options\console>java -jar target\ServerConsole-V1.jar Nov 28, 2017 3:18:01 PM org.glassfish.grizzly.http.server.HttpServer start INFO: [HttpServer] Started. web server started success
c:\Java\src\options\console>java -jar target\ServerConsole-V1.jar
Nov 28, 2017 3:18:01 PM org.glassfish.grizzly.http.server.HttpServer start
INFO: [HttpServer] Started.
web server started successfully at http://localhost:8080/
...press any key to stop the process
现在我想把这项服务部署到云端。我找到了关于部署SpringBoot应用程序和使用jar文件创建webjobs的文章,但这些都不适用于我的应用程序的工作方式
我是要重新设计Web服务器(到spring boot),还是可以按原样部署它?如果是,怎么做
thnx,马特
编辑:
我遵循了下面的步骤。我创建了一个web.config作为
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
</handlers>
<httpPlatform processPath="%JAVA_HOME%\bin\java.exe"
arguments="-Djava.net.preferIPv4Stack=true -Dserver.port=8080 -jar "%HOME%\site\wwwroot\ServerConsole-V1.jar"">
</httpPlatform>
</system.webServer>
</configuration>
当我尝试使用浏览器或PostMan访问RESTFul API时,会出现超时错误。如何诊断此问题?下面是在azure服务器上运行.jar文件的教程。您可以使用Azure的web服务,它为您提供运行应用程序所需的java环境和web服务器
根据您的描述,您的情况类似于将jar包部署到azure Web App。 我上传了一个SpringBoot项目,作为如何将jar部署到Azure Web应用程序的示例 第1点:请使用
mvn package
在pom.xml
文件所在的目录中创建JAR包
]
第2点:请确保web.config中配置的jar包名称与上载的jar包名称相同
web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
</handlers>
<httpPlatform processPath="%JAVA_HOME%\bin\java.exe"
arguments="-Djava.net.preferIPv4Stack=true -Dserver.port=%HTTP_PLATFORM_PORT% -jar "%HOME%\site\wwwroot\<your jar name>"">
</httpPlatform>
</system.webServer>
</configuration>
希望它能帮助你。你不必重新构建你的应用程序,grizzly可以正常运行,唯一的问题是文档不够清晰
jar教程很有帮助,但在部署基于grizzly的应用程序时缺少两个关键点
我使用与部署Spring引导应用程序类似的maven配置
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-webapp-maven-plugin</artifactId>
<version>1.9.0</version>
<configuration>
<schemaVersion>V2</schemaVersion>
<resourceGroup>Your_apps_resource_group_name</resourceGroup>
<appName>Your-app-name</appName>
<region>eastus2</region>
<pricingTier>F1</pricingTier>
<runtime>
<os>linux</os>
<javaVersion>jre8</javaVersion>
<webContainer>jre8</webContainer>
</runtime>
<!-- Begin of App Settings -->
<appSettings>
<property>
<name>JAVA_OPTS</name>
<value>-Dserver.port=80 -Djava.net.preferIPv4Stack=true</value>
</property>
</appSettings>
<!-- End of App Settings -->
<deployment>
<resources>
<resource>
<directory>${project.basedir}/target</directory>
<includes>
<include>*.jar</include>
</includes>
</resource>
</resources>
</deployment>
</configuration>
</plugin>
应用程序将在Azure容器上运行,该容器在启动时随机分配服务器端口,因此grizzly的常规8080在99.9999%的时间内无法工作
为了设置端口,您需要获取系统属性
public static void main(String[] args) throws IOException, ServletException {
String port = "8080";
if (!Objects.isNull(System.getProperty("server.port"))) {
port = System.getProperty("server.port");
}
System.out.println("Using server port " + port);
String host = "0.0.0.0";
final String baseUri = String.format("http://%s:%s/", host, port);
startServer(baseUri);
System.out.println(String.format("Jersey app started with WADL available at "
+ "%sapplication.wadl%nHit enter to stop it...", baseUri));
System.out.println(String.format("Jersey app started with WADL available at "
+ "%sswagger.json%nHit enter to stop it...", baseUri));
}
这里的另一个关键是,为了在容器内运行并绑定IP地址,grizzly需要从0.0.0.0开始,而不是从本地主机()开始
start server方法接收基本URI,而不是使用私有静态URI,如下所示
public static HttpServer startServer(final String baseUri) throws ServletException {
WebappContext webAppContext = new WebappContext(
"GrizzlyContext", "/");
HttpServer server = GrizzlyHttpServerFactory.createHttpServer(URI.create(baseUri));
CLStaticHttpHandler staticHttpHandler
= new CLStaticHttpHandler(Main.class.getClassLoader(),"/www/");
server.getServerConfiguration().addHttpHandler(staticHttpHandler,"/");
webAppContext.deploy(server);
return server;
}
也许自从@Jinesh提到的教程编写以来,情况已经发生了变化,但我发现没有必要使用web.config
我遵循的步骤是:
在Azure中创建应用程序服务,将web服务器选择为“Java SE(嵌入式web服务器)”
将jar文件上载到/home/site/wwwroot文件夹中
在常规设置页面中,将启动命令设置为java-jar/home/site/wwwroot/xxxx.jar
在应用程序设置页面中,将端口设置为应用程序将侦听的端口号
这些说明基于本教程
public static HttpServer startServer(final String baseUri) throws ServletException {
WebappContext webAppContext = new WebappContext(
"GrizzlyContext", "/");
HttpServer server = GrizzlyHttpServerFactory.createHttpServer(URI.create(baseUri));
CLStaticHttpHandler staticHttpHandler
= new CLStaticHttpHandler(Main.class.getClassLoader(),"/www/");
server.getServerConfiguration().addHttpHandler(staticHttpHandler,"/");
webAppContext.deploy(server);
return server;
}