记录学习SpringBoot接入Apollo(阿波罗--分布式配置中心)

SpringBoot框架接入Apollo

第一次学习配置,共同学习

1、从官网下载包

基于Apollo源码配置。
官网地址:https://github.com/ctripcorp/apollo/releases
关于核心的三个包:
1.apollo-configservice-x.x.x-github.zip
2.apollo-adminservice-x.x.x-github.zip
3.apollo-portal-x.x.x-github.zip

2、配置数据库信息

Apollo服务端需要知道如何连接到你前面创建的数据库,数据库连接串信息位于上一步下载的压缩包中的config/application-github.properties中。

3、配置apollo-configservice的数据库连接信息

1.解压apollo-configservice-x.x.x-github.zip
2.用程序员专用编辑器(如vim,notepad++,sublime等)打开config目录下的application-github.properties文件
3.填写正确的ApolloConfigDB数据库连接串信息,注意用户名和密码后面不要有空格!
4.修改完的效果如下:(需要添加时区)
DataSource
spring.datasource.url = jdbc:mysql://localhost:3306/ApolloConfigDB?useSSL=false&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username = someuser
spring.datasource.password = somepwd
注:由于ApolloConfigDB在每个环境都有部署,所以对不同的环境config-service需要配置对应环境的数据库参数,配置数据源时,需要添加时区,当时未加时区应用没跑起来

4、配置apollo-adminservice的数据库连接信息

1.解压apollo-adminservice-x.x.x-github.zip
2.用程序员专用编辑器(如vim,notepad++,sublime等)打开config目录下的application-github.properties文件
3.填写正确的ApolloConfigDB数据库连接串信息,注意用户名和密码后面不要有空格!
4.修改完的效果如下:
#DataSource
spring.datasource.url=jdbc:mysql://localhost:3306/ApolloConfigDB?useSSL=false&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username = someuser
spring.datasource.password = somepwd
注:由于ApolloConfigDB在每个环境都有部署,所以对不同的环境admin-service需要配置对应环境的数据库参数

5、配置apollo-portal的数据库连接信息

1.解压apollo-portal-x.x.x-github.zip
2.用程序员专用编辑器(如vim,notepad++,sublime等)打开config目录下的application-github.properties文件
3.填写正确的ApolloPortalDB数据库连接串信息,注意用户名和密码后面不要有空格!
4.修改完的效果如下:
5.DataSource
spring.datasource.url=jdbc:mysql://localhost:3306/ApolloPortalDB?useSSL=false&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username= someuser
spring.datasource.password = somepwd

6、配置apollo-portal的meta service信息

1、Apollo Portal需要在不同的环境访问不同的meta service(apollo-configservice)地址,所以我们需要在配置中提供这些信息。默认情况下,meta service和config service是部署在同一个JVM进程,所以meta service的地址就是config service的地址。
对于1.6.0及以上版本,可以通过ApolloPortalDB.ServerConfig中的配置项来配置Meta Service地址,详见apollo.portal.meta.servers - 各环境Meta Service列表
2、使用程序员专用编辑器(如vim,notepad++,sublime等)打开apollo-portal-x.x.x-github.zip中config目录下的apollo-env.properties文件。
假设DEV的apollo-configservice未绑定域名,地址是1.1.1.1:8080,FAT的apollo-configservice绑定了域名apollo.fat.xxx.com,UAT的apollo-configservice绑定了域名apollo.uat.xxx.com,PRO的apollo-configservice绑定了域名apollo.xxx.com,那么可以如下修改各环境meta service服务地址,格式为 e n v . m e t a = h t t p : / / {env}.meta=http:// env.meta=http://{config-service-url:port},如果某个环境不需要,也可以直接删除对应的配置项(如lpt.meta):
dev.meta=http://1.1.1.1:8080
fat.meta=http://apollo.fat.xxx.com
uat.meta=http://apollo.uat.xxx.com
pro.meta=http://apollo.xxx.com

如果需要安装其他环境,每个环境的apolloconfigdb请分开部署,其他请参照ConfigService,AdminService的安装,然后把MetaService地址(ConfigService)地址更新到 PortalService的apollo-env.properties中,并修改apolloportaldb库的serviceconfig表中的apollo.portal.envs的value值增加对应的环境值,并重新启动就可以了。

7、代码启动类添加

@EnableApolloConfig

依赖添加

<!--apollo相关jar包 -->
<dependency>
    <groupId>com.ctrip.framework.apollo</groupId>
    <artifactId>apollo-client</artifactId>
    <version>1.1.0</version>
</dependency>
<dependency>
    <groupId>com.ctrip.framework.apollo</groupId>
    <artifactId>apollo-core</artifactId>
    <version>1.1.0</version>
</dependency>

启动类加入监听机制,实时(1s)获取Apollo配置

//启用Apollo配置中心
@EnableApolloConfig
@Configuration
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class APPApplication
{
    private static final Logger logger = LoggerFactory.getLogger(APPApplication.class);
    private String DEFAULT_VALUE = "undefined";
    private Config config;

    public APPApplication() {
     	//Apollo用的是监听机制,动态加载配置中心修改的配置
        ConfigChangeListener changeListener = new ConfigChangeListener() {
            @Override
            public void onChange(ConfigChangeEvent changeEvent) {
                logger.info("Changes for namespace {}", changeEvent.getNamespace());
                for (String key : changeEvent.changedKeys()) {
                    ConfigChange change = changeEvent.getChange(key);
                    logger.info("Change - key: {}, oldValue: {}, newValue: {}, changeType: {}",
                            change.getPropertyName(), change.getOldValue(), change.getNewValue(),
                            change.getChangeType());
                }
            }
        };
        config = ConfigService.getAppConfig();
        config.addChangeListener(changeListener);
    }

    private String getConfig(String key) {
        String result = config.getProperty(key, DEFAULT_VALUE);
        logger.info(String.format("Loading key : %s with value: %s", key, result));
        return result;
    }
    public static void main(String[] args) throws IOException {
  
        SpringApplication.run(APPApplication.class, args);
        APPApplication apolloConfigDemo = new APPApplication();
        System.out.println(
                "Apollo Config Demo. Please input key to get the value. Input quit to exit.");
            //死循环加载应用启动,实时(1s)获取Apollo的配置
        while (true) {
            System.out.print("> ");
            String input = new BufferedReader(new InputStreamReader(System.in, Charsets.UTF_8)).readLine();
            if (input == null || input.length() == 0) {
                continue;
            }
            input = input.trim();
            if (input.equalsIgnoreCase("quit")) {
                System.exit(0);
            }
            apolloConfigDemo.getConfig(input);
        }
    }
} 

需要一个监听类

@Component
public class ApolloConfigChanged implements ApplicationContextAware
{
    private static final Logger log = LoggerFactory.getLogger(ApolloConfigChanged.class);
    private ApplicationContext applicationContext;

    @ApolloConfigChangeListener
    private void someChangeHandler(ConfigChangeEvent changeEvent) {
        for (String key : changeEvent.changedKeys()) {
            ConfigChange change = changeEvent.getChange(key);
            log.info("Found change - {}", change.toString());
        }

        // 更新相应的bean的属性值,主要是存在@ConfigurationProperties注解的bean
        this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}

Application.yml配置

#euraka的地址
app:
  id: XXX #添加项目AppId的唯一标识
apollo:
  meta: http://127.0.0.1:8080 #eureka注册地址
  autoUpdateInjectedSpringProperties: true
  bootstrap:
    namespaces: application,application-druid #Apollo的namespace
    enabled: true #向spring注入被托管的application.properties文件的配置信息
    eagerLoad:
      enabled: true #将Apollo配置加载提到初始化日志系统之前
  cluster: application,XXX #集群名称

8、部署环境

Portal部署一个环境就可以。另外两个service部署多个环境
启动顺序:先启动configService再启动adminService,最后启动portal。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值