예제 #1
0
파일: kv.go 프로젝트: micro/go-platform
func main() {
	// Create a new service. Optionally include some options here.
	service := micro.NewService()
	service.Init()

	// Start the key value gossiping
	keyval := kv.NewKV()
	defer keyval.Close()

	// Start server since we have to be
	// part of the hash ring for now
	go service.Run()

	go exit()

	id := rand.Int() % 100

	for i := 0; i < 100; i++ {
		key := fmt.Sprintf("foo%d-%d", i, id)

		err := keyval.Put(&kv.Item{
			Key:        key,
			Value:      []byte(`hello`),
			Expiration: time.Second * 20,
		})
		if err != nil {
			fmt.Println("put err", err)
		}

		item, err := keyval.Get(key)
		if err != nil {
			fmt.Println("get err", err)
		} else {
			fmt.Println("get", item)
		}

		time.Sleep(time.Second)
	}
}
예제 #2
0
파일: kv.go 프로젝트: micro/platform
func srv(ctx *cli.Context) {
	service := micro.NewService(
		micro.Name("go.micro.srv.kv"),
		micro.Version("latest"),
		micro.RegisterTTL(
			time.Duration(ctx.GlobalInt("register_ttl"))*time.Second,
		),
		micro.RegisterInterval(
			time.Duration(ctx.GlobalInt("register_interval"))*time.Second,
		),
		micro.Flags(),
	)

	opts := []kv.Option{
		kv.Client(service.Client()),
		kv.Server(service.Server()),
	}

	if len(ctx.String("namespace")) > 0 {
		opts = append(opts, kv.Namespace(ctx.String("namespace")))
	}

	keyval := kv.NewKV(opts...)
	defer keyval.Close()

	service.Server().Init(server.WrapHandler(func(fn server.HandlerFunc) server.HandlerFunc {
		return func(ctx context.Context, req server.Request, rsp interface{}) error {
			ctx = kv.NewContext(ctx, keyval)
			return fn(ctx, req, rsp)
		}
	}))

	proto.RegisterStoreHandler(service.Server(), new(handler.Store))

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