The main file prepares a microservice deployment environment via a call to the deployServices helper function. The idea here is to string together the services in such a manner so that tracing a request through the system will yield an interesting trace graph. Let's see how this is done.
First, the helper starts three Provider instances and keeps track of their addresses:
var err error providerAddrs := make([]string, 3) for i := 0; i < len(providerAddrs); i++ { provider := service.NewProvider(fmt.Sprintf("vendor-%d", i)) if providerAddrs[i], err = provider.Serve(ctx); err != nil { return nil, err } }
Then, it starts an Aggregator instance and sets it up to connect to providers 1 and 2 from the preceding list:
aggr1 := service.NewAggregator("aggr-1", providerAddrs[1:]) aggr1Addr, err := aggr1.Serve(ctx) if err != nil { return nil, err }
Following that, it instantiates yet another Aggregator type and connects it to provider 0 and the aggregator we just created:
aggr0 := service.NewAggregator("aggr-0", []string{providerAddrs[0], aggr1Addr}) aggr0Addr, err := aggr0.Serve(ctx) if err != nil { return nil, err }
Finally, a Gateway instance is created with the preceding aggregator as its target and returned to the caller:
The Gateway instance that's returned by the deployServices function is used by runMain to trigger the execution of a quote query that marks the beginning of a new request trace:
func runMain(ctx context.Context) error { gw, err := deployServices(ctx) if err != nil { return err } defer func() { _ = gw.Close() }() res, err := gw.CollectQuotes(ctx, "example") if err != nil { return err } fmt.Printf("Collected the following quotes:\n") for k, v := range res { fmt.Printf(" %q: %3.2f\n", k, v) } return nil }
In the following section, we will be hooking up a tracer implementation to our code so that we can capture and visualize the request traces that our code generates.