I Tried Using Dapr in Java To Facilitate Microservice Development
I Tried Using Dapr in Java To Facilitate Microservice Development
What is Dapr?
Dapr is a framework developed by Microsoft that simplifies the implementation of microservices by implementing non-functional
requirements such as inter-service invocation, state management, and inter-service messaging with a sidecar (Proxy). Since it is
developed by OSS, it can be used from the following.
https://dapr.io/
https://github.com/dapr/dapr
I thought that it was the same as a service mesh such as Istio because it was a sidecar, but when I listened to the session, it felt a little
different, and abstracted file and state management (data persistence), or queuing such as Kafka. There seemed to be a role to play.
It's more like Java EE's JNDI, DataSource, or JCA (Java Connector Architecture). ~~ If you're an uncle ~~ It's a chance to say, "This is
the one I did at Shinkenzemi!"
If it is a Java EE container such as Weblogic, I think that this area is in the same memory space and talks with T3 in the first place, but
Dapr and each application talk with HTTP or gRPC.
$ mvn io.quarkus:quarkus-maven-plugin:1.4.2.Final:create \
-DprojectGroupId=dev.nklab \
-DprojectArtifactId=dapr-app \
-DprojectVersion=1.0.0-SNAPSHOT \
-DclassName="dev.nklab.example.dapr.HelloResource"
$ cd dapr-app
$ ./mvnw quarkus:dev
$ curl http://localhost:8080/hello
hello
Install Dapr when you can confirm the operation of the application. k8s works without any special, but Docker seems to need to be
installed in advance.
The installation is now complete. If you get the following error, you probably forgot dapr init .
Next, wrap the Quarkus application created earlier with Dapr with the following command and execute it.
$ dapr run --app-id javaapp --app-port 8080 --port 3500 ./mvnw quarkus:dev
ℹ
...
✅
Updating metadata for app command: ./mvnw quarkus:dev
You're up and running! Both Dapr and your app logs will appear here.
--app-port is the Quarkus port number and --port is the Dapr port number. Now let's access Dapr with curl.
$ curl http://localhost:3500/v1.0/invoke/javaapp/method/hello
hello
Since Dapr is acting as a sidecar, that is, a Proxy, you can see that you could access the backside app with 3500 instead of 8080 . The
javaapp part is the ʻaap-id specified at runtime earlier. It seems that it will be a process that links with the
application on the back side with ʻinvoke .
https://linuxtut.com/en/bf50cb3aa14b88243ad2/ 1/5
14/9/2021 I tried using Dapr in Java to facilitate microservice development
A user sends a request to Application (Java) via Dapr, hits Dapr's API from the application and writes to Redis via Dapr. It's interesting
not to go directly through Redis for data persistence, as the application only talks to Dapr.
{
"data": {
"orderId": "42"
}
}
[{
key: "order",
value:Store orderId here
}]
Also, when a GET request is sent to Application, the current orderId is returned.
Application implementation
Now let's implement the application. Since we will use JSON this time, add the library to Quarkus.
HelloResource.java
https://linuxtut.com/en/bf50cb3aa14b88243ad2/ 2/5
14/9/2021 I tried using Dapr in Java to facilitate microservice development
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class HelloResource {
@ConfigProperty(name = "daprapp.daprport")
String daprPort;
@GET
@Path("/order")
public Map<String, Object> order() throws IOException, InterruptedException {
return Map.of("orderId", get(stateUrl() + "/order").body());
}
@POST
@Path("/neworder")
public HttpResponse neworder(Map<String, Map<String, Object>> data) throws IOException, InterruptedException {
System.out.println("orderId: " + data.get("data").get("orderId"));
I haven't done anything special as JAX-RS, so I won't go into details, but new order is the endpoint for registration and ʻorder` is the
endpoint for reference.
You are accessing http: // localhost: 3500 / v1.0 / state / statestore in each method. This is the endpoint of Dapr's state
management API. This time, the substance of this state management API is Redis. The request and response of the state management
API will be JSON as shown below.
[{
key:value,
value:value
}]
components/statestore.yaml
https://linuxtut.com/en/bf50cb3aa14b88243ad2/ 3/5
14/9/2021 I tried using Dapr in Java to facilitate microservice development
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: statestore
spec:
type: state.redis
metadata:
- name: redisHost
value: localhost:6379
- name: redisPassword
value: ""
- name: actorStateStore
value: "true"
test
Now that the implementation is complete, let's check the operation. First, start Dapr.
$ dapr run --app-id javaapp --app-port 8080 --port 3500 ./mvnw quarkus:dev
I think you can confirm that the requested value you threw is stored and you can get it. You can also write POST with the Dapr
command as follows.
$ dapr invoke --app-id javaapp --method neworder --payload '{"data": { "orderId": "41" } }'
Summary
For the time being, I tried using Dapr from Java. Since it is REST, I was able to implement it without any problems. Dapr has a Jakarta
EE feeling more than Istio, and apart from Dapr itself, this idea itself seems to be in the right direction from the viewpoint of ease of
development. It is a basic idea to hide the substance of services and data including DAO patterns, and non-functionals should be
integrated into the infrastructure side as much as possible.
On the other hand, if the persistence layer also goes through Proxy, it seems that a certain amount of overhead is inevitable even if
gRPC is used. I think that it will be required in the future how to deal with this area by design.
Dapr itself is still in its infancy and there are many rough edges, but I would like to touch it a little more in the future.
Reference link
Distributed Application Runtime - Dapr - Microsoft releases "Dapr", which facilitates microservice development, as open source.
Providing inter-service calling, state management, inter-service messaging, etc. - Dapr + Hello world with Java.
Recommended Posts
[Note] Execute java program in the integrated development environment Eclipse-I tried using git
[Java] I tried to connect using a connection pool with Servlet (tomcat) & MySQL & Java
I tried to explain Effective Java 3rd edition "almost all chapters" in "easy-to-read Japanese".
https://linuxtut.com/en/bf50cb3aa14b88243ad2/ 5/5