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:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>io.dapr</groupId> <artifactId>dapr-sdk-springboot</artifactId> <version>1.6.0</version> </dependency> </dependencies>
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:
@Configuration public class DaprConfig { private static final DaprClientBuilder BUILDER = new DaprClientBuilder(); @Bean public DaprClient daprClient() { return BUILDER.build(); } }
2.3 Request return body
The request and return classes are defined as follows:
@Data public class PkslowRequest { private String name; private String age; } @Data 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:
@GetMapping("/hello") public PkslowResponse hello() { log.info("hello"); PkslowRequest request = new PkslowRequest(); request.setName("Larry Deng"); request.setAge("18"); PkslowResponse response = daprClient.invokeMethod( "pkslow-data", "/pkslow/data", request, HttpExtension.POST, PkslowResponse.class ).block(); 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:
@PostMapping("/data") public PkslowResponse data(@RequestBody PkslowRequest request) { log.info("data"); daprClient.invokeMethod( "pkslow-hello", "/pkslow/feedback", "", HttpExtension.GET, PkslowResponse.class ).block(); PkslowResponse response = new PkslowResponse(); response.setCode(200); response.setStatus("OK"); 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:
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.4.2</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>exec</classifier> </configuration> </execution> </executions> </plugin>
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...