go-sdk/examples/hello-world
hunter007 3929306e36
Update go version to 1.19 (#403)
* update go version to 1.19

Signed-off-by: hunter007 <wentao79@gmail.com>

* add timeout for golangci-lint

Signed-off-by: hunter007 <wentao79@gmail.com>

* fix golangci-lint

Signed-off-by: hunter007 <wentao79@gmail.com>

* use revive instead of goliint in .golangci.yml

Signed-off-by: hunter007 <wentao79@gmail.com>

* if-return: redundant if ...; err != nil check, just return error instead.

Signed-off-by: hunter007 <wentao79@gmail.com>

* fix

Signed-off-by: hunter007 <wentao79@gmail.com>

* update golangci-lint from v1.50.1 to v1.52.2

Signed-off-by: hunter007 <wentao79@gmail.com>

---------

Signed-off-by: hunter007 <wentao79@gmail.com>
2023-05-20 07:36:37 -07:00
..
img Added 'hello-world' example (#171) 2021-06-18 15:21:05 -07:00
.gitignore chore: remove the binary and add gitignore (#315) 2022-09-30 20:28:16 -07:00
Makefile Added 'hello-world' example (#171) 2021-06-18 15:21:05 -07:00
README.md Update docs link for hello world tutorial (#270) 2022-03-31 12:27:10 -07:00
go.mod Update go version to 1.19 (#403) 2023-05-20 07:36:37 -07:00
go.sum Update go version to 1.19 (#403) 2023-05-20 07:36:37 -07:00
order.go allow passage of metadata to state store API (#262) 2022-02-24 09:39:05 +08:00

README.md

Hello World

This tutorial will demonstrate how to instrument your application with Dapr, and run it locally on your machine. You will deploying order applications with the flow identical to Hello World.

The application invokes Dapr API via Dapr client, which, in turn, calls Dapr runtime.

The following architecture diagram illustrates the components that make up this quickstart:

Architecture Diagram

Dapr runtime supports multiple applications that could communicate with one another. In a later example, you'll deploy two instances of the order app - one will update the state store, while another will read from it.

The architecture diagram below shows the addition of the new component:

Architecture Diagram Final

Prerequisites

This quickstart requires you to have the following installed on your machine:

Step 1 - Setup Dapr

Follow instructions to download and install the Dapr CLI and initialize Dapr.

Step 2 - Understand the code

The order.go is a simple command line application, that implements four commands:

  • put sends an order with configurable order ID.
  • get return the current order number.
  • del deletes the order.
  • seq streams a sequence of orders with incrementing order IDs.

First, the app instantiates Dapr client:

    client, err := dapr.NewClientWithPort(port)
    if err != nil {
        panic(err)
    }
    defer client.Close()

Then, depending on the command line argument, the app invokes corresponding method:

Persist the state:

    err := client.SaveState(ctx, stateStoreName, "order", []byte(strconv.Itoa(orderID)), nil)

Retrieve the state:

    item, err := client.GetState(ctx, stateStoreName, "order", nil)

Delete the state:

    err := client.DeleteState(ctx, stateStoreName, "order", nil)

Step 3 - Run the app with Dapr

  1. Build the app
go mod vendor
go build -o order order.go
  1. Run the app

There are two ways to launch Dapr applications. You can pass the app executable to the Dapr runtime:

dapr run --app-id order-app --dapr-grpc-port 3500 --log-level error -- ./order put --id 20
dapr run --app-id order-app --dapr-grpc-port 3500 --log-level error ./order get

Screenshot1 Screenshot2

Alternatively, you can start a standalone Dapr runtime, and call the app from another shell:

dapr run --app-id order-app --dapr-grpc-port 3500 --log-level error
./order put --id 10

./order get

To terminate your services, simply stop the "dapr run" process, or use the Dapr CLI "stop" command:

dapr stop --app-id order-app

Screenshot3

  1. Run multiple apps

You can run more than one app in Dapr runtime. In this example you will call order seq which sends a sequence of orders. Another instance of the order app will read the state.

dapr run --app-id order-app --dapr-grpc-port 3500 --log-level error ./order seq
./order get

./order get

Screenshot4