基于gradle和nginx实现快速部署
第一步:build.gradle文件配置
apply plugin: 'org.springframework.boot'
apply plugin: 'java'
apply plugin: 'idea'
group = 'cn.com.jetflow'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
archivesBaseName = 'AutomaticDeployment'
springBoot {
mainClass = 'cn.com.tingqiu.AutomaticDeploymentApplication'
}
bootRun {
addResources = true
}
jar {
manifest {
attributes("Manifest-Version": "${version}")
attributes("Created-By": "${group}")
attributes("Implementation-Title": "${archivesBaseName}")
attributes("Implementation-Version": "${version}")
}
}
task createJavaProject << {
sourceSets*.java.srcDirs*.each { it.mkdirs() }
sourceSets*.resources.srcDirs*.each { it.mkdirs() }
}
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
configurations {
custom {
it.extendsFrom implementation
}
}
bootRepackage {
customConfiguration = 'custom'
}
buildscript {
ext {
springBootVersion = '1.5.6.RELEASE'
}
repositories {
mavenLocal()
jcenter()
flatDir {
dirs "$rootProject.projectDir/libs"
}
maven {
url 'https://plugins.gradle.org/m2/'
}
maven {
url 'https://maven.aliyun.com/repository/central/'
}
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.6.RELEASE")
classpath('org.hidetake:gradle-ssh-plugin:2.6.0')
}
}
dependencies {
implementation fileTree(dir: 'lib', include: ['*.jar'])
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation group: 'org.springframework.boot', name: 'spring-boot-gradle-plugin', version: '1.5.6.RELEASE'
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.6.RELEASE'
implementation group: 'com.baomidou', name: 'mybatis-plus', version: '3.5.0'
implementation group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: '3.5.0'
implementation group: 'com.baomidou', name: 'mybatis-plus-generator', version: '3.5.0'
implementation group: 'com.alibaba', name: 'fastjson', version: '1.2.36'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.6'
implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.22'
implementation group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.2'
implementation group: 'com.alibaba', name: 'druid', version: '1.0.28'
implementation group: 'commons-io', name: 'commons-io', version: '2.5'
implementation group: 'org.apache.velocity', name: 'velocity-engine-core', version: '2.3'
implementation group: 'org.bouncycastle', name: 'bcpkix-jdk15on', version: '1.70'
implementation group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.70'
implementation group: 'org.projectlombok', name: 'lombok', version: '1.18.24'
implementation group: 'org.hidetake', name: 'gradle-ssh-plugin', version: '2.6.0'
implementation group: 'com.jcraft', name: 'jsch', version: '0.1.55'
implementation group: 'org.springframework.boot', name: 'spring-boot-configuration-processor', version: '1.5.6.RELEASE'
compileOnly group: 'org.springframework.boot', name: 'spring-boot-dependencies', version: '1.5.6.RELEASE'
}
repositories {
mavenLocal()
jcenter()
flatDir {
dirs "$rootProject.projectDir/libs"
}
maven {
url 'https://plugins.gradle.org/m2/'
}
maven {
url 'https://maven.aliyun.com/repository/central/'
}
mavenCentral()
}
apply plugin: 'org.hidetake.ssh'
remotes {
deployServer {
host = '10.0.0.4'
user = 'root'
password = 'root'
}
}
def remoteDir = '/data/project/nangang_03'
def sh = 'AutomaticDeployment.sh'
task deploy(dependsOn: [clean, bootRepackage]) {
bootRepackage.mustRunAfter clean
doLast {
ssh.settings {
knownHosts = allowAnyHosts
}
ssh.run {
session(remotes.deployServer) {
println '>>> start copying jar...'
put from: buildDir.toString() + '/libs/' + archivesBaseName + '-' + version + '.jar', into: remoteDir + '/web'
println '>>> start app...'
executeScript '''#!/bin/sh
cd ''' + remoteDir + '''
./''' + sh + '''
'''
println '>>> remote deploy OK...'
}
}
}
}
第二步:nginx.conf文件配置
server {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
# nginx监听端口
listen 8699;
server_name localhost;
location ~* \.(eot|ttf|woff|svg|otf)$ {
add_header Access-Control-Allow-Origin *;
}
location / {
# 前端代码存储路径
root /root/dev/projectName/dist;
index index.html index.htm;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;
# 解决跨域问题
add_header Access-Control-Allow-Origin *;
}
location ^~ /api/ {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
# nginx代理的服务地址(端口在启动脚本中配置)
proxy_pass http://127.0.0.1:8555/api/;
index /;
client_max_body_size 100m;
}
}
第三步:启动脚本配置
#!/bin/sh
PROJECT_HOME=/data/project
NGINX_HOME=/usr/local/nginx/sbin
# 重启nginx服务
echo "tingqiu" | sudo -S pkill -9 nginx
echo "tingqiu" | sudo -S ${NGINX_HOME}/nginx -c ${PROJECT_HOME}/web/nginx.conf
#sleep 3
ID=`ps -ef | grep demo-rest-0.0.1-SNAPSHOT | grep -v grep| awk '{print $2}'`
for id in ${ID}
do
kill -9 $id
echo "kill demo-rest $id"
done
# 启动项目
nohup java -Dconfig.serverPorts=8555
-Dwolf.lockuser-open=false
-Dserver.session.cookie.name=wolfSID
-Dspring.profiles.active=dev
-Dwolf.speaker-open=false
# 启动端口(即nginx配置中proxy_pass代理的服务端口)
-Dserver.port=8555
-jar ${PROJECT_HOME}/web/demo-rest-0.0.1-SNAPSHOT.jar >/dev/null 2>&1 &
第三步:IDEA上快速部署服务
点击"deploy"开始部署项目到服务器上
遇到的问题
描述:
打包得到的 jar 包中缺少依赖包
原因:
引入依赖包由原来的 compile 变成了 implement,springboot 2.X 之前的版本打包得到的 jar 包不会将依赖包一起打包。
解决方案:
- 升级 springboot 为2.x 之后的版本。
- build.gradle 中添加如下配置:
configurations {
custom {
it.extendsFrom implementation
}
}
bootRepackage {
customConfiguration = 'custom'
}