У меня есть простой код, такой как:
Код: Выделить всё
spring:
application:
name: client-application
security:
oauth2:
client:
registration:
hello-client:
provider: hello-provider
client-id: someusername
client-secret: somepassword
authorization-grant-type: client_credentials
scope: resolve
hi-client:
provider: hi-provider
client-id: anotherusername
client-secret: anotherpassword
authorization-grant-type: client_credentials
scope: download
provider:
hello-provider:
token-uri: https://tokenprovider.com/token
hi-provider:
token-uri: https://secureservice.com/token
application:
url:
hello: https://somecoolcompany.com/api/v1.0/hello
hi: https://veryawsomeendpoint.com/v2/hi
Код: Выделить всё
@Service
public class HelloServiceClient {
private static final Logger log = LoggerFactory.getLogger(HelloServiceClient.class);
private final RestClient restClient;
public HelloServiceClient(RestClient.Builder builder, @Value("${application.url.hello}") String urlHello) {
this.restClient = builder
.baseUrl(urlHello)
.requestInterceptor(
(request, body, execution) -> {
log.info("Lambda Interceptor: modifying before sending request");
ClientHttpResponse response = execution.execute(request, body);
log.info("Lambda Interceptor: modifying after receiving response");
return response;
}
)
.build();
}
public String hello() {
return this.restClient.get()
.uri("/hello")
.attributes(clientRegistrationId("hello-client"))
.retrieve()
.body(String.class);
}
}
Код: Выделить всё
package vn.cloud.restclientdemo.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
import static org.springframework.security.oauth2.client.web.client.RequestAttributeClientRegistrationIdResolver.clientRegistrationId;
@Service
public class HiServiceClient {
private final RestClient restClient;
public HiServiceClient(RestClient.Builder builder, @Value("${application.url.hi}") String urlHi) {
this.restClient = builder
.baseUrl(urlHi)
.build();
}
public String hi() {
return this.restClient.get()
.uri("/hi")
.attributes(clientRegistrationId("hi-client"))
.retrieve()
.body(String.class);
}
}
Бизнес-логика проста: вызовите конечную точку A, но для этой конечной точки A нужен токен из токена. поставщика B.
Затем вызовите конечную точку ресурса C, которой также нужен еще один токен от поставщика токенов D.
всего 4.
Приложение находится под высокой нагрузкой. И политики токенов разные.
Например, первый токен действителен 9 минут, а второй токен действителен только 1 минуту.
Кэширование здесь имеет решающее значение, поскольку при высокой нагрузке мы хотели бы избегайте ненужных запросов на получение токена, пока он еще активен. Было бы здорово его кешировать.
Но не только кешировать, еще и уметь настраивать.
Я на что-то надеялся вот так:
Код: Выделить всё
spring:
application:
name: client-application
security:
oauth2:
client:
registration:
hello-client:
provider: hello-provider
client-id: someusername
client-secret: somepassword
authorization-grant-type: client_credentials
scope: resolve
TTL: 9m
hi-client:
provider: hi-provider
client-id: anotherusername
client-secret: anotherpassword
authorization-grant-type: client_credentials
scope: download
TTL: 1m
provider:
hello-provider:
token-uri: https://tokenprovider.com/token
hi-provider:
token-uri: https://secureservice.com/token
application:
url:
hello: https://somecoolcompany.com/api/v1.0/hello
hi: https://veryawsomeendpoint.com/v2/hi
Подробнее здесь: https://stackoverflow.com/questions/793 ... ient-token