携程配置中心Apollo的Java客户端API的使用

一、SpringBoot集成

1.发布配置信息

  • 设置本机为DEV环境:Linux在/opt/settings/server.properties增加配置env=DEV,windows在c:\opt\settings\server.properties

<div align=center>

图1

<div align=left>

  • 在apollo portal上新建项目后,默认就有了application命名空间。在DEV环境下新建一个名为test_namespace的命名空间。
  • 在application上发布:spring.applicaton.name=apollo_demo,server.port=9000。
  • 在test_name上发布:name=name1, value=value2。

2.引入依赖

<dependency>
       <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-stater</artifactId>
</dependency>

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${lombok.version}</version>
        <scope>provided</scope>
</dependency>

<!-- apollo客户端 -->
<dependency>
        <groupId>com.ctrip.framework.apollo</groupId>
        <artifactId>apollo-client</artifactId>
        <version>${apollo.version}</version>
</dependency>

3.项目配置

  1. 在项目的application.properties(applicaiton.yml)或者/META-INF/app.properties填入app.id=appId(在apollo-portal上新建项目时填写的appId,表示获取的是那个配置项目的配置信息)。
  2. resources目录下新建apollo-env.properties,填写各个环境的meta server地址:

<div align=center>

图2

<div align=left> 3) 或者不在项目配置apollo-env.properties,而是直接在application.properties指定apollo.meta=ip:port的方式来执行需要读取配置的的服务

  1. 使用application命名空间的配置信息来启动SpringBoot应用 入口方法增加@EnableApolloConfig注解
@SpringBootApplication
@EnableApolloConfig
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

如果不使用@EnableApolloConfig注解,可以在application.properties里面配置apollo.bootstrap.enabled=true,效果一样。 使用apollo.bootstrap.namespaces = application,test_namespace可以指定命名空间。

启动项目:

<div align=center>

图3

<div align=left> 可以看到应用在启动前从配置中心获取配置信息来启动应用。 `@EnableApolloConfig`默认是从application命名空间获取配置的,相当于`@EnableApolloConfig("application")`.。

4.获取配置

application命名空间配置信息 java bean:

@Component
@EnableApolloConfig
@Getter
@Setter
@ToString
public class AppNamespace {

    @Value("${spring.application.name:}")
    private String name;

    @Value("${server.port:}")
    private String value;
}

java bean:

@Component
@EnableApolloConfig("CASE.test_namespace")
@Getter
@Setter
@ToString
public class TestNamespace {

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

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

使用:

@RestController
public class DemoController {

    @Autowired
    private TestNamespace demo;

    @Autowired
    private AppNamespace application;

    @ApolloConfig
    private Config appConfig;

    @ApolloConfig("CASE.test_namespace")
    private Config testConfig1;

    private Config testConfig2 = ConfigService.getConfig("CASE.test_namespace");
}

以上两种方式获取配置信息的值,会跟配置中心的更改同步(1秒内);还可以使用@ConfigurationProperties来获取配置信息,但这种方式不会同步更新,需要额外的编码配置才能实现,具体查看官方文档。

5.其他

@ApolloJsonValue注解,作用相当于@Value,将JSON字符串转成对象。

@ApolloConfigChangeListener注解::

@ApolloConfigChangeListener
private void someOnChange(ConfigChangeEvent changeEvent) {
        //update injected value of batch if it is changed in Apollo
        if (changeEvent.isChanged("key")) {
                System.out.println(config.getIntProperty("key", ""));
        }
}

@ApolloConfigChangeListener相当于@ApolloConfigChangeListener("application")

相当于:

Config config = ConfigService.getAppConfig(); 
config.addChangeListener(new ConfigChangeListener() {
    @Override
    public void onChange(ConfigChangeEvent changeEvent) {
        System.out.println("Changes for namespace " + changeEvent.getNamespace());
        for (String key : changeEvent.changedKeys()) {
            ConfigChange change = changeEvent.getChange(key);
            System.out.println(String.format("Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()));
        }
    }
});

如果同时以两种方式绑定changeListener的方式,只有ConfigService实例的监听器会生效。

2.其他

Quick start连接

转载于:https://my.oschina.net/u/2347651/blog/3001099

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值