dapr's locally hosted service call experience is integrated with Java SDK's Spring Boot

1 Introduction

Previously in the article " Getting started with dapr and trying local hosting mode "Introduced dapr and local hosting. In this article, we will introduce how to use dapr's service call function in the code and integrate it into Spring Boot.

The logic of the Dapr service call is as follows:

This experiment will create two services:

pkslow-data, providing data services for returning data;

pkslow-hello, which provides query services and is called by the outside world, will call the interface of pkslow-data to obtain data, and after pkslow-data is processed, it will also call the interface of pkslow-hello to feedback the result.

2 Create a Spring Boot project

2.1 Introducing related dependencies

First create a regular Spring Boot project, while introducing the Web, also introduce the dapr SDK:


2.2 Configure dapr client

We need Dapr's Client to call the service, so that the service can be called directly through the service name, method and other information:

public class DaprConfig {
    private static final DaprClientBuilder BUILDER = new DaprClientBuilder();
    public DaprClient daprClient() {
        return BUILDER.build();

2.3 Request return body

The request and return classes are defined as follows:

public class PkslowRequest {
    private String name;
    private String age;

public class PkslowResponse {
    private int code;
    private String status;
    private String body;

hello requester

hello, as the requester, needs to send a request to data to get the data, and use DaprClient to call, as follows:

public PkslowResponse hello() {

  PkslowRequest request = new PkslowRequest();
  request.setName("Larry Deng");

  PkslowResponse response = daprClient.invokeMethod(

  return response;

2.5 data return party

After receiving the request, the return party processes the returned data and returns hello. At the same time, we also call an interface of hello to give feedback, so that in this example, the two parties can call each other:

public PkslowResponse data(@RequestBody PkslowRequest request) {

  PkslowResponse response = new PkslowResponse();
  response.setBody("This is data from www.pkslow.com: " + request.toString());
  return response;

3 Packaging, starting and invoking

3.1 Packaging

Packaging directly through maven requires the following plugins:


Execute the packaging command as follows:

mvn clean package

3.2 Start Dapr service

Specify the service name and port, and start the data service as follows:

$ dapr run --app-id pkslow-data --app-port 8090 --dapr-http-port 8190 -- java -jar -Dserver.port=8090 target/dapr-springboot-1.0-SNAPSHOT-exec.jar

Then start the hello service as follows:

$ dapr run --app-id pkslow-hello --app-port 8091 --dapr-http-port 8191 -- java -jar -Dserver.port=8091 target/dapr-springboot-1.0-SNAPSHOT-exec.jar

Open Dapr Dashboard to view the application as follows:

3.3 Access hello service

Access the hello service through a browser as follows:

3.4 View call chain

Open Zipkin, you can view the call as follows:

You can also view some Tracing information as follows:

4 Summary

So far, we have tried Dapr's service call function, which is quite convenient. You don't need to know the IP and address of the other party, and you can call it directly by name. This is the same as most microservice architectures because it provides service discovery.

5 codes

See the code on GitHub: https://github.com/LarryDpk/p...

Tags: Back-end

Posted by definewebsites on Thu, 02 Feb 2023 22:51:33 +0530