Notice: 函数 WP_Scripts::localize 的调用方法不正确$l10n 参数必须是一个数组。若要将任意数据传递给脚本,请改用 wp_add_inline_script() 函数。 请查阅调试 WordPress来获取更多信息。 (这个消息是在 5.7.0 版本添加的。) in /data/www/appblog/wp-includes/functions.php on line 6131

Apollo配置中心基本操作

创建配置项

以创建service-a配置中心为例,点击新建项目

Apollo创建项目

填写如上配置,注意应用id这个属性很重要,后面在客户端中也需要用到这个配置,所以请妥善配置

Apollo新增配置

Apollo添加配置项

Apollo配置发布

发布之后 配置中心就配置完成了! 接下来我们配置客户端来使用这个配置中心

客户端连接配置

(1)Java客户端依赖配置

<!-- https://mvnrepository.com/artifact/com.ctrip.framework.apollo/apollo-client -->
<dependency>
    <groupId>com.ctrip.framework.apollo</groupId>
    <artifactId>apollo-client</artifactId>
    <version>1.4.0</version>
</dependency>

(2)在入口文件中加入@EnableApolloConfig开启配置中心服务

@EnableApolloConfig
@SpringBootApplication
public class MyApplication {

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

}

(3)在application.yml中配置前面创建的应用id和地址

app:
  id: service-provider
apollo:
  meta: http://192.168.1.20:8980

配置好后,service-a会自动关联Apollo配置中心

(4)创建测试接口

@RestController
public class ConfigController {
    @ApolloConfig
    private Config config;

    @GetMapping("/hello")
    public String hello() {
        return config.getProperty("name", "default");
    }
}

访问接口:http://localhost:8080/hello,能够获取刚才在配置中心配置的值

(5)更新配置

Apollo修改配置

Apollo重新发布

再次访问接口:http://localhost:8080/hello,可以看到返回为最新配置的值

上一篇 携程Apollo服务端部署与操作
下一篇 Spring Boot项目整合Apollo错误:for env UNKNOWN from com.ctrip.framework.apollo.internals.DefaultMetaServerProvider