Skip to content

Commit

Permalink
add example to customize WebClientBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang committed Nov 15, 2024
1 parent b1e48e5 commit 4b65739
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 8 deletions.
6 changes: 0 additions & 6 deletions .idea/CoApi.iml

This file was deleted.

12 changes: 12 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.example.consumer

import me.ahoo.coapi.spring.CoApiDefinition
import me.ahoo.coapi.spring.client.reactive.WebClientBuilderCustomizer
import org.springframework.http.client.reactive.ReactorClientHttpConnector
import org.springframework.stereotype.Service
import org.springframework.web.reactive.function.client.WebClient
import reactor.netty.http.client.HttpClient
import reactor.netty.resources.ConnectionProvider
import java.time.Duration

@Service
class ConsumerWebClientBuilderCustomizer : WebClientBuilderCustomizer {
override fun customize(
coApiDefinition: CoApiDefinition,
builder: WebClient.Builder
) {
/**
* https://github.com/reactor/reactor-netty/issues/388#issuecomment-704069492
*
* https://github.com/reactor/reactor-netty/issues/1774#issuecomment-908066283
*/
val connectionProvider = ConnectionProvider.builder(coApiDefinition.name)
.maxConnections(500)
.maxIdleTime(Duration.ofSeconds(20))
.maxLifeTime(Duration.ofSeconds(60))
.pendingAcquireTimeout(Duration.ofSeconds(60))
.evictInBackground(Duration.ofSeconds(120)).build()
val httpClient = HttpClient.create(connectionProvider)
val clientHttpConnector = ReactorClientHttpConnector(httpClient)
builder.clientConnector(clientHttpConnector)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import org.springframework.context.ApplicationContextAware
import org.springframework.web.reactive.function.client.ExchangeFilterFunction
import org.springframework.web.reactive.function.client.WebClient

/**
* @see reactor.netty.resources.ConnectionProvider
* @see org.springframework.boot.autoconfigure.web.reactive.function.client.ReactorClientHttpConnectorFactory
* @see org.springframework.http.client.ReactorResourceFactory
*/
abstract class AbstractWebClientFactoryBean(override val definition: CoApiDefinition) :
IHttpClientFactoryBean,
FactoryBean<WebClient>,
Expand All @@ -40,7 +45,6 @@ abstract class AbstractWebClientFactoryBean(override val definition: CoApiDefini
val clientProperties = appContext.getBean(ClientProperties::class.java)
val baseUrl = getBaseUrl()
clientBuilder.baseUrl(baseUrl)

val filterDefinition = clientProperties.getFilter(definition.name)
clientBuilder.filters {
filterDefinition.initFilters(it)
Expand Down

0 comments on commit 4b65739

Please sign in to comment.