Skip to content

Commit

Permalink
feat($api-gateway): dynamic service registration sense for OpenAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Jun 26, 2022
1 parent c1b823c commit 72f4277
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ spring:
prefer-ip-address: true
ip-address: ${spring.cloud.client.ip-address}
health-check-critical-timeout: 15s
catalog-services-watch-delay: 30_000
gateway:
discovery:
locator:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class OpenApiConfiguration(
private val discoveryClient: DiscoveryClient
) {
companion object {
const val GROUPED_OPEN_API_LIST_NAME = "groupedOpenApiList"
private const val SWAGGER_API_URI = "/v3/api-docs"
private val log = logger()
}
Expand All @@ -56,8 +57,12 @@ class OpenApiConfiguration(
)
}

@Bean
@Bean(GROUPED_OPEN_API_LIST_NAME)
fun groupedOpenApiList(): List<GroupedOpenApi> {
return createGroupedOpenApiList()
}

fun createGroupedOpenApiList(): List<GroupedOpenApi> {
val groups = mutableListOf<GroupedOpenApi>()
swaggerUiConfigProperties.urls = mutableSetOf()
mutableListOf<String>().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class RedirectConfiguration {
fun doc(): RouterFunction<ServerResponse> {
return RouterFunctions.route(RequestPredicates.GET("/doc")) {
log.info("Redirect to Bootstrap Swagger API documentation.")
ServerResponse.temporaryRedirect(URI.create("/doc.html")).build()
ServerResponse.temporaryRedirect(URI.create("/doc.html#/home/en-US")).build()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.jmsoftware.maf.apigateway.event

import cn.hutool.extra.spring.SpringUtil
import com.jmsoftware.maf.apigateway.configuration.OpenApiConfiguration
import com.jmsoftware.maf.apigateway.configuration.OpenApiConfiguration.Companion.GROUPED_OPEN_API_LIST_NAME
import com.jmsoftware.maf.common.util.logger
import org.springframework.cloud.client.discovery.event.HeartbeatEvent
import org.springframework.context.event.EventListener
import org.springframework.stereotype.Component
import java.util.concurrent.atomic.AtomicReference

/**
* # ConsulCatalogWatchEventSubscriber
*
* Change description here.
*
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, 6/26/22 9:35 AM
**/
@Component
class ConsulCatalogWatchEventSubscriber(
private val openApiConfiguration: OpenApiConfiguration
) {
private val consulIndex: AtomicReference<Long> = AtomicReference(0L)

companion object {
private val log = logger()
}

@EventListener
fun onApplicationEvent(heartbeatEvent: HeartbeatEvent) {
val newValue = heartbeatEvent.value as Long
if (consulIndex.get().equals(newValue)) {
return
}
consulIndex.set(newValue)
log.info("Consul index has changed, consulIndex: ${consulIndex.get()}")
SpringUtil.unregisterBean(GROUPED_OPEN_API_LIST_NAME)
log.warn("Unregistered bean groupedOpenApiList")
SpringUtil.registerBean(GROUPED_OPEN_API_LIST_NAME, openApiConfiguration.createGroupedOpenApiList())
log.warn("Re-registered bean groupedOpenApiList")
}
}

0 comments on commit 72f4277

Please sign in to comment.