MiSSy (Microservice Support System) is an SDK for creating REST services that talk to each other. It provides the following functionality:
- Routing with gorrila/mux
- Logging
- Configuration with environment variables
- Monitoring with Prometheus
- /info /health
- Service discovery
- REST client
- Security
Example for a simple hello world service
Create a .missy.yml
config file in the root directory of your service with the following content
name: hello
# main.go
package main
import (
"github.com/microdevs/missy/service"
"net/http"
"fmt"
)
func main() {
s := service.New("hello")
s.HandleFunc("/hello/{name}", HelloHandler).Methods("GET")
s.HandleFunc("/json", JsonHandler).Methods("GET")
s.Start()
}
func HelloHandler(w http.ResponseWriter, r *http.Request) {
vars := service.Vars(r)
w.Write([]byte(fmt.Sprintf("Hello %s", vars["name"])))
}
// marshalling example
type MyType struct {
A int
B string
}
func JsonHandler(w http.ResponseWriter, r *http.Request) {
mytype := MyType{
A: 123,
B: "Hello world",
}
data.Marshal(w, r, mytype)
}
// response body {A:123,B:"Hello world"}
go run main.go
curl "http://localhost:8088/hello/microdevs"
Get Prometheus Metrics (all metrics are exposed on 8090 as default). To change this please set METRICS_LISTEN_PORT
env. var.:
curl http://localhost:8090/metrics
http://localhost:8090/info
Response:
Name hello
Uptime 14.504883092s
http://localhost:8090/health
Response:
OK
Use messaging.Reader and messaging.Writer to subscribe and publish messages. It uses kafka underneath. Example usage:
Reader with brokers hosts, group-id and topic
reader := messaging.NewReader([]string{"localhost:9092"}, "group-id", "topic")
err := reader.Read(func(msg Message) error{
// do something with msg
// return nil or error (if commit should not happen)
})
// remember to close reader after use
defer reader.Close()
#####Dead letter queue
If you need to save messages that couldn't be processed, you have to use constructor NewReaderWithDLQ which takes name of DLQ topic as additional parameter.
#####Writer with brokers hosts and topic
writer := messaging.NewWriter([]string{"localhost:9092"}, "topic")
err := writer.Write([]byte("key"), []byte("value"))
// remember to close writer after use
defer writer.Close()