目录儿
引入(废话写在前边)
众所周知,写好的项目肯定是要放到服务器上运行的,这里给大家演示一种超简单的部署方式,堪称奶奶级教学
1、打包springcloud项目
项目肯定是要打成jar包滴,总不能服务器上装个idea点运行吧……
1.1、简单的微服务项目结构
下面是我的项目结构目录(看个乐就行,不要在意细节)
1.2、首先确保你项目父工程的pom文件的打包方式为 pom, 即
<packaging>pom</packaging>
1.3、其次确保你项目父工程有maven的打包插件,即
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
1.4、再然后,找到你 需要启动运行 的子工程
这里划重点:微服务中一般都有很多个模块,有的模块是公共类不需要运行,而有的模块是业务类需要运行,这里我们需要找到那些需要启动运行的业务类模块,在它们的pom文件中添加maven的打包插件,即
在上述的各个业务类中的pom文件中添加:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
并确保这些pom文件的packing属性都为jar,即
<packaging>jar</packaging>
剩余的公共类不需要处理,第一步到此结束。
2、运行maven打包命令,打jar包
配置好了插件就要使用
2.1、找到右侧maven面板,打开父工程的Lifecycle
按照如下顺序依次点击即可
(正常情况下到这里就成功了,下面说一下特殊情况)
2.1.1、程序没问题,但是经过上述三连操作后,某个子程序运行不起来
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-12-29 09:11:14.410 ERROR 12604 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
上述报错去网上查了一下,可以参考以下两篇博客
Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded 的三种解决办法
SpringBoot中“Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datas
3、将jar包放到服务器上
3.1、上传jar包
可以使用例如 xftp 等之类的文件上传工具,将jar包放到服务器的某个地方
上传时要注意服务器上放jar包的文件夹需要有可写的属性
3.2、确保你的服务器有java环境
这里就不多赘述了,网上一大堆,下面放链接
Linux下安装Java环境
4、运行你的jar包
可以通过如下命令运行(这个是前台运行,关闭xshell窗口程序就停止了)
java -jar xxx.jar
还可以通过如下命令运行(这个是后台运行,关闭xshell窗口程序依然运行)
nohup java -jar xxx.jar &
如果想要指定日志文件的打印位置,可以通过如下命令运行
nohup java -jar xxx.jar > ./xxx.log 2>&1 &
这样日志文件就会重定向输出到当前目录下的xxx.log中了
总结(废话也可以写在后边)
综上所述,就完成了对java程序的 打包 -> 放置 -> 运行 一键三连的操作,这都只是比较笼统的操作,具体的可能程序中需要redis,nacos,mysql之类的环境,都需要在服务器上事先安装好的,如果只是简单的java项目,用上面的方式肯定是没问题的!
以上内容,仅供参考,有任何错误大家都可以随时指出,知错就改,善莫大焉~