简体   繁体   English

WAR / JAR文件上的Spring Boot问题

[英]Spring boot questions on WAR/JAR files

After a lot of head-scratching and going round in circles at websites I managed to get my Spring boot RESTful webservice deployed as a war file into Tomcat, using gradle. 在网站上进行了大量的反复研究之后,我设法使用gradle将Spring boot RESTful Webservice作为war文件部署到Tomcat中。 It was very much simpler than the documentation or other websites make out. 它比文档或其他网站制作的要简单得多。

However, I just have a few questions: 但是,我有几个问题:

  1. How can I remove the bloat of spring from my 13Mb file and instead use a shared/deployed Spring version instead, to more easily support updating libraries? 如何从13Mb文件中删除spring的膨胀,而改为使用共享/部署的Spring版本,以便更轻松地支持更新库?

  2. Why is embedded tomcat still put into my war file (under WEB-INF\\lib-provided)? 为什么嵌入式tomcat仍放在我的war文件中(在WEB-INF \\ lib提供的目录下)?

  3. Is there a way to also have the simpler jar version built for development purposes? 有没有办法为开发目的而构建更简单的jar版本?

  4. For a production system how is a jar produced file as good as a war file deployed to a container - you cannot easily modify configuration, you cannot scale it (in terms or load balancing the web server) and there is no management front-end to it. 对于生产系统,jar产生的文件和war文件如何部署到容器中一样好-您无法轻松修改配置,无法缩放规模(就Web服务器而言或负载均衡),并且没有管理前端它。 If the intent in spring (or spring boot) is to really create self-contained micro servers I still can't understand scaling it or even how you run a jar in Windows that is not a console application... 如果春季(或春季启动)的意图是真正创建独立的微服务器,我仍然无法理解缩放它,甚至无法理解如何在非控制台应用程序的Windows中运行jar。

Here are the salient points of my files: 这是我文件的重点:

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile('org.springframework.boot:spring-boot-starter-test')
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
}

Code: 码:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }

}

Thanks. 谢谢。

This should probably be several questions, so I'll (try to) answer the ones I can. 这可能应该是几个问题,所以我将尽力回答。 I don't use Gradle, but I know Maven pretty well and the principles are the same. 我不使用Gradle,但是我非常了解Maven,并且原理相同。

1 . 1 I don't know why a 13Mb jar is cause for concern, but you may be able to declare the dependency as compileOnly scope. 我不知道为什么13Mb jar引起关注,但是您可以将依赖项声明为compileOnly范围。 Assuming the classpath of the server has the needed dependency available, this should work, though I've never tested it. 假设服务器的类路径具有所需的依赖项可用,尽管我从未测试过,但这应该可以工作。

There are other potential issues here, however. 但是,这里还有其他潜在问题。 Your goal: "use a shared/deployed Spring version instead, to more easily support updating libraries?" 您的目标: “改为使用共享/部署的Spring版本,以便更轻松地支持更新库?” seems like a good idea, until you update the version on your server and it breaks your applications. 在您更新服务器上的版本并破坏应用程序之前,这似乎是一个好主意。 It's much safer for each application to manage its own dependencies and deploy a dependency upgrade (as a new version of the application) only after it's been tested. 每个应用程序只有在经过测试后,才可以管理自己的依赖关系并部署依赖关系升级(作为应用程序的新版本)要安全得多。

2. The embedded Tomcat is a dependency included in spring-boot-starter-web . 2.嵌入式Tomcat是spring-boot-starter-web包含的依赖项。 You should be able to exclude it this way: 您应该可以通过以下方式排除它:

configurations {
    runtime.exclude module: 'spring-boot-starter-tomcat'
}

3. How do you define simpler? 3.您如何定义更简单? Again, not seeing the issue, but it seems like a bad idea anyway. 再次,没有看到问题,但是无论如何这似乎是一个坏主意。 I'd want whatever I'm working on to exactly mirror (Or as closely as possible) what I'm going to be putting into production. 我想要我正在做的任何事情都能准确地(或尽可能地接近)我将要投入生产的东西。

4. There are pros and cons to any approach. 4.任何方法都各有利弊。 You use what is best for your situation. 您使用最适合自己的情况。 A JAR can be self-contained, and for a small service, maybe that's better. JAR可以是独立的,对于小型服务,也许更好。

if your working with maven then creating spring-boot war file needs deployment discriptor file(web.xml) or its equalent java base configuration like initializer class for this configuration below snippet code you have write to make war file as below 如果您使用maven然后创建spring-boot war文件,则需要在片段代码下方针对此配置使用部署描述符文件(web.xml)或其等同的Java基本配置(例如,初始化程序类),您已编写了以下内容以制作war文件

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }

}

and exclude the default embedded server dependecy, and if packaging is jar then change the jar to war and then run the maven goal like maven clean install then you will get war file 并排除默认的嵌入式服务器依赖项,如果包装为jar,则将jar更改为war,然后运行maven目标(如maven clean install),则将获得war文件

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

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