当我使用@LoadBalanced注释的WebClient.Builder时,出现WebClientResponseException $ ServiceUnavailable错误

问题描述 投票:0回答:1

[当我对WebClient.Builder使用@LoadBalanced批注时,我从外部API获得以下堆栈跟踪。我在此之前编写的演示返回硬编码数据,并且使用了EurekaServer。该应用程序也出现错误,因为我没有使用@LoadBalanced注释我的WebClient.Builder,所以我怎么能同时使用这两种用法,我相信它的发生是因为服务器具有一个或多个实例。

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)
java spring-boot spring-webflux spring-rest spring-webclient
1个回答
0
投票

我不赞成使用LoadBalancerExchangeFilterFunction,我希望看到更新的实现,并看到下面的新实现;

MainApp具有以下bean;

    @Bean
    @LoadBalanced
    public WebClient.Builder builder(){
        return WebClient.builder();
    }

    @Bean
    public WebClient getWebClient() {
        return WebClient.builder().build();

    }

然后是我的RestController这样;

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());

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.