How to communicate between microservices (request/response) - microservices

can anyone help on below issue?
I have two microservices A and B where service A sends request to B, B fetches data from database and sends the response back to A, based on received data, I am doing few operations.
At present, I am using Http client call.I don't want to use http request? How can I proceed further?
Thanks,
Revathi

If you want to use HTTP to communicate between 2 services, you need a HTTP client that allows you to specify the parameters related to your request. Example in Java (more details here):
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("<YOUR_ENDPOINT>"))
.build();
client.sendAsync(request, BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
If you don't want to use HTTP 1.1, you can take a look at gRPC, there are plenty examples on the web on how to use both of them

Related

Best way to pass messages between 2 different protocol routes connected to same client

I have a http route /checkout which initiates a workflow process in Zeebe. The checkout route will return 200 response straight-away to calling client. Now, workflow will run for a while. So to push the response back to client after completion, I have a /sse separate route for server-sent events. Here, I will store all the client connection in a global map.
My doubt is how do I find the exact client to send the response back through sse once?
Example: Client A listening to /sse and calls /checkout endpoint which will return 200. The /sse has to return the response to client A after completion.
Currently, I thought of using cookie to identify the client. Is there a better way?
If you are already using cookies in your app than that's the way to go since the very purpose of cookies is identifying the client, so if you already have that, you should use it.
But if you rely on another authentication mechanism (like JWT), what you could do is using the url as a query.
So in the client instead of
let eventSource = new EventSource("/sse");
Do
let eventSource = new EventSource("/sse?authorization=jwt_token");
In the backend, you would validate that token, extract the Client ID and hit that global map with it to retrieve the corresponding connection.
(PS: instead of a global map, you should use a proper store, like redis, or an embedded key/value store like bbolt)

HttpSession vs RequestDispatcher

Please let me help to understand when to use session instead of RequestDispatcher.
So far I have seen I can pass data from servlet to servlet and jsp forwarding the request and It can pretty much distinguish two different request. So when and why should I use Session? Please help me to understand that.
HTTP is stateless protocol following request/response pattern. It means that you get request from client and send back response. There is no conversational state between client and server.
So if you need to keep conversational state (example : shopping card, wizard, etc.) - you need to recognize your client (understand which request comes from which client). This is what is session used for.

grpc header/cookie in Go

I want to do place on server application which can be called by Go APP and Java app both.
for some reason ,there's a cookie authentication and oAuth mechanism ,so I want to set one Go app as Auth Micro-service for the authentication purpose.
As GRPC is built on the HTTP2 ,so The headers and cookies are on the protocol.but I did not find out how to carry on header and cookie when the rpc occurs,implemented by Go, on GitHub I only found the JAVA-Implementation for headers at :
https://github.com/grpc/grpc-java/tree/master/examples/src/main/java/io/grpc/examples/header
Can anybody give me some direction of Go implementation for this purpose?
Headers in gRPC are called "Metadata." Clients can only send "headers". Servers can send both "headers" and "trailers."
You want to:
Use the google.golang.org/grpc/metadata package and metadata.NewContext() to send metadata from the client-side.
Use grpc.SendHeader() and grpc.SetTrailer() to send metadata from the server-side.
Use the grpc.Header() and grpc.Trailer() CallOptions for receiving the Metadata on the client-side.
Use metadata.FromContext() for receiving metadata on the server-side.

Spring client for server send events

I am using the ResponseBodyEmitter in Spring 4.2 RC1 for sending JSON object as part of the server send events. I am able to see the response in web browser. Is there any spring client for the same ?
I tried using AsyncRestTemplate, but I am getting all the response together. Ie if I am sending 3 objects one after another using ResponseBodyEmitter , using AsyncRestTemplate, I get all of them together. I would like to receive in client as and when the server sends it.
You could give a go to Reactor HTTP client if you're into async and reactive programming: https://github.com/reactor/reactor/blob/master/reactor-net/src/test/groovy/reactor/io/net/http/HttpSpec.groovy#L74.

PostCatcher Equivalent For Https?

I have been using postcatcher.in to test my HTTP post requests. But, I have no been able to find an equivalent service which supports HTTPS. Does anyone knows of something like that?
Beeceptor - can help you. It is real-time and supports HTTPs.
Request payload inspection
Mocking the response when a request path matches
(Disclaimer: I am the author and developed this as a need to proxy requests to an HTTP endpoint and still want to mock a few requests)

Resources