-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($api-gateway): dynamic service registration sense for OpenAPI
- Loading branch information
1 parent
c1b823c
commit 72f4277
Showing
4 changed files
with
50 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
.../src/main/kotlin/com/jmsoftware/maf/apigateway/event/ConsulCatalogWatchEventSubscriber.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |