Apollo的搭建

Apollo的搭建

1.环境准备

jdk : 1.8+         mysql  5.6.5+

2.下载

两种方式:

a.下载源码自己编译(需要修改源码的可以选择)

源码:https://github.com/ctripcorp/apoll

b.编译好的直接使用

地址:Releases · apolloconfig/apollo · GitHub

3.将apollo-adminservice 、apollo-configservice和apollo-portal上传至服务器并解压

4.编辑配置文件

a.修改apollo-adminservice和apollo-configservice文件config/application-github.properties

# DataSource

spring.datasource.url = jdbc:mysql://sh-cdb-2h0hokg2.sql.tencentcdb.com:61540/kxg_apollo_config_db_dev?characterEncoding=utf8

spring.datasource.username = kxg_apollo

spring.datasource.password = QmFzZTY05piv572R57uc5LiK5pyA5bi46KeB55

b.修改apollo-portal文件config/application-github.properties

# DataSource

spring.datasource.url = jdbc:mysql://sh-cdb-2h0hokg2.sql.tencentcdb.com:61540/kxg_apollo_portal_db_dev?characterEncoding=utf8

spring.datasource.username = kxg_apollo

spring.datasource.password = QmFzZTY05piv572R57uc5LiK5pyA5bi46KeB55

c.修改apollo-portal文件config/apollo-env.properties

#local.meta=http://localhost:8080

dev.meta=http://localhost:8080      8899 和configservice中的端口一样,改成外置eureka另说

pro.meta=http://localhost:8080   

5.公网使用

修改apollo-adminservice和apollo-configservice文件scrips/start.sh

PS -: 注意端口占用

Apollo的使用

导入maven依赖

<dependency>

    <groupId>com.ctrip.framework.apollo</groupId>

    <artifactId>apollo-client</artifactId>

    <version>1.7.0</version>

</dependency>

<dependency>

    <groupId>com.ctrip.framework.apollo</groupId>

    <artifactId>apollo-core</artifactId>

    <version>1.7.0</version>

</dependency>

在recourse的目录下配置

写一个java类

import com.ctrip.framework.apollo.Config;

import com.ctrip.framework.apollo.model.ConfigChange;

import com.ctrip.framework.apollo.model.ConfigChangeEvent;

import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;

import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;

import org.springframework.beans.factory.annotation.Value;

/**

 * @Author: Niu Lilu

 * @Date: 2020/8/9 9:49

 * @Description:牛立露你写点注释吧!

 */

public class TestJavaConfigBean {

    @ApolloConfig("application")

    private Config config; //inject config for namespace application

    @Value("${batch:400}")//如果配置中心没有值,默认key为test的value值为test

    private String name;

    //config change listener for namespace application

    @ApolloConfigChangeListener("application")

    private void anotherOnChange(ConfigChangeEvent changeEvent) {

        ConfigChange change = changeEvent.getChange("batch");

        System.out.println(String.format("Found change - key: %s, oldValue: %s,"

                + " newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()));

    }

}

启动类注解

使用

@RestController

public class TestApolloAnnotationBean {

    @ApolloConfig

    private Config config; //inject config for namespace application

    @Value("${batch:100}")

    private int batch;

    //config change listener for namespace application

    @ApolloConfigChangeListener

    private void someOnChange(ConfigChangeEvent changeEvent) {

        //update injected value of batch if it is changed in Apollo

        if (changeEvent.isChanged("batch")) {

            batch = config.getIntProperty("batch", 100);

        }

    }

    //config change listener for namespace application

    @ApolloConfigChangeListener("application")

    private void anotherOnChange(ConfigChangeEvent changeEvent) {

        if (changeEvent.isChanged("batch")) {

            batch = config.getIntProperty("batch", 100);

        }

    }

    //example of getting config from injected value

    //the program needs to update the injected value when batch is changed in Apollo using @ApolloConfigChangeListener shown above

    public int getBatch() {

        return this.batch;

    }

    @GetMapping("/")

    public String get(){

        return getBatch()+"";

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值