一、下载Apollo 2.0.0压缩包,并创建2个数据库:
ApolloPortalDB
ApolloConfigDB
然后分别导入压缩包中sql子目录中的sql文件到数据库
二、修改压缩包中的demo.sh,主要包括数据库的配置等,示例如下:(里边的ip地址尽量使用外网地址,方便进行远程测试)

#!/bin/bash

# handle env
if [[ -n "$JAVA_OPTS" ]]; then
echo JAVA_OPTS = $JAVA_OPTS
fi

if [[ -n "$APOLLO_CONFIG_DB_USERNAME" ]]; then
echo APOLLO_CONFIG_DB_USERNAME = "$APOLLO_CONFIG_DB_USERNAME"
fi

if [[ -n "$APOLLO_CONFIG_DB_PASSWORD" ]]; then
echo APOLLO_CONFIG_DB_PASSWORD = "${APOLLO_CONFIG_DB_PASSWORD//?/*}"
fi

if [[ -n "$APOLLO_PORTAL_DB_USERNAME" ]]; then
echo APOLLO_PORTAL_DB_USERNAME = "$APOLLO_PORTAL_DB_USERNAME"
fi

if [[ -n "$APOLLO_PORTAL_DB_PASSWORD" ]]; then
echo APOLLO_PORTAL_DB_PASSWORD = "${APOLLO_PORTAL_DB_PASSWORD//?/*}"
fi

# apollo config db info
apollo_config_db_url="jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false"
apollo_config_db_username=root
apollo_config_db_password=yourpassword

# apollo portal db info
apollo_portal_db_url="jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false"
apollo_portal_db_username=root
apollo_portal_db_password=yourpassword

# =============== 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://139.xxx.xxx.xxx:8080
admin_server_url=http://139.xxx.xxx.xxx:8090
eureka_service_url=$config_server_url/eureka/
portal_url=http://localhost:8070

# JAVA OPTS
BASE_JAVA_OPTS="$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 APP_NAME="apollo-service"
export JAVA_OPTS="$SERVER_JAVA_OPTS -Dlogging.file.name=./apollo-service.log -Dspring.datasource.url=$apollo_config_db_url -Dspring.datasource.username=$apollo_config_db_username -Dspring.datasource.password=$apollo_config_db_password -Deureka.instance.ip-address=139.xxx.xxx.xxx"

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 APP_NAME="apollo-portal"
export JAVA_OPTS="$PORTAL_JAVA_OPTS -Dlogging.file.name=./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 ===="
export APP_NAME="apollo-portal"
cd $PORTAL_DIR
./$PORTAL_JAR_NAME stop

cd ..

echo "==== stopping service ===="
export APP_NAME="apollo-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

四、运行demo.sh,等待Apollo启动完成。Apollo 2.0默认需要Eureka的支持,因此,它会默认在8080端口启动一个Eureka Server.
启动完成后输入ip:8070,使用默认用户
apollo
admin
进行登录
五、然后在apollo配置中心创建配置:
1创建项目:例如beidou
2.创建namespace,例如k3
3.创建配置并Release配置:例如birthday=2022
六、创建Spring Boot项目,并引入apollo相关依赖

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>cn.edu.tju</groupId>
<artifactId>apollo-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>apollo-springboot</name>
<url>http://maven.apache.org</url>


<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>

七、在application.properties中配置apollo相关参数

server.port=8089
app.id=beidou
apollo.meta=http://139.xxx.xxx.xxx:8080
apollo.bootstrap.enabled=true
apollo.bootstrap.namespaces=k3

八、在controller中使用配置数据(在apollo中配置的birthday)

package cn.edu.tju.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {
@Value("${birthday}")
private String birthday;

@RequestMapping("/hi")
public String hi(){
return birthday;
}
}

九、在配置中心修改birthday的值,重新访问/hi接口,可以看到获取到的是更改后的配置值