https://github.com/hyperjiang/lunar
Probably the most elegant ctrip apollo client in golang. This library has no third-party dependency.
https://github.com/hyperjiang/lunar
apollo golang
Last synced: about 1 year ago
JSON representation
Probably the most elegant ctrip apollo client in golang. This library has no third-party dependency.
- Host: GitHub
- URL: https://github.com/hyperjiang/lunar
- Owner: hyperjiang
- License: mit
- Created: 2020-04-02T01:34:44.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-10-11T08:34:07.000Z (over 1 year ago)
- Last Synced: 2025-04-08T22:41:34.510Z (about 1 year ago)
- Topics: apollo, golang
- Language: Go
- Homepage:
- Size: 36.1 KB
- Stars: 12
- Watchers: 1
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# lunar
[](https://godoc.org/github.com/hyperjiang/lunar)
[](https://github.com/hyperjiang/lunar/actions/workflows/ci.yml)
[](https://goreportcard.com/report/github.com/hyperjiang/lunar)
[](https://codecov.io/gh/hyperjiang/lunar)
[](https://github.com/hyperjiang/lunar/releases)
Probably the most elegant ctrip apollo client in golang. This library has no third-party dependency.
Ctrip Apollo: https://github.com/ctripcorp/apollo
Default settings of `lunar`:
- default namespace is `application`
- default server is `localhost:8080`
- default http client timeout is `90s`
*Require golang version >= 1.16 after v0.6.0*
## Usage
```
import "github.com/hyperjiang/lunar"
key := "foo"
app := lunar.New("myAppID", lunar.WithServer("localhost:8080"))
// get value of key in default namespace
app.GetValue(key)
// get value of key in namespace ns
app.GetValueInNamespace(key, "ns")
// get all the items in default namespace
app.GetItems()
// get all the items in namespace ns
app.GetItemsInNamespace("ns")
// get the content of ns namespace, if the format of ns is properties then will return json string
app.GetContent("ns")
// it will fetch items from apollo directly without reading local cache
app.GetNamespaceFromApollo("ns")
// watch changes of given namespaces
watchChan, errChan := app.Watch("ns1", "ns2", ...)
for {
select {
case n := <-watchChan:
fmt.Println(n)
case <-errChan:
app.Stop() // stop watcher
return
}
}
```
## Logging
`lunar` does not write logs by default, if you want to see logs for debugging, you can replace it with any logger which implements `lunar.Logger` interface.
`lunar` also provide a simple logger `lunar.Printf` which writes to stdout:
```
app := lunar.New("myAppID", lunar.WithServer("localhost:8080"), lunar.WithLogger(lunar.Printf))
```
Or you can use `UseLogger` method:
```
app.UseLogger(lunar.Printf)
```
## Caching
`lunar` use memory cache by default, you can replace it with any cache which implements `lunar.Cache` interface.
`lunar` also provide a file cache `lunar.FileCache` which use files for caching:
```
app := lunar.New("myAppID")
app.UseCache(lunar.NewFileCache("myAppID", "/tmp"))
```
## Enable Access Key
Starting from v1.6.0, apollo supports access key feature, you can use `WithAccessKeySecret` to set the secret:
```
app := lunar.New(
"myAppID",
lunar.WithServer("localhost:8080"),
lunar.WithAccessKeySecret("mySecret"),
)
```