apollo-自定义安装整合

310 阅读1分钟

下载代码

apollo1.9.0版本服务端

初始化sql脚本

创建ApolloPortalDB和ApolloConfigDB数据库,执行sql脚本。

修改三个项目的数据库连接信息

config/application-github.properties

spring.datasource.url=jdbc:mysql://10.211.55.6:3306/ApolloConfigDB?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
spring.datasource.username=cancan
spring.datasource.password=uURUx%7i

自定义端口号

apollo-configservice:18080
apollo-adminservice:18090
apollo-portal:18070

修改script/start.sh启动脚本的端口号

apollo-configservice

SERVER_PORT=${SERVER_PORT:=18080}

apollo-adminservice

SERVER_PORT=${SERVER_PORT:=18090}

apollo-portal

SERVER_PORT=${SERVER_PORT:=18070}

修改数据库eureka服务连接端口

修改ServerConfig表连接eureka的端口号:18080

启动

执行start.sh脚本依次启动服务,查看启动日志:

apollo-configservice:/opt/logs/100003171
apollo-adminservice:/opt/logs/100003172
apollo-portal:/opt/logs/100003173

admin注册成功

登录apollo管理端

默认密码:apollo/admin

springboot整合apollo

依赖包

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

启动类apollo配置类

@EnableApolloConfig

application.properties

#apollo项目id
app.id=enterprise-order
apollo.bootstrap.enabled=true
#默认空间名称application
apollo.bootstrap.namespaces=application

监听修改配置属性

package com.enterprise.order.config;

import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Slf4j
@Data
@Component
public class StellarApolloConfig {

    @Value("${test}")
    private String test;

    @ApolloConfigChangeListener
    private void listenRestartChange(ConfigChangeEvent changeEvent) {
        if (changeEvent.isChanged("test")) {
            log.info("test变化");
            test = changeEvent.getChange("test").getNewValue().trim();
            log.info("test变化,test={}", test);
        }
    }

}

apollo服务端创建应用配置属性

启动项目和guava版本冲突解决,升级guava包

解决

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>20.0</version>
</dependency>

启动参数:

-Dspring.profiles.active=dev -Denv=dev -Dapollo.configService=http://10.211.55.6:18080

启动日志:

有一个注册实例正在使用:

修改配置监听到事件:

修改配置监听到事件