It is Dapr following I tried using Dapr that facilitates microservice development in Java.
Corresponds to Step 6 of Tutorial. See below for code https://github.com/koduki/example-dapr/tree/v01
The tutorial uses Python, but I'm using Ruby for my hobby. The point of this configuration is that Ruby and Java do not communicate directly, but Ruby only talks to its sidecar Dapr, not to Dapr running Java.
Since the test is local, you can actually speak directly, but for the sake of clarity, let's set the Java Dapr port to 3500 and the Ruby Dapr port to 3600.
This time, it's a super-simple code that just makes a request to the target API once a second.
javaapp
is the appid of the Java app. Use this to automatically find the right app for Dpar. There is no need to specify the IP or port number on which the API is running.
The http: // localhost: # {MY_DAPR_PORT}
specified in the Ruby code is not the Dapr on the Java side, but the Dapr on the Ruby side. So it's always localhost, and the port number is 3600 instead of 3500 in this case.
require 'net/https'
require "json"
MY_DAPR_PORT=ARGV[0]
n = 0
while true do
n += 1
params = {data: {orderId: n}}
url = "http://localhost:#{MY_DAPR_PORT}/v1.0/invoke/javaapp/method/neworder"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
headers = { "Content-Type" => "application/json" }
response = http.post(uri.path, params.to_json, headers)
sleep 1
end
Let's run it. Dapr seems to be able to wrap not only web applications but also commands without problems. First, launch the API side.
$ dapr run --app-id javaapp --app-port 8080 --port 3500 ./mvnw quarkus:dev
Then launch the client side in another terminal.
$ dapr run --app-id rubyapp --port 3600 ruby app.rb 3600
Looking at the execution result on the Java side, it is as follows.
== APP == orderId: 1
== APP == orderId: 2
== APP == orderId: 3
You can see that it is running regularly every second.
You can also check the list of Dapr that is currently running by executing dapr list
.
$dapr list
APP ID HTTP PORT GRPC PORT APP PORT COMMAND AGE CREATED PID
rubyapp 3600 55932 0 ruby app.rb 3600 28m 2020-05-24 21:08.13 44897
javaapp 3500 56777 8080 ./mvnw quarkus:dev 3d 2020-05-21 21:21.30 27471
I was able to successfully communicate with Ruby and Java via Dapr.
The interesting thing about this architecture is that both Ruby and Java code are only talking to Dapr, so you don't have to worry too much about security during that time with HTTP or gRPC. There is no problem if Daprs do communication encryption and authentication, so it is great to be able to remove the security related part.
There should be other connectors such as distributed tracing, Twitter API, Kafka, etc., so I would like to try that area as well. At first I thought that it was a layer similar to KNative because it was the same microservice support, but while that was from the viewpoint of infrastructure such as build environment, FaaS, Serveless, this is considerably more mechanism than application, so it was adopted by CNCF. There is no doubt that it is a tool that is worrisome in the future.
Recommended Posts