Streamlining Microservices with API Gateway in Spring Boot: Simplify Integration and Enhance Performance

Mar 10, 2025

Follow us on


Explore the power of API Gateway in Spring Boot for orchestrating microservices architecture. Our comprehensive guide illuminates the path to seamless integration, enhanced security, and optimized performance.

Streamlining Microservices with API Gateway in Spring Boot: Simplify Integration and Enhance Performance

Streamlining Microservices with API Gateway in Spring Boot: Simplify Integration and Enhance Performance


FOLLOW THESE STEPS


Step 1: POM.XML

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.1</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.heycolleagues</groupId>
	<artifactId>Student-Service</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Student-Service</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>



Step 2: application.properties

    
    # MySQL connection
    server.port=9090
   spring.datasource.url=jdbc:mysql://localhost:3306/studentservice
   spring.datasource.username=root
   spring.datasource.password=root
           
   # hibernate Configuration
   spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
   spring.jpa.hibernate.ddl-auto=update
   spring.jpa.show-sql=true
   spring.jpa.properties.hibernate.format_sql=true
   
   
   

Step 3: class- Student.java

    
    package com.heycolleagues.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Student {
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long studentId;
	private String studentName;
	private String studentAge;
	public Long getStudentId() {
		return studentId;
	}
	public void setStudentId(Long studentId) {
		this.studentId = studentId;
	}
	public String getStudentName() {
		return studentName;
	}
	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}
	public String getStudentAge() {
		return studentAge;
	}
	public void setStudentAge(String studentAge) {
		this.studentAge = studentAge;
	}
	public Student(Long studentId, String studentName, String studentAge) {
		super();
		this.studentId = studentId;
		this.studentName = studentName;
		this.studentAge = studentAge;
	}
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
	
	

}



Step 4: interface- StudentRepository.java

    package com.heycolleagues.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.heycolleagues.model.Student;

public interface StudentRepository extends JpaRepository<Student, Long>{

}



 

Step 5: Interface- StudentService.java

    
    package com.heycolleagues.service;


    import com.heycolleagues.model.Student;
    import com.heycolleagues.response.templates.StudentDegreeResponse;
    
    public interface StudentService {
        
        public Student createStudent(Student student);
        public Student getStudent(Long studentId);
        
    
    }
    

Step 6: class- StudentServiceImplement.java

    
    package com.heycolleagues.serviceImplement;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.heycolleagues.model.Student;
import com.heycolleagues.repository.StudentRepository;
import com.heycolleagues.response.templates.Degree;
import com.heycolleagues.response.templates.StudentDegreeResponse;
import com.heycolleagues.service.StudentService;

@Service
public class StudentServiceImplement implements StudentService{
	

	@Autowired
	StudentRepository studentRepository;

	@Override
	public Student createStudent(Student student) {
		return this.studentRepository.save(student);
	}

	@Override
	public Student getStudent(Long studentId) {
		return this.studentRepository.findById(studentId).get();
	}

}   

Step 7: Class- StudentController.java


    package com.heycolleagues.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.heycolleagues.model.Student;
import com.heycolleagues.response.templates.StudentDegreeResponse;
import com.heycolleagues.serviceImplement.StudentServiceImplement;

@RestController
@RequestMapping("/student")
public class StudentController {
	
	@Autowired
	StudentServiceImplement serviceImplement;
	
	@PostMapping("/save")
	public ResponseEntity<Student> createStudent(@RequestBody Student student){
		return ResponseEntity.ok(this.serviceImplement.createStudent(student));
	}
	
	@GetMapping("/{id}")
	public Student getStudentById(@PathVariable("id") Long studentid) {
		return serviceImplement.getStudent(studentid);
	}
	
	@GetMapping("/degree/{id}")
	public StudentDegreeResponse getStudentWithDegree(@PathVariable("id") Long studentid) {
		return serviceImplement.getStudentWithDegree(studentid);
	}

}




2nd MICROSERVICE


Step 1: POM.XML

    
    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.1</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.heycolleagues</groupId>
	<artifactId>Degree-Service</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Degree-Service</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>



Step 2: application.properties

    
    # MySQL connection
server.port=9091
spring.datasource.url=jdbc:mysql://localhost:3306/degreeservice
spring.datasource.username=root
spring.datasource.password=root
    
# hibernate Configuration
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

#Application Configuration
spring.application.name=Degree-Service



Step 3: Class- Degree.java

    
    package com.heycolleagues.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Degree {
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long degreeId;
	private String degreeName;
	public Long getDegreeId() {
		return degreeId;
	}
	public void setDegreeId(Long degreeId) {
		this.degreeId = degreeId;
	}
	public String getDegreeName() {
		return degreeName;
	}
	public void setDegreeName(String degreeName) {
		this.degreeName = degreeName;
	}
	public Degree(Long degreeId, String degreeName) {
		super();
		this.degreeId = degreeId;
		this.degreeName = degreeName;
	}
	public Degree() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	

}



Step 4:Interface- DegreeRepository.java

    package com.heycolleagues.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.heycolleagues.model.Degree;

public interface DegreeRepository extends JpaRepository<Degree, Long>{

}



Step 5: Interface- DegreeService.java

    package com.heycolleagues.service;

import com.heycolleagues.model.Degree;

public interface DegreeService {
	
	public Degree createDegree(Degree degree);
	public Degree getDegree(Long degreeId);
	

}



Step 6: Class- DegreeServiceImplement.java

    
    package com.heycolleagues.serviceImplement;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.heycolleagues.model.Degree;
import com.heycolleagues.repository.DegreeRepository;
import com.heycolleagues.service.DegreeService;

@Service
public class DegreeServiceImplement implements DegreeService{
	
	@Autowired
	DegreeRepository degreeRepository;

	@Override
	public Degree createDegree(Degree degree) {
		return this.degreeRepository.save(degree);
	}

	@Override
	public Degree getDegree(Long degreeId) {
		return this.degreeRepository.findById(degreeId).get();
	}

}



Step 7: Class- DegreeController.java

    package com.heycolleagues.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.heycolleagues.model.Degree;
import com.heycolleagues.serviceImplement.DegreeServiceImplement;

@RestController
@RequestMapping("/degree")
public class DegreeController {

	@Autowired
	DegreeServiceImplement degreeServiceImplement;
	
	@PostMapping("/save")
	public ResponseEntity<Degree> createDegree(@RequestBody Degree degree){
		return ResponseEntity.ok(this.degreeServiceImplement.createDegree(degree));
	}
	
	@GetMapping("/{id}")
	public Degree getDegreeById(@PathVariable("id") Long degreeId) {
		return this.degreeServiceImplement.getDegree(degreeId);
	}
}




Make two Microservices Communicate


👉 MICROSERVICES 1st

Step 1: Class- Degree.java

   
	package com.heycolleagues.response.templates;

public class Degree {
	
	private Long degreeId;
	private String degreeName;
	public Long getDegreeId() {
		return degreeId;
	}
	public void setDegreeId(Long degreeId) {
		this.degreeId = degreeId;
	}
	public String getDegreeName() {
		return degreeName;
	}
	public void setDegreeName(String degreeName) {
		this.degreeName = degreeName;
	}
	public Degree(Long degreeId, String degreeName) {
		super();
		this.degreeId = degreeId;
		this.degreeName = degreeName;
	}
	public Degree() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	

}



Step 2: Class- StudentDegreeResponse.java

    
    package com.heycolleagues.response.templates;

    import com.heycolleagues.model.Student;
    
    public class StudentDegreeResponse {
        
        public Student student;
        public Degree degree;
        public Student getStudent() {
            return student;
        }
        public void setStudent(Student student) {
            this.student = student;
        }
        public Degree getDegree() {
            return degree;
        }
        public void setDegree(Degree degree) {
            this.degree = degree;
        }
        public StudentDegreeResponse(Student student, Degree degree) {
            super();
            this.student = student;
            this.degree = degree;
        }
        public StudentDegreeResponse() {
            super();
            // TODO Auto-generated constructor stub
        }
        
        
    
    }
    

Step 3: Interface- StudentService.java

    public StudentDegreeResponse  getStudentWithDegree(Long studentId);

Step 4: Class- StudentServiceApplication.java

    @Bean
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}


Step 5: Class- StudentServiceImplement.java

    @Autowired
	RestTemplate restTemplate;
    

    @Override
	public StudentDegreeResponse getStudentWithDegree(Long studentId) {
		StudentDegreeResponse studentDegreeResponse = new StudentDegreeResponse();
		Student student = this.studentRepository.findById(studentId).get();
		Degree degree = restTemplate.getForObject("http://localhost:9091/degree/"+ student.getStudentId() , Degree.class);
		studentDegreeResponse.setStudent(student);
		studentDegreeResponse.setDegree(degree);
		return studentDegreeResponse;
	}
	

Step 7: Class- StudentController.java

    
    @GetMapping("/degree/{id}")
	public StudentDegreeResponse getStudentWithDegree(@PathVariable("id") Long studentid) {
		return serviceImplement.getStudentWithDegree(studentid);
	}


API GATEWAY


Step 1: POM.XML

    
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.7.1</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.heycolleagues</groupId>
        <artifactId>API-Gateway</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>API-Gateway</name>
        <description>Demo project for Spring Boot</description>
        <properties>
            <java.version>17</java.version>
            <spring-cloud.version>2021.0.3</spring-cloud.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-gateway</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>
    
    </project>
    

Step 2: Application.yml

    server:
  port: 9093

spring:
  application:
    name: API-GATEWAY
  cloud:
    gateway:
      routes:
        - id: Student
          uri: http://localhost:9090/student/**
          predicates:
            - Path=/student/**
        - id: Degree
          uri: http://localhost:9091/degree/**
          predicates:
            - Path=/degree/**  
  

 

© 2025 News 24 Taaza. All rights reserved.