go-sdk/service/grpc
Mike Nguyen 81312e9da9
merge release-1.12 into main (#703)
* Adding the name of the failing activity. For more detail (#678)

Signed-off-by: arturo <jarturotrenard@gmail.com>

* add deadlettertopic support to non-streaming subscriptions (#685)

* add deadlettertopic support to non-streaming subscriptions

Signed-off-by: yaron2 <schneider.yaron@live.com>

* fix tests

Signed-off-by: yaron2 <schneider.yaron@live.com>

---------

Signed-off-by: yaron2 <schneider.yaron@live.com>

* Pick #674 and bump to rc13 (#686)

* Bump gover, tag, x/deps and dapr (#674)

* release: bump to rc2

Signed-off-by: mikeee <hey@mike.ee>

* chore: upgrade x/net and x/crypto

Signed-off-by: mikeee <hey@mike.ee>

* release: bump go to 1.23.5 and dapr to rc5

Signed-off-by: mikeee <hey@mike.ee>

* ci: bump validation workflow versions

Signed-off-by: mikeee <hey@mike.ee>

* bump cli and runtime to latest rc

Signed-off-by: Mike Nguyen <hey@mike.ee>

* chore: bump dapr to rc7 and dt-go to head

Signed-off-by: Mike Nguyen <hey@mike.ee>

* chore: bump to rc8

Signed-off-by: mikeee <hey@mike.ee>

* chore(release): bump to latest

Signed-off-by: mikeee <hey@mike.ee>

---------

Signed-off-by: mikeee <hey@mike.ee>
Signed-off-by: Mike Nguyen <hey@mike.ee>

* chore: bump cli and runtime vers

Signed-off-by: Mike Nguyen <hey@mike.ee>

* chore: bump to rc13

Signed-off-by: Mike Nguyen <hey@mike.ee>

---------

Signed-off-by: mikeee <hey@mike.ee>
Signed-off-by: Mike Nguyen <hey@mike.ee>

* feat: reconnect stream when grpc code is unknown / unavailable (#692)

* feat: reconnect stream when grpc code is unknown / unavailable

Signed-off-by: Eileen Yu <eileenylj@gmail.com>

* log error for closing stream

Signed-off-by: Eileen Yu <eileenylj@gmail.com>

---------

Signed-off-by: Eileen Yu <eileenylj@gmail.com>

* ci: include pre-release label for RCs (#675)

* ci: include pre-release label for RCs

Signed-off-by: Mike Nguyen <hey@mike.ee>

* ci: enumerate if statements

Signed-off-by: Mike Nguyen <hey@mike.ee>

---------

Signed-off-by: Mike Nguyen <hey@mike.ee>

* docs(sdk): add basic workflow example (#691)

* docs(sdk): add basic workflow example

Signed-off-by: Mike Nguyen <hey@mike.ee>

* docs: fix indentations

Signed-off-by: Mike Nguyen <hey@mike.ee>

---------

Signed-off-by: Mike Nguyen <hey@mike.ee>

* update conversation api field name (#695)

Signed-off-by: yaron2 <schneider.yaron@live.com>

* fix(examples): update deprecated flag (#689)

* fix(examples): update deprecated flag

Signed-off-by: Mike Nguyen <hey@mike.ee>

* ci: test cli PR

Signed-off-by: Mike Nguyen <hey@mike.ee>

* test(service): bump body size to 41Mi

Signed-off-by: Mike Nguyen <hey@mike.ee>

* chore: bump cli to rc6 and runtime to rc16

Signed-off-by: Mike Nguyen <hey@mike.ee>

---------

Signed-off-by: Mike Nguyen <hey@mike.ee>
Co-authored-by: Yaron Schneider <schneider.yaron@live.com>

* release: v1.12.0 version (#700)

Signed-off-by: Mike Nguyen <hey@mike.ee>

* ci: revert rc tests (#701)

Signed-off-by: Mike Nguyen <hey@mike.ee>

---------

Signed-off-by: arturo <jarturotrenard@gmail.com>
Signed-off-by: yaron2 <schneider.yaron@live.com>
Signed-off-by: mikeee <hey@mike.ee>
Signed-off-by: Mike Nguyen <hey@mike.ee>
Signed-off-by: Eileen Yu <eileenylj@gmail.com>
Co-authored-by: Arturo Trenard <jarturotrenard@gmail.com>
Co-authored-by: Yaron Schneider <schneider.yaron@live.com>
Co-authored-by: Eileen Yu <48944635+Eileen-Yu@users.noreply.github.com>
2025-02-28 12:30:46 -08:00
..
Readme.md Add example for dapr grpc proxy mode (#414) 2023-06-06 12:00:37 +08:00
binding.go chore!: upgrade lint & go (#649) 2024-11-13 22:01:59 -07:00
binding_test.go Update github.com/dapr/dapr to v1.13.0-rc.1 (#503) 2024-02-07 07:54:01 -08:00
health_check.go chore!: upgrade lint & go (#649) 2024-11-13 22:01:59 -07:00
health_check_test.go feat: go1.20 and golangci-lint v1.55.2 (#480) 2023-12-11 10:51:26 +08:00
invoke.go chore!: upgrade lint & go (#649) 2024-11-13 22:01:59 -07:00
invoke_test.go feat: go1.20 and golangci-lint v1.55.2 (#480) 2023-12-11 10:51:26 +08:00
scheduling.go Implement distributed scheduler building block (#562) 2024-07-17 09:04:45 -07:00
service.go Implement distributed scheduler building block (#562) 2024-07-17 09:04:45 -07:00
service_test.go feat: go1.20 and golangci-lint v1.55.2 (#480) 2023-12-11 10:51:26 +08:00
topic.go merge release-1.12 into main (#703) 2025-02-28 12:30:46 -08:00
topic_test.go Update github.com/dapr/dapr to v1.13.0-rc.1 (#503) 2024-02-07 07:54:01 -08:00

Readme.md

Dapr gRPC Service SDK for Go

Start by importing Dapr Go service/grpc package:

daprd "github.com/dapr/go-sdk/service/grpc"

Creating and Starting Service

To create a gRPC Dapr service, first, create a Dapr callback instance with a specific address:

s, err := daprd.NewService(":50001")
if err != nil {
    log.Fatalf("failed to start the server: %v", err)
}

Or with address and an existing net.Listener in case you want to combine existing server listener:

list, err := net.Listen("tcp", "localhost:0")
if err != nil {
	log.Fatalf("gRPC listener creation failed: %s", err)
}
s := daprd.NewServiceWithListener(list)

Dapr gRPC service supports using existed gRPC server with the help of NewServiceWithGrpcServer. You can use RegisterGreeterServer to add existed gRPC service either:

lis, err := net.Listen("tcp", port)
if err != nil {
	log.Fatalf("failed to listen: %v", err)
}

grpcServer := grpc.NewServer()

// register existed service
// pb.RegisterGreeterServer(grpcServer, &existedGrpcServer{})

// new dapr grpc service
s := daprd.NewServiceWithGrpcServer(lis, grpcServer)

Once you create a service instance, you can "attach" to that service any number of event, binding, and service invocation logic handlers as shown below. Onces the logic is defined, you are ready to start the service:

if err := s.Start(); err != nil {
    log.Fatalf("server error: %v", err)
}

Event Handling

To handle events from specific topic you need to add at least one topic event handler before starting the service:

sub := &common.Subscription{
		PubsubName: "messages",
		Topic:      "topic1",
	}
if err := s.AddTopicEventHandler(sub, eventHandler); err != nil {
    log.Fatalf("error adding topic subscription: %v", err)
}

The handler method itself can be any method with the expected signature:

func eventHandler(ctx context.Context, e *common.TopicEvent) (retry bool, err error) {
	log.Printf("event - PubsubName:%s, Topic:%s, ID:%s, Data: %v", e.PubsubName, e.Topic, e.ID, e.Data)
	// do something with the event
	return true, nil
}

Service Invocation Handler

To handle service invocations you will need to add at least one service invocation handler before starting the service:

if err := s.AddServiceInvocationHandler("echo", echoHandler); err != nil {
    log.Fatalf("error adding invocation handler: %v", err)
}

The handler method itself can be any method with the expected signature:

func echoHandler(ctx context.Context, in *common.InvocationEvent) (out *common.Content, err error) {
	log.Printf("echo - ContentType:%s, Verb:%s, QueryString:%s, %+v", in.ContentType, in.Verb, in.QueryString, string(in.Data))
	// do something with the invocation here
	out = &common.Content{
		Data:        in.Data,
		ContentType: in.ContentType,
		DataTypeURL: in.DataTypeURL,
	}
	return
}

Binding Invocation Handler

To handle binding invocations you will need to add at least one binding invocation handler before starting the service:

if err := s.AddBindingInvocationHandler("run", runHandler); err != nil {
    log.Fatalf("error adding binding handler: %v", err)
}

The handler method itself can be any method with the expected signature:

func runHandler(ctx context.Context, in *common.BindingEvent) (out []byte, err error) {
	log.Printf("binding - Data:%v, Meta:%v", in.Data, in.Metadata)
	// do something with the invocation here
	return nil, nil
}

Templates

To accelerate your gRPC Dapr app development in Go even further you can use one of the GitHub templates integrating the gRPC Dapr callback package:

Contributing to Dapr Go client

See the Contribution Guide to get started with building and developing.