SpringBoot项目的云服务器部署

来源:互联网 发布:网络助手怎么用? 编辑:程序博客网 时间:2024/06/05 22:29

1.场景还原

     springboot配置相当简单,人人皆知。怎么把springboot工程部署到云服务器上呢?可能有人会说,博主你前篇不是讲了java工程的云部署把;但是我想澄清一点的是,我前篇的工程都是ssm框架搭建的,而springboot可是自带tomcat喽!这就有点麻烦….淡定,往下看

2.配置解析

①application.properties文件

# EMBEDDED SERVER CONFIGURATION (ServerProperties)server.port=8010server.session-timeout=1800server.context-path=server.tomcat.max-threads=0server.tomcat.uri-encoding=UTF-8server.tomcat.basedir=target/tomcat# HTTP encoding (HttpEncodingProperties)spring.http.encoding.charset=UTF-8spring.http.encoding.enabled=true spring.http.encoding.force=true#datasourcespring.datasource.url=jdbc:mysql://192.168.0.129/ccoee?useUnicode=true&characterEncoding=UTF-8spring.datasource.username=zhangxingspring.datasource.password=zxp52077spring.datasource.driverClassName=com.mysql.jdbc.Driver#mybatismybatis.type-aliases-package=com.cckj.modelmybatis.mapper-locations=classpath:mapper/*.xml#mappermapper.mappers=tk.mybatis.mapper.common.Mappermapper.not-empty=falsemapper.identity=MYSQL#pagehelperpagehelper.helperDialect=mysqlpagehelper.reasonable=truepagehelper.supportMethodsArguments=truepagehelper.params=count=countSql#logging#logging.level.root=INFO#logging.level.org.springframework.web=DEBUGlogging.level.org.zhangxing=DEBUG

就是这个文件 包罗了ssm框架的烦琐配置,哪个程序员不爱呢?那么既然爱,请深爱!将远程数据库连接配好,这里不再赘述。

②pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <packaging>jar</packaging>  <groupId>magic</groupId>  <artifactId>magic</artifactId>  <version>1.0-SNAPSHOT</version>  <parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>1.4.3.RELEASE</version>  </parent>  <dependencies>    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-jdbc</artifactId>    </dependency>    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-test</artifactId>    </dependency>    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <dependency>      <groupId>mysql</groupId>      <artifactId>mysql-connector-java</artifactId>    </dependency>    <dependency>      <groupId>com.alibaba</groupId>      <artifactId>druid</artifactId>      <version>1.0.10</version>    </dependency>    <dependency>      <groupId>org.mybatis.spring.boot</groupId>      <artifactId>mybatis-spring-boot-starter</artifactId>      <version>1.1.1</version>    </dependency>    <dependency>      <groupId>tk.mybatis</groupId>      <artifactId>mapper-spring-boot-starter</artifactId>      <version>1.0.0</version>    </dependency>    <dependency>      <groupId>com.github.pagehelper</groupId>      <artifactId>pagehelper-spring-boot-starter</artifactId>      <version>1.0.0</version>    </dependency>  </dependencies>  <build>    <finalName>helloworld</finalName>    <plugins>      <plugin>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-maven-plugin</artifactId>      </plugin>    </plugins>  </build>  <repositories>    <repository>      <id>spring-milestone</id>      <url>http://repo.spring.io/libs-release</url>    </repository>  </repositories></project>
这里要注意的是,打包方式jar,不再是war

<packaging>jar</packaging>
自定义打包名称

<build>    <finalName>helloworld</finalName>    <plugins>      <plugin>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-maven-plugin</artifactId>      </plugin>    </plugins>  </build>  <repositories>    <repository>      <id>spring-milestone</id>      <url>http://repo.spring.io/libs-release</url>    </repository>  </repositories>

③Application启动类代码

@SpringBootApplication@MapperScan(basePackages = "com.cckj.dao", markerInterface =Mapper.class)public class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}

注意:各个方法中的测试主函数最好全部注释掉!

3.实现方案

准备工作:在部署之前,首先确认云服务器是否装有tomcat环境,并且服务时候开启!若无环境,请自行安装并开启服务

①开始打jar包

跟前篇步骤一样,点开右边的Maven Project,点击Lifecycle,最后双击packet,生成jar包


②将刚打的jar上传至云服务器的java目录下


③运行命令:java -jar helloworld.jar,然后springboot工程的内置tomcat就开启了

注意:部署好之后,需要开放端口号:3306,8089

访问效果:


这里还没完呢,当然你关闭当前的xshell 命令界面时,再次访问就失效了,这是因为springboot内置的tomcat随之也关闭了,那么有人会问,这叫什么云部署呀?跟本地没两样,是啊,我也这么觉得;别急,往下看

④常驻云服务器

运行命令: nohup java -jar helloworld.jar &

nohup的意思不挂服务,常驻的意思,除非云服务器重启,那就没法了;最后一个&表示执行命令后要生成日志文件nohup.out


好了,这样就不担心关闭xshell,只要云服务器不关闭,总能访问的。