13

我有一个非常标准的 Spring Boot 应用程序(application.properties属性文件位于标准/src/main/resources文件夹中),我将其作为“胖 JAR”部署在 AWS Elastic Beanstalk 上。它工作得很好,但在服务器上上传图像存在问题。经过一番调查,似乎需要调整 NGINX 配置(增加client_max_body_size一些东西以便它可以接受上传到10MB),因此我在下面添加了一个.ebextensions文件夹,/src/main/resources其中包含以下内容的文件(取自这个答案):

files:
    "/etc/nginx/conf.d/proxy.conf":
        mode: "000755"
        owner: root
        group: root
        content: |
           client_max_body_size 20M;

但是,当我mvn在我的构建上运行时,它不会.ebextensions在根文件夹中创建,我想知道什么是最好的解决方案。我的pom.xml文件非常小,目前包含以下内容:

    ...

    <packaging>jar</packaging>

    ....

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>

            <dependencies>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>springloaded</artifactId>
                    <version>1.2.6.RELEASE</version>
                </dependency>
            </dependencies>

        </plugin>

提前致谢!


更新 1

@Lorena 当我将<resources> ...XML 插入我pom.xml的服务器然后启动服务器时,它会崩溃并出现以下情况:-

2017-03-20 21:40:29.504  WARN 10784 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'emailApiSpringBootMail': Unsatisfied dependency expressed through field 'javaMailSender'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.mail.javamail.JavaMailSender' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2017-03-20 21:40:29.507  INFO 10784 --- [           main] o.apache.catalina.core.StandardService   : Stopping service Tomcat
2017-03-20 21:40:29.533  WARN 10784 --- [           main] o.s.boot.SpringApplication               : Error handling failed (Error creating bean with name 'delegatingApplicationListener' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.annotation.ProxyCachingConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available)
2017-03-20 21:40:29.637 ERROR 10784 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field javaMailSender in com.myapp.server.api.impl.EmailApiSpringBootMail required a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found.
    - Bean method 'mailSender' not loaded because AnyNestedCondition 0 matched 2 did not; NestedCondition on MailSenderAutoConfiguration.MailSenderCondition.JndiNameProperty @ConditionalOnProperty (spring.mail.jndi-name) did not find property 'jndi-name'; NestedCondition on MailSenderAutoConfiguration.MailSenderCondition.HostProperty @ConditionalOnProperty (spring.mail.host) did not find property 'host'

再次删除 XML 解决了这个问题,所以很遗憾这不起作用。


更新 2

上一节中描述的问题似乎是<resources>指向的 new.ebextentions覆盖了以下<resources>定义的块:-

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

为了让一切正常工作,我将其复制并附加到末尾,如下所示:-

    <resources>

        <resource>
            <directory>src/main/resources/ebextensions</directory>
            <targetPath>.ebextensions</targetPath>
            <filtering>true</filtering>
        </resource>

        <!-- Followed is copied from `spring-boot-starter-parent.pom` -->

        <resource>
            <directory>${basedir}/src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/application*.yml</include>
                <include>**/application*.properties</include>
            </includes>
        </resource>
        <resource>
            <directory>${basedir}/src/main/resources</directory>
            <excludes>
                <exclude>**/application*.yml</exclude>
                <exclude>**/application*.properties</exclude>
            </excludes>
        </resource>

    </resources>

感谢大家的帮助!

4

5 回答 5

7

这是一个可以使其适用于 JAR 的 pom 片段:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/resources/ebextensions</directory>
            <targetPath>.ebextensions</targetPath>
            <filtering>true</filtering>
        </resource>
        <!-- Followed is copied from `spring-boot-starter-parent.pom` -->
        <resource>
            <directory>${basedir}/src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/application*.yml</include>
                <include>**/application*.properties</include>
            </includes>
        </resource>
        <resource>
            <directory>${basedir}/src/main/resources</directory>
            <excludes>
                <exclude>**/application*.yml</exclude>
                <exclude>**/application*.properties</exclude>
            </excludes>
        </resource>
    </resources>
</build>

它的工作方式与将 .ebextensions 移动到 .war 文件中的根目录 相同

实际上,我将示例上传到了这个repo。做一个mvn package

于 2017-03-20T17:12:34.467 回答
6

Lorena Salamanca Spring Boot + Elastic Beanstalk .ebextensions in JAR提出的解决方案不适用于我...:\

我的解决方案适用于 Spring 1.5.3.RELEASE

在项目的根目录中添加 .ebextensions 并在插件部分的末尾添加此代码段:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <id>prepare</id>
                    <phase>package</phase>
                    <configuration>
                        <tasks>
                            <unzip src="${project.build.directory}/${project.build.finalName}.jar" dest="${project.build.directory}/${project.build.finalName}" />
                            <copy todir="${project.build.directory}/${project.build.finalName}/" overwrite="false">
                                <fileset dir="./" includes=".ebextensions/**"/>
                            </copy>
                            <!-- <jar jarfile="${project.build.directory}/${project.build.finalName}.jar" basedir="${project.build.directory}/${project.build.finalName}"/>-->
                            <zip compress="false" destfile="${project.build.directory}/${project.build.finalName}.jar" basedir="${project.build.directory}/${project.build.finalName}"/>
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

这个插件使用ant解压spring boot生成的最终jar,复制根目录下的.ebextensions,再用同名的zip(jar)解压。经过测试并在生产中工作:)

于 2017-06-23T07:54:25.680 回答
3

我相信在这种情况下使用独立的 JAR,使用基于 Procfile 的配置更容易,然后将 JAR 捆绑.ebextensions到一个 zip 文件中。

首先创建一个文件,Procfile在项目的根目录中调用,内容如下:

web: java -jar sample_app-1.0.0.jar

然后创建一个 zip 文件,其中包含 JAR 以及Procfile文件和.ebextensions目录:

sample_app.zip
|
|_.ebextensions
|   |_ nginx
|      |_ conf.d
|         |_ proxy.conf
|
|_ sample_app-1.0.0.jar
|_ Procfile
于 2018-05-23T20:39:08.927 回答
0

您收到的错误消息是找不到 javaMailSender 属性。

pom.xml 中提到的资源路径会覆盖默认资源根路径。

因此,JavaMailSender 正在此新资源路径下查找 jndi 配置或邮件属性,但无法找到它。因此出现错误。

spring.mail.jndi-name=mail/testmail

或者

spring.mail.host=testserver.com
spring.mail.port=25
spring.mail.username=test
spring.mail.password=test123
spring.mail.protocol=smtp
spring.mail.defaultEncoding=UTF-8
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.auth.mechanisms=NTLM
spring.mail.properties.mail.smtp.auth.ntlm.domain=DOMAIN
spring.mail.properties.mail.debug=true
于 2017-03-21T09:37:03.437 回答
0

实际上这是与spring-boot jar相关的问题。由于您使用的是 1.2.6 版本的 spring-boot,它有一些错误,如 javamailsender 问题。您需要将您的 spring boot 版本升级到 1.3.0.release 以上。

在这张票#3478中,邮件发件人问题已针对 spring-boot 进行了修复。因此,您可以升级到最新版本或 1.3.0 以上的其他版本。

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <version>1.3.5.RELEASE</version>
</dependency>

希望这能解决您的问题。

于 2017-03-25T21:04:27.360 回答