如何在Linux下安装apollo并在springboot中集成使用

总结  收藏
0 / 2198

linux centos安装apollo

环境依赖:

JDK:1.8.0_161

Maven:3.5.2

MySQL:5.7.18

apollo:1.6.1

下载 apollo

mkdir /data/apollo

wget https://github.com/ctripcorp/apollo/archive/v1.6.1.tar.gz tar zxvf apollo-1.6.1.tar.gz

导入数据库文件

登录 MySQL 命令行,然后执行

/data/apollo/apollo-1.6.1/scripts/sql/apolloconfigdb.sql /data/apollo/apollo-1.6.1/scripts/sql/apolloportaldb.sql

打包

修改/data/apollo/apollo-1.6.1/scripts/build.sh,把数据库账号密码修改为我们的密码以及 MySQL 服务器地址。只保留 dev_meta,其他的都删除 # dev_meta=http://IP:8080 META_SERVERS_OPTS="-Ddev_meta=$dev_meta"

如果是多网卡,有可能读取的是私网IP,需要手动更改为公网IP: 官方方法:

image.png

注意文件不要修改错了!!!

然后执行

./build.sh

该脚本会依次打包 apollo-configservice, apollo-adminservice, apollo-portal 和 apollo-client

因为依赖maven打包,所以需要提前安装maven:

记得添加环境变量:

vi /etc/profile

export MAVEN_HOME=/usr/local/apache-maven-3.6.1 export PATH=$MAVEN_HOME/bin:$PATH

source /etc/profile

查看安装是否成功:

mvn -v

添加镜像:

<mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>       
</mirror>

启动

** apollo-configservice:**

切换到目录 /data/apollo/apollo-1.6.1/apollo-configservice/target,解压 apollo-configservice-1.6.1-github.zip

unzip apollo-configservice-1.6.1-github.zip –d test

执行脚本启动服务

./startup.sh

** apollo-adminservice:**

切换到目录 /data/apollo/apollo-1.6.1/apollo-adminservice/target,解压 apollo-adminservice-1.6.1-github.zip

unzip apollo-adminservice-1.6.1-github.zip –d test

执行脚本启动服务

./startup.sh

apollo-portal:

apollo-portal 的默认端口是 8080,和 apollo-configservice 一致,所以如果需要在一台机器上同时启动 apollo-portal 和 apollo-configservice 的话,需要修改 apollo-portal 的端口。直接修改 startup.sh 中的 SERVER_PORT 即可,如 SERVER_PORT=8070。

切换到目录 /data/apollo/apollo-1.6.1/apollo-portal/target,解压 apollo-portal-1.6.1-github.zip

unzip apollo-portal-1.6.1-github.zip –d test

修改端口:

image.png

执行脚本启动服务

./startup.sh

另一种方法:

执行以下命令 :

apollo项目解压在/data/apollo/apollo下,先修改配置build后执行以下命令,所有命令在/data/apollo执行

rm -rf apollo-adminservice/ apollo-configservice/ apollo-portal/
mkdir apollo-adminservice apollo-configservice apollo-portal
mv /data/apollo/apollo/apollo-configservice/target/apollo-configservice-1.7.0-SNAPSHOT-github.zip apollo-configservice/
mv /data/apollo/apollo/apollo-adminservice/target/apollo-adminservice-1.7.0-SNAPSHOT-github.zip apollo-adminservice/
mv /data/apollo/apollo/apollo-portal/target/apollo-portal-1.7.0-SNAPSHOT-github.zip apollo-portal/
unzip apollo-configservice/apollo-configservice-1.7.0-SNAPSHOT-github.zip -d apollo-configservice/
unzip apollo-adminservice/apollo-adminservice-1.7.0-SNAPSHOT-github.zip -d apollo-adminservice/
unzip apollo-portal/apollo-portal-1.7.0-SNAPSHOT-github.zip -d apollo-portal/
./apollo-configservice/scripts/startup.sh
./apollo-adminservice/scripts/startup.sh
./apollo-portal/scripts/startup.sh

修改apollo部门:

apolloportaldb库serverconfig

image.png

springboot集成

添加依赖

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

添加监听

@EnableApolloConfig
public class MainApplication {
        *****
}
/**
 * Apollo 配置监听
 */
@Configuration
public class ApolloConfigListener implements ApplicationContextAware {
    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(ApolloConfigListener.class);
    /**
     * 日志配置常量
     */
    private static final String LOGGER_TAG = "logging.level.";

    @Resource
    private LoggingSystem loggingSystem;

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }


    /**
     * 配置监听
     * ApolloConfigChangeListener > value 属性默认 命名空间 "application"
     */
    @ApolloConfigChangeListener
    private void onChangeConfig(ConfigChangeEvent changeEvent) {
        LOGGER.info("【Apollo-config-change】>> start");
        for (String key : changeEvent.changedKeys()) {
            ConfigChange change = changeEvent.getChange(key);
            LOGGER.info("【Apollo-config-change】>> key={} , propertyName={} , oldValue={} , newValue={} ",
                    key, change.getPropertyName(), change.getOldValue(), change.getNewValue());
            //是否为日志配置
            if (StringUtils.containsIgnoreCase(key, LOGGER_TAG)) {
                //日志配置刷新
                changeLoggingLevel(key, change);
                continue;
            }
            // 更新相应的bean的属性值,主要是存在@ConfigurationProperties注解的bean
            //this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));
        }
        LOGGER.info("【Apollo-config-change】>> end");
    }

    /**
     * 刷新日志级别
     */
    private void changeLoggingLevel(String key, ConfigChange change) {
        if (null == loggingSystem) {
            return;
        }
        String newLevel = change.getNewValue();
        LogLevel level = LogLevel.valueOf(newLevel.toUpperCase());
        loggingSystem.setLogLevel(key.replace(LOGGER_TAG, ""), level);
        LOGGER.info("【Apollo-logger-config-change】>> {} -> {}", key, newLevel);
    }
}

完成集成~~~

tuser