54 lines
2.1 KiB
Java
54 lines
2.1 KiB
Java
package dev.mars3142.fhq.config;
|
|
|
|
import dev.mars3142.fhq.client.AuthClient;
|
|
import dev.mars3142.fhq.client.TimeZoneClient;
|
|
import dev.mars3142.fhq.config.interceptor.AuthInterceptor;
|
|
import lombok.val;
|
|
import org.springframework.boot.web.client.ClientHttpRequestFactories;
|
|
import org.springframework.boot.web.client.ClientHttpRequestFactorySettings;
|
|
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.http.client.ClientHttpRequestFactory;
|
|
import org.springframework.web.client.RestClient;
|
|
import org.springframework.web.client.support.RestClientAdapter;
|
|
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
|
|
|
|
@Configuration
|
|
public class RestClientConfig {
|
|
|
|
@LoadBalanced
|
|
@Bean
|
|
RestClient.Builder restClientBuilder() {
|
|
return RestClient.builder()
|
|
.requestInterceptor(new AuthInterceptor());
|
|
}
|
|
|
|
@Bean
|
|
public AuthClient authClient(RestClient.Builder builder) {
|
|
val restClient = builder
|
|
.baseUrl("http://auth-service")
|
|
.requestFactory(getClientRequestFactory())
|
|
.build();
|
|
val restClientAdapter = RestClientAdapter.create(restClient);
|
|
val httpServiceProxyFactory = HttpServiceProxyFactory.builderFor(restClientAdapter).build();
|
|
return httpServiceProxyFactory.createClient(AuthClient.class);
|
|
}
|
|
|
|
@Bean
|
|
public TimeZoneClient timeZoneClient(RestClient.Builder builder) {
|
|
val restClient = builder
|
|
.baseUrl("http://timezone-service")
|
|
.requestFactory(getClientRequestFactory())
|
|
.build();
|
|
val restClientAdapter = RestClientAdapter.create(restClient);
|
|
val httpServiceProxyFactory = HttpServiceProxyFactory.builderFor(restClientAdapter).build();
|
|
return httpServiceProxyFactory.createClient(TimeZoneClient.class);
|
|
}
|
|
|
|
private ClientHttpRequestFactory getClientRequestFactory() {
|
|
val clientHttpRequestFactorySettings = ClientHttpRequestFactorySettings.DEFAULTS;
|
|
return ClientHttpRequestFactories.get(clientHttpRequestFactorySettings);
|
|
}
|
|
}
|