Example #1
0
func main() {
	cmd.Init()

	r := router.NewRouter()
	c := client.NewClient(client.Selector(r))
	c = router.NewLabelWrapper(c)

	// Create new request to service go.micro.srv.greeter, method Say.Hello
	req := c.NewRequest("go.micro.srv.greeter", "Say.Hello", &hello.Request{
		Name: "John",
	})

	rsp := &hello.Response{}

	// Set arbitrary headers in context
	ctx := metadata.NewContext(context.Background(), map[string]string{
		router.LabelPrefix + "Greeter": "one",
	})

	// Call service
	if err := c.Call(ctx, req, rsp); err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(rsp.Msg)
}
Example #2
0
func main() {
	cmd.Init()
	log = logrus.New()

	service = micro.NewService(
		micro.Flags(cli.StringFlag{
			Name:   "pair",
			Value:  "USD",
			Usage:  "Select pair to subscribe",
			EnvVar: "PAIR",
		}),
		micro.Flags(cli.StringFlag{
			Name:   "currency",
			Value:  "JPY",
			Usage:  "Select pair to subscribe",
			EnvVar: "CURRENCY",
		}),
		micro.Action(func(c *cli.Context) {
			settings.pair = c.String("pair")
			settings.currency = c.String("currency")
		}),
		micro.Name("PairSubscriber"),
	)
	service.Init(getOptions)
	go exec(settings.pair, settings.currency)
	select {}
}
Example #3
0
func main() {
	cmd.Init()
	fmt.Println("\n--- Publisher example ---\n")
	for i := 0; i < 10; i++ {
		pub(i)
	}
}
Example #4
0
func main() {
	cmd.Init()

	t := trace.NewTrace()

	srv := &registry.Service{
		Name: "go.client",
	}

	client.DefaultClient = client.NewClient(
		client.Wrap(
			trace.ClientWrapper(t, srv),
		),
	)

	fmt.Println("Starting trace")
	if err := t.Start(); err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("\n--- Traced Call example ---\n")
	i := 0
	for {
		call(i)
		i++
		<-time.After(time.Second * 5)
	}

	if err := t.Stop(); err != nil {
		fmt.Println(err)
	}
}
Example #5
0
func main() {
	// optionally setup command line usage
	cmd.Init()

	// Initialise Server
	server.Init(
		server.Name("go.micro.srv.example"),
	)

	// Register Subscribers
	server.Subscribe(
		server.NewSubscriber(
			"topic.go.micro.srv.example",
			new(subscriber.Example),
		),
	)

	server.Subscribe(
		server.NewSubscriber(
			"topic.go.micro.srv.example",
			subscriber.Handler,
		),
	)

	// Register Handler
	example.RegisterExampleHandler(
		server.DefaultServer, new(Example),
	)

	// Run server
	if err := server.Run(); err != nil {
		log.Fatal(err)
	}
}
Example #6
0
func main() {
	cmd.Init()

	t := zipkin.NewTrace(
		trace.Collectors("192.168.99.100:9092"),
	)
	defer t.Close()

	client.DefaultClient = client.NewClient(
		client.Wrap(
			trace.ClientWrapper(t, nil),
		),
	)

	fmt.Println("\n--- Traced Call example ---\n")
	for i := 0; i < 10; i++ {
		call(i)
	}

	/*
		fmt.Println("\n--- Streamer example ---\n")
		stream(10)

		fmt.Println("\n--- Ping Pong example ---\n")
		pingPong(10)

		fmt.Println("\n--- Publisher example ---\n")
		pub()
	*/
	<-time.After(time.Second * 10)
}
Example #7
0
func main() {
	cmd.Init()

	r := router.NewRouter()
	defer r.Close()

	ch := make(chan map[string]int, routines)
	stats := make(map[string]int)

	for i := 0; i < routines; i++ {
		go func(j int) {
			st := selector(j, r)
			str := ""
			for k, v := range st {
				str += fmt.Sprintf("stats %d %s %d\n", j, k, v)
			}
			fmt.Println(str)
			ch <- st
		}(i)
	}

	// collate stats
	for i := 0; i < routines; i++ {
		st := <-ch
		for k, v := range st {
			stats[k] += v
		}
	}

	for k, v := range stats {
		fmt.Println("overall stats", k, v)
	}
}
Example #8
0
func main() {
	cmd.Init()

	database := db.NewDB(
		db.Database("foo"),
		db.Table("bar"),
	)

	type Thing struct {
		Name string
	}

	record := db.NewRecord(uuid.NewUUID().String(), db.Metadata{"key": "value"}, &Thing{"dbthing"})

	fmt.Printf("Creating record: id: %s metadata: %+v bytes: %+v\n", record.Id(), record.Metadata(), string(record.Bytes()))
	if err := database.Create(record); err != nil {
		fmt.Println(err)
		return
	}

	rec, err := database.Read(record.Id())
	if err != nil {
		fmt.Println(err)
		return
	}

	thing := new(Thing)

	if err := rec.Scan(&thing); err != nil {
		fmt.Println("Error scanning read record", err)
		return
	}

	fmt.Printf("Read record: id: %s metadata: %+v bytes: %+v\n", rec.Id(), rec.Metadata(), thing)

	fmt.Println("Searching for metadata key:value")
	records, err := database.Search(db.Metadata{"key": "value"}, 10, 0)
	if err != nil {
		fmt.Println(err)
		return
	}

	for _, record := range records {
		thing := new(Thing)

		if err := record.Scan(&thing); err != nil {
			fmt.Println("Error scanning record", record.Id(), err)
			return
		}

		fmt.Printf("Record: id: %s metadata: %+v bytes: %+v\n", record.Id(), record.Metadata(), thing)
	}

	fmt.Println("Deleting", record.Id())
	if err := database.Delete(record.Id()); err != nil {
		fmt.Println(err)
		return
	}
}
Example #9
0
func main() {
	cmd.Init()

	// create event handler
	ev := event.NewEvent()

	// subscribe to events
	go sub(ev)

	// publish events
	pub(ev)
}
Example #10
0
func main() {
	cmd.Init()

	client.DefaultClient = client.NewClient(
		client.Selector(DCSelector()),
	)

	fmt.Println("\n--- Call example ---\n")
	for i := 0; i < 10; i++ {
		call(i)
	}
}
Example #11
0
func main() {
	cmd.Init()

	client.DefaultClient = client.NewClient(
		client.Wrap(NewDCWrapper),
	)

	fmt.Println("\n--- Call example ---\n")
	for i := 0; i < 10; i++ {
		call(i)
	}
}
Example #12
0
func main() {
	cmd.Init()

	if err := broker.Init(); err != nil {
		log.Fatalf("Broker Init error: %v", err)
	}

	if err := broker.Connect(); err != nil {
		log.Fatalf("Broker Connect error: %v", err)
	}

	pub()
}
Example #13
0
func main() {
	cmd.Init()

	fmt.Println("\n--- Call example ---\n")
	for i := 0; i < 10; i++ {
		call(i)
	}

	fmt.Println("\n--- Streamer example ---\n")
	stream(10)

	fmt.Println("\n--- Ping Pong example ---\n")
	pingPong(10)
}
Example #14
0
func main() {
	// optionally setup command line usage
	cmd.Init()

	md := server.DefaultOptions().Metadata
	md["datacenter"] = "local"

	server.DefaultServer = server.NewServer(
		server.WrapHandler(logWrapper),
		server.WrapSubscriber(logSubWrapper),
		server.Metadata(md),
	)

	// Initialise Server
	server.Init(
		server.Name("go.micro.srv.example"),
	)

	// Register Handlers
	server.Handle(
		server.NewHandler(
			new(handler.Example),
		),
	)

	// Register Subscribers
	if err := server.Subscribe(
		server.NewSubscriber(
			"topic.go.micro.srv.example",
			new(subscriber.Example),
		),
	); err != nil {
		log.Fatal(err)
	}

	if err := server.Subscribe(
		server.NewSubscriber(
			"topic.go.micro.srv.example",
			subscriber.Handler,
		),
	); err != nil {
		log.Fatal(err)
	}

	// Run server
	if err := server.Run(); err != nil {
		log.Fatal(err)
	}
}
Example #15
0
func main() {
	cmd.Init()

	if err := broker.Init(); err != nil {
		log.Fatalf("Broker Init error: %v", err)
	}
	if err := broker.Connect(); err != nil {
		log.Fatalf("Broker Connect error: %v", err)
	}

	go pub()
	go sub()

	<-time.After(time.Second * 10)
}
Example #16
0
func main() {
	app := cmd.App()
	app.Commands = append(app.Commands, api.Commands()...)
	app.Commands = append(app.Commands, cli.Commands()...)
	app.Commands = append(app.Commands, car.Commands()...)
	app.Commands = append(app.Commands, web.Commands()...)
	app.Action = func(context *ccli.Context) { ccli.ShowAppHelp(context) }

	setup(app)

	cmd.Init(
		cmd.Name("micro"),
		cmd.Description("A microservices toolkit"),
		cmd.Version("latest"),
	)
}
Example #17
0
func main() {
	// optionally setup command line usage
	cmd.Init()

	t := trace.NewTrace()
	defer t.Close()

	srv := &registry.Service{
		Name: "go.micro.srv.example",
	}

	client.DefaultClient = client.NewClient(
		client.Wrap(
			trace.ClientWrapper(t, srv),
		),
	)

	server.DefaultServer = server.NewServer(
		server.WrapHandler(trace.HandlerWrapper(t, srv)),
	)

	// Initialise Server
	server.Init(
		server.Name("go.micro.srv.example"),
	)

	// Register Handlers
	server.Handle(
		server.NewHandler(
			new(handler.Example),
		),
	)

	server.Handle(
		server.NewHandler(
			new(Ping),
		),
	)

	// Run server
	if err := server.Run(); err != nil {
		log.Fatal(err)
	}
}
Example #18
0
func main() {
	cmd.Init()
	m := monitor.NewMonitor(
		monitor.Interval(time.Second),
	)

	defer m.Close()

	hc1 := m.NewHealthChecker("go.micro.healthcheck.ping", "This is a ping healthcheck that succeeds", success)
	hc2 := m.NewHealthChecker("go.micro.healthcheck.pong", "This is a pong healthcheck that fails", failure)

	m.Register(hc1)
	m.Register(hc2)

	go record(m)

	<-time.After(time.Second * 10)
	fmt.Println("Stopping monitor")
}
Example #19
0
func main() {
	cmd.Init()

	fmt.Println("\n--- Log Wrapper example ---\n")

	// Wrap the default client
	client.DefaultClient = logWrap(client.DefaultClient)

	call(0)

	fmt.Println("\n--- Log+Trace Wrapper example ---\n")

	// Wrap using client.Wrap option
	client.DefaultClient = client.NewClient(
		client.Wrap(traceWrap),
		client.Wrap(logWrap),
	)

	call(1)
}
Example #20
0
func main() {
	// optionally setup command line usage
	cmd.Init()

	// Initialise Server
	server.Init(
		server.Name("go.micro.api.greeter"),
	)

	// Register Handlers
	server.Handle(
		server.NewHandler(
			new(Say),
		),
	)

	// Run server
	if err := server.Run(); err != nil {
		log.Fatal(err)
	}
}
Example #21
0
func main() {
	cmd.Init()

	t := zipkin.NewTrace(
		trace.Collectors([]string{"192.168.99.100:9092"}),
	)

	client.DefaultClient = client.NewClient(
		client.Wrap(
			trace.ClientWrapper(t, nil),
		),
	)

	fmt.Println("Starting trace")
	if err := t.Start(); err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println("\n--- Traced Call example ---\n")
	for i := 0; i < 10; i++ {
		call(i)
	}

	/*
		fmt.Println("\n--- Streamer example ---\n")
		stream(10)

		fmt.Println("\n--- Ping Pong example ---\n")
		pingPong(10)

		fmt.Println("\n--- Publisher example ---\n")
		pub()
	*/
	<-time.After(time.Second * 10)

	if err := t.Stop(); err != nil {
		fmt.Println(err)
	}
}
Example #22
0
func main() {
	// optionally setup command line usage
	cmd.Init()

	t := trace.NewTrace()

	if err := t.Start(); err != nil {
		log.Fatal(err)
	}

	srv := &registry.Service{
		Name: "go.server",
	}

	server.DefaultServer = server.NewServer(
		server.WrapHandler(trace.HandlerWrapper(t, srv)),
	)

	// Initialise Server
	server.Init(
		server.Name("go.micro.srv.example"),
	)

	// Register Handlers
	server.Handle(
		server.NewHandler(
			new(handler.Example),
		),
	)

	// Run server
	if err := server.Run(); err != nil {
		log.Fatal(err)
	}

	if err := t.Stop(); err != nil {
		log.Fatal(err)
	}
}
Example #23
0
func main() {
	cmd.Init()

	// use the generated client stub
	cl := hello.NewSayClient("go.micro.srv.greeter", client.DefaultClient)

	// Set arbitrary headers in context
	ctx := c.WithMetadata(context.Background(), map[string]string{
		"X-User-Id": "john",
		"X-From-Id": "script",
	})

	rsp, err := cl.Hello(ctx, &hello.Request{
		Name: "John",
	})
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(rsp.Msg)
}
Example #24
0
func main() {
	cmd.Init()

	r := proto.NewRouterClient(service, client.DefaultClient)

	stream, err := r.SelectStream(context.TODO(), &proto.SelectRequest{Service: service})
	if err != nil {
		fmt.Println("error streaming", err)
		return
	}

	for i := 0; i <= 3; {
		fmt.Println("waiting on stream")
		rsp, err := stream.Recv()
		if err != nil {
			fmt.Println("error receiving", err)
			return
		}
		fmt.Println("got stream response, expires", rsp.Expires)
		for _, s := range rsp.Services {
			fmt.Printf("received %s %s %+v\n", s.Name, s.Version, s.Nodes)
		}
	}
}
Example #25
0
func main() {
	app := cmd.App()
	app.Commands = append(app.Commands, auth.Commands()...)
	app.Commands = append(app.Commands, config.Commands()...)
	app.Commands = append(app.Commands, discovery.Commands()...)
	app.Commands = append(app.Commands, db.Commands()...)
	app.Commands = append(app.Commands, event.Commands()...)
	app.Commands = append(app.Commands, kv.Commands()...)
	//	app.Commands = append(app.Commands, log.Commands()...)
	//	app.Commands = append(app.Commands, metrics.Commands()...)
	app.Commands = append(app.Commands, monitor.Commands()...)
	app.Commands = append(app.Commands, router.Commands()...)
	//	app.Commands = append(app.Commands, sync.Commands()...)
	app.Commands = append(app.Commands, trace.Commands()...)
	app.Action = func(context *ccli.Context) { ccli.ShowAppHelp(context) }

	setup(app)

	cmd.Init(
		cmd.Name("os"),
		cmd.Description("A microservices operating system"),
		cmd.Version("latest"),
	)
}
Example #26
0
func main() {
	cmd.Init()

	t := trace.NewTrace()
	defer t.Close()

	srv := &registry.Service{
		Name: "go.micro.client.example",
	}

	client.DefaultClient = client.NewClient(
		client.Wrap(
			trace.ClientWrapper(t, srv),
		),
	)

	fmt.Println("\n--- Traced Call example ---\n")
	i := 0
	for {
		call(i)
		i++
		<-time.After(time.Second * 5)
	}
}
Example #27
0
func main() {
	cmd.Init()

	// Create new request to service go.micro.srv.greeter, method Say.Hello
	req := client.NewRequest("go.micro.srv.greeter", "Say.Hello", &hello.Request{
		Name: "John",
	})

	// Set arbitrary headers in context
	ctx := metadata.NewContext(context.Background(), map[string]string{
		"X-User-Id": "john",
		"X-From-Id": "script",
	})

	rsp := &hello.Response{}

	// Call service
	if err := client.Call(ctx, req, rsp); err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(rsp.Msg)
}
Example #28
0
func (s *service) Init(opts ...Option) error {
	app := cmd.App()

	app.Flags = append(app.Flags,
		cli.IntFlag{
			Name:   "register_ttl",
			EnvVar: "MICRO_REGISTER_TTL",
			Usage:  "Register TTL in seconds",
		},
		cli.IntFlag{
			Name:   "register_interval",
			EnvVar: "MICRO_REGISTER_INTERVAL",
			Usage:  "Register interval in seconds",
		},
	)

	before := app.Before

	app.Before = func(ctx *cli.Context) error {
		if ttl := ctx.Int("register_ttl"); ttl > 0 {
			s.opts.RegisterTTL = time.Duration(ttl) * time.Second
		}

		if interval := ctx.Int("register_interval"); interval > 0 {
			s.opts.RegisterInterval = time.Duration(interval) * time.Second
		}

		if name := ctx.String("server_name"); len(name) > 0 {
			s.opts.Name = name
		}

		if ver := ctx.String("server_version"); len(ver) > 0 {
			s.opts.Version = ver
		}

		if id := ctx.String("server_id"); len(id) > 0 {
			s.opts.Id = id
		}

		if addr := ctx.String("server_address"); len(addr) > 0 {
			s.opts.Address = addr
		}

		if adv := ctx.String("server_advertise"); len(adv) > 0 {
			s.opts.Advertise = adv
		}

		return before(ctx)
	}

	cmd.Init()

	for _, o := range opts {
		o(&s.opts)
	}

	srv := s.genSrv()
	srv.Endpoints = s.srv.Endpoints
	s.srv = srv

	return nil
}
Example #29
0
func main() {
	cmd.Init()
	d := discovery.NewDiscovery(
		discovery.Interval(time.Second),
	)

	defer d.Close()

	service := &registry.Service{
		Name:    "go.micro.srv.foo",
		Version: "latest",
		Metadata: map[string]string{
			"foo": "bar",
		},
		Endpoints: []*registry.Endpoint{
			&registry.Endpoint{
				Name: "/index",
				Request: &registry.Value{
					Name: "request",
					Type: "Request",
				},
				Response: &registry.Value{
					Name: "response",
					Type: "Response",
				},
				Metadata: map[string]string{
					"index": "Handles index requests",
				},
			},
		},
		Nodes: []*registry.Node{
			&registry.Node{
				Id:      "foo-123",
				Address: "localhost",
				Port:    8080,
				Metadata: map[string]string{
					"bar": "baz",
				},
			},
		},
	}

	// register
	if err := d.Register(service); err != nil {
		fmt.Println(err)
	}
	// find self
	rsp, err := d.GetService("go.micro.srv.foo")
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Printf("Got service %+v\n", rsp[0])
	}

	<-time.After(time.Second * 10)

	// deregister
	if err := d.Deregister(service); err != nil {
		fmt.Println(err)
	}

}