Пример #1
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)
	}
}
Пример #2
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)
	}
}
Пример #3
0
func TestMockServer(t *testing.T) {
	srv := NewServer(
		server.Name("mock"),
		server.Version("latest"),
	)

	if srv.Options().Name != "mock" {
		t.Fatalf("Expected name mock, got %s", srv.Options().Name)
	}

	if srv.Options().Version != "latest" {
		t.Fatalf("Expected version latest, got %s", srv.Options().Version)
	}

	srv.Init(server.Version("test"))
	if srv.Options().Version != "test" {
		t.Fatalf("Expected version test, got %s", srv.Options().Version)
	}

	h := srv.NewHandler(func() string { return "foo" })
	if err := srv.Handle(h); err != nil {
		t.Fatal(err)
	}

	sub := srv.NewSubscriber("test", func() string { return "foo" })
	if err := srv.Subscribe(sub); err != nil {
		t.Fatal(err)
	}

	if sub.Topic() != "test" {
		t.Fatalf("Expected topic test got %s", sub.Topic())
	}

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

	if err := srv.Register(); err != nil {
		t.Fatal(err)
	}

	if err := srv.Deregister(); err != nil {
		t.Fatal(err)
	}

	if err := srv.Stop(); err != nil {
		t.Fatal(err)
	}
}
Пример #4
0
func TestGRPCServer(t *testing.T) {
	r := mock.NewRegistry()
	s := NewServer(
		server.Name("foo"),
		server.Registry(r),
	)

	pb.RegisterSayHandler(s, &sayServer{})

	if err := s.Start(); err != nil {
		t.Fatalf("failed to start: %v", err)
	}

	if err := s.Register(); err != nil {
		t.Fatalf("failed to register: %v", err)
	}

	// check registration
	services, err := r.GetService("foo")
	if err != nil || len(services) == 0 {
		t.Fatal("failed to get service: %v # %d", err, len(services))
	}

	defer func() {
		if err := s.Deregister(); err != nil {
			t.Fatalf("failed to deregister: %v", err)
		}

		if err := s.Stop(); err != nil {
			t.Fatalf("failed to stop: %v", err)
		}
	}()

	cc, err := grpc.Dial(s.Options().Address, grpc.WithInsecure())
	if err != nil {
		t.Fatal("failed to dial server: %v", err)
	}

	rsp := pb.Response{}

	if err := grpc.Invoke(context.Background(), "Say.Hello", &pb.Request{Name: "John"}, &rsp, cc); err != nil {
		t.Fatal("error calling server: %v", err)
	}

	if rsp.Msg != "Hello John" {
		t.Fatalf("Got unexpected response %v", rsp.Msg)
	}
}
Пример #5
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)
	}
}
Пример #6
0
func main() {
	flag.Parse()
	rabbitmq.DefaultExchange = "b2a"
	rabbitmq.DefaultRabbitURL = "amqp://localhost:5672"

	s := server.NewServer(
		server.Name("foo"),
		server.Id("foo"),
		server.Address("foo"),
		server.Transport(rabbitmq.NewTransport([]string{})),
		server.Codec("application/x-protobuf", mcodec.NewCodec),
	)
	s.Handle(
		s.NewHandler(&Say{}),
	)

	s.Start()
	select {}
}
Пример #7
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)
	}
}
Пример #8
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)
	}
}
Пример #9
0
// Name of the service
func Name(n string) Option {
	return func(o *Options) {
		o.Server.Init(server.Name(n))
	}
}
Пример #10
0
func (c *cmd) Before(ctx *cli.Context) error {
	// Due to logger issues with glog, we need to do this
	os.Args = os.Args[:1]
	flag.Set("logtostderr", fmt.Sprintf("%v", ctx.Bool("logtostderr")))
	flag.Set("alsologtostderr", fmt.Sprintf("%v", ctx.Bool("alsologtostderr")))
	flag.Set("stderrthreshold", ctx.String("stderrthreshold"))
	flag.Set("log_backtrace_at", ctx.String("log_backtrace_at"))
	flag.Set("log_dir", ctx.String("log_dir"))
	flag.Set("vmodule", ctx.String("vmodule"))
	flag.Set("v", ctx.String("v"))
	flag.Parse()

	// If flags are set then use them otherwise do nothing
	var serverOpts []server.Option
	var clientOpts []client.Option

	// Set the broker
	if len(ctx.String("broker")) > 0 {
		if b, ok := c.opts.Brokers[ctx.String("broker")]; ok {
			n := b(strings.Split(ctx.String("broker_address"), ","))
			*c.opts.Broker = n
		} else {
			return fmt.Errorf("Broker %s not found", ctx.String("broker"))
		}

		serverOpts = append(serverOpts, server.Broker(*c.opts.Broker))
		clientOpts = append(clientOpts, client.Broker(*c.opts.Broker))

	}

	// Set the registry
	if len(ctx.String("registry")) > 0 {
		if r, ok := c.opts.Registries[ctx.String("registry")]; ok {
			n := r(strings.Split(ctx.String("registry_address"), ","))
			*c.opts.Registry = n
		} else {
			return fmt.Errorf("Registry %s not found", ctx.String("registry"))
		}

		serverOpts = append(serverOpts, server.Registry(*c.opts.Registry))
		clientOpts = append(clientOpts, client.Registry(*c.opts.Registry))

		(*c.opts.Selector).Init(selector.Registry(*c.opts.Registry))
		clientOpts = append(clientOpts, client.Selector(*c.opts.Selector))

		(*c.opts.Broker).Init(broker.Registry(*c.opts.Registry))
	}

	// Set the selector
	if len(ctx.String("selector")) > 0 {
		if s, ok := c.opts.Selectors[ctx.String("selector")]; ok {
			n := s(selector.Registry(*c.opts.Registry))
			*c.opts.Selector = n
		} else {
			return fmt.Errorf("Selector %s not found", ctx.String("selector"))
		}

		// No server option here. Should there be?
		clientOpts = append(clientOpts, client.Selector(*c.opts.Selector))
	}

	// Set the transport
	if len(ctx.String("transport")) > 0 {
		if t, ok := c.opts.Transports[ctx.String("transport")]; ok {
			n := t(strings.Split(ctx.String("transport_address"), ","))
			*c.opts.Transport = n
		} else {
			return fmt.Errorf("Transport %s not found", ctx.String("transport"))
		}

		serverOpts = append(serverOpts, server.Transport(*c.opts.Transport))
		clientOpts = append(clientOpts, client.Transport(*c.opts.Transport))
	}

	// Parse the server options
	metadata := make(map[string]string)
	for _, d := range ctx.StringSlice("server_metadata") {
		var key, val string
		parts := strings.Split(d, "=")
		key = parts[0]
		if len(parts) > 1 {
			val = strings.Join(parts[1:], "=")
		}
		metadata[key] = val
	}

	if len(metadata) > 0 {
		serverOpts = append(serverOpts, server.Metadata(metadata))
	}

	if len(ctx.String("server_name")) > 0 {
		serverOpts = append(serverOpts, server.Name(ctx.String("server_name")))
	}

	if len(ctx.String("server_version")) > 0 {
		serverOpts = append(serverOpts, server.Version(ctx.String("server_version")))
	}

	if len(ctx.String("server_id")) > 0 {
		serverOpts = append(serverOpts, server.Id(ctx.String("server_id")))
	}

	if len(ctx.String("server_address")) > 0 {
		serverOpts = append(serverOpts, server.Address(ctx.String("server_address")))
	}

	if len(ctx.String("server_advertise")) > 0 {
		serverOpts = append(serverOpts, server.Advertise(ctx.String("server_advertise")))
	}

	// We have some command line opts for the server.
	// Lets set it up
	if len(serverOpts) > 0 {
		(*c.opts.Server).Init(serverOpts...)
	}

	// Use an init option?
	if len(clientOpts) > 0 {
		(*c.opts.Client).Init(clientOpts...)
	}

	return nil
}
Пример #11
0
func (c *cmd) Before(ctx *cli.Context) error {
	// If flags are set then use them otherwise do nothing
	var serverOpts []server.Option
	var clientOpts []client.Option

	// Set the broker
	if name := ctx.String("broker"); len(name) > 0 || len(ctx.String("broker_address")) > 0 {
		if len(name) == 0 {
			name = defaultBroker
		}

		if b, ok := c.opts.Brokers[name]; ok {
			n := b(broker.Addrs(strings.Split(ctx.String("broker_address"), ",")...))
			*c.opts.Broker = n
		} else {
			return fmt.Errorf("Broker %s not found", name)
		}

		serverOpts = append(serverOpts, server.Broker(*c.opts.Broker))
		clientOpts = append(clientOpts, client.Broker(*c.opts.Broker))

	}

	// Set the registry
	if name := ctx.String("registry"); len(name) > 0 || len(ctx.String("registry_address")) > 0 {
		if len(name) == 0 {
			name = defaultRegistry
		}

		if r, ok := c.opts.Registries[name]; ok {
			n := r(registry.Addrs(strings.Split(ctx.String("registry_address"), ",")...))
			*c.opts.Registry = n
		} else {
			return fmt.Errorf("Registry %s not found", name)
		}

		serverOpts = append(serverOpts, server.Registry(*c.opts.Registry))
		clientOpts = append(clientOpts, client.Registry(*c.opts.Registry))

		(*c.opts.Selector).Init(selector.Registry(*c.opts.Registry))
		clientOpts = append(clientOpts, client.Selector(*c.opts.Selector))

		(*c.opts.Broker).Init(broker.Registry(*c.opts.Registry))
	}

	// Set the selector
	if name := ctx.String("selector"); len(name) > 0 {
		if s, ok := c.opts.Selectors[name]; ok {
			n := s(selector.Registry(*c.opts.Registry))
			*c.opts.Selector = n
		} else {
			return fmt.Errorf("Selector %s not found", name)
		}

		// No server option here. Should there be?
		clientOpts = append(clientOpts, client.Selector(*c.opts.Selector))
	}

	// Set the transport
	if name := ctx.String("transport"); len(name) > 0 || len(ctx.String("transport_address")) > 0 {
		if len(name) == 0 {
			name = defaultTransport
		}

		if t, ok := c.opts.Transports[name]; ok {
			n := t(transport.Addrs(strings.Split(ctx.String("transport_address"), ",")...))
			*c.opts.Transport = n
		} else {
			return fmt.Errorf("Transport %s not found", name)
		}

		serverOpts = append(serverOpts, server.Transport(*c.opts.Transport))
		clientOpts = append(clientOpts, client.Transport(*c.opts.Transport))
	}

	// Parse the server options
	metadata := make(map[string]string)
	for _, d := range ctx.StringSlice("server_metadata") {
		var key, val string
		parts := strings.Split(d, "=")
		key = parts[0]
		if len(parts) > 1 {
			val = strings.Join(parts[1:], "=")
		}
		metadata[key] = val
	}

	if len(metadata) > 0 {
		serverOpts = append(serverOpts, server.Metadata(metadata))
	}

	if len(ctx.String("server_name")) > 0 {
		serverOpts = append(serverOpts, server.Name(ctx.String("server_name")))
	}

	if len(ctx.String("server_version")) > 0 {
		serverOpts = append(serverOpts, server.Version(ctx.String("server_version")))
	}

	if len(ctx.String("server_id")) > 0 {
		serverOpts = append(serverOpts, server.Id(ctx.String("server_id")))
	}

	if len(ctx.String("server_address")) > 0 {
		serverOpts = append(serverOpts, server.Address(ctx.String("server_address")))
	}

	if len(ctx.String("server_advertise")) > 0 {
		serverOpts = append(serverOpts, server.Advertise(ctx.String("server_advertise")))
	}

	// client opts
	if r := ctx.Int("client_retries"); r > 0 {
		clientOpts = append(clientOpts, client.Retries(r))
	}

	if t := ctx.String("client_request_timeout"); len(t) > 0 {
		d, err := time.ParseDuration(t)
		if err != nil {
			return fmt.Errorf("failed to parse client_request_timeout: %v", t)
		}
		clientOpts = append(clientOpts, client.RequestTimeout(d))
	}

	if r := ctx.Int("client_pool_size"); r > 0 {
		clientOpts = append(clientOpts, client.PoolSize(r))
	}

	if t := ctx.String("client_pool_ttl"); len(t) > 0 {
		d, err := time.ParseDuration(t)
		if err != nil {
			return fmt.Errorf("failed to parse client_pool_ttl: %v", t)
		}
		clientOpts = append(clientOpts, client.PoolTTL(d))
	}

	// We have some command line opts for the server.
	// Lets set it up
	if len(serverOpts) > 0 {
		(*c.opts.Server).Init(serverOpts...)
	}

	// Use an init option?
	if len(clientOpts) > 0 {
		(*c.opts.Client).Init(clientOpts...)
	}

	return nil
}
Пример #12
0
func TestRPCHandler(t *testing.T) {
	r := rmock.NewRegistry()

	(*cmd.DefaultOptions().Client).Init(
		client.Registry(r),
		client.Selector(selector.NewSelector(selector.Registry(r))),
	)

	(*cmd.DefaultOptions().Server).Init(
		server.Name("test"),
		server.Registry(r),
	)

	(*cmd.DefaultOptions().Server).Handle(
		(*cmd.DefaultOptions().Server).NewHandler(&TestHandler{t, metadata.Metadata{"Foo": "Bar"}}),
	)

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

	if err := server.Register(); err != nil {
		t.Fatal(err)
	}

	w := httptest.NewRecorder()

	request := map[string]string{
		"service": "test",
		"method":  "TestHandler.Exec",
		"request": "{}",
	}

	rb, err := json.Marshal(request)
	if err != nil {
		t.Fatal(err)
	}

	b := bytes.NewBuffer(rb)

	req, err := http.NewRequest("POST", "/rpc", b)
	if err != nil {
		t.Fatal(err)
	}
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Foo", "Bar")

	RPC(w, req)

	if err := server.Deregister(); err != nil {
		t.Fatal(err)
	}

	if err := server.Stop(); err != nil {
		t.Fatal(err)
	}

	if w.Code != 200 {
		t.Fatalf("Expected 200 response got %d %s", w.Code, w.Body.String())
	}

}