美文网首页
spring boot 项目部署到阿里云ECS服务器小结

spring boot 项目部署到阿里云ECS服务器小结

作者: 剁椒鸡蛋zy | 来源:发表于2017-06-01 17:36 被阅读0次

我采用的是最传统的方法把项目打包war,上传到服务器tomcat webapps 目录下。服务器Ubuntu系统

1. 阿里云服务器配置

先安装mysql
apt-get install mysql-server

然后修改mysql配置 让3306端口可以远程访问,修改/etc/mysql/mysql.conf.d/mysqld.cnf文件,把bind-address = 127.0.0.1这行注释掉。
然后grant all on *.* to root@'%' identified by 'password';授权root用户远程访问mysql。
执行netstat -an | grep 3306检测3306端口
tcp6 0 0 :::3306 :::* LISTEN 3306前面为0表示已经可以远程访问了。
但是阿里云服务器3306端口没有开启,需要 安全组->配置规则里面添加3306.如图

用MySQLWorkbench远程连接mysql 填写服务器公网IP,mysql 用户名 root,和密码 测试连

然后修改项目数据库地址
spring.datasource.url=jdbc:mysql://*.*.*.*:3306/databasename?autoReconnect=true&useSSL=false
运行项目,如果没有问题,就进行下一步打包。

2. 项目打包

spring boot 打war包。基本步骤

  1. 修改pom文件,jar 中jar改成war。

  2. 修改web 依赖 去掉内嵌的tomcat。


<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

  1. 修改Application.java 文件
// 继承SpringBootServletInitializer
public class Application  extends SpringBootServletInitializer{
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

// 重写方法
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(applicationClass);
    }

    private static Class applicationClass = Application.class;
}
  1. 这个时候如果直接点击运行按钮的话,会出错,这是Idea的bug,需要mvn spring-boot:run一遍就好了。然后打war包。

3.上传

上传
scp ~/Desktop/demo.war root@x.x.x.x:/usr/java/tomcat/apache-tomcat-8.5.15/webapps/

4. 访问

在浏览器中输入http:x.x.x.x/demo/就可以访问了。

可能遇到的问题

如果不能访问,可能是阿里云服务器默认80端口没开启,去配置一下就行。
如果返回404,去tomcat/logs 目录下,查看一下log,查找原因。

相关文章

网友评论

      本文标题:spring boot 项目部署到阿里云ECS服务器小结

      本文链接:https://www.haomeiwen.com/subject/clvefxtx.html