springCloud Finchley microservice architecture from entry to proficiency 【Eleven】Tomcat deployment project

  It is more troublesome to use tomcat to deploy projects in old versions of spring cloud. You may need to manually exclude tomcat in pom.xml to deploy to the online environment. After excluding tomcat, you cannot use the SpringBootApplication class to start the project in eclipse.

  In the new version of Spring Cloud, developers have solved this problem. Now it is very simple to use Tomcat to deploy projects. It only takes two simple steps to ensure that it is available locally and online at the same time.

###1. Modify Pom.xml configuration###

Tomcat deploys the war package, so the Maven packaging format should be changed to war

<packaging>war</packaging>

Springcloud application deployment is usually a tomcat deploying an application. For convenience, all its microservices are labeled as war packages with the name ROOT.war

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

###2. Modify the startup class###
Inherit the SpringBootServletInitializer class and override the configure method

package com.mayi.springcloud;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class CommonserviceEurekaApplication extends SpringBootServletInitializer{
    
    

	public static void main(String[] args) {
    
    
		SpringApplication.run(CommonserviceEurekaApplication.class, args);
	}
	
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
    
    
		return builder.sources(CommonserviceEurekaApplication.class);
	}
}

###3. Start test###

  • Start directly through the startup class in eclipse
  • Use mvn clean package -DskipTests to make a war package, and then put the war package into the online tomcat startup

Write picture description here

Note: It is better to keep the port number in the configuration file consistent with tomcat (the default priority is tomcat configuration), otherwise it is easy to cause confusion

Guess you like

Origin blog.csdn.net/wcblog/article/details/80648988