weblogic-nginx
weblogic-nginx
weblogic-nginx项目的具体配置。
总体流程:构建镜像>启动镜像>通过nginx && ./startup.sh来启动。此时,nginx监听7001端口,代理到weblogic的监听端口7999,外部访问 ip:7001/pro/即访问 weblogic。
启动流程:startup.sh > checkAndInstallApp.sh > wlst.sh autodeploy.py > startWebLogic.sh
文件配置
Dockerfile
Dockerfile如下:
FROM 10.131.9.12:5000/base/weblogic-redis-nginx-autodeploy-apm:2.0.0
COPY nginx.conf /etc/nginx
ENV LANG zh_CN.utf8
WORKDIR /u01/oracle/autodeploy
ADD 项目目录 ./项目目录
ADD ./resouceDefine.py .
CMD nginx && ./startup.sh
ENV指定环境变量。
如:ENV JENKINS_HOME /opt/jenkins/data
RUN mkdir -p $JENKINS_HOME/plugins
WORKDIR指定工作目录。此设置会影响到?复制文件?运行文件?
CMD启动nginx,并运行startup.sh脚本(可能在镜像中已经存在),
思考:也可以不用数组?两种形式实际有如何区别?
Jenkinsfile
正常的构建docker镜像脚本。(云效平台上用不到。)
node () {
def version = "1.0.1"
def environment = "dev"
if (environment == "dev") {
stage('开始构建测试镜像') {
checkout scm
docker.withRegistry('http://10.131.9.12:5000', 'f217bb64-8ebf-409a-9228-8e691a64ec69') {
def customImage = docker.build("10.131.9.12:5000/a-pro/pro:${version}.${env.BUILD_ID}")
customImage.push()
sh "docker rmi 10.131.9.12:5000/a-pro/pro:${version}.${env.BUILD_ID}"
}
}
} else if (environment == "prod") {
stage('开始构建发布镜像') {
checkout scm
docker.withRegistry('http://10.131.9.15:5000', 'f217bb64-8ebf-409a-9228-8e691a64ec69') {
def customImage = docker.build("10.131.9.15:5000/a-pro/pro:${version}.${env.BUILD_ID}")
customImage.push()
sh "docker rmi 10.131.9.15:5000/a-pro/pro:${version}.${env.BUILD_ID}"
}
}
}
}
nginx.conf
构建镜像的Dockerfile文件,会在构建的时候,copy此nginx.conf配置,作为容器内的nginx配置。
端口:7001(nginx) -> 7999 (weblogic) 端口。
路由匹配 /pro/ 时,实际上访问的是会影射到 7999端口。
user root root;
worker_processes 2;
events {
worker_connections 8192;
}
http {
# 2 config
include mime.types;
default_type application/octet-stream;
client_max_body_size 128m;
sendfile on;
keepalive_timeout 65;
server_tokens off;
proxy_connect_timeout 120s;
proxy_send_timeout 600;
proxy_read_timeout 600;
# 3 zip
gzip on;
gzip_min_length 4096;
gzip_buffers 4 8k;
gzip_types text/* text/css application/javascript application/x-javascript;
gzip_comp_level 1;
gzip_vary on;
gzip_http_version 1.1;
# 4 server
server {
listen 7001;
server_name localhost;
root /var/www/html;
#access_log off;
#error_log off;
index index.html index.jsp;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# 5 filter
if ($request_uri ~* "(cost\()|(concat\()|sleep\(") {
return 404;
}
if ($request_uri ~* "[+|(%20)]union[+|(%20)]") {
return 404;
}
if ($request_uri ~* "[+|(%20)]and[+|(%20)]") {
return 404;
}
if ($request_uri ~* "[+|(%20)]select[+|(%20)]") {
return 404;
}
if ( $query_string ~* ".*[\;'\<\>].*" ){
return 404;
}
# 6 match
location ~ /pro/ {
proxy_set_header Host $host:port;
#proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:7999;
}
location ~ /3WnA5Gc14RduRC8i9hUA/ {
deny all;
}
location ~ /_async/ {
deny all;
}
location ~ /WEB-INF/ {
deny all;
}
}
}
resouceDefine.py
Python语法,行末不需要带分号;。
作用: 主要作用是通过脚本读取这个文件 把数据源写到 weblogic 。
import os;
applist = [{"APP_NAME":"pro","WAR_NAME":"pro"}];
jdbclist = [{"JNDI_NAME":"jndi_cod",
"DB_URL":os.getenv("DB_DSN")+"?useUnicode=true&autoReconnect=true&failOverReadOnly=false&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&noAccessToProcedureBodies=true",
"DRIVER_NAME":"com.mysql.jdbc.Driver",
"DB_USER":os.getenv("DB_USER"),
"DB_PASS":os.getenv("DB_PWD"),
"POOL_INIT":5,
"POOL_MAX":100,
"POOL_MIN":5
}];
启动
上面是构建镜像需要用到的4个示例文件。根据Dockerfile中CMD nginx && ./startup.sh,容器启动后,首先启动nginx,再启动startup.sh脚本。而在容器内部,脚本内容如下:
startup.sh
#/bin/sh
nohup ./checkAndInstallApp.sh >> checkAndInstallApp.log 2>&1 &
seqrand(){
length=16
i=1
seq=(0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)
num_seq=${#seq[@]}
while [ "$i" -le "$length" ]
do
seqrand[$i]=${seq[$((RANDOM%num_seq))]}
let "i=i+1"
done
for j in ${seqrand[@]}
do
echo -n $j
done
}
pwd=$(seqrand)
echo "$pwd" > dwp.txt
export WEBLOGIC_PWD=$pwd
# rm ldap
rm -rf /u01/oracle/weblogic/user_projects/domains/base_domain/servers/AdminServer/data/ldap
# generate ldap
java -cp /u01/oracle/weblogic/wlserver/server/lib/weblogic.jar weblogic.security.utils.AdminAccount weblogic $WEBLOGIC_PWD .
# replace ldap
cp DefaultAuthenticatorInit.ldift /u01/oracle/weblogic/user_projects/domains/base_domain/security/
# edit boot
cat > /u01/oracle/weblogic/user_projects/domains/base_domain/servers/AdminServer/security/boot.properties << EOF
#Thu Aug 15 10:13:17 CST 2019
password=${WEBLOGIC_PWD}
username=weblogic
EOF
/u01/oracle/weblogic/user_projects/domains/base_domain/bin/startWebLogic.sh
脚本执行了 checkAndInstallApp.sh
checkAndInstallApp.sh
#/bin/sh
isWeblogicStarted=`ps -elf |grep weblogic.Server |grep -v grep|wc -l`
while [ $isWeblogicStarted -ne "1" ];do
echo ">>>wait for weblogic starting...."
isWeblogicStarted=`ps -elf |grep weblogic.Server |grep -v grep|wc -l`
sleep 1
done
curl -s --connect-timeout 1 localhost:7999
lastExitCode=$?
while [ $lastExitCode -ne "0" ];do
echo ">>>wait for connecting to localhost:7999"
curl -s --connect-timeout 1 localhost:7999
lastExitCode=$?
sleep 1
done
pwd=`head -1 dwp.txt`
export WEBLOGIC_PWD=$pwd
/u01/oracle/weblogic/oracle_common/common/bin/wlst.sh autodeploy.py
exit
路径/u01/oracle/weblogic/oracle_common/common/bin/下有很多脚本,其中wlst.sh内容如下:
wlst.sh
#!/bin/sh
mypwd="`pwd`"
case `uname -s` in
Windows_NT*)
CLASSPATHSEP=\;
;;
CYGWIN*)
CLASSPATHSEP=\;
;;
*)
CLASSPATHSEP=:
;;
esac
# Determine the location of this script...
# Note: this will not work if the script is sourced (. ./wlst.sh)
SCRIPTNAME=$0
case ${SCRIPTNAME} in
/*) SCRIPTPATH=`dirname "${SCRIPTNAME}"` ;;
*) SCRIPTPATH=`dirname "${mypwd}/${SCRIPTNAME}"` ;;
esac
# Set CURRENT_HOME...
CURRENT_HOME=`cd "${SCRIPTPATH}/../.." ; pwd`
export CURRENT_HOME
# Set the MW_HOME relative to the CURRENT_HOME...
MW_HOME=`cd "${CURRENT_HOME}/.." ; pwd`
export MW_HOME
# Set the home directories...
. "${SCRIPTPATH}/setHomeDirs.sh"
# Set the DELEGATE_ORACLE_HOME to CURRENT_HOME if it's not set...
ORACLE_HOME="${DELEGATE_ORACLE_HOME:=${CURRENT_HOME}}"
export DELEGATE_ORACLE_HOME ORACLE_HOME
# Set the directory to get wlst commands from...
COMMON_WLST_HOME="${COMMON_COMPONENTS_HOME}/common/wlst"
WLST_HOME="${COMMON_WLST_HOME}${CLASSPATHSEP}${WLST_HOME}"
export WLST_HOME
# Some scripts in WLST_HOME reference ORACLE_HOME
WLST_PROPERTIES="${WLST_PROPERTIES} -DORACLE_HOME='${ORACLE_HOME}' -DCOMMON_COMPONENTS_HOME='${COMMON_COMPONENTS_HOME}'"
export WLST_PROPERTIES
# Set the WLST extended env...
if [ -f "${COMMON_COMPONENTS_HOME}"/common/bin/setWlstEnv.sh ] ; then
. "${COMMON_COMPONENTS_HOME}"/common/bin/setWlstEnv.sh
fi
# Appending additional jar files to the CLASSPATH...
if [ -d "${COMMON_WLST_HOME}/lib" ] ; then
for file in "${COMMON_WLST_HOME}"/lib/*.jar ; do
CLASSPATH="${CLASSPATH}${CLASSPATHSEP}${file}"
done
fi
# Appending additional resource bundles to the CLASSPATH...
if [ -d "${COMMON_WLST_HOME}/resources" ] ; then
for file in "${COMMON_WLST_HOME}"/resources/*.jar ; do
CLASSPATH="${CLASSPATH}${CLASSPATHSEP}${file}"
done
fi
export CLASSPATH
umask 027
# set up common environment
if [ ! -z "${WLS_NOT_BRIEF_ENV}" ]; then
if [ "${WLS_NOT_BRIEF_ENV}" = "true" -o "${WLS_NOT_BRIEF_ENV}" = "TRUE" ]; then
WLS_NOT_BRIEF_ENV=
export WLS_NOT_BRIEF_ENV
fi
else
WLS_NOT_BRIEF_ENV=false
export WLS_NOT_BRIEF_ENV
fi
if [ -f "${WL_HOME}/server/bin/setWLSEnv.sh" ] ; then
. "${WL_HOME}/server/bin/setWLSEnv.sh"
else
. "${MW_HOME}/oracle_common/common/bin/commEnv.sh"
fi
CLASSPATH="${CLASSPATH}${CLASSPATHSEP}${FMWLAUNCH_CLASSPATH}${CLASSPATHSEP}${DERBY_CLASSPATH}${CLASSPATHSEP}${DERBY_TOOLS}"
export CLASSPATH
if [ -f "${SCRIPTPATH}/cam_wlst.sh" ] ; then
. "${SCRIPTPATH}/cam_wlst.sh"
fi
if [ "${WLST_HOME}" != "" ] ; then
WLST_PROPERTIES="-Dweblogic.wlstHome='${WLST_HOME}' ${WLST_PROPERTIES}"
export WLST_PROPERTIES
fi
if [ "${WLS_NOT_BRIEF_ENV}" = "" ] ; then
echo
echo CLASSPATH=${CLASSPATH}
fi
JVM_ARGS="-Dprod.props.file='${WL_HOME}'/.product.properties ${WLST_PROPERTIES} ${JVM_D64} ${UTILS_MEM_ARGS} ${COMMON_JVM_ARGS} ${CONFIG_JVM_ARGS}"
if [ -d "${JAVA_HOME}" ]; then
eval '"${JAVA_HOME}/bin/java"' ${JVM_ARGS} weblogic.WLST '"$@"'
else
exit 1
fi
## startWebLogic.sh
startup.sh中的最后一步,执行/u01/oracle/weblogic/user_projects/domains/base_domain/bin/路径下的 startWebLogic.sh。
这个内容略过。