Apollo学习一阿里云部署

1,547 阅读1分钟

Apollo阿里云部署

1. 简介

Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。

2. 部署前准备

  • 参考github分布式部署指南

  • 需要注意的地方

    • Git拉取源码:源码
    • 修改网络策略 - apollo-configservice和apollo-adminservice修改application.yml
      spring:
            application:
                name: apollo-configservice
            profiles:
              active: ${apollo_profile}
            cloud:
              inetutils:
                ignoredInterfaces:
                  - docker0
                  - veth.*
      
    • 创建数据库
      导入ApolloPortalDB,ApolloConfigDB
    • 修改scripts/build.sh

      # mysql db config 此处变量名可自定义
      uat_sql_url=127.0.0.1
      uat_sql_user_name=root
      uat_sql_pwd=yourpassword
      
      # apollo config db info
      apollo_config_db_url=jdbc:mysql://${uat_sql_url}:3306/ApolloConfigDB?characterEncoding=utf8
      apollo_config_db_username=${uat_sql_user_name}
      apollo_config_db_password=${uat_sql_pwd}
      
      # apollo portal db info
      apollo_portal_db_url=jdbc:mysql://${uat_sql_url}:3306/ApolloPortalDB?characterEncoding=utf8
      apollo_portal_db_username=${uat_sql_user_name}
      apollo_portal_db_password=${uat_sql_pwd}
      
      # 本机部署,暂时开启dev环境
      # meta server url, different environments should have different meta server addresses
      dev_meta=http://${uat_sql_url}:8080
      #fat_meta=http://fill-in-fat-meta-server:8080
      #uat_meta=http://${uat_sql_url}:8080
      #pro_meta=http://fill-in-pro-meta-server:8080
      

3. 部署

  1. 从源码根目录进入scripts,执行sh build.sh

  2. 进入apollo-configservice/target

    • 解压apollo-configservice-1.4.0-github.zip
    • 执行:sh scripts/startup.sh
  3. 进入apollo-adminservice/target

    • 解压apollo-adminservice-1.4.0-github.zip
    • 执行:sh scripts/startup.sh
  4. 进入apollo-portal/target

    • 解压apollo-portal-1.4.0-github.zip
    • 执行:sh scripts/startup.sh
  5. apollo url: localhost:8070

    apollo admin: localhost:8080

PS: 注意:需要使用8070, 8080, 8090端口,也可以在源码中更改端口号

4. Springboot 使用 - 以mac为例

  1. 增加环境配置文件/opt/settings/server.properties, 内容为

    env=DEV
    
  2. springboot增加apollo-env.properties,app.properties

    • apollo-env.properties路径为classpath:config/apollo-env.properties

      dev.meta=http://ip:8080
      
    • app.properties路径为classpath:META-INF/app.properties

      app.id=coco
      apollo.meta=http://ip:8080
      

      app.id为在apollo配置中心配置的AppId

  3. 样例

    import com.ctrip.framework.apollo.Config;
    import com.ctrip.framework.apollo.ConfigService;
    
    @RestController
    @CrossOrigin
    public class MyController {
      
        @GetMapping("/index00")
        public String getIndex() {
    
            String somePublicNamespace = "application";
            Config appConfig = ConfigService.getAppConfig();
            String key = "aaa";
            String defaultValue = "xx";
            String info = appConfig.getProperty(key, defaultValue);
            return info;
        }
    }