CentOS7-部署測試Apollo

linux部署apollo環境要求:jdk1.八、mysql5.7html

centos7安裝jdk1.8跟mysql5.7能夠參考我這兩篇文章java

https://www.cnblogs.com/reasonzzy/p/11149530.htmlmysql

https://www.cnblogs.com/reasonzzy/p/11150131.htmllinux

 

從github上將源碼down下來git

https://github.com/ctripcorp/apollo.gitgithub

 

這兩個是打包腳本,均可以執行,sh腳本能夠用git bash面板執行,spring

執行前能夠修改eureka註冊ip跟數據庫帳號密碼參數,若是是本地,sql

可不作修改,打包方式有兩種,一種是quick start方式,另外一種是打成三個壓縮包形式數據庫

quick start方式參考以下文檔:windows

https://github.com/ctripcorp/apollo/wiki/Quick-Start

源碼地址:

https://github.com/nobodyiam/apollo-build-scripts.git

主要步驟

修改apollo-configservice, apollo-adminservice和apollo-portal的pom.xml,註釋掉spring-boot-maven-plugin和maven-assembly-plugin

在根目錄下執行mvn clean package -pl apollo-assembly -am -DskipTests=true

複製apollo-assembly/target下的jar包,rename爲apollo-all-in-one.jar

 

第二種方式壓縮包github上面也有現成的,若是不須要調整源碼,直接從github上下載

https://github.com/ctripcorp/apollo/releases 

 

 數據庫腳本在source code.zip這個壓縮包裏面,源代碼scripts/db/migration文件夾裏面的sql文件也能夠

壓縮包下載好以後,只須要修改下配置文件便可

 

#!/bin/bash # apollo config db info apollo_config_db_url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8
apollo_config_db_username=root apollo_config_db_password= # apollo portal db info apollo_portal_db_url=jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8
apollo_portal_db_username=root apollo_portal_db_password= # =============== Please do not modify the following content =============== # if [ "$(uname)" == "Darwin" ]; then windows="0"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then windows="0"
elif [ "$(expr substr $(uname -s) 1 5)" == "MINGW" ]; then windows="1"
else windows="0"
fi # meta server url config_server_url=http://localhost:8080
admin_server_url=http://localhost:8090
eureka_service_url=$config_server_url/eureka/ portal_url=http://localhost:8070
 # JAVA OPTS BASE_JAVA_OPTS="-Denv=dev" CLIENT_JAVA_OPTS="$BASE_JAVA_OPTS -Dapollo.meta=$config_server_url" SERVER_JAVA_OPTS="$BASE_JAVA_OPTS -Dspring.profiles.active=github -Deureka.service.url=$eureka_service_url" PORTAL_JAVA_OPTS="$BASE_JAVA_OPTS -Ddev_meta=$config_server_url -Dspring.profiles.active=github,auth -Deureka.client.enabled=false -Dhibernate.query.plan_cache_max_size=192" # executable JAR_FILE=apollo-all-in-one.jar SERVICE_DIR=./service SERVICE_JAR_NAME=apollo-service.jar SERVICE_JAR=$SERVICE_DIR/$SERVICE_JAR_NAME SERVICE_LOG=$SERVICE_DIR/apollo-service.log PORTAL_DIR=./portal PORTAL_JAR_NAME=apollo-portal.jar PORTAL_JAR=$PORTAL_DIR/$PORTAL_JAR_NAME PORTAL_LOG=$PORTAL_DIR/apollo-portal.log CLIENT_DIR=./client CLIENT_JAR=$CLIENT_DIR/apollo-demo.jar # go to script directory cd "${0%/*}"

function checkJava { if [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]];  then
      if [ "$windows" == "1" ]; then tmp_java_home=`cygpath -sw "$JAVA_HOME"` export JAVA_HOME=`cygpath -u $tmp_java_home` echo "Windows new JAVA_HOME is: $JAVA_HOME"
      fi _java="$JAVA_HOME/bin/java"
  elif type -p java > /dev/null; then _java=java else
      echo "Could not find java executable, please check PATH and JAVA_HOME variables." exit 1
  fi

  if [[ "$_java" ]]; then version=$("$_java" -version 2>&1 | awk -F '"' '/version/ {print $2}') if [[ "$version" < "1.8" ]]; then
          echo "Java version is $version, please make sure java 1.8+ is in the path" exit 1
      fi
  fi } function checkServerAlive { declare -i counter=0 declare -i max_counter=24 # 24*5=120s declare -i total_time=0 SERVER_URL="$1"

  until [[ (( counter -ge max_counter )) || "$(curl -X GET --silent --connect-timeout 1 --max-time 2 --head $SERVER_URL | grep "HTTP")" != "" ]]; do printf "." counter+=1
    sleep 5
  done total_time=counter*5

  if [[ (( counter -ge max_counter )) ]]; then return $total_time fi return 0 } checkJava if [ "$1" = "start" ] ; then
  echo "==== starting service ===="
  echo "Service logging file is $SERVICE_LOG" export JAVA_OPTS="$SERVER_JAVA_OPTS -Dlogging.file=./apollo-service.log -Dspring.datasource.url=$apollo_config_db_url -Dspring.datasource.username=$apollo_config_db_username -Dspring.datasource.password=$apollo_config_db_password"

  if [[ -f $SERVICE_JAR ]]; then
    rm -rf $SERVICE_JAR fi

  ln $JAR_FILE $SERVICE_JAR chmod a+x $SERVICE_JAR $SERVICE_JAR start --configservice --adminservice rc=$?
  if [[ $rc != 0 ]]; then
    echo "Failed to start service, return code: $rc. Please check $SERVICE_LOG for more information." exit $rc; fi printf "Waiting for config service startup" checkServerAlive $config_server_url rc=$?
  if [[ $rc != 0 ]]; then printf "\nConfig service failed to start in $rc seconds! Please check $SERVICE_LOG for more information.\n" exit 1; fi printf "\nConfig service started. You may visit $config_server_url for service status now!\n" printf "Waiting for admin service startup" checkServerAlive $admin_server_url rc=$?
  if [[ $rc != 0 ]]; then printf "\nAdmin service failed to start in $rc seconds! Please check $SERVICE_LOG for more information.\n" exit 1; fi printf "\nAdmin service started\n"

  echo "==== starting portal ===="
  echo "Portal logging file is $PORTAL_LOG" export JAVA_OPTS="$PORTAL_JAVA_OPTS -Dlogging.file=./apollo-portal.log -Dserver.port=8070 -Dspring.datasource.url=$apollo_portal_db_url -Dspring.datasource.username=$apollo_portal_db_username -Dspring.datasource.password=$apollo_portal_db_password"

  if [[ -f $PORTAL_JAR ]]; then
    rm -rf $PORTAL_JAR fi

  ln $JAR_FILE $PORTAL_JAR chmod a+x $PORTAL_JAR $PORTAL_JAR start --portal rc=$?
  if [[ $rc != 0 ]]; then
    echo "Failed to start portal, return code: $rc. Please check $PORTAL_LOG for more information." exit $rc; fi printf "Waiting for portal startup" checkServerAlive $portal_url rc=$?
  if [[ $rc != 0 ]]; then printf "\nPortal failed to start in $rc seconds! Please check $PORTAL_LOG for more information.\n" exit 1; fi printf "\nPortal started. You can visit $portal_url now!\n" exit 0; elif [ "$1" = "client" ] ; then
  if [ "$windows" == "1" ]; then java -classpath "$CLIENT_DIR;$CLIENT_JAR" $CLIENT_JAVA_OPTS com.ctrip.framework.apollo.demo.api.SimpleApolloConfigDemo else java -classpath $CLIENT_DIR:$CLIENT_JAR $CLIENT_JAVA_OPTS com.ctrip.framework.apollo.demo.api.SimpleApolloConfigDemo fi

elif [ "$1" = "stop" ] ; then
  echo "==== stopping portal ====" cd $PORTAL_DIR ./$PORTAL_JAR_NAME stop cd .. echo "==== stopping service ====" cd $SERVICE_DIR ./$SERVICE_JAR_NAME stop else
  echo "Usage: demo.sh ( commands ... )"
  echo "commands:"
  echo " start start services and portal"
  echo " client start client demo program"
  echo " stop stop services and portal" exit 1
fi
View Code

quick start方式修改下demo.sh這個文件,將localhost改爲對應的ip,還有數據庫帳號密碼。

第二種方式,若是是部署在linux系統上,有且只有一個網卡的狀況,只須要修改下數據庫配置文件,

若是是用的virtualbox安裝的centos7系統,而且有兩個網卡的狀況,相似於雲主機,一樣適用我下面

這種方法,詳情請參考官方文檔分佈式部署指南:

https://github.com/ctripcorp/apollo/wiki/%E5%88%86%E5%B8%83%E5%BC%8F%E9%83%A8%E7%BD%B2%E6%8C%87%E5%8D%97

 1.4節網絡策略

在啓動腳本里面強制指定eureka註冊的ip

-Deureka.instance.ip-address=${指定的IP}

官方文檔寫是寫得很詳細,可是對於我這種小白來講,一臉懵逼,

我當時看到這裏,一臉黑人問號,這行代碼加在哪,後來羣裏各類

尋求幫助,終於有大神告訴我了,詳細操做看代碼,

修改apollo-adminservice或apollo-configservice的startup.sh

 

#!/bin/bash SERVICE_NAME=apollo-configservice ## Adjust log dir if necessary LOG_DIR=/opt/logs/100003171 ## Adjust server port if necessary #SERVER_PORT=8080 SERVER_PORT=${SERVER_PORT:=8080} ## Create log directory if not existed because JDK 8+ won't do that
mkdir -p $LOG_DIR ## Adjust memory settings if necessary #export JAVA_OPTS="-Xms6144m -Xmx6144m -Xss256k -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=384m -XX:NewSize=4096m -XX:MaxNewSize=4096m -XX:SurvivorRatio=8" ## Only uncomment the following when you are using server jvm #export JAVA_OPTS="$JAVA_OPTS -server -XX:-ReduceInitialCardMarks" ########### The following is the same for configservice, adminservice, portal ########### export JAVA_OPTS="$JAVA_OPTS -XX:ParallelGCThreads=4 -XX:MaxTenuringThreshold=9 -XX:+DisableExplicitGC -XX:+ScavengeBeforeFullGC -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+ExplicitGCInvokesConcurrent -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -Duser.timezone=Asia/Shanghai -Dclient.encoding.override=UTF-8 -Dfile.encoding=UTF-8 -Djava.security.egd=file:/dev/./urandom -Deureka.instance.ip-address=${指定的IP}" # DataSource URL USERNAME PASSWORD if [ "$DS_URL"x != x ] then export JAVA_OPTS="$JAVA_OPTS -Dspring.datasource.url=$DS_URL -Dspring.datasource.username=$DS_USERNAME -Dspring.datasource.password=$DS_PASSWORD"
fi export JAVA_OPTS="$JAVA_OPTS -Dserver.port=$SERVER_PORT -Dlogging.file=$LOG_DIR/$SERVICE_NAME.log -XX:HeapDumpPath=$LOG_DIR/HeapDumpOnOutOfMemoryError/" PATH_TO_JAR=$SERVICE_NAME".jar" SERVER_URL="http://localhost:$SERVER_PORT"

function checkPidAlive { for i in `ls -t $SERVICE_NAME*.pid 2>/dev/null` do read pid < $i result=$(ps -p "$pid") if [ "$?" -eq 0 ]; then return 0
        else printf "\npid - $pid just quit unexpectedly, please check logs under $LOG_DIR and /tmp for more information!\n" exit 1; fi
    done printf "\nNo pid file found, startup may failed. Please check logs under $LOG_DIR and /tmp for more information!\n" exit 1; } if [ "$(uname)" == "Darwin" ]; then windows="0"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then windows="0"
elif [ "$(expr substr $(uname -s) 1 5)" == "MINGW" ]; then windows="1"
else windows="0"
fi # for Windows if [ "$windows" == "1" ] && [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then tmp_java_home=`cygpath -sw "$JAVA_HOME"` export JAVA_HOME=`cygpath -u $tmp_java_home` echo "Windows new JAVA_HOME is: $JAVA_HOME"
fi # Find Java if [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then javaexe="$JAVA_HOME/bin/java"
elif type -p java > /dev/null 2>&1; then javaexe=$(type -p java) elif [[ -x "/usr/bin/java" ]];  then javaexe="/usr/bin/java"
else
    echo "Unable to find Java" exit 1
fi

if [[ "$javaexe" ]]; then version=$("$javaexe" -version 2>&1 | awk -F '"' '/version/ {print $2}') version=$(echo "$version" | awk -F. '{printf("%03d%03d",$1,$2);}') # now version is of format 009003 (9.3.x) if [ $version -ge 011000 ]; then JAVA_OPTS="$JAVA_OPTS -Xlog:gc*:$LOG_DIR/gc.log:time,level,tags -Xlog:safepoint -Xlog:gc+heap=trace"
    elif [ $version -ge 010000 ]; then JAVA_OPTS="$JAVA_OPTS -Xlog:gc*:$LOG_DIR/gc.log:time,level,tags -Xlog:safepoint -Xlog:gc+heap=trace"
    elif [ $version -ge 009000 ]; then JAVA_OPTS="$JAVA_OPTS -Xlog:gc*:$LOG_DIR/gc.log:time,level,tags -Xlog:safepoint -Xlog:gc+heap=trace"
    else JAVA_OPTS="$JAVA_OPTS -XX:+UseParNewGC" JAVA_OPTS="$JAVA_OPTS -Xloggc:$LOG_DIR/gc.log -XX:+PrintGCDetails" JAVA_OPTS="$JAVA_OPTS -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=60 -XX:+CMSClassUnloadingEnabled -XX:+CMSParallelRemarkEnabled -XX:CMSFullGCsBeforeCompaction=9 -XX:+CMSClassUnloadingEnabled -XX:+PrintGCDateStamps -XX:+PrintGCApplicationConcurrentTime -XX:+PrintHeapAtGC -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=5M"
    fi
fi cd `dirname $0`/.. for i in `ls $SERVICE_NAME-*.jar 2>/dev/null` do
    if [[ ! $i == *"-sources.jar" ]] then PATH_TO_JAR=$i break fi
done

if [[ ! -f PATH_TO_JAR && -d current ]]; then cd current for i in `ls $SERVICE_NAME-*.jar 2>/dev/null` do
        if [[ ! $i == *"-sources.jar" ]] then PATH_TO_JAR=$i break fi
    done
fi

if [[ -f $SERVICE_NAME".jar" ]]; then
  rm -rf $SERVICE_NAME".jar"
fi printf "$(date) ==== Starting ==== \n"

ln $PATH_TO_JAR $SERVICE_NAME".jar"
chmod a+x $SERVICE_NAME".jar" ./$SERVICE_NAME".jar" start rc=$?; if [[ $rc != 0 ]]; then
    echo "$(date) Failed to start $SERVICE_NAME.jar, return code: $rc" exit $rc; fi declare -i counter=0 declare -i max_counter=48 # 48*5=240s declare -i total_time=0 printf "Waiting for server startup"
until [[ (( counter -ge max_counter )) || "$(curl -X GET --silent --connect-timeout 1 --max-time 2 --head $SERVER_URL | grep "HTTP")" != "" ]]; do printf "." counter+=1
    sleep 5 checkPidAlive done total_time=counter*5

if [[ (( counter -ge max_counter )) ]]; then printf "\n$(date) Server failed to start in $total_time seconds!\n" exit 1; fi printf "\n$(date) Server started in $total_time seconds!\n" exit 0;
View Code

修改apollo-portal裏面的apollo-env.properties文件

dev.meta=http://{內網ip}:{端口}

修改完以後,依次執行apollo-configservice、apollo-adminservice、apollo-portal

若是報錯請查看日誌,日誌在opt/logs/目錄下

 若是是本地開發作測試的話,須要引入maven依賴

<dependency>
    <groupId>com.ctrip.framework.apollo</groupId>
    <artifactId>apollo-client</artifactId>
    <version>1.4.0</version>
</dependency>
<dependency>
    <groupId>com.ctrip.framework.apollo</groupId>
    <artifactId>apollo-core</artifactId>
    <version>1.4.0</version>
</dependency>

 

建立配置文件application.properties

app.id=SimpleAapp apollo.meta=http://192.168.56.6:8080

 

啓動類加上@EnableApolloConfig註解

app.id對應如圖的AppId

 

 

若是啓動項目報錯說不能註冊未知的服務,拒絕鏈接eureka,

能夠強制指定下IDEA或者Eclipse的啓動參數

-Dapollo.configService=http://config-service的公網IP:端口

 

至此,部署算完成了,若有不懂,請詳細閱讀下官方文檔。

相關文章
相關標籤/搜索