Skip to main content

Posts

Showing posts from July, 2023

How to communicate two microservices with different ports?

  When two microservices need to communicate with each other but are running on different ports, there are a few approaches : 1.Direct Communication: If the microservices are deployed on the same network or infrastructure, they can communicate directly using their network addresses. You can use HTTP or any other appropriate protocol for communication. In this approach, each microservice would need to know the network address (IP address or domain name) and the port of the other microservice. Example: Microservice A running on port 8080 wants to communicate with Microservice B running on port 9090. Microservice A can make an HTTP request to Microservice B using its network address and port: HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("http://localhost:9090/api/resource")) .build(); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); 2.Service Disco

New Features in Java 11

  Java 11 introduced several new features and enhancements. Here are some notable features with examples: Local-Variable Syntax for Lambda Parameters: Java 11 allows var to be used in lambda parameters, which provides improved readability and conciseness. Example: java // Before Java 11   someList.forEach((String item) -> { System.out.println(item); }); // With Java 11   someList.forEach(( var item) -> { System.out.println(item); }) HTTP Client (Standard API): Java 11 introduced a new HTTP client API as a replacement for the deprecated HttpURLConnection . It provides a more modern and flexible way to send HTTP requests and handle responses. Example import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; public class HttpClientExample { public static void main (String[] args) throws Exception { HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder(