繁体   English   中英

如何将 jar 文件部署到 azure?

[英]how to deploy jar file to azure?

我使用 grizzly 和 google guice 创建了一个 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 successfully at http://localhost:8080/ 
...press any key to stop the process

现在我想将此服务部署到云中。 我找到了关于部署 SpringBoot 应用程序和使用 jar 文件创建 webjobs 的文章,但这些都不适用于我的应用程序的工作方式。

我是否必须重新设计网络服务器(到 spring 启动),或者我可以按原样部署它吗? 如果是这样,如何?

谢谢,马特

编辑:我已经按照下面的 Jay 步骤进行了操作。 我创建了一个 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 &quot;%HOME%\site\wwwroot\ServerConsole-V1.jar&quot;">
    </httpPlatform>
  </system.webServer>
</configuration>

当我尝试使用浏览器或 PostMan 访问 RESTFul API 时,出现超时错误。 我该如何诊断?

根据您的描述,您的情况类似于将jar包部署到azure Web App。 我上传了一个 spring-boot 项目作为如何将 jar 部署到 Azure Web App 的示例。

Point 1:请使用mvn packagepom.xml文件所在目录下构建 JAR 包。

在此处输入图片说明 ]

第2点:请确保web.config中配置的jar包名称与上传的jar包名称一致。

在此处输入图片说明

网页配置

<?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 &quot;%HOME%\site\wwwroot\<your jar name>&quot;">
    </httpPlatform>
  </system.webServer>
</configuration>

Point 3:请使用FTP将jar filesweb.config发布到KUDU的D:\\home\\site\\wwwroot\\目录下。

Point 4:请确保ApplicationSettings与您的项目匹配,例如jdk versiontomcat version

在此处输入图片说明

希望对你有帮助。

下面是在 azure 服务器上运行 .jar 文件的教程。 您可以使用 Azure 的 Web 服务,它为您提供运行应用程序所需的 Java 环境和 Web 服务器。

运行 java jar 文件以在 Azure 应用服务 Web 应用上提供 Web 请求

你不必重新构建你的应用程序,grizzly 会运行得很好,唯一的问题是文档不够清楚。

run.jar 教程很有帮助,但为了部署基于 grizzly 的应用程序,缺少两个关键点。

我使用类似的 maven 配置作为用于部署 Spring 启动应用程序的配置。

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

这里的关键是 JAVA_OPTS 属性值

-Dserver.port=80  -Djava.net.preferIPv4Stack=true

该应用程序将在 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 而非本地主机上启动( 0.0.0.0、127.0.0.1 和 localhost 之间有什么区别?

启动服务器方法接收基本 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。

我遵循的步骤是:

  1. 在 Azure 中创建应用服务,选择 Web 服务器为“Java SE(嵌入式 Web 服务器)”
  2. 将您的 jar 文件上传到 /home/site/wwwroot 文件夹
  3. 在常规设置页面,将启动命令设置为java -jar /home/site/wwwroot/xxxx.jar
  4. 在应用程序设置页面中,将 PORT 设置为您的应用程序将侦听的端口号

这些说明基于本教程https://blog.zuehlke.cloud/2020/03/simple-java-app-jar-in-azure-appservice/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM