Use load balancer name for Microservices instead of host, port for URI in Spring Cloud Gateway

Introduction

Here we will see how to use load balancer name or service name for Microservices instead of host, port for URI while building API gateway using Spring Cloud Gateway – the tutorial example which we have created earlier. We will use the same tutorial to use load balancer name instead of URL, such as, http://localhost:9000, http://localhost:9001, etc. for the URI value in application.yml configuration file.

The advantage of using the load balancer name is you don’t need to worry about the change of the internal URLs for the microservices. This also works as a security mechanism as the internal URL of the microservice is hidden from the gateway.

Prerequisites

How to build Spring Cloud Gateway for Microservices

Update build.gradle Script

Now we need to update the build.gradle script to include the required dependency for netflix eureka client.

implementation("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client")

The whole build.gradle script file is given below:

buildscript {
	ext {
		springBootVersion = '2.2.6.RELEASE'
	}
    repositories {
    	mavenLocal()
    	mavenCentral()
    }
    dependencies {
    	classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

plugins {
    id "io.spring.dependency-management" version "1.0.9.RELEASE"
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
    
sourceCompatibility = 12
targetCompatibility = 12

repositories {
	mavenLocal()
    mavenCentral()
}

dependencies {
	implementation("org.springframework.cloud:spring-cloud-starter")
	implementation("org.springframework.cloud:spring-cloud-starter-gateway")
	implementation("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client")
}

dependencyManagement {
    imports {
        mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Hoxton.SR3'
    }
}

Update application.yml File

Now we will update the src/main/resources/application.yml file to register the gateway with the Eureka server.

Look at the below configuration file carefully. We have added few configurations.

We have registered the gateway application with Eureka server. We have also changed the URLs to load balancer names using lb:// prefix for currency conversion and forex services.

server:
   port: 9090
   
spring:
   application:
       name: gateway
   cloud:
       gateway:
           routes:
            - id: currency-conversion-service
              #uri: http://localhost:9100/
              uri: lb://currency-conversion-service/
              predicates:
               - Path=/cc-converter/**
              filters:
               - RewritePath=/cc-converter/from/(?<from>.*)/to/(?<to>.*)/quantity/(?<quantity>.*), /currency-converter/from/$\{from}/to/$\{to}/quantity/$\{quantity}
            - id: forex-service
              #uri: http://localhost:9000/, http://localhost:9001/
              uri: lb://forex-service/
              predicates:
               - Path=/fx-exchange/**
              filters:
               - RewritePath=/fx-exchange/from/(?<from>.*)/to/(?<to>.*), /forex-exchange/from/$\{from}/to/$\{to}
           discovery:
              locator:
                 enabled: true
                 lower-case-service-id: true

eureka:
   client:
      service-url:
         default-zone: http://localhost:8761/eureka

Update Main Class

Finally we will update the main class to make it as a client to the Eureka server by putting @EnableEurekaClient annotation.

package com.roytuts.spring.cloud.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
public class SpringCloudGatewayApp {

	public static void main(String[] args) {
		SpringApplication.run(SpringCloudGatewayApp.class, args);
	}

}

Testing the Application

Make sure you deploy all your applications. Now you will see all your services are up in the Eureka server at URL http://localhost:8761.

load balancer name for microservices instead of host port for uri in spring cloud gateway

In the above image you clearly see all services have the corresponding names.

Now you can test the same URLs you tested for How to build Spring Cloud Gateway for Microservices and you will see the same output.

Thanks for reading.

Related posts

Leave a Comment