【问题标题】:How do I deploy Broadleaf 5.2 on Tomcat 8.5如何在 Tomcat 8.5 上部署 Broadleaf 5.2
【发布时间】:2019-03-23 20:22:21
【问题描述】:

我正在使用 Eclipse (photon) 和 Broadleaf 社区演示项目(站点、管理、api 和核心)。

我已经能够使用嵌入式 Eclipse Web 服务器在我的本地开发机器上运行 siteadminapi,并且现在我正在尝试将站点、管理员和 api 项目作为 WAR 部署在运行 Tomcat 8.5 (JVM 1.7) 的 AWS EC2 实例上。

我遵循了 Broadleaf 文档:

加上 许多 其他关于该主题的 Google 搜索网站,但都没有成功。那是从 2012 年到 2015 年,并且都引用了 BL 的旧版本(Servlet 3.0 之前)。

我什至尝试过“web.xml”方法:

Maven clean/install 生成的 WAR 文件(我也尝试过“package”),看起来不像我习惯的典型 WAR 文件:

站点:(ROOT.war)

META-INF
org
WEB-INF

有人可以举一个例子或网站,说明如何在 Tomcat 服务器上将 BLC 项目部署为 WAR 吗?

我得到的结果要么是端口 8443 上的 404,要么是空白页面(使用 web.xml 方法时)。

对于这个相当开放/含糊的问题,我很抱歉,但是对于 BLC 5.2 几乎没有这方面的文档,我已经向 Stack Overflow 寻求帮助。

谢谢


网站应用程序

@SpringBootApplication
@EnableAutoConfiguration
public class SiteApplication extends SpringBootServletInitializer {

    @Configuration
    @EnableBroadleafSiteAutoConfiguration
    public static class BroadleafFrameworkConfiguration {}

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SiteApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(SiteApplication.class, args);
    }

}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>com.mycompany-community</groupId>
        <artifactId>boot-community-demo</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>boot-community-demo-site</artifactId>
    <packaging>war</packaging>

    <name>Community Demo Site</name>
    <description>Web Module For Customized Broadleaf Commerce Site</description>

    <properties>
        <debug.port>8000</debug.port>
    </properties>

    <build>
        <finalName>ROOT</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.mycompany-community</groupId>
            <artifactId>boot-community-demo-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.broadleafcommerce</groupId>
            <artifactId>broadleaf-framework-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.broadleafcommerce</groupId>
            <artifactId>broadleaf-boot-starter-solr</artifactId>
        </dependency>
        <!--
        <dependency>
            <groupId>com.broadleafcommerce</groupId>
            <artifactId>broadleaf-boot-starter-hsql-database</artifactId>
        </dependency>
        -->
        <dependency>
            <groupId>com.broadleafcommerce</groupId>
            <artifactId>broadleaf-boot-starter-database</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>  
        <dependency>
            <groupId>org.broadleafcommerce</groupId>
            <artifactId>broadleaf-thymeleaf3-presentation</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.zhanhb</groupId>
            <artifactId>thymeleaf-layout-dialect</artifactId>
            <version>2.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>        
    </dependencies>

</project>

【问题讨论】:

    标签: broadleaf-commerce


    【解决方案1】:

    好的,我们想通了。我想分享我们采取的步骤,因为它是来自不同来源(BLC 和 Spring Boot 站点)的步骤/信息的组合。


    第 1 步:BLC 代码库

    下载来源:https://github.com/BroadleafCommerce/DemoSite

    我使用的文件是“DemoSite-develop-5.2.x”。

    将其解压缩到您的 IDE 可以查看/访问的位置。


    第 2 步:IDE(本例中为 Eclipse)

    按照本网站的步骤(几乎是逐字逐句):

    https://www.broadleafcommerce.com/docs/core/current/getting-started/ide-setup/eclipse-setup

    您现在应该让 siteadminapi 在本地运行,与内部 HSQL 数据库通信。

    • 管理员:https://localhost:8444/admin
    • 站点:https://localhost:8443/
    • API: https://localhost:8445/api/v1/swagger-ui.html

    第 3 步:更改为 MySQL 数据库。

    我几乎按照这些说明一步一步地进行操作(尽管我的机器上已经安装了 MySQL):

    https://www.broadleafcommerce.com/docs/core/current/broadleaf-concepts/key-aspects-and-configuration/database-configuration/mysql#


    第 4 步:BLC 项目代码/xml 更改

    一切从这个链接开始:

    https://www.broadleafcommerce.com/docs/core/current/broadleaf-concepts/key-aspects-and-configuration/app-server-configuration/deploying-a-.war-file

    您在哪里找到:

    • 部署 .war 文件
    • 我的 web.xml 在哪里?

    按照以下 87.1 中的步骤进行操作:

    https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html#howto-create-a-deployable-war-file

    基本上:

    • SiteApplication.java 中的 SpringBootServletInitializer
    • &lt;packaging&gt;war&lt;/packaging&gt;
    • pom.xml 文件中的“spring-boot-starter-tomcat”行。

    然后我创建了一个“web.xml”文件,(虽然我不是 100% 确定我需要这个,因为我的 servlet 容器是 Servlet 3.1 并且 BLC 文档说所有这些 web.xml 配置现在都由注释处理) .

    https://www.broadleafcommerce.com/docs/core/current/broadleaf-concepts/key-aspects-and-configuration/app-server-configuration/using-a-web.xml

    基本上:

    • SiteApplication.java 中的 BroadleafBootServletContextInitializer
      • 您会注意到我们之前添加了“SpringBootServletInitializer”。
      • 现在我们将“SpringBootServletInitializer”替换为“BroadleafBootServletContextInitializer”。
    • 添加页面底部的“web.xml”。
    • 不要忘记将“context-param”标签的“param-value”更改为您正确的包位置。

    第 5 步:Tomcat 更改

    另一个包含您需要的大部分(如果不是全部)的链接:

    https://www.broadleafcommerce.com/docs/core/current/broadleaf-concepts/key-aspects-and-configuration/app-server-configuration/tomcat

    /bin

    • 新的“setenv.sh”文件包含:

      export CATALINA_OPTS="-Xmx1536M -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"
      

    /conf

    • context.xml - 取消注释以下行

      `<Manager pathname="" />`
      
    • somenamehere-keystore(需要生成,或者在BLC代码库中获取)

    • server.xml - 添加了 HTTPS 连接器:

      <Connector SSLEnabled="true" 
              clientAuth="false" 
              keystoreFile="/path/to/server/apache-tomcat-8.5.29/conf/*somenamehere*.keystore" 
              keystorePass="BLCPassword" 
              keyPass="BLCPassword" 
              keystoreType="PKCS12"
              ciphers="TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,TLS_ECDHE_RSA_WITH_RC4_128_SHA,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_256_CBC_SHA256,TLS_RSA_WITH_AES_256_CBC_SHA,SSL_RSA_WITH_RC4_128_SHA"
              maxThreads="150" port="8443" protocol="HTTP/1.1" scheme="https" secure="true" sslProtocol="TLSv1.2"/>
      
      
      
      Note that I had to add the "keystoreType" attribute due to 
      my keystore being in the PKCS12 format, as opposed to JKS.
      
      The "ciphers" attribute was added as I got an exception 
      complaining about the TLS I was using.
      
    • tomcat-users.xml

      Added the following two lines:
      
      <role rolename="manager-gui"/>
      <user username="admin" password="ReallyHardToGuessPW" roles="manager-gui,admin-gui"/>
      
      I'm not sure if this step was 100% needed, but it allows me 
      to see the BLC projects within the "manager" app of Tomcat.
      

    /webapps

    • host-managermanager 应用程序

      In the "META-INF" of these apps I commented out both the "Valve" and "Manager" 
      tags in the "Context".
      
      This was extracted from this link:
      
          https://www.howtoforge.com/tutorial/how-to-install-apache-tomcat-8-5-on-ubuntu-16-04/
      
      (Look at "Step 6")
      
      This appears to be the only difference in my earlier attempts in 
      getting this running (pre-cry-for-SO-help), as yesterday and this 
      morning I had already performed all the steps in this post.   
      

    第 6 步:Maven 清理和安装

    在 Eclipse 中,在每个站点和管理项目(“boot-community-site”和“boot-community-admin”)上,右键单击并选择 Run As >> Maven clean .完成后运行 Run As >> Maven install

    现在在每个项目的“target”目录中,您应该会看到一个 WAR 文件,(“ROOT”代表 Site 项目,“admin”代表 Admin 项目)。


    第 7 步:启动 Tomcat 服务器

    我启动了 Tomcat 服务器,其中的常驻应用程序(文档、示例、主机管理器、管理器和 ROOT)保持不变。然后我将 BLC“admin.war”和“ROOT.war”(站点)热部署到 Tomcat /webapps 目录。

    如果这对您不起作用,您可以随时停止 Tomcat 服务器并将 ROOT.war 和 admin.war 文件放在 /webapps 目录中。然后启动服务器。

    如果一切顺利,在启动时查看“catalina.out”日志文件,您会看到一条 ASCII“Broadleaf Commerce”消息滚动,每个 BLC 应用程序一次,然后是一堆 MySQL Hibernate 消息。在“m5.large”AWS EC2 实例上,站点和管理员的整个启动时间似乎都约为 4 分钟。

    当我想到我错过的内容时,我会补充一下,但我现在在 Tomcat 8.5 容器上运行 BLC 5.2。

    希望它对其有相同问题的其他人有用。

    【讨论】:

    • 非常好的指南,感谢您的贡献!快速说明:您是对的,web.xml 是不必要的,您可以从 SpringBootServletInitializer 扩展而不是 Broadleaf 特定的扩展。虽然它们基本相同,但如果您有 web.xml,则无法使用 Spring @Beans(即额外的 ServletContextInitializers)进行一些自动 servlet 注册。唯一需要 web.xml 的情况是,如果您收到关于类转换的非常讨厌的错误消息,并且某些类加载得太早。这不会在 Tomcat >= 8 中发生。
    • 我收到以下错误。一个组件需要一个名为“blSessionFixationProtectionFilter”的 bean,但找不到该 bean。我检查了它在一个 jar 文件中有 bean。
    【解决方案2】:

    非常有用的指南。我想添加一些关于步骤 5 的内容,尤其是关于负载平衡器环境中的 spring 安全性。

    由于 SSL 将发生在前端代理上,spring 的安全 requiresSecure() 可能会导致无限循环的重定向。

    这在herehere有深入解释

    解决办法是:

    在前端代理上安装 ssl 证书后,在您的 .properties 文件中包含这两个请求标头:

    server.tomcat.remote-ip-header=x-forwarded-for
    server.tomcat.protocol-header=x-forwarded-proto
    

    无需从 tomcat 进行重定向,因此您不必在 server.xml 中包含 https 连接器,如第 5 步所述。

    只需添加这两行

    secure="真"

    scheme="https"

    在你的 tomcat 的 server.xml 的默认连接器上

    <Connector port="hereisyourdefaultportofyourtomcat" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               secure="true"
               scheme="https" 
               />
    

    请注意:前端代理应发送上述请求标头。大多数情况下默认情况下会发生这种情况。检查here

    【讨论】:

    • @FaizFareed 谢谢。我希望这可以避免那些试图解决这个问题的人度过很多不眠之夜。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 2019-03-21
    • 1970-01-01
    • 2019-01-17
    • 2020-10-16
    相关资源
    最近更新 更多