Try to link Ruby and Java with Dapr

Introduction

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

Configuration overview

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.

dapr_sample01.png

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.

code

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

Run & Confirm

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  

Summary

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

Try to link Ruby and Java with Dapr
Link Java and C ++ code with SWIG
Let's try WebSocket with Java and javascript!
Convert JSON to TSV and TSV to JSON with Ruby
[Raspberry Pi] Try to link Apache2 and Tomcat
From Java to Ruby !!
Try to get redmine API key with ruby
Try to implement TCP / IP + NIO with JAVA
Solving with Ruby and Java AtCoder ABC129 D 2D array
[Java] Refer to and set private variables with reflection
I want to transition screens with kotlin and java!
Try connecting to AzureCosmosDB Emulator for Docker with Java
Try DB connection with Java
Try gRPC with Java, Maven
Connect to DB with Java
Connect to MySQL 8 with Java
Setting method to link Java of Eclipse and Github / September 2017
[Beginner] Try to make a simple RPG game with Java ①
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
Sample code to parse date and time with Java SimpleDateFormat
I want to implement various functions with kotlin and java!
How to deal with different versions of rbenv and Ruby
Write DiscordBot to Spreadsheets Write in Ruby and run with Docker
Solving with Ruby, Perl and Java AtCoder ABC 129 C (Part 1)
Java to learn with ramen [Part 1]
Use java with MSYS and Cygwin
Distributed tracing with OpenCensus and Java
Install Java and Tomcat with Ansible
Try to implement Yubaba in Ruby
[Java] Points to note with Arrays.asList ()
Dare to challenge Kaggle with Java (1)
Use JDBC with Java and Scala.
Try using Redis with Java (jar)
I tried to interact with Java
Try to imitate marshmallows with MiniMagick
Follow the link with Selenium (Java)
Output PDF and TIFF with Java 8
[Java] Try to implement using generics
Try to extract java public method
Try bidirectional communication with gRPC Java
Try to implement Yubaba in Java
Java, arrays to start with beginners
Encrypt with Java and decrypt with C #
Design patterns to try with Swift-Iterator patterns that support Arrays and Dictionary
I want to return to the previous screen with kotlin and java!
Try to speed up Java console program startup with GraalVM native-image
Run logstash with Docker and try uploading data to Elastic Cloud
AtCoder ARC 081 C hash to solve in Ruby, Perl and Java
How to encrypt and decrypt with RSA public key in Java
Read the data of Shizuoka point cloud DB with Java and try to detect the tree height.
With ruby ● × Game and Othello (basic review)
Interface Try to make Java problem TypeScript 7-3
[Ruby / Refactoring] From Ruby iterative processing like Java and C language to Ruby-like iterative processing
Try to solve Project Euler in Java
How to compile Java with VsCode & Ant
Monitor Java applications with jolokia and hawtio
Getting Started with Ruby for Java Engineers
[Java] How to compare with equals method
Try to implement n-ary addition in Java
Introduction to Ruby basic grammar with yakiniku