Skip to content

Commit

Permalink
feat: Support judging whether to add a load balancing interceptor bas…
Browse files Browse the repository at this point in the history
…ed on the definition
  • Loading branch information
Ahoo-Wang committed Jul 1, 2024
1 parent 64cb07d commit 35e5a31
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 13 deletions.
10 changes: 9 additions & 1 deletion spring/src/main/kotlin/me/ahoo/coapi/spring/CoApiDefinition.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ data class CoApiDefinition(
name = resolveClientName(coApi),
apiType = this,
baseUrl = baseUrl,
loadBalanced = coApi.serviceId.isNotBlank()
loadBalanced = coApi.resolveLoadBalanced()
)
}

@Suppress("ReturnCount")
private fun CoApi.resolveBaseUrl(environment: Environment): String {
if (baseUrl.isNotBlank()) {
return environment.resolvePlaceholders(baseUrl)
Expand All @@ -55,6 +56,13 @@ data class CoApiDefinition(
}
return simpleName
}

private fun CoApi.resolveLoadBalanced(): Boolean {
if (baseUrl.isNotBlank()) {
return false
}
return serviceId.isNotBlank()
}
}

val httpClientBeanName: String by lazy {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright [2022-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.ahoo.coapi.spring.client

import me.ahoo.coapi.spring.CoApiDefinition
import org.springframework.context.ApplicationContext

interface IHttpClientFactoryBean {
val definition: CoApiDefinition
val appContext: ApplicationContext

fun getBaseUrl(): String {

Check warning on line 23 in spring/src/main/kotlin/me/ahoo/coapi/spring/client/IHttpClientFactoryBean.kt

View check run for this annotation

Codecov / codecov/patch

spring/src/main/kotlin/me/ahoo/coapi/spring/client/IHttpClientFactoryBean.kt#L23

Added line #L23 was not covered by tests
val clientProperties = appContext.getBean(ClientProperties::class.java)
return clientProperties.getBaseUri(definition.name).ifBlank {
definition.baseUrl
}
}

fun loadBalanced(): Boolean {

Check warning on line 30 in spring/src/main/kotlin/me/ahoo/coapi/spring/client/IHttpClientFactoryBean.kt

View check run for this annotation

Codecov / codecov/patch

spring/src/main/kotlin/me/ahoo/coapi/spring/client/IHttpClientFactoryBean.kt#L30

Added line #L30 was not covered by tests
val clientProperties = appContext.getBean(ClientProperties::class.java)
if (clientProperties.getBaseUri(definition.name).isNotBlank()) {
return false

Check warning on line 33 in spring/src/main/kotlin/me/ahoo/coapi/spring/client/IHttpClientFactoryBean.kt

View check run for this annotation

Codecov / codecov/patch

spring/src/main/kotlin/me/ahoo/coapi/spring/client/IHttpClientFactoryBean.kt#L33

Added line #L33 was not covered by tests
}
return definition.loadBalanced
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ package me.ahoo.coapi.spring.client.reactive

import me.ahoo.coapi.spring.CoApiDefinition
import me.ahoo.coapi.spring.client.ClientProperties
import me.ahoo.coapi.spring.client.IHttpClientFactoryBean
import org.springframework.beans.factory.FactoryBean
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
import org.springframework.web.reactive.function.client.ExchangeFilterFunction
import org.springframework.web.reactive.function.client.WebClient

abstract class AbstractWebClientFactoryBean(private val definition: CoApiDefinition) :
abstract class AbstractWebClientFactoryBean(override val definition: CoApiDefinition) :
IHttpClientFactoryBean,
FactoryBean<WebClient>,
ApplicationContextAware {
protected lateinit var appContext: ApplicationContext
override lateinit var appContext: ApplicationContext

protected open val builderCustomizer: WebClientBuilderCustomizer = WebClientBuilderCustomizer.NoOp

Expand All @@ -36,9 +38,7 @@ abstract class AbstractWebClientFactoryBean(private val definition: CoApiDefinit
val clientBuilder = appContext
.getBean(WebClient.Builder::class.java)
val clientProperties = appContext.getBean(ClientProperties::class.java)
val baseUrl = clientProperties.getBaseUri(definition.name).ifBlank {
definition.baseUrl
}
val baseUrl = getBaseUrl()
clientBuilder.baseUrl(baseUrl)

val filterDefinition = clientProperties.getFilter(definition.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class LoadBalancedWebClientFactoryBean(definition: CoApiDefinition) :
private val loadBalancedFilterClass = LoadBalancedExchangeFilterFunction::class.java
}

override val builderCustomizer: WebClientBuilderCustomizer = LoadBalancedWebClientBuilderCustomizer()
override val builderCustomizer: WebClientBuilderCustomizer by lazy {
if (loadBalanced()) LoadBalancedWebClientBuilderCustomizer() else WebClientBuilderCustomizer.NoOp
}

inner class LoadBalancedWebClientBuilderCustomizer : WebClientBuilderCustomizer {
override fun customize(coApiDefinition: CoApiDefinition, builder: WebClient.Builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ package me.ahoo.coapi.spring.client.sync

import me.ahoo.coapi.spring.CoApiDefinition
import me.ahoo.coapi.spring.client.ClientProperties
import me.ahoo.coapi.spring.client.IHttpClientFactoryBean
import org.springframework.beans.factory.FactoryBean
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
import org.springframework.http.client.ClientHttpRequestInterceptor
import org.springframework.web.client.RestClient

abstract class AbstractRestClientFactoryBean(private val definition: CoApiDefinition) :
abstract class AbstractRestClientFactoryBean(override val definition: CoApiDefinition) :
IHttpClientFactoryBean,
FactoryBean<RestClient>,
ApplicationContextAware {

protected lateinit var appContext: ApplicationContext
override lateinit var appContext: ApplicationContext

protected open val builderCustomizer: RestClientBuilderCustomizer = RestClientBuilderCustomizer.NoOp

override fun getObject(): RestClient {
val clientBuilder = appContext
.getBean(RestClient.Builder::class.java)
val clientProperties = appContext.getBean(ClientProperties::class.java)
val baseUrl = clientProperties.getBaseUri(definition.name).ifBlank {
definition.baseUrl
}
val baseUrl = getBaseUrl()
clientBuilder.baseUrl(baseUrl)

val interceptorDefinition = clientProperties.getInterceptor(definition.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class LoadBalancedRestClientFactoryBean(definition: CoApiDefinition) : AbstractR
private val loadBalancerInterceptorClass = LoadBalancerInterceptor::class.java
}

override val builderCustomizer: RestClientBuilderCustomizer = LoadBalancedRestClientBuilderCustomizer()
override val builderCustomizer: RestClientBuilderCustomizer by lazy {
if (loadBalanced()) LoadBalancedRestClientBuilderCustomizer() else RestClientBuilderCustomizer.NoOp
}

inner class LoadBalancedRestClientBuilderCustomizer : RestClientBuilderCustomizer {
override fun customize(coApiDefinition: CoApiDefinition, builder: RestClient.Builder) {
Expand Down

0 comments on commit 35e5a31

Please sign in to comment.