携程Apollo配置中心分布式部署(包含windows和linux注意事项及些许不同)

1.一定要仔细阅读apollo分布式部署指南,你想要的都在里面,尤其注意指南里的【注:】

https://github.com/ctripcorp/apollo/wiki/%E5%88%86%E5%B8%83%E5%BC%8F%E9%83%A8%E7%BD%B2%E6%8C%87%E5%8D%97

2.安装jdk1.8

3.安装mysql5.7 ,并将两个数据库sql文件导入:apolloportaldb.sqlapolloconfigdb.sql

4.下载三个服务包:从GitHub Release页面下载最新版本的apollo-configservice-x.x.x-github.zipapollo-adminservice-x.x.x-github.zipapollo-portal-x.x.x-github.zip

5.windows部署需要安装git bash,模拟linux环境。

6.三个包解压之后需要修改的地方:

        6.1.configservice包中apollo-configservice.conf文件修改LOG_FOLDER(日志路径);

              scripts/start.sh中修改LOG_DIR和SERVER_PORT=8080(根据需要修改);【注1】

              application-github.properties中修改数据库连接信息。

        6.2.adminservice包中apollo-adminservice.conf文件修改LOG_FOLDER;

              scripts/start.sh中修改LOG_DIR和SERVER_PORT=8090(根据需要修改);【注1】

              application-github.properties中修改数据库连接信息。

        6.3.portal包中apollo-portal.conf文件修改LOG_FOLDER;

              scripts/start.sh中修改LOG_DIR和SERVER_PORT=9101(根据需要修改);

             application-github.properties中修改数据库连接信息;

             apollo-env.properties文件中增加生产环境pro.meta=http://localhost:8080(该端口就是configservice服务的端口)。

        6.4.mysql数据库中ApolloConfigDB.ServerConfig表中第一行的value为http://localhost:8080/eureka/(该端口就是configservice服务的端口)。

        6.5.mysql数据库中ApolloPortalDB.ServerConfig表中第一行的value改为pro(此处直接使用生产环境)。

        6.6.【可以先不设置】如果阿波罗服务器内存不足,可以修改三个包中的scripts/start.sh里面的JAVA_OPTS参数。

        6.7.【可以先不设置】如果有多个环境,则每个环境都需要独立部署一套config-service、admin-service和ApolloConfigDB;

              ApolloPortalDB和portal只需要在生产环境部署一个即可;

              而在文件apollo-env.properties中需要配置所有环境的{env}.meta的信息。

7.所有修改完之后,将三个解压文件夹上传到linux服务器目录下/opt/apollo【可自己指定】,

      在/opt/apollo/目录下,按照顺序执行三个启动命令:

      ./apollo-configservice-1.4.0-github/scripts/startup.sh

      ./apollo-adminservice-1.4.0-github/scripts/startup.sh

      ./apollo-portal-1.4.0-github/scripts/startup.sh

 本地windows使用git bash启动情况如图:

   【停止服务也是按照顺序,在/opt/apollo/目录下,执行三个停止命令:】
      ./apollo-portal-1.4.0-github/scripts/shutdown.sh
      ./apollo-adminservice-1.4.0-github/scripts/shutdown.sh
      ./apollo-configservice-1.4.0-github/scripts/shutdown.sh

8.浏览器访问:http://localhost:9101   【访问服务器需要ip和端口都对外开放】

登录页面用户名:apollo,密码:admin

刚进去是没有这些信息的,需要自己创建项目信息,AppId用来客户端连接使用。

9.客户端访问,直接使用API方式,最快最简单:

           9.1.调用apollo配置的客户端,需要在客户端服务器目录/opt/settings/下增加settings文件,里面写上env=PRO,这样才能调用apollo的生产环境配置。
           9.2.在项目resources目录下增加/META-INF/app.properties文件,里面写
#该id就是当前项目在阿波罗配置中心的appid,一定要对应
app.id=10000001
#apollo.meta地址就是config service的地址
apollo.meta=http://{config-service-ipAddress}:8080
           9.3.在项目pom文件中增加引用
        <dependency>
            <groupId>com.ctrip.framework.apollo</groupId>
            <artifactId>apollo-client</artifactId>
            <version>1.1.0</version>
        </dependency>
          9.4.在java代码中应用:
private Config config = ConfigService.getAppConfig();//直接获取私有namespace的单例对象
private Config anotherConfig = ConfigService.getConfig("Name");//直接获取名称为Name的公有namespace的单例对象

10.windows下更改配置的值之后客户端可能几秒钟就获取到了,但是服务器上有可能30秒~1分钟才能收到。

11.具体的springboot客户端测试代码:

package com.wong.gogo.controller;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 连接分布式配置中心:
 *   获取阿波罗配置中心配置 (Java API方式)
 */
@RestController
@RequestMapping("/apollo")
public class ApolloController {

    //从项目路径下的环境配置文件中获取
    @Value("${my.profile.userName}")
    private String name;

    //config instance is singleton for each namespace and is never null
    private Config config = ConfigService.getAppConfig();

    /**
     * 获取默认私有namespace的配置
     */
    @PostMapping("/getConfigValue")
    public String getConfigValue() {
        String someKey = "testKey";
        String someDefaultValue = "123";
        String value = config.getProperty(someKey, someDefaultValue);
        return value;
    }

    /**
     * 获取特定私有namespace下的配置,即使和resources目录下的项目原生配置文件有相同配置,也都能获取
     * @return
     */
    @PostMapping("/getPrivateConfigValue")
    public String getPrivateConfigValue() {
        Config config = ConfigService.getConfig("myPrivate"); //config instance is singleton for each namespace and is never null
        String someKey = "my.profile.userName";
        String someDefaultValue = name;//默认值可以使用resources目录下的配置文件的配置
        String value = config.getProperty(someKey, someDefaultValue);

        System.out.println("使用了apollo之后,resources目录下的配置文件依然有效,my.profile.userName="+name);
        return value;
    }

    /**
     * 获取公共Namespace的配置
     * @return
     */
    @PostMapping("/getPublicConfigValue")
    public String getPublicConfigValue() {
        String somePublicNamespace = "AIPARK.myPublic";
        Config config = ConfigService.getConfig(somePublicNamespace); //config instance is singleton for each namespace and is never null
        String someKey = "wanted";
        String someDefaultValue = "defaultValue";
        String value = config.getProperty(someKey, someDefaultValue);
        return value;
    }

    /**
     * 获取公共Namespace的配置
     * @return
     */
    @PostMapping("/getPublicConfigValueByName")
    public String getPublicConfigValueByName(@RequestBody Map<String, String> paraMap) {
        String nameSpace = paraMap.get("nameSpace");
        Config config = ConfigService.getConfig(nameSpace); //config instance is singleton for each namespace and is never null
        String key = paraMap.get("key");
        String value = config.getProperty(key, "defaultValue");
        return value;
    }

    public static void main(String[] args) throws InterruptedException {
        Config config = ConfigService.getConfig("myPrivate"); //config instance is singleton for each namespace and is never null
        String someKey = "my.profile.userName";
        String someDefaultValue = "defaultValue";

        //获取公共Namespace的名称【注意:要写全称】
        /*Config config = ConfigService.getConfig("AIPARK.myPublic"); //config instance is singleton for each namespace and is never null
        String someKey = "wanted";
        String someDefaultValue = "defaultValue";*/

        while(true) {
            String value = config.getProperty(someKey, someDefaultValue);
            System.out.println(value);
            Thread.sleep(5000);
        }
    }
}

客户端源码网盘地址:

链接:https://pan.baidu.com/s/1oUS3T7-gGOigKxwV4Lpcag 
提取码:q631 

 

【注1】windows随便修改端口没问题,但是linux服务器如果只有部分端口对外网开放,那么config和admin服务需要使用内网未开放端口,portal使用开放端口,才能正常启动。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值