Skip to content

Commit

Permalink
Add ConfigOptions so clients find correct ports
Browse files Browse the repository at this point in the history
Signed-off-by: Corey Boornazian <coreyb220@gmail.com>
  • Loading branch information
cboornaz17 committed Jul 25, 2018
1 parent fe43631 commit 43a5d22
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 50 deletions.
11 changes: 1 addition & 10 deletions examples/hotrod/cmd/customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var customerCmd = &cobra.Command{
zapLogger := logger.With(zap.String("service", "customer"))
logger := log.NewFactory(zapLogger)
server := customer.NewServer(
net.JoinHostPort(customerOptions.serverInterface, strconv.Itoa(customerOptions.serverPort)),
net.JoinHostPort("0.0.0.0", strconv.Itoa(customerPort)),
tracing.Init("customer", metricsFactory.Namespace("customer", nil), logger, jAgentHostPort),
metricsFactory,
logger,
Expand All @@ -45,16 +45,7 @@ var customerCmd = &cobra.Command{
},
}

var (
customerOptions struct {
serverInterface string
serverPort int
}
)

func init() {
RootCmd.AddCommand(customerCmd)

customerCmd.Flags().StringVarP(&customerOptions.serverInterface, "bind", "", "0.0.0.0", "interface to which the Customer server will bind")
customerCmd.Flags().IntVarP(&customerOptions.serverPort, "port", "p", 8081, "port on which the Customer server will listen")
}
11 changes: 1 addition & 10 deletions examples/hotrod/cmd/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var driverCmd = &cobra.Command{
zapLogger := logger.With(zap.String("service", "driver"))
logger := log.NewFactory(zapLogger)
server := driver.NewServer(
net.JoinHostPort(driverOptions.serverInterface, strconv.Itoa(driverOptions.serverPort)),
net.JoinHostPort("0.0.0.0", strconv.Itoa(driverPort)),
tracing.Init("driver", metricsFactory.Namespace("driver", nil), logger, jAgentHostPort),
metricsFactory,
logger,
Expand All @@ -45,16 +45,7 @@ var driverCmd = &cobra.Command{
},
}

var (
driverOptions struct {
serverInterface string
serverPort int
}
)

func init() {
RootCmd.AddCommand(driverCmd)

driverCmd.Flags().StringVarP(&driverOptions.serverInterface, "bind", "", "0.0.0.0", "interface to which the driver server will bind")
driverCmd.Flags().IntVarP(&driverOptions.serverPort, "port", "p", 8082, "port on which the driver server will listen")
}
17 changes: 8 additions & 9 deletions examples/hotrod/cmd/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,26 @@ var frontendCmd = &cobra.Command{
Short: "Starts Frontend service",
Long: `Starts Frontend service.`,
RunE: func(cmd *cobra.Command, args []string) error {

options.FrontendHostPort = net.JoinHostPort("0.0.0.0", strconv.Itoa(frontendPort))
options.DriverHostPort = net.JoinHostPort("0.0.0.0", strconv.Itoa(driverPort))
options.CustomerHostPort = net.JoinHostPort("0.0.0.0", strconv.Itoa(customerPort))
options.RouteHostPort = net.JoinHostPort("0.0.0.0", strconv.Itoa(routePort))

zapLogger := logger.With(zap.String("service", "frontend"))
logger := log.NewFactory(zapLogger)
server := frontend.NewServer(
net.JoinHostPort(frontendOptions.serverInterface, strconv.Itoa(frontendOptions.serverPort)),
options,
tracing.Init("frontend", metricsFactory.Namespace("frontend", nil), logger, jAgentHostPort),
logger,
)
return logError(zapLogger, server.Run())
},
}

var (
frontendOptions struct {
serverInterface string
serverPort int
}
)
var options frontend.ConfigOptions

func init() {
RootCmd.AddCommand(frontendCmd)

frontendCmd.Flags().StringVarP(&frontendOptions.serverInterface, "bind", "", "0.0.0.0", "interface to which the frontend server will bind")
frontendCmd.Flags().IntVarP(&frontendOptions.serverPort, "port", "p", 8080, "port on which the frontend server will listen")
}
30 changes: 30 additions & 0 deletions examples/hotrod/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ var (
fixDBConnDelay time.Duration
fixDBConnDisableMutex bool
fixRouteWorkerPoolSize int

customerPort int
driverPort int
frontendPort int
routePort int
Routesdf int
)

// RootCmd represents the base command when called without any subcommands
Expand All @@ -57,11 +63,19 @@ func Execute() {
}

func init() {
Routesdf = 8000
RootCmd.PersistentFlags().StringVarP(&metricsBackend, "metrics", "m", "expvar", "Metrics backend (expvar|prometheus)")
RootCmd.PersistentFlags().StringVarP(&jAgentHostPort, "jaeger-agent.host-port", "a", "0.0.0.0:6831", "String representing jaeger-agent UDP host:port, or jaeger-collector HTTP endpoint address, e.g. http://localhost:14268/api/traces.")
RootCmd.PersistentFlags().DurationVarP(&fixDBConnDelay, "fix-db-query-delay", "D", 300*time.Millisecond, "Average lagency of MySQL DB query")
RootCmd.PersistentFlags().BoolVarP(&fixDBConnDisableMutex, "fix-disable-db-conn-mutex", "M", false, "Disables the mutex guarding db connection")
RootCmd.PersistentFlags().IntVarP(&fixRouteWorkerPoolSize, "fix-route-worker-pool-size", "W", 3, "Default worker pool size")

// Add flags to choose ports for services
RootCmd.PersistentFlags().IntVarP(&customerPort, "customer-service-port", "c", 8081, "Port for customer service")
RootCmd.PersistentFlags().IntVarP(&driverPort, "driver-service-port", "d", 8082, "Port for driver service")
RootCmd.PersistentFlags().IntVarP(&frontendPort, "frontend-service-port", "f", 8080, "Port for frontend service")
RootCmd.PersistentFlags().IntVarP(&routePort, "route-service-port", "r", 8083, "Port for routing service")

rand.Seed(int64(time.Now().Nanosecond()))
logger, _ = zap.NewDevelopment(zap.AddStacktrace(zapcore.FatalLevel))
cobra.OnInitialize(onInitialize)
Expand Down Expand Up @@ -90,6 +104,22 @@ func onInitialize() {
logger.Info("fix: overriding route worker pool size", zap.Int("old", config.RouteWorkerPoolSize), zap.Int("new", fixRouteWorkerPoolSize))
config.RouteWorkerPoolSize = fixRouteWorkerPoolSize
}

if customerPort != 8081 {
logger.Info("changing customer service port", zap.Int("old", 8081), zap.Int("new", customerPort))
}

if driverPort != 8082 {
logger.Info("changing driver service port", zap.Int("old", 8082), zap.Int("new", driverPort))
}

if frontendPort != 8080 {
logger.Info("changing frontend service port", zap.Int("old", 8080), zap.Int("new", frontendPort))
}

if routePort != 8083 {
logger.Info("changing route service port", zap.Int("old", 8083), zap.Int("new", routePort))
}
}

func logError(logger *zap.Logger, err error) error {
Expand Down
11 changes: 1 addition & 10 deletions examples/hotrod/cmd/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,15 @@ var routeCmd = &cobra.Command{
zapLogger := logger.With(zap.String("service", "route"))
logger := log.NewFactory(zapLogger)
server := route.NewServer(
net.JoinHostPort(routeOptions.serverInterface, strconv.Itoa(routeOptions.serverPort)),
net.JoinHostPort("0.0.0.0", strconv.Itoa(routePort)),
tracing.Init("route", metricsFactory.Namespace("route", nil), logger, jAgentHostPort),
logger,
)
return logError(zapLogger, server.Run())
},
}

var (
routeOptions struct {
serverInterface string
serverPort int
}
)

func init() {
RootCmd.AddCommand(routeCmd)

routeCmd.Flags().StringVarP(&routeOptions.serverInterface, "bind", "", "0.0.0.0", "interface to which the Route server will bind")
routeCmd.Flags().IntVarP(&routeOptions.serverPort, "port", "p", 8083, "port on which the Route server will listen")
}
1 change: 1 addition & 0 deletions examples/hotrod/services/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ var (

// RouteCalcDelayStdDev is standard deviation
RouteCalcDelayStdDev = RouteCalcDelay / 4

)
7 changes: 5 additions & 2 deletions examples/hotrod/services/customer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,28 @@ type Client struct {
tracer opentracing.Tracer
logger log.Factory
client *tracing.HTTPClient
hostPort string
}

// NewClient creates a new customer.Client
func NewClient(tracer opentracing.Tracer, logger log.Factory) *Client {
func NewClient(tracer opentracing.Tracer, logger log.Factory, hostPort string) *Client {
return &Client{
tracer: tracer,
logger: logger,
client: &tracing.HTTPClient{
Client: &http.Client{Transport: &nethttp.Transport{}},
Tracer: tracer,
},
hostPort: hostPort,
}
}

// Get implements customer.Interface#Get as an RPC
func (c *Client) Get(ctx context.Context, customerID string) (*Customer, error) {
c.logger.For(ctx).Info("Getting customer", zap.String("customer_id", customerID))

url := fmt.Sprintf("http://127.0.0.1:8081/customer?customer=%s", customerID)
url := fmt.Sprintf("http://" + c.hostPort + "/customer?customer=%s", customerID)
fmt.Println(url)
var customer Customer
if err := c.client.GetJSON(ctx, "/customer", url, &customer); err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions examples/hotrod/services/driver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Client struct {
}

// NewClient creates a new driver.Client
func NewClient(tracer opentracing.Tracer, logger log.Factory) *Client {
func NewClient(tracer opentracing.Tracer, logger log.Factory, hostPort string) *Client {
channelOpts := &tchannel.ChannelOptions{
//Logger: logger,
//StatsReporter: statsReporter,
Expand All @@ -47,7 +47,7 @@ func NewClient(tracer opentracing.Tracer, logger log.Factory) *Client {
logger.Bg().Fatal("Cannot create TChannel", zap.Error(err))
}
clientOpts := &thrift.ClientOptions{
HostPort: "127.0.0.1:8082",
HostPort: hostPort,
}
thriftClient := thrift.NewClient(ch, "driver", clientOpts)
client := driver.NewTChanDriverClient(thriftClient)
Expand Down
6 changes: 5 additions & 1 deletion examples/hotrod/services/frontend/best_eta.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type bestETA struct {
route route.Interface
pool *pool.Pool
logger log.Factory
options ConfigOptions
}

// Response contains ETA for a trip.
Expand All @@ -46,19 +47,22 @@ type Response struct {
ETA time.Duration
}

func newBestETA(tracer opentracing.Tracer, logger log.Factory) *bestETA {
func newBestETA(tracer opentracing.Tracer, logger log.Factory, options ConfigOptions) *bestETA {
return &bestETA{
customer: customer.NewClient(
tracer,
logger.With(zap.String("component", "customer_client")),
options.CustomerHostPort,
),
driver: driver.NewClient(
tracer,
logger.With(zap.String("component", "driver_client")),
options.DriverHostPort,
),
route: route.NewClient(
tracer,
logger.With(zap.String("component", "route_client")),
options.RouteHostPort,
),
pool: pool.New(config.RouteWorkerPoolSize),
logger: logger,
Expand Down
15 changes: 12 additions & 3 deletions examples/hotrod/services/frontend/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,26 @@ type Server struct {
assetFS http.FileSystem
}

// ConfigOptions used to make sure service clients
// can find correct server ports
type ConfigOptions struct {
FrontendHostPort string
DriverHostPort string
CustomerHostPort string
RouteHostPort string
}

// NewServer creates a new frontend.Server
func NewServer(hostPort string, tracer opentracing.Tracer, logger log.Factory) *Server {
func NewServer(options ConfigOptions, tracer opentracing.Tracer, logger log.Factory) *Server {
assetFS, err := fs.New()
if err != nil {
logger.Bg().Fatal("cannot import web assets", zap.Error(err))
}
return &Server{
hostPort: hostPort,
hostPort: options.FrontendHostPort,
tracer: tracer,
logger: logger,
bestETA: newBestETA(tracer, logger),
bestETA: newBestETA(tracer, logger, options),
assetFS: assetFS,
}
}
Expand Down
10 changes: 7 additions & 3 deletions examples/hotrod/services/route/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"net/http"
"net/url"

"fmt"

"github.com/opentracing-contrib/go-stdlib/nethttp"
"github.com/opentracing/opentracing-go"
"go.uber.org/zap"
Expand All @@ -32,17 +34,19 @@ type Client struct {
tracer opentracing.Tracer
logger log.Factory
client *tracing.HTTPClient
hostPort string
}

// NewClient creates a new route.Client
func NewClient(tracer opentracing.Tracer, logger log.Factory) *Client {
func NewClient(tracer opentracing.Tracer, logger log.Factory, hostPort string) *Client {
return &Client{
tracer: tracer,
logger: logger,
client: &tracing.HTTPClient{
Client: &http.Client{Transport: &nethttp.Transport{}},
Tracer: tracer,
},
hostPort: hostPort,
}
}

Expand All @@ -53,8 +57,8 @@ func (c *Client) FindRoute(ctx context.Context, pickup, dropoff string) (*Route,
v := url.Values{}
v.Set("pickup", pickup)
v.Set("dropoff", dropoff)
url := "http://127.0.0.1:8083/route?" + v.Encode()

url := "http://" + c.hostPort + "/route?" + v.Encode()
fmt.Println(url)
var route Route
if err := c.client.GetJSON(ctx, "/route", url, &route); err != nil {
c.logger.For(ctx).Error("Error getting route", zap.Error(err))
Expand Down

0 comments on commit 43a5d22

Please sign in to comment.