Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat : upgrade to spring boot 3.4.1 #1589

Merged
merged 6 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions httpClients/boot-http-proxy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.7</version>
<version>3.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.rest.proxy</groupId>
Expand All @@ -20,7 +20,7 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>21</java.version>
<springdoc-openapi.version>2.6.0</springdoc-openapi.version>
<springdoc-openapi.version>2.7.0</springdoc-openapi.version>

<project.testresult.directory>${project.build.directory}/test-results</project.testresult.directory>
<spotless.version>2.43.0</spotless.version>
Expand Down Expand Up @@ -221,7 +221,7 @@
<configuration>
<java>
<googleJavaFormat>
<version>1.25.0</version>
<version>1.25.2</version>
<style>AOSP</style>
</googleJavaFormat>
</java>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@

import com.example.rest.proxy.client.JsonPlaceholderService;
import io.micrometer.observation.ObservationRegistry;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import org.apache.hc.client5.http.config.ConnectionConfig;
import org.apache.hc.client5.http.config.RequestConfig;
import javax.net.ssl.SSLContext;
import org.apache.hc.client5.http.impl.DefaultConnectionKeepAliveStrategy;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.socket.ConnectionSocketFactory;
import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.core5.http.config.Registry;
import org.apache.hc.core5.http.config.RegistryBuilder;
import org.apache.hc.core5.http.io.SocketConfig;
import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
import org.apache.hc.client5.http.ssl.HostnameVerificationPolicy;
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
import org.apache.hc.core5.ssl.SSLContextBuilder;
import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.http.client.ClientHttpRequestFactoryBuilder;
import org.springframework.boot.http.client.HttpComponentsClientHttpRequestFactoryBuilder;
import org.springframework.boot.web.client.RestClientCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.http.MediaType;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.support.RestClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
Expand All @@ -30,50 +31,65 @@
@Configuration(proxyBeanMethods = false)
class RestClientConfiguration {

private static final Logger log = LoggerFactory.getLogger(RestClientConfiguration.class);

private final ApplicationProperties applicationProperties;
private final Environment environment;

RestClientConfiguration(ApplicationProperties applicationProperties) {
RestClientConfiguration(ApplicationProperties applicationProperties, Environment environment) {
this.applicationProperties = applicationProperties;
this.environment = environment;
}

@Bean
public CloseableHttpClient httpClient() {
Registry<ConnectionSocketFactory> registry =
RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", SSLConnectionSocketFactory.getSocketFactory())
.build();
PoolingHttpClientConnectionManager poolingConnectionManager =
new PoolingHttpClientConnectionManager(registry);

poolingConnectionManager.setDefaultSocketConfig(
SocketConfig.custom().setSoTimeout(Timeout.ofSeconds(2)).build());
poolingConnectionManager.setDefaultConnectionConfig(
ConnectionConfig.custom().setConnectTimeout(Timeout.ofSeconds(2)).build());
public HttpComponentsClientHttpRequestFactoryBuilder
httpComponentsClientHttpRequestFactoryBuilder() {

// set total amount of connections across all HTTP routes
poolingConnectionManager.setMaxTotal(200);
// set maximum amount of connections for each http route in pool
poolingConnectionManager.setDefaultMaxPerRoute(100);
SSLContext sslContext;
try {
// Configure SSLContext with a permissive TrustStrategy
SSLContextBuilder sslContextBuilder = SSLContextBuilder.create();
if (!isProdEnvironment()) {
log.warn("Using permissive certificate validation - NOT FOR PRODUCTION USE");
sslContextBuilder.loadTrustMaterial((chain, authType) -> true);
}
sslContext = sslContextBuilder.build();
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
throw new RuntimeException("Failed to initialize SSL context", e);
}

RequestConfig requestConfig =
RequestConfig.custom()
.setConnectionKeepAlive(TimeValue.ofSeconds(10))
.setConnectionRequestTimeout(Timeout.ofSeconds(2))
.setResponseTimeout(Timeout.ofSeconds(2))
.build();
return ClientHttpRequestFactoryBuilder.httpComponents()
.withHttpClientCustomizer(
httpClientBuilder ->
httpClientBuilder.setKeepAliveStrategy(
DefaultConnectionKeepAliveStrategy.INSTANCE))
.withConnectionManagerCustomizer(
poolingHttpClientConnectionManagerBuilder -> {
poolingHttpClientConnectionManagerBuilder.setMaxConnTotal(200);
poolingHttpClientConnectionManagerBuilder.setMaxConnPerRoute(100);
poolingHttpClientConnectionManagerBuilder.setTlsSocketStrategy(
new DefaultClientTlsStrategy(
sslContext,
HostnameVerificationPolicy.CLIENT,
isProdEnvironment()
? null
: NoopHostnameVerifier.INSTANCE));
})
.withDefaultRequestConfigCustomizer(
(builder) -> {
builder.setConnectionKeepAlive(TimeValue.ofSeconds(10));
builder.setConnectionRequestTimeout(Timeout.ofSeconds(30));
builder.setResponseTimeout(Timeout.ofSeconds(60));
});
}

return HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.setConnectionManager(poolingConnectionManager)
.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())
.build();
private boolean isProdEnvironment() {
return List.of(environment.getActiveProfiles()).contains("prod");
}

@Bean
RestClientCustomizer restClientCustomizer(
ObservationRegistry observationRegistry,
CloseableHttpClient httpClient,
LogbookClientHttpRequestInterceptor interceptor) {
return restClientBuilder ->
restClientBuilder
Expand All @@ -83,7 +99,6 @@ RestClientCustomizer restClientCustomizer(
httpHeaders.setAccept(List.of(MediaType.APPLICATION_JSON));
})
.baseUrl(applicationProperties.getPostServiceUrl())
.requestFactory(new HttpComponentsClientHttpRequestFactory(httpClient))
.observationRegistry(observationRegistry)
.requestInterceptor(interceptor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,29 @@ public List<PostComment> savePostComments(List<PostCommentDto> postCommentDtoLis
@Transactional
public PostResponse savePost(Post post) {
Post fetchedPost = jsonPlaceholderService.createPost(post);
Post savedPost = postRepository.save(fetchedPost);
// To fix optimisticLock Exception
// Create a new local entity from the external post
Post localPost = new Post();
localPost.setUserId(fetchedPost.getUserId());
localPost.setTitle(fetchedPost.getTitle());
localPost.setBody(fetchedPost.getBody());
Post savedPost = postRepository.save(localPost);
return postMapper.mapToPostResponse(savedPost);
}

@Transactional
public Post save(Post postEntity) {
return postRepository.save(postEntity);
// Create a new local entity from the external post
Post localPost = new Post();
localPost.setUserId(postEntity.getUserId());
localPost.setTitle(postEntity.getTitle());
localPost.setBody(postEntity.getBody());
return postRepository.save(localPost);
}

@Transactional
public PostResponse saveAndConvertToResponse(Post post) {
Post savedPost = save(post);
Post savedPost = postRepository.save(post);
return postMapper.mapToPostResponse(savedPost);
}

Expand Down
Loading