Monday, June 9, 2025

Troubleshoot API-Gateway

 

Create a Proper Test Controller

java
Copy
Download
package com.example.api_gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class ApiGatewayApplication {

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

@RestController
class TestController {
    
    @GetMapping("/gateway-test")
    public String testEndpoint() {
        return "Gateway is working properly!";
    }
}

2.Verify Service Accessibility

Create a simple test to ensure your greet service is reachable:

java
Copy
Download
package com.example.api_gateway;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;

@Configuration
class ServiceHealthCheck {
    
    @Bean
    public String checkGreetService() {
        WebClient.create("http://localhost:8001")
            .get()
            .uri("/greet")
            .retrieve()
            .toBodilessEntity()
            .subscribe(
                response -> System.out.println("Greet service is UP! Status: " + response.getStatusCode()),
                error -> System.err.println("Greet service is DOWN! Error: " + error.getMessage())
            );
        return "Health check initiated";
    }
}

3. Final Troubleshooting Steps

  1. Remove any existing build artifacts:

    bash
    Copy
    Download
    mvn clean package
  2. Check for port conflicts:

    bash
    Copy
    Download
    netstat -ano | findstr :9191
    netstat -ano | findstr :8001
  3. Verify service response:

    bash
    Copy
    Download
    curl -v http://localhost:8001/greet
  4. Test gateway endpoints:

    bash
    Copy
    Download
    curl -v http://localhost:9191/gateway-test
    curl -v http://localhost:9191/greet

Expected Behavior After Fix

  1. http://localhost:9191/gateway-test should return:

    text
    Copy
    Download
    Gateway is working properly!
  2. http://localhost:9191/greet should proxy to your service at http://localhost:8001/greet

If You Still Encounter Issues

Add this controller to debug routing:

java
Copy
Download
package com.example.api_gateway;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DebugConfig {

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
            .route("debug-greet-route", r -> r
                .path("/greet/**")
                .filters(f -> f.rewritePath("/greet/(?<segment>.*)", "/${segment}"))
                .uri("http://localhost:8001"))
            .route("direct-test", r -> r
                .path("/direct-test")
                .uri("http://httpbin.org/get"))
            .build();
    }
}

Then test:

text
Copy
Download
http://localhost:9191/direct-test

This should return a response from httpbin.org, proving your gateway is working. If this works but /greet doesn't, the issue is with your service or the URI configuration.

No comments:

Post a Comment