å¨Ubuntuä¸å®è£ Java8
以ä¸æ¯ä¸ä¸ªç®åçå®è£ æ¹æ³ã
$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer
$ java -version
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)
2. æ¬å°ä½¿ç¨Gradleåå¸Spring Bootåºç¨
æè¿é使ç¨Jetty9ä½ä¸ºå ç½®çæå¡å¨ã
// ...
bootRun {
systemProperties = System.properties
}
configurations {
compile.exclude module: "spring-boot-starter-tomcat"
}
dependencies {
// spring boot
compile "org.springframework.boot:spring-boot-starter-web:1.3.0.M5"
compile "org.springframework.boot:spring-boot-starter-jetty"
// ...
}
//...
å¨æ¬å°è¿è¡é»è®¤ä½¿ç¨src/main/resources/application.ymlä½ä¸ºé ç½®æ件ï¼èå¨productionç¯å¢ä¸æ们系ç»å®æ¯ææä¾å¤é¨çé ç½®æ件application-production.ymlã
./gradlew bootRun # å¼åç¯å¢ä¸é»è®¤ä½¿ç¨é¡¹ç®éçapplication.yml
# å¨æ¬å°æµè¯ä½¿ç¨å¤é¨é ç½®æ件
./gradlew bootRun -Dspring.config.location=/path/to/application-production.yml
# åå¸
./gradlew build
# è¿è¡
java -jar build/libs/SpringBlog-0.1.jar # é»è®¤ä½¿ç¨jarå éé¢çapplication.ymlé ç½®æ件
# 使ç¨å¤é¨é ç½®æ件
java -jar build/libs/SpringBlog-0.1.jar --spring.config.location=/path/to/application-production.yml
3. å¨Ubuntuæå¡å¨ä¸é¨ç½²Spring Bootåºç¨
# ä¸ä¼ SpringBlog-0.1.jarå°æå¡å¨
scp build/libs/SpringBlog-0.1.jar root@your_server_ip:/root/spring-blog/current
# å¨æå¡å¨ä¸é ç½®ç产ç¯å¢çé ç½®æ件
scp application-production.yml root@your_server_ip:/root/spring-blog/current
ç¶åSSHç»å½æå¡å¨ï¼ä¿®æ¹é ç½®æ件application-production.ymlï¼è¯è¿è¡åºç¨ã
ssh root@your_server_ip
cd spring-blog/current
java -jar SpringBlog-0.1.jar --spring.config.location=application-production.yml
# application-production.yml
server:
address: raysmond.com # 使ç¨ååæè IPï¼å¯å¨ä¹åå°±å¯ä»¥è¿ä¸ªååæIP访é®ç½ç«äº
port: 80
contextPath:
spring:
profiles:
active: production
thymeleaf:
cache: true
jade4j:
caching: true
dataSource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1/spring_blog
username: root
password:
hibernate:
dialect: org.hibernate.dialect.MySQLDialect
hbm2ddl.auto: update
show_sql: false
redis:
host: localhost
port: 6379
4. å¦ä½å¨Ubuntuä¸åå°è¿è¡Spring Bootåºç¨ï¼
æ¨è使ç¨nohupè¿ä¸ªå½ä»¤ã
cd /root/spring-blog/current
nohup java -jar SpringBlog-0.1.jar --spring.config.location=application-production.yml \
> ../logs/production.log 2> ../logs/production.err &
å¨Ubuntuè¿å¯ä»¥/etc/init.dç®å½ä¸æ°å»ºä¸ä¸ªèæ¬ï¼æSpringBlogä½ä¸ºserviceæ¥è¿è¡ï¼è¿æ ·ä¸ç¨æ¯æ¬¡é½æè¿ä¹ç¹ççå½ä»¤äºãæ°å»ºä¸ä¸ª/etc/init.d/spring_blogæ件ï¼å 容å¦ä¸ï¼
#!/bin/sh
SERVICE_NAME=spring_blog
HOME=/root/spring-blog
PATH_TO_JAR=$HOME/current/SpringBlog-0.1.jar
PID_PATH_NAME=/tmp/spring_blog.pid
LOG=$HOME/logs/production.log
ERROR_LOG=$HOME/logs/production.err
CONFIG=$HOME/application-production.yml
case $1 in
start)
echo "Starting $SERVICE_NAME ..."
if [ ! -f $PID_PATH_NAME ]; then
cd $HOME/current
nohup java -jar $PATH_TO_JAR --spring.config.location=application-production.yml > $LOG 2> $ERROR_LOG &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is already running ..."
fi
;;
stop)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stoping ..."
kill $PID;
echo "$SERVICE_NAME stopped ..."
rm $PID_PATH_NAME
else
echo "$SERVICE_NAME is not running ..."
fi
;;
restart)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stopping ...";
kill $PID;
echo "$SERVICE_NAME stopped ...";
rm $PID_PATH_NAME
echo "$SERVICE_NAME starting ..."
cd $HOME/current
nohup java -jar $PATH_TO_JAR --spring.config.location=application-production.yml > $LOG 2> $ERROR_LOG &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is not running ..."
fi
;;
esac
ç°å¨å°±å¯ä»¥ä½¿ç¨serviceçæ¹å¼æ¥è¿è¡ç½ç«äºã
sudo service spring_blog start
sudo service spring_blog stop
sudo service spring_blog restart
5. å¨æ¬å°èªå¨åé¨ç½²ç½ç«å°è¿ç¨æå¡å¨
å¨æ¬å°æç¨äºä¸ä¸ªshellèæ¬åä¸ä¸ªpythonèæ¬æ¥é åèªå¨åé¨ç½²ã
deploy.sh 使ç¨gradleçå½ä»¤åå¸jarå ï¼ä½¿ç¨scpå½ä»¤å§jarå ä¸ä¼ å°æå¡å¨ä¸ï¼
deploy.py 使ç¨SSHè¿ç¨ç»å½æå¡å¨ï¼å¹¶å¨æå¡å¨ä¸æ§è¡é¨ç½²å½ä»¤ã
# deploy.sh
#!/bin/bash
SERVER="your_server_ip"
JAR="build/libs/SpringBlog-0.1.jar"
echo "Building $JAR..."
./gradlew build
echo "Upload $JAR to server $SERVER..."
scp $JAR root@$SERVER:/root/spring-blog/
python deploy.py
deploy.py主è¦ä½¿ç¨äºä¸ä¸ªparamikoåºï¼ç¨äºSSHè¿ç¨ç»å½æå¡å¨ï¼å¹¶æ§è¡å½ä»¤ãè¿ä¸ªèæ¬ä¼ææå¡å¨ä¸/root/spring-blog/current/SpringBlog-0.1.jarå¤ä»½å°/root/spring-blog/releasesä¸ï¼å¹¶ææ°åå¸çjarå æ¾å°/root/spring-blog/currentä¸ï¼ç¶åéå¯spring_blogæå¡ã
å ¨éå¤å¶æ¾è¿ç¬è®°#!/usr/bin/python
import paramiko
import threading
import time
ip = 'your_server_ip'
user = 'root'
password = ''
jar = 'SpringBlog-0.1.jar'
home='/root/spring-blog'
current=home+"/current"
releases=home+"/releases"
def execute_cmds(ip, user, passwd, cmd):
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,22,user,passwd,timeout=5)
for m in cmd:
print m
stdin, stdout, stderr = ssh.exec_command(m)
# stdin.write("Y")
out = stdout.readlines()
for o in out:
print o,
print '%s\tOK\n'%(ip)
ssh.close()
except :
print '%s\tError\n'%(ip)
if __name__=='__main__':
print 'Start deploying %s to server %s'%(jar, ip)
now = time.strftime("%Y%m%d%H%M%S")
cmd = [
'echo Stop spring_blog service... && service spring_blog stop',
'echo Flush all redis cache data... && redis-cli -r 1 flushall',
'echo Stop redis server... && service redis_6379 stop',
'echo Use new jar... ' + \
' && mv ' + current + '/' + jar + ' ' + releases + '/' + now + '_' + jar ,
'mv ' + home + '/' + jar + ' ' + current + '/' + jar,
'echo Stop redis... && service redis_6379 start',
'echo Start spring_blog service... && service spring_blog start ' + \
' && echo All done.'
]
a=threading.Thread(target=execute_cmds, args=(ip,user,password,cmd))
a.start()
é ç½®å®ä»¥åï¼å¨æ¬å°åå®ä»£ç å°±å¯ä»¥è¿è¡./deploy.shä¸é®é¨ç½²å°è¿ç¨æå¡å¨äºã