IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> Ubuntu 下部署 SpringBoot -> 正文阅读

[Java知识库]Ubuntu 下部署 SpringBoot

Ubuntu 下部署 SpringBoot

上一篇:Ubuntu 下搭建 MySql 环境

第一步: 下载 Idea

  • 我这次是一切从 0 开始的,所以 Idea 也是电脑上也是没有,安装过程就不水文了,不会的去网上搜一下,教程一大把。
  • PoJie 教程也可以去网上搜,我 Idea 下载的是目前官方最新版(2021.1.3 Ultimate x64),PoJie 教程亲测今日可用(2021.7.12)
  • 多说一句,2021 版的 Idea 第一次用,默认的关键字颜色变了,挺好看的~

第二步: 新建 SpringBoot 项目

  • 这一步也不水文了,网上教程太多了。

  • 这里把 pom.xml 文件、包结构、新增的控制器代码贴一下

    • 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <parent>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-parent</artifactId>
              <version>2.5.2</version>
              <relativePath/> <!-- lookup parent from repository -->
          </parent>
          <groupId>com.example</groupId>
          <artifactId>demo</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <name>demo</name>
          <description>Demo project for Spring Boot</description>
          <properties>
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
              <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
              <java.version>1.8</java.version>
          </properties>
          <dependencies>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-test</artifactId>
                  <scope>test</scope>
              </dependency>
          </dependencies>
      
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                  </plugin>
              </plugins>
          </build>
      </project>
      

      在这里插入图片描述

第三步: 打包 .jar 文件

在这里插入图片描述

第四步: 在 Ubuntu 上部署 .jar 包

  • mkdir demo 命令创建一个名为 demo 的命令
  • cd demo 命令进入该目录
  • nohup java -jar demo-0.0.1-SNAPSHOT.jar 命令启动 jar 包。
    • nohup xxx & : 是 no hang up 的缩写,意为不挂起,用于在系统后台不断运行命令,退出终端不会影响程序的运行

    • 运行完之后会生成一个 nohup.out 文件,里面是启动过程的一些日志

    • 打开 nohup.out 文件,如果正常启动的话,会看到类似这样的输出

      Started DemoApplication in xxx seconds
      
    • 但是,我就没那么幸运了,我遇到的是这样的

          .   ____          _            __ _ _
          /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
          ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
          \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
          '  |____| .__|_| |_|_| |_\__, | / / / /
          =========|_|==============|___/=/_/_/_/
          :: Spring Boot ::                (v2.5.2)
      
          2021-07-14 22:42:22.087  INFO 39193 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication v0.0.1-SNAPSHOT using Java 1.8.0_291 on iZwz92d94t8mb03s9z327hZ with PID 39193 (/opt/xiaodudu/demo/demo-0.0.1-SNAPSHOT.jar started by root in /opt/xiaodudu/demo)
          2021-07-14 22:42:22.090  INFO 39193 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
          2021-07-14 22:42:23.276  INFO 39193 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
          2021-07-14 22:42:23.291  INFO 39193 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
          2021-07-14 22:42:23.291  INFO 39193 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.48]
          2021-07-14 22:42:23.345  INFO 39193 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
          2021-07-14 22:42:23.345  INFO 39193 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1141 ms
          2021-07-14 22:42:23.673  WARN 39193 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'; nested exception is org.springframework.boot.web.server.PortInUseException: Port 8080 is already in use
          2021-07-14 22:42:23.677  INFO 39193 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
          2021-07-14 22:42:23.691  INFO 39193 --- [           main] ConditionEvaluationReportLoggingListener : 
      
          Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
          2021-07-14 22:42:23.713 ERROR 39193 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 
      
          ***************************
          APPLICATION FAILED TO START
          ***************************
      
          Description:
      
          Web server failed to start. Port 8080 was already in use.
      
          Action:
      
          Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.        
      
      • Port 8080 was already in use. 端口被占用,是比较常见的一个异常。

      • 执行 netstat -anp | grep 8080 查看占用端口的程序 pid。(netstat 有很多参数,可以使用 netstat -h 查询)

        root@iZwz92d94t8mb03s9z327hZ:~# netstat -anp | grep 8080
        tcp6       0      0 :::8080                 :::*                    LISTEN      19032/java
        
      • 执行 kill -9 xxxxx 命令终止该程序

        root@iZwz92d94t8mb03s9z327hZ:~# kill -9 19032
        
      • 重新执行 nohup xxx & 命令部署 jar 程序。(nohup.out 是否删除可以自己决定,删除就是重新生成,不删除就是追加内容)

        root@iZwz92d94t8mb03s9z327hZ:/opt/xiaodudu/demo# nohup java -jar demo-0.0.1-SNAPSHOT.jar &
        [1] 39342
        root@iZwz92d94t8mb03s9z327hZ:/opt/xiaodudu/demo# nohup: ignoring input and appending output to 'nohup.out'
        
      • 查看 nohup.out 文件

             .   ____          _            __ _ _
            /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
            ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
            \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
            '  |____| .__|_| |_|_| |_\__, | / / / /
            =========|_|==============|___/=/_/_/_/
            :: Spring Boot ::                (v2.5.2)
        
            2021-07-14 23:05:16.089  INFO 39342 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication v0.0.1-SNAPSHOT using Java 1.8.0_291 on iZwz92d94t8mb03s9z327hZ with PID 39342 (/opt/xiaodudu/demo/demo-0.0.1-SNAPSHOT.jar started by root in /opt/xiaodudu/demo)
            2021-07-14 23:05:16.093  INFO 39342 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
            2021-07-14 23:05:17.228  INFO 39342 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
            2021-07-14 23:05:17.239  INFO 39342 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
            2021-07-14 23:05:17.240  INFO 39342 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.48]
            2021-07-14 23:05:17.300  INFO 39342 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
            2021-07-14 23:05:17.300  INFO 39342 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1095 ms
            2021-07-14 23:05:17.755  INFO 39342 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
            2021-07-14 23:05:17.764  INFO 39342 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 2.338 seconds (JVM running for 2.769)
        

远程访问接口

  • 访问 Ubuntu 服务器IP: 8080/hello/springboot

在这里插入图片描述

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-07-15 23:43:10  更:2021-07-15 23:44:13 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/5 5:59:26-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码