Skip to content

Commit 1a37483

Browse files
cboornaz17isaachier
authored andcommitted
Add flags to allow changing ports for HotROD services (jaegertracing#951)
* Add ConfigOptions so clients find correct ports Signed-off-by: Corey Boornazian <coreyb220@gmail.com> Update client.go Signed-off-by: Corey Boornazian <coreyb220@gmail.com> * Fix typo on client.go Signed-off-by: Corey Boornazian <coreyb220@gmail.com> Fix typo on root.go Signed-off-by: Corey Boornazian <coreyb220@gmail.com>
1 parent 88c41d6 commit 1a37483

File tree

11 files changed

+68
-50
lines changed

11 files changed

+68
-50
lines changed

examples/hotrod/cmd/customer.go

+1-10
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var customerCmd = &cobra.Command{
3535
zapLogger := logger.With(zap.String("service", "customer"))
3636
logger := log.NewFactory(zapLogger)
3737
server := customer.NewServer(
38-
net.JoinHostPort(customerOptions.serverInterface, strconv.Itoa(customerOptions.serverPort)),
38+
net.JoinHostPort("0.0.0.0", strconv.Itoa(customerPort)),
3939
tracing.Init("customer", metricsFactory.Namespace("customer", nil), logger, jAgentHostPort),
4040
metricsFactory,
4141
logger,
@@ -45,16 +45,7 @@ var customerCmd = &cobra.Command{
4545
},
4646
}
4747

48-
var (
49-
customerOptions struct {
50-
serverInterface string
51-
serverPort int
52-
}
53-
)
54-
5548
func init() {
5649
RootCmd.AddCommand(customerCmd)
5750

58-
customerCmd.Flags().StringVarP(&customerOptions.serverInterface, "bind", "", "0.0.0.0", "interface to which the Customer server will bind")
59-
customerCmd.Flags().IntVarP(&customerOptions.serverPort, "port", "p", 8081, "port on which the Customer server will listen")
6051
}

examples/hotrod/cmd/driver.go

+1-10
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var driverCmd = &cobra.Command{
3535
zapLogger := logger.With(zap.String("service", "driver"))
3636
logger := log.NewFactory(zapLogger)
3737
server := driver.NewServer(
38-
net.JoinHostPort(driverOptions.serverInterface, strconv.Itoa(driverOptions.serverPort)),
38+
net.JoinHostPort("0.0.0.0", strconv.Itoa(driverPort)),
3939
tracing.Init("driver", metricsFactory.Namespace("driver", nil), logger, jAgentHostPort),
4040
metricsFactory,
4141
logger,
@@ -45,16 +45,7 @@ var driverCmd = &cobra.Command{
4545
},
4646
}
4747

48-
var (
49-
driverOptions struct {
50-
serverInterface string
51-
serverPort int
52-
}
53-
)
54-
5548
func init() {
5649
RootCmd.AddCommand(driverCmd)
5750

58-
driverCmd.Flags().StringVarP(&driverOptions.serverInterface, "bind", "", "0.0.0.0", "interface to which the driver server will bind")
59-
driverCmd.Flags().IntVarP(&driverOptions.serverPort, "port", "p", 8082, "port on which the driver server will listen")
6051
}

examples/hotrod/cmd/frontend.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,26 @@ var frontendCmd = &cobra.Command{
3232
Short: "Starts Frontend service",
3333
Long: `Starts Frontend service.`,
3434
RunE: func(cmd *cobra.Command, args []string) error {
35+
36+
options.FrontendHostPort = net.JoinHostPort("0.0.0.0", strconv.Itoa(frontendPort))
37+
options.DriverHostPort = net.JoinHostPort("0.0.0.0", strconv.Itoa(driverPort))
38+
options.CustomerHostPort = net.JoinHostPort("0.0.0.0", strconv.Itoa(customerPort))
39+
options.RouteHostPort = net.JoinHostPort("0.0.0.0", strconv.Itoa(routePort))
40+
3541
zapLogger := logger.With(zap.String("service", "frontend"))
3642
logger := log.NewFactory(zapLogger)
3743
server := frontend.NewServer(
38-
net.JoinHostPort(frontendOptions.serverInterface, strconv.Itoa(frontendOptions.serverPort)),
44+
options,
3945
tracing.Init("frontend", metricsFactory.Namespace("frontend", nil), logger, jAgentHostPort),
4046
logger,
4147
)
4248
return logError(zapLogger, server.Run())
4349
},
4450
}
4551

46-
var (
47-
frontendOptions struct {
48-
serverInterface string
49-
serverPort int
50-
}
51-
)
52+
var options frontend.ConfigOptions
5253

5354
func init() {
5455
RootCmd.AddCommand(frontendCmd)
5556

56-
frontendCmd.Flags().StringVarP(&frontendOptions.serverInterface, "bind", "", "0.0.0.0", "interface to which the frontend server will bind")
57-
frontendCmd.Flags().IntVarP(&frontendOptions.serverPort, "port", "p", 8080, "port on which the frontend server will listen")
5857
}

examples/hotrod/cmd/root.go

+28
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ var (
3838
fixDBConnDelay time.Duration
3939
fixDBConnDisableMutex bool
4040
fixRouteWorkerPoolSize int
41+
42+
customerPort int
43+
driverPort int
44+
frontendPort int
45+
routePort int
4146
)
4247

4348
// RootCmd represents the base command when called without any subcommands
@@ -62,6 +67,13 @@ func init() {
6267
RootCmd.PersistentFlags().DurationVarP(&fixDBConnDelay, "fix-db-query-delay", "D", 300*time.Millisecond, "Average lagency of MySQL DB query")
6368
RootCmd.PersistentFlags().BoolVarP(&fixDBConnDisableMutex, "fix-disable-db-conn-mutex", "M", false, "Disables the mutex guarding db connection")
6469
RootCmd.PersistentFlags().IntVarP(&fixRouteWorkerPoolSize, "fix-route-worker-pool-size", "W", 3, "Default worker pool size")
70+
71+
// Add flags to choose ports for services
72+
RootCmd.PersistentFlags().IntVarP(&customerPort, "customer-service-port", "c", 8081, "Port for customer service")
73+
RootCmd.PersistentFlags().IntVarP(&driverPort, "driver-service-port", "d", 8082, "Port for driver service")
74+
RootCmd.PersistentFlags().IntVarP(&frontendPort, "frontend-service-port", "f", 8080, "Port for frontend service")
75+
RootCmd.PersistentFlags().IntVarP(&routePort, "route-service-port", "r", 8083, "Port for routing service")
76+
6577
rand.Seed(int64(time.Now().Nanosecond()))
6678
logger, _ = zap.NewDevelopment(zap.AddStacktrace(zapcore.FatalLevel))
6779
cobra.OnInitialize(onInitialize)
@@ -90,6 +102,22 @@ func onInitialize() {
90102
logger.Info("fix: overriding route worker pool size", zap.Int("old", config.RouteWorkerPoolSize), zap.Int("new", fixRouteWorkerPoolSize))
91103
config.RouteWorkerPoolSize = fixRouteWorkerPoolSize
92104
}
105+
106+
if customerPort != 8081 {
107+
logger.Info("changing customer service port", zap.Int("old", 8081), zap.Int("new", customerPort))
108+
}
109+
110+
if driverPort != 8082 {
111+
logger.Info("changing driver service port", zap.Int("old", 8082), zap.Int("new", driverPort))
112+
}
113+
114+
if frontendPort != 8080 {
115+
logger.Info("changing frontend service port", zap.Int("old", 8080), zap.Int("new", frontendPort))
116+
}
117+
118+
if routePort != 8083 {
119+
logger.Info("changing route service port", zap.Int("old", 8083), zap.Int("new", routePort))
120+
}
93121
}
94122

95123
func logError(logger *zap.Logger, err error) error {

examples/hotrod/cmd/route.go

+1-10
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,15 @@ var routeCmd = &cobra.Command{
3535
zapLogger := logger.With(zap.String("service", "route"))
3636
logger := log.NewFactory(zapLogger)
3737
server := route.NewServer(
38-
net.JoinHostPort(routeOptions.serverInterface, strconv.Itoa(routeOptions.serverPort)),
38+
net.JoinHostPort("0.0.0.0", strconv.Itoa(routePort)),
3939
tracing.Init("route", metricsFactory.Namespace("route", nil), logger, jAgentHostPort),
4040
logger,
4141
)
4242
return logError(zapLogger, server.Run())
4343
},
4444
}
4545

46-
var (
47-
routeOptions struct {
48-
serverInterface string
49-
serverPort int
50-
}
51-
)
52-
5346
func init() {
5447
RootCmd.AddCommand(routeCmd)
5548

56-
routeCmd.Flags().StringVarP(&routeOptions.serverInterface, "bind", "", "0.0.0.0", "interface to which the Route server will bind")
57-
routeCmd.Flags().IntVarP(&routeOptions.serverPort, "port", "p", 8083, "port on which the Route server will listen")
5849
}

examples/hotrod/services/config/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@ var (
5959

6060
// RouteCalcDelayStdDev is standard deviation
6161
RouteCalcDelayStdDev = RouteCalcDelay / 4
62+
6263
)

examples/hotrod/services/customer/client.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,28 @@ type Client struct {
3232
tracer opentracing.Tracer
3333
logger log.Factory
3434
client *tracing.HTTPClient
35+
hostPort string
3536
}
3637

3738
// NewClient creates a new customer.Client
38-
func NewClient(tracer opentracing.Tracer, logger log.Factory) *Client {
39+
func NewClient(tracer opentracing.Tracer, logger log.Factory, hostPort string) *Client {
3940
return &Client{
4041
tracer: tracer,
4142
logger: logger,
4243
client: &tracing.HTTPClient{
4344
Client: &http.Client{Transport: &nethttp.Transport{}},
4445
Tracer: tracer,
4546
},
47+
hostPort: hostPort,
4648
}
4749
}
4850

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

53-
url := fmt.Sprintf("http://127.0.0.1:8081/customer?customer=%s", customerID)
55+
url := fmt.Sprintf("http://" + c.hostPort + "/customer?customer=%s", customerID)
56+
fmt.Println(url)
5457
var customer Customer
5558
if err := c.client.GetJSON(ctx, "/customer", url, &customer); err != nil {
5659
return nil, err

examples/hotrod/services/driver/client.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type Client struct {
3636
}
3737

3838
// NewClient creates a new driver.Client
39-
func NewClient(tracer opentracing.Tracer, logger log.Factory) *Client {
39+
func NewClient(tracer opentracing.Tracer, logger log.Factory, hostPort string) *Client {
4040
channelOpts := &tchannel.ChannelOptions{
4141
//Logger: logger,
4242
//StatsReporter: statsReporter,
@@ -47,7 +47,7 @@ func NewClient(tracer opentracing.Tracer, logger log.Factory) *Client {
4747
logger.Bg().Fatal("Cannot create TChannel", zap.Error(err))
4848
}
4949
clientOpts := &thrift.ClientOptions{
50-
HostPort: "127.0.0.1:8082",
50+
HostPort: hostPort,
5151
}
5252
thriftClient := thrift.NewClient(ch, "driver", clientOpts)
5353
client := driver.NewTChanDriverClient(thriftClient)

examples/hotrod/services/frontend/best_eta.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type bestETA struct {
3838
route route.Interface
3939
pool *pool.Pool
4040
logger log.Factory
41+
options ConfigOptions
4142
}
4243

4344
// Response contains ETA for a trip.
@@ -46,19 +47,22 @@ type Response struct {
4647
ETA time.Duration
4748
}
4849

49-
func newBestETA(tracer opentracing.Tracer, logger log.Factory) *bestETA {
50+
func newBestETA(tracer opentracing.Tracer, logger log.Factory, options ConfigOptions) *bestETA {
5051
return &bestETA{
5152
customer: customer.NewClient(
5253
tracer,
5354
logger.With(zap.String("component", "customer_client")),
55+
options.CustomerHostPort,
5456
),
5557
driver: driver.NewClient(
5658
tracer,
5759
logger.With(zap.String("component", "driver_client")),
60+
options.DriverHostPort,
5861
),
5962
route: route.NewClient(
6063
tracer,
6164
logger.With(zap.String("component", "route_client")),
65+
options.RouteHostPort,
6266
),
6367
pool: pool.New(config.RouteWorkerPoolSize),
6468
logger: logger,

examples/hotrod/services/frontend/server.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,26 @@ type Server struct {
3737
assetFS http.FileSystem
3838
}
3939

40+
// ConfigOptions used to make sure service clients
41+
// can find correct server ports
42+
type ConfigOptions struct {
43+
FrontendHostPort string
44+
DriverHostPort string
45+
CustomerHostPort string
46+
RouteHostPort string
47+
}
48+
4049
// NewServer creates a new frontend.Server
41-
func NewServer(hostPort string, tracer opentracing.Tracer, logger log.Factory) *Server {
50+
func NewServer(options ConfigOptions, tracer opentracing.Tracer, logger log.Factory) *Server {
4251
assetFS, err := fs.New()
4352
if err != nil {
4453
logger.Bg().Fatal("cannot import web assets", zap.Error(err))
4554
}
4655
return &Server{
47-
hostPort: hostPort,
56+
hostPort: options.FrontendHostPort,
4857
tracer: tracer,
4958
logger: logger,
50-
bestETA: newBestETA(tracer, logger),
59+
bestETA: newBestETA(tracer, logger, options),
5160
assetFS: assetFS,
5261
}
5362
}

examples/hotrod/services/route/client.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,19 @@ type Client struct {
3232
tracer opentracing.Tracer
3333
logger log.Factory
3434
client *tracing.HTTPClient
35+
hostPort string
3536
}
3637

3738
// NewClient creates a new route.Client
38-
func NewClient(tracer opentracing.Tracer, logger log.Factory) *Client {
39+
func NewClient(tracer opentracing.Tracer, logger log.Factory, hostPort string) *Client {
3940
return &Client{
4041
tracer: tracer,
4142
logger: logger,
4243
client: &tracing.HTTPClient{
4344
Client: &http.Client{Transport: &nethttp.Transport{}},
4445
Tracer: tracer,
4546
},
47+
hostPort: hostPort,
4648
}
4749
}
4850

@@ -53,8 +55,7 @@ func (c *Client) FindRoute(ctx context.Context, pickup, dropoff string) (*Route,
5355
v := url.Values{}
5456
v.Set("pickup", pickup)
5557
v.Set("dropoff", dropoff)
56-
url := "http://127.0.0.1:8083/route?" + v.Encode()
57-
58+
url := "http://" + c.hostPort + "/route?" + v.Encode()
5859
var route Route
5960
if err := c.client.GetJSON(ctx, "/route", url, &route); err != nil {
6061
c.logger.For(ctx).Error("Error getting route", zap.Error(err))

0 commit comments

Comments
 (0)