-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ultilize Module Event Listener (#133)
* finish unit test * fix grammar error * add a new error named ErrIgnoreChange of updateEvent to tell the caller not to notify listener * add doc and example Co-authored-by: Donghong Huang <[email protected]>
- Loading branch information
1 parent
fb897c6
commit a0ffe95
Showing
9 changed files
with
286 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module_event.go keep a file under archaius's management, and watch module changes, | ||
so that if there are multiple changes under the module **test.person**, | ||
the events will be triggered once, and listener will receive the event list | ||
|
||
``` | ||
go build module_event.go | ||
./module_event | ||
``` | ||
|
||
change configs under **test.person** in the module_event.yaml | ||
|
||
check the stdout to see events |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/go-chassis/go-archaius" | ||
"github.com/go-chassis/go-archaius/event" | ||
"github.com/go-chassis/openlog" | ||
) | ||
|
||
//Listener is a struct used for Event listener | ||
type Person struct { | ||
Name string `yaml:"name"` | ||
Age int `yaml:"age"` | ||
Favorites map[string]string `yaml:"favorites"` | ||
} | ||
type Listener struct { | ||
Person Person `yaml:"test.person"` | ||
} | ||
|
||
//Event is a method for QPS event listening | ||
func (e *Listener) Event(events []*event.Event) { | ||
for i, ev := range events { | ||
openlog.GetLogger().Info(fmt.Sprintf("%dth event:%+v", i, ev)) | ||
} | ||
archaius.UnmarshalConfig(e) | ||
} | ||
|
||
func main() { | ||
err := archaius.Init(archaius.WithRequiredFiles([]string{ | ||
"./module_event.yaml", | ||
})) | ||
if err != nil { | ||
openlog.Error("Error:" + err.Error()) | ||
return | ||
} | ||
lis := &Listener{} | ||
archaius.RegisterModuleListener(lis, "test.person") | ||
for { | ||
log.Printf("%+v\n", lis) | ||
time.Sleep(5 * time.Second) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
test: | ||
person: | ||
name: tom | ||
age: 24 | ||
favorites: | ||
food: dumplins | ||
color: red |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.