miniapollo

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 13 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

English | 中文

miniapollo

An embedded Apollo config center mock, built for unit tests.

Spinning up a real Apollo in tests is heavy (Meta + Config + Admin + MySQL). miniapollo is a pure standard-library, zero-dependency in-process HTTP server that faithfully simulates the Config Service behavior exposed to Apollo clients, so real Apollo clients (Go's agollo, the Java SDK, apollo-client, etc.) can connect without any modification.

Why miniapollo

When testing Apollo client logic, you typically face two options:

Approach Problem
Spin up real Apollo Meta + Config + Admin trio, MySQL, Eureka — too heavy for CI, high maintenance cost
Hand-rolled HTTP mock Easy to miss long-poll semantics, service discovery, file-namespace folding — mock drifts from real behavior

miniapollo's approach: replace real Apollo with an in-process HTTP server that only simulates what clients can observe (/configs, the /configfiles family, /notifications/v2 long-poll, /services/config service discovery). Real clients connect transparently; test code just calls Set / SetWith to publish configs.

Use cases
  • Unit tests for business logic that reads Apollo configs
  • Testing hot-reload behavior on config changes (OnChange / OnNewestChange callbacks)
  • Local development without an external Apollo instance
  • CI pipelines running integration tests where a full Apollo stack is impractical
m := miniapollo.StartT(t, "SampleApp", "default")  // start server, auto-closed on test end
m.Set("application", map[string]string{            // publish a namespace (auto new release)
    "rpc.timeout": "100",
})
m.Set("application.yml", map[string]string{"log": "info"})  // file-type namespaces work too

// Point your client / code under test at m.URL()
//   agollo:  env APOLLO_META=m.URL(), APP_ID=SampleApp
//   Java:    -Dapollo.meta=m.URL() -Dapp.id=SampleApp

What miniapollo simulates

Apollo behavior miniapollo Notes
GET /configs/{appId}/{cluster}/{namespace} ✅ Returns ApolloConfigDTO: configurations + releaseKey; unpublished → 404 (client falls back to local cache, same as real Apollo)
GET /configfiles/{appId}/{cluster}/{namespace} ✅ Configurations flattened to k=v text, text/plain (ConfigFileController properties form)
GET /configfiles/json/{appId}/{cluster}/{namespace} ✅ Configurations as JSON — the form agollo requests at first sync; file-type namespaces arrive as {"content": "<file>"}; unpublished → 404 with empty body (bare 404 here, unlike /configs, same as real Apollo)
GET /configfiles/raw/{appId}/{cluster}/{namespace} ✅ File content verbatim; Content-Type by format: .json → application/json, .yml/.yaml → application/yaml, else text/plain; properties namespaces flatten to k=v
properties / .yml / .yaml / .json / .txt namespaces ✅ File-type namespaces folded into configurations["content"] the real way; .json is JSON-serialized map, .yml/.yaml is flat key: value, .txt is key=value
GET /notifications/v2 long-poll ✅ Client id behind → immediate 200 with every changed namespace (real Apollo's mismatch check answers all at once; its publish wake answers the released one — here the wake re-diffs, a superset); no change → request hangs until publish or timeout; timeout returns 404 ("no change", client continues next round)
trigger parameter (v2) ✅ Specifies hang duration in seconds, clamped to 1s..60s; invalid values fall back to default, matching real server behavior
Non-existent namespaces in notifications ✅ Server treats them as id -1: client initial -1 equals it → no notification triggered (real Apollo semantics)
notificationId semantics ✅ Increments by 1 on each publish, counted independently per (appId, cluster, namespace)
releaseKey format ✅ yyyyMMddHHmmss-<12hex>, rotated on each publish
GET /notifications (v1) ✅ Same logic as v2 (real client compatibility layer)
GET /services/config (Meta service discovery) ✅ Returns a single APOLLO-CONFIGSERVICE instance with homepageUrl pointing to this server — clients given only the meta address can still find the config service
Default cluster=default ✅ Notification requests missing cluster are treated as default
404/400 error bodies ✅ {"message": "..."} (Spring-style error body)
Client disconnect ✅ Hung long-polls released via r.Context().Done(), no goroutine leaks
Concurrent publishes / multi-client polling ✅ Publish atomically wakes all waiters; on wake, full diff is recomputed — no events lost
Not simulated (differences from real Apollo)
  • Portal / OpenAPI (/openapi/v1/*): management-plane APIs, not needed for client tests.
  • Grayscale release, publish approval, public namespace association: config service doesn't expose these concepts to clients.
  • Set is an implicit publish (no "modified but unpublished" intermediate state) — one less step in tests, more direct semantics; for realistic scenarios use SetContent + Publish.
  • Admin Service /services/admin: no consumers, omitted.
  • Multi-instance load balancing within a cluster: a single instance represents the cluster.

Design principle: only simulate what clients can observe. Anything invisible to clients (Portal UI, DB, Eureka cluster topology) is intentionally omitted — this is the fundamental difference between miniapollo and "spinning up real Apollo".

Verified real clients

Client Status Notes
agollo v5 (Go SDK) ✅ Example passes Works out of the box (v5.0.0); first sync lands real values immediately; see example/agollo/main.go (go run example/agollo/main.go)
agollo v4 (Go SDK) ✅ Test passes v4.4.0 incl. strict MustStart — first sync needs /configfiles/json, previously failed with "no config was read"; see example/agollo/agollo_test.go (go test ./example/agollo)

agollo integration notes (same applies to other clients): set AppConfig.IP to m.URL() (meta address), and keep AppID/Cluster/NamespaceName consistent with the publish dimensions. miniapollo's /services/config registers itself as the config service, so clients need no extra address.

AddChangeListener / OnNewestChange

agollo v5's AddChangeListener accepts a storage.ChangeListener interface (requiring both OnChange and OnNewestChange methods), not a bare func. The two callbacks differ:

Callback Event type Trigger timing Data content
OnChange *storage.ChangeEvent On each detected config change Only changed keys; ConfigChange.OldValue/NewValue are interface{} (need .(string) assertion)
OnNewestChange *storage.FullChangeEvent On each config refresh (before OnChange) Full key-value snapshot of the namespace; Changes is map[string]interface{}

See example/agollo/main.go for a complete example.

Installation & Usage

go get github.com/odkrjs/miniapollo
Unit tests
func TestFetchConfig(t *testing.T) {
    m := miniapollo.StartT(t, "shop", "dev")
    m.Set("application", map[string]string{"rpc.timeout": "100"})
    m.SetContent("layout.yaml", "items: [a, b]")

    // Assert directly on the endpoint, or inject m.URL() into your real client
    resp, err := http.Get(m.URL() + "/configs/shop/dev/application")
    require.NoError(t, err)
    defer resp.Body.Close()
    // ...json.Decode / assertions
}
Testing the "config change push" path (long-poll wake-up)
func TestOnConfigChange(t *testing.T) {
    m := miniapollo.StartT(t, "SampleApp", "default")
    m.SetPollTimeout(150 * time.Millisecond)          // don't wait 30s in tests
    m.Set("application", map[string]string{"k": "v"})

    got := make(chan struct{})
    go func() {                                       // your client: hang on long-poll
        resp, _ := http.Get(m.URL() + "/notifications/v2?appId=SampleApp&cluster=default" +
            `&notifications=[{"namespaceName":"application","notificationId":1}]`)
        if resp.StatusCode == 200 { close(got) }       // woken up → received 200
    }()

    time.Sleep(60 * time.Millisecond)                 // let the client hang first
    m.Set("application", map[string]string{"k": "v2"}) // publish → immediate wake-up

    select {
    case <-got: // success
    case <-time.After(3 * time.Second):
        t.Fatal("long poll was not woken by publish")
    }
}
Connecting a real client (agollo example)
m := miniapollo.StartT(t, "SampleApp", "default")
m.Set("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 address, miniapollo handles service discovery
    }, nil
})
// client.GetValue("k") → "v"
// client.AddChangeListener(...) → OnChange / OnNewestChange callbacks

A fully runnable example is in example/agollo/main.go (go run example/agollo/main.go), covering three scenarios:

  1. Full lifecycle (service discovery → initial fetch → hot reload)
  2. AddChangeListener → OnChange (OldValue/NewValue delta callback)
  3. OnNewestChange (full snapshot callback)
Custom dimensions at server creation

All the shortcut APIs (Set / SetContent / Publish / Config / LoadDir / ...) publish under the dimensions specified at server creation. NewServer(appID, cluster) / StartT(t, appID, cluster) let you set them once, instead of repeating SetWith on every call:

m := miniapollo.StartT(t, "shop", "dev")
m.Set("application", map[string]string{"rpc.timeout": "100"})
// served at /configs/shop/dev/application — no SetWith repetition
Loading local config files (file name = namespace)

LoadDir publishes every recognized config file in a directory as a namespace — handy when you already keep configs as files on disk and want the embedded server to serve them:

m := miniapollo.StartT(t, "shop", "dev")

loaded, err := m.LoadDir("./configs")
// loaded == []string{"application", "layout.yml", "rules.json"}

Naming follows real Apollo conventions:

File Namespace Content
application.properties application parsed key=value lines (flat kv map)
layout.yml / .yaml layout.yml raw file text as configurations["content"]
rules.json rules.json raw file text as content
notes.txt notes.txt raw file text as content

Sub-directories, dot-files and other extensions are skipped. Each file is published immediately (fresh release + notification), so parked long-pollers are woken. LoadFile does the same for a single file; LoadDirWith / LoadFileWith take explicit dimensions.

API Reference

Method Description
NewServer(appID, cluster) (*Server, error) Start server (127.0.0.1 random port), shortcut APIs use the given dimensions
StartT(t, appID, cluster) Test helper: start + t.Cleanup(Close), sets dimensions
Close() Close; safe to call repeatedly
URL() / Host() http://127.0.0.1:port / 127.0.0.1:port, for APOLLO_META / meta address
Set(ns, kv) Publish namespace (server's dimensions), auto new release
SetWith(app, cluster, ns, kv) Same, with explicit three dimensions
SetContent(ns, s) / SetContentWith(...) Publish file-type namespace as raw content (real YAML/JSON blob)
LoadDir(dir) ([]string, error) Load every config file in dir as a namespace (file name = namespace); returns sorted list
LoadDirWith(app, cluster, dir) Same, for explicit dimensions
LoadFile(file) (string, error) / LoadFileWith(...) Load a single config file; returns the namespace it was published as
Publish(ns) / PublishWith(...) Publish again without changing values (id+1, releaseKey rotation, wake polls)
Config(ns) / ConfigWith(...) Read back current config (copy), for assertions
ReleaseKey(ns) / ReleaseKeyWith(...) Current releaseKey
NotificationID(ns) / NotificationIDWith(...) Current notification id (publish sequence number)
SetPollTimeout(d) Long-poll hang duration, default 30s (same as real Apollo); recommend 100ms range for tests

Project Structure

miniapollo/
├── miniapollo.go              # Server core (NewServer)
├── configs.go                 # /configs endpoint
├── configfiles.go             # /configfiles family: properties / json / raw (agollo first-sync)
├── notifications.go           # /notifications long-poll endpoint
├── services.go                # /services/config service discovery
├── api.go                     # Public API (Set, SetWith, Publish, Config, ...)
├── loader.go                  # LoadDir / LoadFile: load local config files as namespaces
├── miniapollo_test.go         # Unit tests + StartT test helper (run with go test directly)
├── configfiles_test.go        # Unit tests for the /configfiles family
├── loader_test.go             # Unit tests for LoadDir / LoadFile / dimensions
└── example/
    ├── basic/
    │   └── main.go            # Basic example: specified dims + publish + LoadDir
    └── agollo/
        ├── main.go            # agollo v5 client example: lifecycle + OnChange + OnNewestChange
        └── agollo_test.go     # agollo v4 MustStart first-sync test (go test ./example/agollo)
Running tests & examples
# miniapollo unit tests (pure stdlib, no third-party deps)
go test -v

# Basic example
go run example/basic/main.go

# agollo client example (requires agollo dependency)
go run example/agollo/main.go

# agollo v4 strict first-sync test (MustStart, requires agollo dependency)
go test -race ./example/agollo

Design Highlights

  • Zero dependencies: miniapollo itself uses only the standard library (net/http + encoding/json), no third-party imports. The agollo dependency in go.mod is solely for the example/agollo/ example; the library itself has zero dependencies.
  • Go 1.22+: Uses the standard library ServeMux method + wildcard patterns ( GET /configs/{appId}/{cluster}/{namespace}), requiring Go 1.22 or later.
  • Publish broadcast: Publish wakes all long-poll waiters (capacity-1 channel delivery; on wake, full diff is recomputed — a safety net against lost events, consistent with the Redis client lib idiom).
  • Lock granularity: A single sync.Mutex protects storage and the subscription table; handlers never do IO while holding the lock.

Contributing

Issues and PRs are welcome.

  • New endpoint simulation: ensure HTTP response format matches real Apollo Config Service (including error bodies and status code semantics)
  • New client examples: place under example/ as a runnable main.go program
  • Unit tests: place in _test.go files at the project root (package miniapollo), runnable via go test
  • Run go test -v and go vet ./... before submitting to ensure they pass

License

MIT — see 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 /configfiles/{appId}/{cluster}/{namespace}          (k=v properties text)
GET /configfiles/json/{appId}/{cluster}/{namespace}       (configurations as JSON,
                                                          agollo first-sync form)
GET /configfiles/raw/{appId}/{cluster}/{namespace}        (file content as-is)
GET /notifications/v2?appId=&cluster=&notifications=[...]   (long polling)
GET /notifications?...                                      (v1 alias)
GET /services/config                                        (meta service discovery)

Index

Constants

This section is empty.

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(appID, cluster string) (*Server, error)

NewServer starts a mini Apollo server listening on 127.0.0.1 with a random port. All shortcut APIs (Set / SetContent / Publish / Config / ReleaseKey / NotificationID / LoadDir / LoadFile / ...) publish under the given (appID, cluster):

m, _ := miniapollo.NewServer("shop", "dev")
m.Set("application", map[string]string{"rpc.timeout": "100"})
// served at /configs/shop/dev/application

Either value must be non-empty. 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 (server's 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) LoadDir added in v1.0.1

func (s *Server) LoadDir(dir string) ([]string, error)

LoadDir publishes every recognized config file in dir as a namespace under the server's dimensions. The file's base name is the namespace name:

application.properties -> namespace "application"     (parsed: key=value lines)
application.yml        -> namespace "application.yml"  (raw file as content)
application.yaml       -> namespace "application.yaml"
application.json       -> namespace "application.json"
*.txt                  -> namespace "<name>.txt"

Sub-directories, dot-files and files with other extensions are skipped. It returns the namespaces loaded, in sorted order. Each file is published immediately (a fresh release each), so clients polling right after LoadDir see them all — and long-pollers parked on those namespaces are woken.

m, _ := miniapollo.NewServer("shop", "dev")
ns, err := m.LoadDir("./configs")   // []{"application", "layout.yml", ...}

func (*Server) LoadDirWith added in v1.0.1

func (s *Server) LoadDirWith(appID, cluster, dir string) ([]string, error)

LoadDirWith is LoadDir for explicit (appId, cluster).

func (*Server) LoadFile added in v1.0.1

func (s *Server) LoadFile(file string) (string, error)

LoadFile publishes a single config file under the server's dimensions, with the same namespace-naming rules as LoadDir. Returns the namespace the file was published as.

func (*Server) LoadFileWith added in v1.0.1

func (s *Server) LoadFileWith(appID, cluster, file string) (string, error)

LoadFileWith is LoadFile for explicit (appId, cluster).

func (*Server) NotificationID

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

NotificationID returns the current notification id (release sequence number) of ns (server's 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 (server's 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 (server's 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 with kv under the server's dimensions (the ones given to NewServer / StartT). 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"]) under the server's dimensions. 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 patterns: start the embedded server, publish namespaces, load a config directory, and inspect what any Apollo client would see through the HTTP endpoints.
Package main demonstrates the typical miniapollo usage patterns: start the embedded server, publish namespaces, load a config directory, 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