简体   繁体   English

Spring Boot 应用程序组织的最佳实践是什么?

[英]What are best practices for Spring Boot application organization?

I have been developing Spring Boot applications using Eclipse as the IDE (not that I think that's particularly relevant) and all test executions up to this point have been done by launching it from the IDE. I have been developing Spring Boot applications using Eclipse as the IDE (not that I think that's particularly relevant) and all test executions up to this point have been done by launching it from the IDE. This works fine for development, but now the application needs to be "deployable" (ie packaged up in some way, placed on a test system, unpacked, configured and run).这适用于开发,但现在应用程序需要是“可部署的”(即以某种方式打包、放置在测试系统上、解包、配置和运行)。

Are there recommended or "best practices" ways a deployed application should be organized?是否有推荐的或“最佳实践”的方式来组织已部署的应用程序?

Should the application resources be organized into a certain directory structure that is "known" so that when it's launched, it can find everything it needs?应用程序资源是否应该组织到某个“已知”的目录结构中,以便在启动时可以找到所需的一切? Are there resource files that must be named a particular way?是否存在必须以特定方式命名的资源文件?

Current Situation现在的情况

Toward this goal I created a shell script (Linux is the target OS) to launch the program and modified my maven build to collect together the necessary resources (eg jar dependencies, application.yml, log4j2.xml, etc.). Toward this goal I created a shell script (Linux is the target OS) to launch the program and modified my maven build to collect together the necessary resources (eg jar dependencies, application.yml, log4j2.xml, etc.). All of this is zipped up and placed onto the target test Linux box.所有这些都被拉上拉链并放置在目标测试 Linux 盒子上。

When the archive is unpacked, nearly everything sits in the "root".解压缩档案后,几乎所有内容都位于“根”中。 When I've attempted to execute the program, the program does execute (and quickly exits with errors), however there are issues that I think could be due to the run-time not having correct access to the resources it needs.当我尝试执行该程序时,该程序确实执行(并快速退出并出现错误),但是我认为存在一些问题可能是由于运行时无法正确访问它所需的资源。 This is just a guess.这只是一个猜测。

Here's an example:这是一个例子:

Unpacked zip:未包装的 zip:

[centos@route-assessor route-assessor]$ ls -l
total 41016
-rw-r--r--. 1 centos centos     1017 Oct 21 11:34 application.yml
-rw-r--r--. 1 centos centos  5059429 Oct 21 15:05 elastic-apm-agent-1.10.0.jar
drwxr-xr-x. 2 centos centos     8192 Oct 22 10:38 lib
-rw-r--r--. 1 centos centos     1975 Oct 22 15:12 log4j2.xml
drwxrwxr-x. 2 centos centos       71 Oct 22 15:12 logs
drwxrwxr-x. 2 centos centos       62 Oct 22 15:12 metrics
-rwxr-xr-x. 1 centos centos      740 Oct 22 15:09 run-route-assessor.sh
-rw-r--r--. 1 centos centos 36909394 Oct 22 10:38 services-0.0.1-SNAPSHOT.jar

UPDATE :更新

OK, perhaps "deployment" is not the term I should have used.好吧,也许“部署”不是我应该使用的术语。 I'm not planning on "deploying" or "installing" the application onto a machine, I just will place the zip archive on it, unzip it, do some configuration and run it.我不打算将应用程序“部署”或“安装”到机器上,我只是将 zip 存档放在上面,解压缩,进行一些配置并运行它。

With that said, my question pertains to whether (or not) there are recommended practices regarding how the artifacts in the unzipped result should be organized.话虽如此,我的问题是关于是否(或是否)有关于如何组织解压缩结果中的工件的推荐做法。

For example, in the eclipse project, the project is structured to be Maven "friendly" and it appears that Spring must expect this structure (or perhaps something similar).例如,在 eclipse 项目中,该项目的结构是 Maven “友好”,看起来 Spring 必须期待这种结构(或者可能类似的结构)。

So:所以:

<project>
  src/
    main/
      java/
        <java code contained in a package hierarchy>
      resources/
        <various needed resource files>
        log4j2.xml
        application.yml
        etc.

When an application is "packaged up", should a directory structure like this be maintained?当应用程序“打包”时,是否应该维护这样的目录结构? In other words, will the JVM/Spring Boot "runtime" expect to see resources organized like this?换句话说,JVM/Spring Boot“运行时”会期望看到这样组织的资源吗?

The spring-boot docs describe installing to Linux here . spring-boot 文档在此处描述了安装到 Linux。 The resulting single artifact from the maven build has the dependency libraries and resources where spring-boot looks for them.从 maven 构建生成的单个工件具有 spring-boot 查找它们的依赖库和资源。

Should the application resources be organized into a certain directory structure that is "known" so that when it's launched, it can find everything it needs?应用程序资源是否应该组织到某个“已知”的目录结构中,以便在启动时可以找到所需的一切? Are there resource files that must be named a particular way?是否存在必须以特定方式命名的资源文件?

You don't unpack anything to run the application.您无需解压任何东西即可运行该应用程序。 . . . . what's in the jar stays in the jar. jar 中的内容保留在 jar 中。 That's what the maven plugin (mentioned in that link) does.这就是 maven 插件(在该链接中提到)所做的。

Once, you have the build artifact on the server, you need to add configuration (eg db user/pass, etc.) and integrate it to centos's service management, which that doc also describes.一旦您在服务器上拥有构建工件,您需要添加配置(例如 db 用户/密码等)并将其集成到 centos 的服务管理中,该文档也描述了这一点。

That doc also describes how to ship your application to a cloud platforms, if that's your ultimate goal.该文档还描述了如何将您的应用程序发送到云平台,如果这是您的最终目标。

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

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