如何在Spring Boot应用中更换服务器?

huangapple go评论70阅读模式
英文:

How can I change server in spring boot application?

问题

> 我的项目要求使用除了Tomcat之外的另一个服务器?我们如何从Spring Boot应用程序中更改嵌入式服务器?

英文:

> My project requirement is to use another server than tomcat? How can
> we change embedded server from spring boot application?

答案1

得分: 7

你需要从启动依赖中排除 Tomcat:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <exclusions>
    <exclusion>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </exclusion>
  </exclusions>
</dependency>

然后你需要将新的服务器作为依赖包含,例如:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
英文:

You have to exclude tomcat from starter dependency:

&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
  &lt;exclusions&gt;
    &lt;exclusion&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
    &lt;/exclusion&gt;
  &lt;/exclusions&gt;
&lt;/dependency&gt;

and now you need to include new server as a dependency i.e.:

&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  &lt;artifactId&gt;spring-boot-starter-jetty&lt;/artifactId&gt;
&lt;/dependency&gt;

答案2

得分: 6

你需要更新 pom.xml 文件,在其中添加对 spring-boot-starter-jetty 依赖的声明。同时,你需要在 spring-boot-starter-tomcat 依赖声明中使用 exclude 来排除默认添加的 Tomcat 依赖。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

对于 Gradle:

configurations {
    compile.exclude module: "spring-boot-starter-tomcat"
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-jetty")
}
英文:

You will need to update pom.xml, add the dependency for spring-boot-starter-jetty. Also, you will need to exclude default added spring-boot-starter-tomcat dependency.

&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
    &lt;exclusions&gt;
        &lt;exclusion&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
        &lt;/exclusion&gt;
    &lt;/exclusions&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-jetty&lt;/artifactId&gt;
&lt;/dependency&gt;

In gradle,

configurations {
    compile.exclude module: &quot;spring-boot-starter-tomcat&quot;
}
 
dependencies {
    compile(&quot;org.springframework.boot:spring-boot-starter-web&quot;)
    compile(&quot;org.springframework.boot:spring-boot-starter-jetty&quot;)
}

答案3

得分: 1

# Gradle方式

implementation 'org.springframework.boot:spring-boot-starter-jetty'
modules {
    module("org.springframework.boot:spring-boot-starter-jetty") {
        replacedBy("org.springframework.boot:spring-boot-starter-tomcat", "使用Jetty替代Tomcat")
    }
}

# 另一种Gradle方式

compile('org.springframework.boot:spring-boot-starter-webflux') {
    exclude group: 'org.springframework.boot',
            module: 'spring-boot-starter-reactor-netty'
}
compile('org.springframework.boot:spring-boot-starter-jetty')
英文:
# Gradle way

	 implementation &#39;org.springframework.boot:spring-boot-starter-tomcat&#39;
	 implementation &#39;org.springframework.boot:spring-boot-starter-jetty&#39;
	 modules {
      module(&quot;org.springframework.boot:spring-boot-starter-tomcat&quot;) {
          replacedBy(&quot;org.springframework.boot:spring-boot-starter-jetty&quot;, &quot;Use Jetty instead of Tomcat&quot;)
      }
     }

# Gradle Another Way

	compile(&#39;org.springframework.boot:spring-boot-starter-webflux&#39;) {
		exclude group: &#39;org.springframework.boot&#39;,
		module: &#39;spring-boot-starter-reactor-netty&#39;
	}
	compile(&#39;org.springframework.boot:spring-boot-starter-tomcat&#39;)

答案4

得分: 0

默认的 Spring Boot 中的 嵌入式Web服务器Tomcat,但你可以很容易地将其更改为其他服务器。

首先,你需要排除掉 Tomcat 依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <!-- 排除 Tomcat 依赖 -->
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

然后你可以添加你所需的嵌入式Web服务器:

<!-- Jetty 嵌入式Web服务器 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

或者

<!-- Undertow 嵌入式Web服务器 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
英文:

The default Embedded Web Servers in Spring-Boot is Tomcat, but you can easily change it to others.

First of all, you need to exclude tomcat:

&lt;dependency&gt;
	&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
	&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
	&lt;exclusions&gt;
		&lt;!-- Exclude the Tomcat dependency --&gt;
		&lt;exclusion&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
		&lt;/exclusion&gt;
	&lt;/exclusions&gt;
&lt;/dependency&gt;

Then you can add your desired embedded web server:

&lt;!-- Jetty Embedded Web Server --&gt;
&lt;dependency&gt;
	&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
	&lt;artifactId&gt;spring-boot-starter-jetty&lt;/artifactId&gt;
&lt;/dependency&gt; 

or

&lt;!-- Undertow Embedded Web Server --&gt;
&lt;dependency&gt;
	&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
	&lt;artifactId&gt;spring-boot-starter-undertow&lt;/artifactId&gt;
&lt;/dependency&gt; 

huangapple
  • 本文由 发表于 2020年7月23日 20:18:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63054114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定