miniapollo

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 10 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go Reference Go Report Card Go Version License

miniapollo

嵌入式 Apollo 配置中心 mock,专为单元测试而生

在测试里起一个真正的 Apollo 太重了(Meta/Config/Admin 三件套 + MySQL)。miniapollo 是一个纯标准库、零第三方依赖的进程内 HTTP 服务,完整模拟 Apollo Config Service 对客户端暴露的行为,让真实 Apollo 客户端(Go 的 agollo、Java SDK、apollo-client 等)无需感知地连进来。

为什么需要 miniapollo

测试 Apollo 客户端逻辑时,通常面临两个选择:

方案 问题
起一套真 Apollo Meta + Config + Admin 三件套、MySQL、Eureka — CI 里跑不动,维护成本高
手写 mock HTTP 容易遗漏长轮询语义、服务发现、文件型 namespace 折叠等细节,mock 与真实行为脱节

miniapollo 的切入点:用一个进程内 HTTP 服务替代真 Apollo,只模拟客户端能观察到的行为(/configs、/notifications/v2 长轮询、/services/config 服务发现),让真实客户端无感知地连进来,测试代码只需 Set / SetWith 发布配置即可。

适用场景
  • 单元测试中需要读取 Apollo 配置的业务逻辑
  • 测试配置变更后的热刷新行为(OnChange / OnNewestChange 回调)
  • 本地开发调试,不想依赖外部 Apollo 实例
  • CI 流水线中跑集成测试,无法启动完整 Apollo
m := miniapollo.StartT(t)                       // 起服务,测试结束自动关闭
m.Set("application", map[string]string{         // 发布一个 namespace(自动出新 release)
    "rpc.timeout": "100",
})
m.Set("application.yml", map[string]string{"log": "info"})  // 文件型 namespace 也行

// 把你的客户端/被测代码指向 m.URL()
//   agollo:  env APOLLO_META=m.URL(), APP_ID=SampleApp
//   Java:    -Dapollo.meta=m.URL() -Dapp.id=SampleApp

模拟了 Apollo 的哪些行为

Apollo 行为 miniapollo 说明
GET /configs/{appId}/{cluster}/{namespace} ✅ 返回 ApolloConfigDTO:configurations + releaseKey;未发布 → 404(客户端走本地缓存,与真实一致)
properties / .yml / .yaml / .json / .txt namespace ✅ 文件型 namespace 按真实方式折叠为 configurations["content"];.json 为 map 的 JSON 序列化,.yml/.yaml 为扁平 key: value,.txt 为 key=value
GET /notifications/v2 长轮询 ✅ 客户端 id 落后 → 立即 200 返回首个变化的 namespace(真实 Apollo 同样只回一个);无变化 → 挂住请求直到有发布或超时;超时返回 404("无变更",客户端继续下一轮)
trigger 参数(v2) ✅ 按秒指定挂起时长,钳制在 1s..60s;非法值回退默认,与真实服务端一致
未创建的 namespace 参与通知 ✅ 服务端视其为 id -1:客户端初始 -1 与之相等 → 不触发通知(真实 Apollo 语义)
notificationId 语义 ✅ 每次发布 +1,per-(appId,cluster,namespace) 独立计数
releaseKey 格式 ✅ yyyyMMddHHmmss-<12hex>,每次发布轮换
GET /notifications(v1) ✅ 同 v2 逻辑(真实客户端兼容层)
GET /services/config(Meta 服务发现) ✅ 返回 APOLLO-CONFIGSERVICE 单实例,homepageUrl 即本服务 —— 客户端只给 meta 地址也能找到配置服务
默认 cluster=default ✅ 通知请求缺 cluster 时按 default 处理
404/400 错误体 ✅ {"message": "..."}(Spring 风格错误体)
客户端断连 ✅ 挂起的长轮询随 r.Context().Done() 释放,不泄漏 goroutine
并发发布/多客户端轮询 ✅ 发布原子唤醒所有等待者;醒来后重算全部差异,事件不会丢失
暂不模拟(与真实 Apollo 的差异)
  • Portal / OpenAPI(/openapi/v1/*):那是管理面接口,客户端测试用不到。
  • 灰度发布(Grayscale)、发布审批、公共 namespace 关联:config service 对客户端不呈现这些概念。
  • Set 即发布(隐式 release),没有"改了没发布"的中间态 —— 测试里少一步,语义更直接;真实感场景可用 SetContent + Publish 组合。
  • Admin Service /services/admin:无消费方,省略。
  • 集群内多实例负载均衡:单实例即代表集群。

差异设计原则:只模拟客户端可观察到的行为。凡是客户端看不见的(Portal UI、DB、Eureka 集群拓扑),一律不作 —— 这是 miniapollo 与"起一套真 Apollo"的本质区别。

已验证的真实客户端

客户端 状态 说明
agollo v5 (Go SDK) ✅ 示例通过 开箱即用(v5.0.0,API 兼容 v4);见 example/agollo/main.go(go run example/agollo/main.go)

agollo 适配要点(其他客户端同理):AppConfig.IP 填 m.URL()(meta 地址),AppID/Cluster/NamespaceName 与发布维度一致即可。miniapollo 的 /services/config 会把自己注册为 config service,客户端无需额外地址。

AddChangeListener / OnNewestChange

agollo v5 的 AddChangeListener 接受 storage.ChangeListener 接口(需实现 OnChange 和 OnNewestChange 两个方法),不接受裸 func。两个回调的区别:

回调 事件类型 触发时机 数据内容
OnChange *storage.ChangeEvent 每次检测到配置变更 只包含变更的 key,ConfigChange.OldValue/NewValue 为 interface{}(需 .(string) 断言)
OnNewestChange *storage.FullChangeEvent 每次配置刷新(先于 OnChange) 包含 namespace 的全量键值快照,Changes 为 map[string]interface{}

完整示例见 example/agollo/main.go。

安装与使用

go get github.com/odkrjs/miniapollo
单元测试
func TestFetchConfig(t *testing.T) {
    m := miniapollo.StartT(t)
    m.SetWith("shop", "dev", "application", map[string]string{"rpc.timeout": "100"})
    m.SetContentWith("shop", "dev", "layout.yaml", "items: [a, b]")

    // 直接对 endpoint 断言,或把 m.URL() 注入你的真实客户端
    resp, err := http.Get(m.URL() + "/configs/shop/dev/application")
    require.NoError(t, err)
    defer resp.Body.Close()
    // ...json.Decode / 断言
}
测"配置变更推送"路径(长轮询被唤醒)
func TestOnConfigChange(t *testing.T) {
    m := miniapollo.StartT(t)
    m.SetPollTimeout(150 * time.Millisecond)          // 测试别等 30s
    m.Set("application", map[string]string{"k": "v"})

    got := make(chan struct{})
    go func() {                                       // 你的客户端:挂长轮询
        resp, _ := http.Get(m.URL() + "/notifications/v2?appId=SampleApp&cluster=default" +
            `&notifications=[{"namespaceName":"application","notificationId":1}]`)
        if resp.StatusCode == 200 { close(got) }       // 被唤醒即收到 200
    }()

    time.Sleep(60 * time.Millisecond)                 // 让客户端先挂上
    m.Set("application", map[string]string{"k": "v2"}) // 发布 → 立即唤醒

    select {
    case <-got: // success
    case <-time.After(3 * time.Second):
        t.Fatal("long poll was not woken by publish")
    }
}
接真实客户端(agollo 为例)
m := miniapollo.StartT(t)
m.SetWith("SampleApp", "default", "application", map[string]string{"k": "v"})

client, err := agollo.StartWithConfig(func() (*config.AppConfig, error) {
    return &config.AppConfig{
        AppID:         "SampleApp",
        Cluster:       "default",
        NamespaceName: "application",
        IP:            m.URL(),           // meta 地址,miniapollo 自动服务发现
    }, nil
})
// client.GetValue("k") → "v"
// client.AddChangeListener(...) → OnChange / OnNewestChange 回调

完整可运行示例见 example/agollo/main.go(go run example/agollo/main.go),覆盖三个场景:

  1. 完整生命周期(服务发现 → 初始拉取 → 热刷新)
  2. AddChangeListener → OnChange(OldValue/NewValue 差量回调)
  3. OnNewestChange(全量快照回调)

API 一览

方法 说明
NewServer() (*Server, error) 启动(127.0.0.1 随机端口)
StartT(t *testing.T) *Server 测试助手:启动 + t.Cleanup(Close)
Close() 关闭;可重复调用
URL() / Host() http://127.0.0.1:port / 127.0.0.1:port,给 APOLLO_META / meta 地址用
Set(ns, kv) 发布 namespace(SampleApp/default 维度),自动出新 release
SetWith(app, cluster, ns, kv) 同上,显式三维度
SetContent(ns, s) / SetContentWith(...) 以原文发布文件型 namespace(真实 YAML/JSON blob)
Publish(ns) / PublishWith(...) 不改值再发布一次(id+1、releaseKey 轮换、唤醒轮询)
Config(ns) / ConfigWith(...) 读回当前配置(拷贝),断言用
ReleaseKey(ns) / ReleaseKeyWith(...) 当前 releaseKey
NotificationID(ns) / NotificationIDWith(...) 当前通知 id(发布序号)
SetPollTimeout(d) 长轮询挂起时长,默认 30s(同真实 Apollo);测试建议 100ms 级

项目结构

miniapollo/
├── miniapollo.go              # 服务端核心
├── configs.go                 # /configs 端点
├── notifications.go           # /notifications 长轮询端点
├── services.go                # /services/config 服务发现
├── api.go                     # 公共 API(Set, SetWith, Publish, Config, ...)
├── miniapollo_test.go         # 自身单元测试(go test 直接运行)
└── example/
    ├── basic/
    │   └── main.go            # 基础示例:启动服务、发布配置、HTTP 查看
    └── agollo/
        └── main.go            # agollo 客户端示例:lifecycle + OnChange + OnNewestChange
运行测试与示例
# miniapollo 自身单元测试(纯标准库,无需第三方)
go test -v

# 基础示例
go run example/basic/main.go

# agollo 客户端示例(需要 agollo 依赖)
go run example/agollo/main.go

设计要点

  • 零依赖:miniapollo 自身纯标准库(net/http + encoding/json),无第三方引入。go.mod 中的 agollo 依赖仅用于 example/agollo/ 示例,库本身零依赖。
  • Go 1.22+:使用标准库 ServeMux 的方法+通配模式(GET /configs/{appId}/{cluster}/{namespace}),需要 Go 1.22 及以上。
  • 发布广播:Publish 唤醒所有长轮询等待者(容量 1 的 channel 投递,醒来后重算全量差异 —— 不丢事件的兜底,与 Redis 客户端 lib 的惯用法一致)。
  • 锁粒度:单一 sync.Mutex 保护存储与订阅表;handler 不在锁内做 IO。

Contributing

欢迎提 Issue 和 PR。

  • 新增端点模拟:确保与真实 Apollo Config Service 的 HTTP 响应格式一致(含错误体、状态码语义)
  • 新增客户端示例:放在 example/ 目录下,以 main.go 形式提供可运行程序
  • 单元测试:放在项目根目录的 _test.go 文件中(package miniapollo),go test 直接运行
  • 提交前运行 go test -v 和 go vet ./... 确保通过

License

MIT — 见 LICENSE。

Documentation

Overview

Package miniapollo provides an embedded Apollo configuration server for unit tests, in the spirit of miniredis: it speaks enough of the real Apollo config-service protocol that an actual Apollo client can be pointed at it.

Supported Apollo endpoints (see README for the full compatibility matrix):

GET /configs/{appId}/{cluster}/{namespace}
GET /notifications/v2?appId=&cluster=&notifications=[...]   (long polling)
GET /notifications?...                                      (v1 alias)
GET /services/config                                        (meta service discovery)

Index

Constants

View Source
const (
	DefaultAppID   = "SampleApp"
	DefaultCluster = "default"
)

Default dimensions used by the shortcut APIs (Set / Publish / Lookup).

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is the embedded Apollo configuration server. It is safe for concurrent use.

func NewServer

func NewServer() (*Server, error)

NewServer starts a mini Apollo server listening on 127.0.0.1 with a random port and returns it. Always pair with Close (or use StartT in tests).

func (*Server) Close

func (s *Server) Close() error

Close shuts the server down. Safe to call more than once.

func (*Server) Config

func (s *Server) Config(ns string) (map[string]string, bool)

Config returns the live configurations of ns (Default dimensions), or ok=false when it was never set. The map is a copy.

func (*Server) ConfigWith

func (s *Server) ConfigWith(appID, cluster, ns string) (map[string]string, bool)

ConfigWith is Config for explicit (appId, cluster).

func (*Server) Host

func (s *Server) Host() string

Host returns "host:port" of this server.

func (*Server) NotificationID

func (s *Server) NotificationID(ns string) int64

NotificationID returns the current notification id (release sequence number) of ns (Default dimensions); 0 when never published.

func (*Server) NotificationIDWith

func (s *Server) NotificationIDWith(appID, cluster, ns string) int64

NotificationIDWith is NotificationID for explicit (appId, cluster).

func (*Server) Publish

func (s *Server) Publish(ns string)

Publish re-publishes ns (Default dimensions) without changing values: a new releaseKey and a bumped notification id, waking long pollers. Useful to simulate "edited and released again" in tests.

func (*Server) PublishWith

func (s *Server) PublishWith(appID, cluster, ns string)

PublishWith is Publish for explicit (appId, cluster).

func (*Server) ReleaseKey

func (s *Server) ReleaseKey(ns string) (string, bool)

ReleaseKey returns the current releaseKey of ns (Default dimensions).

func (*Server) ReleaseKeyWith

func (s *Server) ReleaseKeyWith(appID, cluster, ns string) (string, bool)

ReleaseKeyWith is ReleaseKey for explicit (appId, cluster).

func (*Server) Set

func (s *Server) Set(ns string, kv map[string]string)

Set publishes namespace ns (DefaultAppID/DefaultCluster) with kv. It auto-publishes a new release, like a portal publish would.

func (*Server) SetContent

func (s *Server) SetContent(ns, content string)

SetContent publishes a file-type namespace from a raw string body (stored as configurations["content"]). Use this for real YAML/JSON blobs.

func (*Server) SetContentWith

func (s *Server) SetContentWith(appID, cluster, ns, content string)

SetContentWith is SetContent for explicit (appId, cluster).

func (*Server) SetPollTimeout

func (s *Server) SetPollTimeout(d time.Duration)

SetPollTimeout overrides how long /notifications holds requests with no changes. Default 30s (real Apollo default). Tests usually want ~100ms.

func (*Server) SetWith

func (s *Server) SetWith(appID, cluster, ns string, kv map[string]string)

SetWith publishes a namespace under explicit (appId, cluster) with kv. File-type namespaces (.yml/.yaml/.json/.txt) get their map flattened into a single "content" entry, exactly like real Apollo delivers them.

func (*Server) URL

func (s *Server) URL() string

URL returns the base URL, e.g. "http://127.0.0.1:49152". Point your client's meta server / APOLLO_META here.

Directories

Path Synopsis
example
agollo command
Package main demonstrates a REAL agollo v5 client connecting to the embedded miniapollo server — no real Apollo, no docker required.
Package main demonstrates a REAL agollo v5 client connecting to the embedded miniapollo server — no real Apollo, no docker required.
basic command
Package main demonstrates the typical miniapollo usage pattern: start the embedded server, publish namespaces, and inspect what any Apollo client would see through the HTTP endpoints.
Package main demonstrates the typical miniapollo usage pattern: start the embedded server, publish namespaces, and inspect what any Apollo client would see through the HTTP endpoints.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL