Giter Site home page Giter Site logo

bluceyee / apollosdk Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thinmonkey/apollosdk

0.0 2.0 0.0 67 KB

这是目前开源的功能最全面的apollo client,完全仿照官方java sdk实现的全功能go apollo client

License: Apache License 2.0

Go 98.47% Shell 1.53%

apollosdk's Introduction

Apollosdk - Go Client for Apollo

Build Status Coverage Status codebeat badge

方便Golang接入配置中心框架 Apollo 所开发的Golang版本客户端。

Installation

如果还没有安装Go开发环境,请参考以下文档Getting Started ,安装完成后,请执行以下命令:

go get -u github.com/cihub/seelog
go get -u github.com/coocood/freecache
go get -u github.com/thinmonkey/apollosdk

请注意: 最好使用Go 1.8进行开发

Features

  • 实时同步配置
  • 灰度配置
  • 客户端容灾
  • 支持多namespace,多集群,多数据中心配置
  • 支持监听配置变更实时变化
  • 支持容器化(docker)多环境配置

使用规则

根据apollo官方client使用规则,apollosdk启动时需要配置相应的apollo相关参数,和一些运行时配置参数(可选配)。

apollo启动参数有多种获取方式:

  1. 配置文件config.properties(文件位置可以动态设置)。
  2. os系统环境变量
  3. apollosdk启动时动态传递apollo配置参数。

配置文件config.properties获取启动参数

配置文件默认寻找当前项目的根目录下的config.json文件,配置内容格式为json,可配置内容如下:

{
    "appId":"app-capability",
    "cluster":"default",
    "metaServer":"http://10.160.1.153:8083",
    "httpRefreshInterval":"300s",
    "httpTimeout":"20s",
    "onErrorRetryInterval":"1s",
    "maxConfigCacheSize":52428800,
    "configCacheExpireTime":60,
    "longPollingRefreshInterval":"2s",
    "longPollingTimeout":"60s"
}

os系统环境变量获取配置,只支持必要apollo参数,其他运行时参数为默认值(方便支持docker容器注入不同环境参数)

  • appId获取:os.Getenv("apollo.appId")
  • cluster获取:os.Getenv("apollo.cluster")
  • metaServer获取:os.Getenv("DOCKER_SERVER")
  • dataCenter获取:os.Getenv("apollo.dataCenter")

启动时动态注入apollo参数和配置文件地址

使用前调用方法:
apollosdk.Start("/opt/config.json","appId","cluster","metaServer","dataCenter")

名词解释:

  • appId对应于apollo appId,当前应用的appId
  • cluster对应于apollo cluster,当前应用所在的集群
  • metaServer对应于apollo配置地址,当前应用连接的apollo环境配置地址
  • dataCenter对应于apollo dataCenter,当前应用连接的数据中心。
  • httpRefreshInterval获取配置接口的调用间隔时间,默认五分钟
  • httpTimeout获取配置接口的超时时间,默认20s
  • onErrorRetryInterval获取配置接口错误之后延迟尝试间隔,默认1s
  • maxConfigCacheSize缓存最大的存储大小,默认50M
  • configCacheExpireTime缓存失效时间,默认60秒
  • longPollingRefreshInterval通知接口的刷新间隔时间,默认2s
  • longPollingTimeout通知接口的keep-alive时间,默认60s

获取apollo配置的优先级:

第3种方式 > 第2种方式 > 第1种方式,优先选择动态传递的参数,其次是系统环境变量,最后是配置文件。

Use

初始化(可选),不设置则按照默认规则

apollosdk.Start("/opt/config.json","appId","cluster","metaServer","dataCenter")

默认的namespace配置获取

config := apollosdk.GetAppConfig()
config.GetStringProperty("mats", "")

自定义的namespace配置获取

config := apollosdk.GetConfig(""app.tc.mat.disable"")
config.GetStringProperty("mats", "")

配置改变实时监听(支持多种监听回调方式)

configNew := apollosdk.GetConfig(""app.tc.mat.disable"")
//方式一:定义变量来监听
var varFunc OnChangeFunc =  func (changeEvent ConfigChangeEvent)  {
		fmt.Println("variable onChange",changeEvent)
}
configNew.AddChangeListenerFunc(varFunc)


//方式二:定义普通函数来监听
func onTestFunc(changeEvent ConfigChangeEvent) {
	fmt.Println("func onChange",changeEvent)
}
configNew.AddChangeListenerFunc(onTestFunc)

//方式三:定义结构实现接口来监听
type SomeThing string

func (s SomeThing)OnChange (changeEvent ConfigChangeEvent)  {
	fmt.Println("struct onChange",changeEvent)
}
var s SomeThing = "s"
configNew.AddChangeListener(s)

//移除监听器
configNew.RemoveChangeListener(s)

apollosdk's People

Contributors

thinmonkey avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.