applicationAn Application is a collection of microservices that run in a single process and share the same lifecycle. The ability to run multiple microservices in a single executable is one of the big benefits of the Microbus approach. It is much easier to develop, debug and test a system when all its microservices can be run in a single debuggable process with minimal memory requirements.
Here’s a simple example of an Application hosting two microservices: a “Hello, World!” microservice and the HTTP ingress microservice.
package main
import (
"net/http"
"os"
"github.com/microbus-io/fabric/application"
"github.com/microbus-io/fabric/connector"
"github.com/microbus-io/fabric/coreservices/httpingress"
)
func main() {
hello := connector.New("helloworld")
hello.Subscribe("GET", "/", func(w http.ResponseWriter, r *http.Request) error {
w.Write([]byte("Hello, World!"))
return nil
})
app := application.New()
app.Add(
httpingress.NewService(),
hello,
)
err := app.Run()
if err != nil {
os.Exit(1)
}
}
Microservices are added to an Application using the Add method. The microservices are not automatically started. A call to Startup starts up all added microservices that are not already started. Conversely, a call to Shutdown shuts down all added microservices that are not already shut down.
The convenient Run method starts up all microservices, waits for an interrupt and then shuts down all microservices. Interrupt allows to programmatically interrupt a running Application.
RunInTest runs the application during the lifetime of a unit test.