Когда я использую аннотацию @LoadBalanced для WebClient.Builder, я получаю следующую трассировку стека из внешнего API. Демо, которое я написал до этого, возвращало жестко закодированные данные, и я использую EurekaServer. Это приложение также выдает ошибку, потому что я не аннотировал свой WebClient.Builder с помощью @LoadBalanced, поэтому, как я могу использовать это для обоих целей, я считаю, что это происходит, потому что на сервере есть один или несколько экземпляров.
MainApp
@SpringBootApplication
@EnableEurekaClient
public class MovieInfoServiceApplication {
@Bean
@LoadBalanced
public WebClient.Builder getWebClientBuilder() {
return WebClient.builder();
}
public static void main(String[] args) {
SpringApplication.run(MovieInfoServiceApplication.class, args);
}
}
RestController
@RestController
@RequestMapping("/movies")
public class MovieRestController {
api key field
@Autowired
private WebClient.Builder webClientBuilder;
@RequestMapping("/{movieId}")
public Movie getMovieById(@PathVariable("movieId") int movieId) {
Movie movieDetails = webClientBuilder
.build()
.get()
.uri("https://api.themoviedb.org/3/movie/" + movieId + "?api_key=" + apiKey)
.retrieve()
.bodyToMono(Movie.class).block();
return new Movie(movieId, movieDetails.getTitle(), movieDetails.getOverview());
}
}
Трассировка стека
500 Internal Server Error from GET http://localhost:8082/movies/550
org.springframework.web.reactive.function.client.WebClientResponseException$InternalServerError: 500 Internal Server Error from GET http://localhost:8082/movies/550
at org.springframework.web.reactive.function.client.WebClientResponseException.create(WebClientResponseException.java:201)
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ⇢ 500 from GET http://movie-info-service/movies/550 [DefaultWebClient]
Stack trace:
at org.springframework.web.reactive.function.client.WebClientResponseException.create(WebClientResponseException.java:201)
at org.springframework.web.reactive.function.client.DefaultClientResponse.lambda$createException$1(DefaultClientResponse.java:209)
at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:100)
После https://github.com/spring-cloud/spring-cloud-commons/issues/295#issuecomment -414206032 эта реализация
MainApp
@SpringBootApplication
@EnableEurekaClient
public class MovieInfoServiceApplication {
@Bean
WebClient webClient(LoadBalancerClient loadBalancerClient) {
return WebClient.builder()
.filter(new LoadBalancerExchangeFilterFunction(loadBalancerClient))
.build();
}
public static void main(String[] args) {
SpringApplication.run(MovieInfoServiceApplication.class, args);
}
}
RestController
@RestController
@RequestMapping("/movies")
public class MovieRestController {
@Value("${api.key}")
private String apiKey;
@Autowired
private WebClient webClient;
@RequestMapping("/{movieId}")
public Movie getMovieById(@PathVariable("movieId") int movieId) {
Movie movieDetails = webClient
.get()
.uri("https://api.themoviedb.org/3/movie/" + movieId + "?api_key=" + apiKey)
.retrieve()
.bodyToMono(Movie.class)
.block();
return new Movie(movieId, movieDetails.getTitle(), movieDetails.getOverview());
}
}
Ошибка
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun May 03 19:15:51 EET 2020
There was an unexpected error (type=Internal Server Error, status=500).
503 Service Unavailable from UNKNOWN
org.springframework.web.reactive.function.client.WebClientResponseException$ServiceUnavailable: 503 Service Unavailable from UNKNOWN
at org.springframework.web.reactive.function.client.WebClientResponseException.create(WebClientResponseException.java:207)
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ⇢ 503 from GET https://api.themoviedb.org/3/movie/551?api_key=apikeyvalue [DefaultWebClient]
Stack trace:
at org.springframework.web.reactive.function.client.WebClientResponseException.create(WebClientResponseException.java:207)
at org.springframework.web.reactive.function.client.DefaultClientResponse.lambda$createException$1(DefaultClientResponse.java:209)
at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:100)
at reactor.core.publisher.FluxDefaultIfEmpty$DefaultIfEmptySubscriber.onNext(FluxDefaultIfEmpty.java:92)
at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onNext(FluxMapFuseable.java:121)
at reactor.core.publisher.FluxContextStart$ContextStartSubscriber.onNext(FluxContextStart.java:103)
at reactor.core.publisher.FluxMapFuseable$MapFuseableConditionalSubscriber.onNext(FluxMapFuseable.java:287)