Apollo-2 项目使用

一、基本使用

(一)创建配置

在apollo管理界面点击”创建项目“,输入项目相关信息创建项目,其中部门有两个默认的,如果想添加自己的部门,可以在数据库ApolloPortalDB中的ServerConfig表中修改keyorganizations对应的value值。
在这里插入图片描述
创建完项目之后效果如下:
在这里插入图片描述
我们可以在这里编辑我们的配置文件,编辑完了之后,点击“发布”对编辑的文件发布,之后就能使用了,我这里加入了以下几个配置:
在这里插入图片描述

(二)在项目中引入apollo

这里以springboot为例,选取最简单的方式进行测试,apollo提供的配置方式有很多,我这里选取个人认为最简单的。

  1. 引入apollo依赖
    <dependency>
      <groupId>com.ctrip.framework.apollo</groupId>
      <artifactId>apollo-client</artifactId>
      <version>1.1.0</version>
    </dependency>
  1. 在resources目录下面创建applicaiton.properties文件,编辑如下内容:
apollo.meta=http://10.211.55.6:8080   #apollo服务器地址,必选
env=DEV #所在环境,可选配置
app.id=apollo-helloworld #应用ID,必选
apollo.bootstrap.enabled=true # 启用apollo,必选
  1. 测试
    创建控制器,显示redis.host内容:
package com.firewolf.apollo.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 作者:刘兴 时间:2019-05-22
 **/
@RestController
public class ApolloController {

  @Value("${redis.host}")
  private String redisHost;

  @GetMapping("/test")
  public String testApollo() {
    return redisHost;
  }
}

启动项目,控制台内容如下:
在这里插入图片描述
可以看到,应用去apollo上面加载了配置,而且,启动端口已经使用了apollo上面配置的9999。
访问http://localhost:9999/test,返回如下:
在这里插入图片描述
更改apollo上面redis.host为1.1.1.1,发布后再次访问,效果如下:
在这里插入图片描述
可以看到,apollo会动态更新配置内容。

猜你喜欢

转载自blog.csdn.net/mail_liuxing/article/details/90447352