avatarAgayev Ilkin

Summary

The provided content outlines the process of configuring a single Swagger UI within an API Gateway to interact with multiple Swagger UI instances from various microservices in a Spring Boot application.

Abstract

The article details the setup required to manage multiple Swagger UI interfaces for a microservices architecture through a centralized API Gateway. It begins by discussing the challenges of interacting with APIs from numerous microservices and the benefits of using an API Gateway for request consolidation. The author then provides a step-by-step guide on configuring the necessary dependencies and YAML settings for both the microservices and the API Gateway. This includes adding specific Gradle dependencies, such as spring-cloud-starter-netflix-eureka-client and springdoc-openapi-starter-webmvc-ui for microservices, and spring-cloud-starter-gateway and springdoc-openapi-starter-webflux-ui for the API Gateway. The configuration also involves setting up application.yaml with service URLs, context paths, and Swagger UI paths. The article concludes with instructions for setting up Eureka Server, which facilitates service discovery between microservices and the API Gateway, and a promotional mention of an AI service, ZAI.chat, as a cost-effective alternative to ChatGPT Plus.

Opinions

  • The author suggests that using a single Swagger UI within the API Gateway is a more efficient approach than navigating individual microservice endpoints.
  • The use of Eureka Server is recommended for service discovery and connection establishment between microservices and the API Gateway.
  • The article promotes the ZAI.chat AI service as a more affordable option compared to ChatGPT Plus, implying its potential value for users.

Swagger UI configuration for API-Gateway in Spring Boot

In this article, we will discuss managing different Swagger-UIs of various microservices using just one Swagger-UI.

OVERVIEW

In our microservices setup, each microservice comes with its own set of unique APIs. When it comes to interacting with these APIs, we’re faced with two options: either navigate through the specific endpoints and individual URLs of each microservice or streamline the process by sending requests to all of them from a central location. This centralized point, often known as an API Gateway, is instrumental in consolidating requests.

Typically, an API Gateway serves as a unified entry point for managing and directing requests. In our case, we plan to leverage the API Gateway to not only simplify the process but also to gain access to the APIs, specifically their Swagger-UIs. By configuring a Swagger-UI within the Gateway, we can conveniently interact with the various Swagger-UIs offered by different microservices.

Firstly

First, let’s take a look at the configs we’ll add in microservices.

  1. Include this dependency in the build.gradle file.
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.4'

!! When copying this, check if the following config is present below; otherwise, it won’t work.

repositories {
    mavenCentral()
    maven { url 'https://repo.spring.io/milestone' }
}

ext {
    set('springCloudVersion', "2023.0.0-RC1")
}

#dependency...

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }

2. Paste the config into the application.yaml file.

server:
  port: 8081
  servlet:
    context-path: /

spring:
  application:
    name: TEST-SERVICE

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka

openapi:
  service:
    title: test-service doc
    version: 1.0.0
    url: http://localhost:8080 #api gateway url
springdoc:
  api-docs:
    path: /test-service/v3/api-docs
  swagger-ui:
    path: /test-service/swagger-ui.html

Here, the config for (eureka) is meant for establishing a connection between API Gateway and microservices.

I’ll inform you about the eureka installation at the end of the article.

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka

Second

Now, let’s configure the settings for API Gateway.

  1. Include this dependency in the build.gradle file.
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' // This config is only for establishing a connection with microservices.
implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
implementation 'org.springdoc:springdoc-openapi-starter-webflux-ui:2.0.2'

2. Paste the config into the application.yaml file.

server:
  servlet:
    context-path:
  port: 8080

spring:
  application:
    name: GATEWAY-SERVICE
  cloud:
    gateway:
      routes:
        - id: test-service-route
          uri: http://localhost:8081
          predicates:
            - Path=/test-service/v3/api-docs
            - Method=GET
  main:
    web-application-type: reactive

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka

springdoc:
  api-docs:
    enabled: true
  swagger-ui:
    enabled: true
    path: /swagger-ui.html
    config-url: /v3/api-docs/swagger-config
    urls:
      - url: /v3/api-docs
        name: API Gateway Service
      - url: /test-service/v3/api-docs
        name: Test Service

Use this for API Gateway Swagger UI. http://localhost:8080/swagger-ui.html

Finally

The configurations used for the Eureka server are meant to establish a connection between microservices and API Gateway.

You can read my article to set up Eureka Server.

Recommended from ReadMedium