Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaJava Spring Boot Microservices – Integration of Eureka and Spring Cloud Gateway

Java Spring Boot Microservices – Integration of Eureka and Spring Cloud Gateway

Microservices are small, loosely coupled distributed services. Microservices architecture evolved as a solution to the scalability, independently deployable, and innovation challenges with Monolithic Architecture. It provides us to take a big application and break it into efficiently manageable small components with some specified responsibilities. It is considered the building block of modern applications.

What is Eureka?

Service Discovery is one of the major things of a microservice-based architecture. Eureka is the Netflix Service Discovery Server and Client. The server can be configured and deployed to be highly functional, with each server copying the state of the registered services to the others.

What is Spring Cloud Gateway?

Spring Cloud Gateway provides a library for making API gateways on top of Spring and Java. It provides a flexible way of routing requests based on a number of criteria, as well as focuses on cross-cutting problems like security, resiliency, and monitoring. Some of the important features of Spring Cloud Gateway are:

  • It is Built on Spring Framework 5, Project Reactor, and Spring Boot 2.0
  • You can integrate Circuit Breaker with Spring Cloud Gateway
  • You can integrate Spring Cloud DiscoveryClient
  • Predicates and filters are specific to routes
  • Path Rewriting
  • It is able to match routes on any request attribute, etc.

Note: Please refer to this article to know more about Spring Cloud Gateway.

So in this article, we will integrate all the 2 popular tools with our Microservices. We are going to develop 1 microservice, 1 API Spring Cloud Gateway, and 1 service discovery using Eureka.

Developing Service Discovery using Eureka

To develop a Service Discovery using Eureka, please refer to this article Java Spring Boot Microservices – Developing Service Discovery.

Developing API Gateway Using Spring Cloud Gateway

Step 1: Create a New Spring Boot Project in Spring Initializr

To create a new Spring Boot project, please refer to How to Create a Spring Boot Project in Spring Initializr and Run it in IntelliJ IDEA. For this project choose the following things

  • Project: Maven
  • Language: Java
  • Packaging: Jar
  • Java: 17

 Please choose the following dependencies while creating the project.

  • Gateway (SPRING CLOUD ROUTING)
  • Eureka Discovery Client

Below is the complete pom.xml file. Please cross-verify if you have missed some dependencies

XML




<?xml version="1.0" encoding="UTF-8"?>
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ey</groupId>
    <artifactId>spring-cloud-gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Spring Cloud Gateway</name>
    <description>Spring Cloud Gateway</description>
    <properties>
        <java.version>17</java.version>
        <spring-cloud.version>2022.0.3</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
 
    <repositories>
        <repository>
            <id>netflix-candidates</id>
            <name>Netflix Candidates</name>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
 
</project>


Step 2: Make Changes in Your application.yaml file

Now make the following changes in your application.yaml file.

server:
port: 8085
spring:
application:
name: API-GATEWAY-SERVICE
cloud:
gateway:
routes:
- id: DEMO-SERVICE
uri: http://localhost:9090
predicates:
- Path=/demo/**


Here,

  • id: You can give any id as of now.
  • uri: Here you have to provide the port in which your microservice is running
  • predicates (- Path): Here we have provided the path “/demo/**”, which means any request starting with path “/demo/**” route it to DEMO-SERVICE.

Develop the DEMO-SERVICE

To create a new service, please refer to How to Create a Spring Boot Project in Spring Initializr and Run it in IntelliJ IDEA. For this project choose the following things

  • Project: Maven
  • Language: Java
  • Packaging: Jar
  • Java: 17

 Please choose the following dependencies while creating the project.

  • Spring Web
  • Eureka Discovery Client

Below is the complete pom.xml file. Please cross-verify if you have missed some dependencies

XML




<?xml version="1.0" encoding="UTF-8"?>
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.gfg</groupId>
    <artifactId>demo-ms</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo-ms</name>
    <description>Demo project for Spring Cloud Gateway</description>
    <properties>
        <java.version>17</java.version>
        <spring-cloud.version>2022.0.2</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
 
    <repositories>
        <repository>
            <id>netflix-candidates</id>
            <name>Netflix Candidates</name>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
 
</project>


Step 2: Make Changes in Your application.properties File

Now make the following changes in your application.properties file.\

server.port=9090
spring.application.name=DEMO-SERVICE

Step 3: Create a DemoController

Go to the src > main > java > controller and create a class DemoController and put the below code. In this, we have created a simple REST API in our controller class.

Java




package com.gfg.demo.controller;
 
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
@RequestMapping("/demo")
public class DemoController {
 
    @GetMapping("/gfg")
    public ResponseEntity<String> getAnonymous() {
        return ResponseEntity.ok("Welcome to Lazyroar");
    }
 
}


Now run your application and test it out.

Testing in Postman

Now run all the 3 applications. Let’s test our API. Hit the following URL

http://localhost:9090/demo/gfg

And you are going to get a response like this

Now we can get the same response by using our API gateway port which is 8085. Now hit the following URL

http://localhost:8085/demo/gfg

And you are going to get a response like this

So this is how the API gateway works. If you have hundreds of microservices then you don’t need to remember the port of all microservices. You can just configure them in your API Gateway and you can access all your API by using only one port. And this is our eureka dashboard.

RELATED ARTICLES

Most Popular

Recent Comments