springboot改造成springcloud(eureka client)并打war包
一、将spring boot项目改造成spring cloud项目
1.在pom.xml 文件<properties>里添加
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
2.引入eureka client依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
3.引入<dependencyManagement>。注意,加在</dependencies>和<build>之间
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
4.Application.class不要忘记加注解@EnableDiscoveryClient
5..yml文件配置项目名称和defaultZone
spring:
appkication:
name: client
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
二、配置war包打包方式
1.pom.xml文件<packaging>默认是jar,修改为war
2.移除内嵌的tomcat模块,但是为了我们在本机测试方便,我们还需要引入它,所以配置如下
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--移除内嵌的tomcat模块,但是为了我们在本机测试方便,我们还需要引入它,所以配置如下-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--移除内嵌的tomcat模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!--添加tomcat-servelt-api依赖-->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>7.0.42</version>
<scope>provided</scope>
</dependency>
3.
<!--添加war插件,用来自定义打包以后的war包的名称
是避免maven打包的时候为我们默认的一个带有版本号的war包名称,
因为我们部署到tomcat以后,在访问项目的时候,需要用到这个war包的名称
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warSourceExcludes>src/main/resources/**</warSourceExcludes>
<warName>springboot</warName>
</configuration>
</plugin>
4.修改入口方法 继承一个SpringBootServletInitializer类,并且覆盖configure方法
public class NontaxApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(NontaxApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(NontaxApplication.class, args);
}
}
5.Terminal窗口执行命令mvn clean package打包
最后打开target目录,可以看到生成了springboot.war包