• 【java】Java项目从开发到部署生产完整流程梳理


    前言

    从事Java开发许久,从最初学习的JDK环境变量开始,到如今开发部署发布,已经逐渐形成了自己的一套体系,当然,其中也不少学习了网上各种资料总结,接下来将在本文对Java项目开发到部署发布整个流程进行归纳梳理。

    一、开发环境

    关于开发环境,在之前写的一篇文章里有详细教学,因为是比较基础的部分,就不在这里过多赘述,如果各位看官需要翻阅可以点击下面链接跳转
    Java开发环境搭建教程

    二、项目搭建

    环境配置好了,自然就是搭建项目框架,这里使用的是idea+maven方式,其他IDE大同小异

    2.1 Maven创建项目

    2.1.1 创建maven项目

    首先file->new->project,注意图中红框的JDK版本

    2.1.2 引入依赖

    打开项目的pom.xml文件,在中配置每一个dependency,而springboot项目,需要添加父依赖

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>***version>
    parent>
    
    <dependencies>
        <dependency>
            <groupId>***groupId>
            <artifactId>***artifactId>
            <version>**version>
        dependency>
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2.1.3 maven常用命令

    一般来说,maven常用命令用clean和install(package),idea为maven项目提供了按钮操作,无需在terminal中使用mvn命令

    在这里插入图片描述

    三、SpringBoot基础配置

    server:
      port: 6013 # tomcat端口
      servlet:
        context-path: /v1 # api context path
    
    spring:
      application:
        name: TEST # app name
      datasource:
        url: jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&characterEncoding=UTF8
        username: root
        password: admin
        driver-class-name: com.mysql.cj.jdbc.Driver
      jpa:
        hibernate: 
          ddl-auto: update
        show-sql: true
    
    logging:
      config: classpath:log/logging.xml # 日志配置路径
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    四、项目打包

    因为是maven构建的springboot项目,故这里仅讲述maven打包的方式

    4.1 打包jar

    在pom.xml中配置build即可

    <build>
    	<finalName>{自行指定打包名称,不配置则默认项目名称}finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <fork>truefork>                   
                    <mainClass>{springboot项目入口,即main方法所在类class}mainClass>
                configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackagegoal>
                        goals>
                    execution>
                executions>
            plugin>
        plugins>
    build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    4.2 打包war

    相对于jar包,打包war就需要排除springboot中内嵌的tomcat

    4.2.1 修改项目打包为war包

    在这里插入图片描述

    4.2.2 排除内嵌的tomcat,引入外部tomcat

    <dependency>  
            <groupId>org.springframework.bootgroupId>  
            <artifactId>spring-boot-starter-webartifactId>  
            <exclusions>  
                <exclusion>  
                    <groupId>org.springframework.bootgroupId>  
                    <artifactId>spring-boot-starter-tomcatartifactId>  
                exclusion>  
            exclusions>  
    dependency>  
    <dependency>  
            <groupId>org.springframework.bootgroupId>  
            <artifactId>spring-boot-starter-tomcatartifactId>  
            <scope>providedscope>  
    dependency> 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    4.2.3 添加servlet-api依赖

    <dependency>  
          <groupId>org.apache.tomcatgroupId>  
          <artifactId>tomcat-servlet-apiartifactId>  
          <version>7.0.42version>  
          <scope>providedscope>  
    dependency>  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    4.2.4 修改app入口方法,继承SpringBootServletInitializer,记得重写configure方法

      @SpringBootApplication  
    @EnableCaching  
    public class TestApplication extends SpringBootServletInitializer {  
      
        @Override  
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {  
            return application.sources(TestApplication.class);  
        }  
      
        public static void main(String[] args) {  
            SpringApplication.run(TestApplication.class, args);  
        }  
    }  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    4.2.5 和打包jar包一样添加war包插件,其中warName就是项目启动后访问项目的contextPath,当然这时候可以去tomcat里配置无需contextPath。默认访问该项目

    <plugin>  
          <groupId>org.apache.maven.pluginsgroupId>  
          <artifactId>maven-war-pluginartifactId>  
          <configuration>  
              <warSourceExcludes>src/main/resources/**warSourceExcludes>  
              <warName>testwarName>  
          configuration>  
    plugin>  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    五、服务部署

    5.1 winserver

    在这里插入图片描述

    winserver的部署和我们在电脑开发类似,打包cmd,运行java -jar jar包完整路径即可

    当然,可以编写bat脚本一键启动等等,这些骚操作就不在这里过多讲述,可自行发挥bat编写

    5.2 centos

    和winserver类似,打包传上去,java -jar jar包路径
    不过由于ssh session的关系,如果直接运行上面命令,会在session前台运行,此时session要么关闭,要么看着jar包运行。如果需要后台运行,则需要用到nohup命令

    nohup java -jar **.jar &
    
    • 1

    控制台输出默认在nohup.out中

    同winserver的bat一样,jar包也通常不会再linux中直接运行,一般使用sh脚本或者写入service中,通过脚本或systemctl等命令运行

    六、Nginx相关配置

    6.1 端口转发

    一般来说,我们开发的端口不会直接使用80,会使用域名转发到我们的端口服务上,在nginx中,可以进行一下配置达到此效果

    server {
            listen       80;
            server_name  www.***.com;
            charset utf-8;
            location / {
                proxy_pass http://127.0.0.1:8080/**;
            }
            error_page  404 400 401 402 403             /404.html;
    		location = /404.html {
    			root html;
    		}
            error_page   500 502 503 504  /500.html;
            location = /500.html {
                root   html;
            }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    6.2 HTTPS

    https首先需要为自己的域名购买或生成证书,具体方法可自行百度或在各大云服务商购买
    有了证书后,将证书中后缀为crt和key的两个文件放入和ngxin的nginx.conf文件平级或者下级的目录中,然后在需要https访问的域名中配置如下

    server {
    		# https 默认443端口
            listen       443 ssl;
            server_name  www.***.com;
            charset utf-8;
            
            #证书文件名称
        	ssl_certificate 1_www.***.com_bundle.crt; 
     		#私钥文件名称
        	ssl_certificate_key 2_www.***.com.key; 
       		ssl_session_timeout 5m;
     		#请按照以下协议配置
        	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
     		#请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
       		ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; 
        	ssl_prefer_server_ciphers on;
            
            location / {
                proxy_pass http://127.0.0.1:8080/**;
            }
            error_page  404 400 401 402 403             /404.html;
    		location = /404.html {
    			root html;
    		}
            error_page   500 502 503 504  /500.html;
            location = /500.html {
                root   html;
            }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    6.3 静态Web

    静态web的配置方式与端口转发类似,只是将proxy_pass换成了root路径

    server {
            listen       80;
            server_name  www.***.com;
            charset utf-8;
            location / {
                root   /usr/local/web/***;
                index  index.html;
            }
            error_page  404 400 401 402 403             /404.html;
    		location = /404.html {
    			root html;
    		}
            error_page   500 502 503 504  /500.html;
            location = /500.html {
                root   html;
            }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    6.4 静态资源访问

    静态资源和静态web配置一致,只需要将web路径换成资源路径即可,访问同样按照web访问资源文件

    server {
            listen       80;
            server_name  file.***.com;
            charset utf-8;
            location / {
                root   /usr/local/file/***;
                index  index.html;
            }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 相关阅读:
    (JavaEE)(多线程案例)线程池 (简单介绍了工厂模式)(含经典面试题ThreadPoolExector构造方法)
    Docker本地镜像发布到阿里云或私有库
    【FPGA】Verilog:实现 RS 触发器 | Flip-Flop | 使用 NOR 的 RS 触发器 | 使用 NAND 的 RS 触发器
    机器人示教编程与离线编程的优缺点对比
    理解http中cookie!C/C++实现网络的HTTP cookie
    金仓数据库KingbaseES本地化支持(6. 附录)
    Vue路由模式:hash和history
    【docker学习记录】docker安装mysql、redis
    如何搭建mysql的主从关系
    详解openGauss客户端工具gsql的高级用法
  • 原文地址:https://blog.csdn.net/u011397981/article/details/133917090