Skip to content

Latest commit

 

History

History
 
 

hello-world

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Hello World

This tutorial will demonstrate how to get Dapr running locally on your machine. You'll be deploying a Node.js app that subscribes to order messages and persists them. The following architecture diagram illustrates the components that make up the first part sample:

Architecture Diagram

Later on, you'll deploy a Python app to act as the publisher. 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

Now that Dapr is set up locally, clone the repo, then navigate to the Hello World quickstart:

git clone [-b <dapr_version_tag>] https://github.com/dapr/quickstarts.git
cd quickstarts/hello-world

Note: See https://github.com/dapr/quickstarts#supported-dapr-runtime-version for supported tags. Use git clone https://github.com/dapr/quickstarts.git when using the edge version of dapr runtime.

In the app.js you'll find a simple express application, which exposes a few routes and handlers. First, take a look at the top of the file:

const daprPort = process.env.DAPR_HTTP_PORT || 3500;
const stateStoreName = `statestore`;
const stateUrl = `http://localhost:${daprPort}/v1.0/state/${stateStoreName}`;

Dapr CLI creates an environment variable for the Dapr port, which defaults to 3500. You'll be using this in step 3 when sending POST messages to the system. The stateStoreName is the name given to the state store. You'll come back to that later on to see how that name is configured.

Next, take a look at the neworder handler:

app.post('/neworder', (req, res) => {
    const data = req.body.data;
    const orderId = data.orderId;
    console.log("Got a new order! Order ID: " + orderId);

    const state = [{
        key: "order",
        value: data
    }];

    fetch(stateUrl, {
        method: "POST",
        body: JSON.stringify(state),
        headers: {
            "Content-Type": "application/json"
        }
    }).then((response) => {
        if (!response.ok) {
            throw "Failed to persist state.";
        }

        console.log("Successfully persisted state.");
        res.status(200).send();
    }).catch((error) => {
        console.log(error);
        res.status(500).send({message: error});
    });
});

Here the app is exposing an endpoint that will receive and handle neworder messages. It first logs the incoming message, and then persist the order ID to the Redis store by posting a state array to the /state/<state-store-name> endpoint.

Alternatively, you could have persisted the state by simply returning it with the response object:

res.json({
        state: [{
            key: "order",
            value: order
        }]
    })

This approach, however, doesn't allow you to verify if the message successfully persisted.

The app also exposes a GET endpoint, /order:

app.get('/order', (_req, res) => {
    fetch(`${stateUrl}/order`)
        .then((response) => {
            if (!response.ok) {
                throw "Could not get state.";
            }

            return response.text();
        }).then((orders) => {
            res.send(orders);
        }).catch((error) => {
            console.log(error);
            res.status(500).send({message: error});
        });
});

This calls out to the Redis cache to retrieve the latest value of the "order" key, which effectively allows the Node.js app to be stateless.

Step 3 - Run the Node.js app with Dapr

  1. Install dependencies:

    npm install

    This will install express and body-parser, dependencies that are shown in the package.json.

  2. Run Node.js app with Dapr:

    dapr run --app-id nodeapp --app-port 3000 --dapr-http-port 3500 node app.js

The command should output text that looks like the following, along with logs:

Starting Dapr with id nodeapp. HTTP Port: 3500. gRPC Port: 9165
You're up and running! Both Dapr and your app logs will appear here.
...

Note: the --app-port (the port the app runs on) is configurable. The Node app happens to run on port 3000, but you could configure it to run on any other port. Also note that the Dapr --app-port parameter is optional, and if not supplied, a random available port is used.

The dapr run command looks for the default components directory which for Linux/MacOS is $HOME/.dapr/components and for Windows is %USERPROFILE%\.dapr\components which holds yaml definition files for components Dapr will be using at runtime. When running locally, the yaml files which provide default definitions for a local development environment are placed in this default components directory. Review the statestore.yaml file in the components directory:

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: statestore
spec:
  type: state.redis
...

You can see the yaml file defined the state store to be Redis and is naming it statestore. This is the name which was used in app.js to make the call to the state store in the application:

const stateStoreName = `statestore`;
const stateUrl = `http://localhost:${daprPort}/v1.0/state/${stateStoreName}`;

While in this tutorial the default yaml files were used, usually a developer would modify them or create custom yaml definitions depending on the application and scenario.

Step 4 - Post messages to the service

Now that Dapr and the Node.js app are running, you can send POST messages against it, using different tools. Note: here the POST message is sent to port 3500 - if you used a different port, be sure to update your URL accordingly.

First, POST the message by using Dapr cli in a new command line terminal:

Windows Command Prompt

dapr invoke --app-id nodeapp --method neworder --data "{\"data\": { \"orderId\": \"42\" } }"

Windows PowerShell

dapr invoke --app-id nodeapp --method neworder --data '{\"data\": { \"orderId\": \"42\" } }'

Linux or MacOS

dapr invoke --app-id nodeapp --method neworder --data '{"data": { "orderId": "42" } }'

Alternatively, using curl:

curl -XPOST -d @sample.json -H "Content-Type:application/json" http://localhost:3500/v1.0/invoke/nodeapp/method/neworder

Or, using the Visual Studio Code Rest Client Plugin

sample.http

POST http://localhost:3500/v1.0/invoke/nodeapp/method/neworder

{
  "data": {
    "orderId": "42"
  } 
}

Last but not least, you can use the Postman GUI.

Open Postman and create a POST request against http://localhost:3500/v1.0/invoke/nodeapp/method/neworder Postman Screenshot In your terminal window, you should see logs indicating that the message was received and state was updated:

== APP == Got a new order! Order ID: 42
== APP == Successfully persisted state.

Step 5 - Confirm successful persistence

Now, to verify the order was successfully persisted to the state store, create a GET request against: http://localhost:3500/v1.0/invoke/nodeapp/method/order. Note: Again, be sure to reflect the right port if you chose a port other than 3500.

curl http://localhost:3500/v1.0/invoke/nodeapp/method/order

or use Dapr CLI

dapr invoke --app-id nodeapp --method order --verb GET

or use the Visual Studio Code Rest Client Plugin

sample.http

GET http://localhost:3500/v1.0/invoke/nodeapp/method/order

or use the Postman GUI

Postman Screenshot 2

This invokes the /order route, which calls out to the Redis store for the latest data. Observe the expected result!

Step 6 - Run the Python app with Dapr

Take a look at the Python App to see how another application can invoke the Node App via Dapr without being aware of the destination's hostname or port. In the app.py file you can find the endpoint definition to call the Node App via Dapr.

dapr_port = os.getenv("DAPR_HTTP_PORT", 3500)
dapr_url = "http://localhost:{}/v1.0/invoke/nodeapp/method/neworder".format(dapr_port)

It is important to notice the Node App's name (nodeapp) in the URL, it will allow Dapr to redirect the request to the right API endpoint. This name needs to match the name used to run the Node App earlier in this exercise.

The code block below shows how the Python App will incrementally post a new orderId every second, or print an exception if the post call fails.

n = 0
while True:
    n += 1
    message = {"data": {"orderId": n}}

    try:
        response = requests.post(dapr_url, json=message)
    except Exception as e:
        print(e)

    time.sleep(1)

Now open a new command line terminal and go to the hello-world directory.

  1. Install dependencies:

    pip3 install requests
  2. Start the Python App with Dapr:

    dapr run --app-id pythonapp python3 app.py
  3. If all went well, the other terminal, running the Node App, should log entries like these:

    Got a new order! Order ID: 1
    Successfully persisted state
    Got a new order! Order ID: 2
    Successfully persisted state
    Got a new order! Order ID: 3
    Successfully persisted state
    

Note: Please refer this issue if you have trouble running python apps with dapr on windows.

  1. Now, perform a GET request a few times and see how the orderId changes every second (enter it into the web browser, use Postman, or curl):

    GET http://localhost:3500/v1.0/invoke/nodeapp/method/order
    {
        "orderId": 3
    }

Note: It is not required to run dapr init in the second command line terminal because dapr was already setup on your local machine initially, running this command again would fail.

Step 7 - Cleanup

To stop your services from running, simply stop the "dapr run" process. Alternatively, you can spin down each of your services with the Dapr CLI "stop" command. For example, to spin down both services, run these commands in a new command line terminal:

dapr stop --app-id nodeapp
dapr stop --app-id pythonapp

To see that services have stopped running, run dapr list, noting that your services no longer appears!

Next steps

Now that you've gotten Dapr running locally on your machine, consider these next steps: