1、项目打包
1.1 修改父工程
打开父项目的pom.xml文件,基本信息如下
<modelVersion>4.0.0</modelVersion>
<groupId>com.xct</groupId>
<artifactId>springcloud_2</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
添加如下信息,这段信息之前是配置在子工程里面的,现在配于此处,子工程则不需要配置:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/>
</parent>
声明其子模块:
<modules>
<module>gateway</module>
<module>goods-service</module>
<module>integral-service</module>
<module>order-service</module>
<module>registry</module>
<module>user-service</module>
</modules>
1.2 修改子模块
这里以registry服务为例,打开pom.xml文件:
基本信息:
<groupId>com.xct</groupId>
<artifactId>registry</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>registry</name>
<description>Demo project for Spring Boot</description>
<packaging>jar</packaging>
继承项目的父工程:
<parent>
<groupId>com.xct</groupId>
<artifactId>springcloud_2</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
最后,需在各个子模块中添加打包插件,这个插件主要是构建可执行的jar:
<build>
<finalName>registry</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
补充:<finalName>registry</finalName>
可以自定义打包后的项目(jar)名称。
1.3最后,开始打包
打开左侧maven工具窗口
先执行clean,清楚之前打的jar包,在进行install,进行打包操作
打包报错
项目打包出现:Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.3.2.RELEASE:repack
项目在进行mvn打包的时候出现
Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.3.2.RELEASE:repackage (repackage) on project microblog-util: Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:2.3.2.RELEASE:repackage failed: Unable to find main class
问题原因
项目里面包含了某些工具类的模块,
而工具类模块是不需要我们启动的,
只是提供给其他微服务引用而已,
不需要启动就意味着 没有main启动类,
但是父工程的pom文件却引用了
springboot打包插件 spring-boot-maven-plugin,即:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
所以在打包的时候mvn会扫描所有的依赖模块,如果发现某个模块下面没有main启动类,就会报错。
解决办法
既然知道了产生这个问题的原因,哪接下就是对症下药解决问题的时候了,直接删掉父工程的打包插件 spring-boot-maven-plugin,然后右键重新导入maven依赖。
打包成功
此时,在项目target目录下就会生成一个jar包,这就算成功了!
二、将项目部署到Linux上运行
1、使用ftp工具将jar文件上传到linux服务器
2、运行jar包
命令
在linux系统下运行jar包的命令如下
1、java -jar xxxxx.jar // 当前ssh窗口被锁定,可按CTRL + C打断程序运行,或直接关闭窗口,程序退出
2、java -jar xxxxx.jar & //当前ssh窗口不被锁定,但是当窗口关闭时,程序中止运行。
3、nohup Java -jar xxxxxx.jar & //意思是不挂断运行命令,当账户退出或终端关闭时,程序仍然运行
4、nohup Java -jar xxxxxx.jar >aa.log //指定输出日志文件,意思是不挂断运行命令,当账户退出或终端关闭时,程序仍然运行
启动registry服务
查看端口
使用netstat -tunlp | grep 8761 查看端口
访问IP:端口/
说明registry项目已经成功在Linux上运行了。如果需要部署其他工程只需跟上面步骤一样即可。
运行报错mysql连接问题
【MySQL8.0】com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
【MySQL8.0】com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
MySQL的版本 8.0.19,和8.0.11差了一点点,但在pom.xml中配置的mysql的版本号,一定要跟所连接的MySQL的数据库完全一致。至此,springboot正常启动,问题解决。
也可尝试新增以下两个配置
#下面这两个配置,可以在每次连接的时候判断一些连接是否有效
spring.datasource.druid.test-on-borrow=true
spring.datasource.druid.test-while-idle=true
以上两个配置
test-on-borrow=true 表示当应用向连接池申请连接时,连接池会判断这条连接是否是可用的
test-while-idle=true 表示申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。