Spring Boot WebClient.Builder bean usage in traditional servlet multi threaded application

对着背影说爱祢 提交于 2019-12-20 02:54:40

问题


I would like to have a http client to call other microservice from Spring Boot not reactive application. Because of RestTemplate will be deprecated I tried to use WebClient.Builder and WebClient. Though I not sure about thread safety. Here example:

@Service
public class MyService{
    @Autowired
    WebClient.Builder webClientBuilder;

    public VenueDTO serviceMethod(){
        //!!! This is not thread safe !!!
        WebClient webClient = webClientBuilder.baseUrl("http://localhost:8000").build();

        VenueDTO venueDTO = webClient.get().uri("/api/venue/{id}", bodDTO.getBusinessOutletId()).
                retrieve().bodyToMono(VenueDTO.class).
                blockOptional(Duration.ofMillis(1000)).
                orElseThrow(() -> new BadRequestException(venueNotFound));
                return VenueDTO;
    }
}

serviceMethod() in this example will be called from few threads, and webClientBuilder is a single bean instance. The WebClient.Builder class contains state: baseUrl, and this seems not thread safe as few threads could call this state update simultaneously. Meanwhile WebClient itself seems is thread safe as mentioned in answer at Right way to use Spring WebClient in multi-thread environment

Should I use WebClient.Builder bean as mentioned in https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-webclient.html

Spring Boot creates and pre-configures a WebClient.Builder for you; it is strongly advised to inject it in your components and use it to create WebClient instances.

One of workaround options I see is to create WebClient without any state passed to builder so instead of:

WebClient webClient = webClientBuilder.baseUrl("http://localhost:8000").build();

I will do:

WebClient webClient = webClientBuilder.build();

and pass full url with protocol and port in uri method call:

webClient.get().uri("full url here", MyDTO.class)

What is the proper way to use it in my case?


回答1:


You're right, WebClient.Builder is not thread-safe.

Spring Boot is creating WebClient.Builder as a prototype bean, so you'll get a new instance for each injection point. In your case, your component seems a bit strange in my opinion.

It should rather look like this:

@Service
public class MyService{

    private final WebClient webClient;

    public MyService(WebClient.Builder webClientBuilder) {
        this.webClient = webClientBuilder.baseUrl("http://localhost:8000").build();
    }

    public VenueDTO serviceMethod(){
        VenueDTO venueDTO = webClient.get().uri("/api/venue/{id}", bodDTO.getBusinessOutletId()).
                retrieve().bodyToMono(VenueDTO.class).
                blockOptional(Duration.ofMillis(1000)).
                orElseThrow(() -> new BadRequestException(venueNotFound));
                return VenueDTO;
    }
}

Now I guess this is a code snippet and your application may have different constraints.

If your application needs to change the base URL often, then I think you should stop configuring it on the builder and pass the full URL as mentioned in your question. If your application has other needs (custom headers for authentication, etc), then you can also do that on the builder or on a per request basis.

In general, you should try and build a single WebClient instance per component, as recreating it for each request is quite wasteful.

In case your application has very specific constraints and really needs to create different instances, then you can always call webClientBuilder.clone() and get a new instance of the builder that you can mutate, without the thread safety issues.



来源:https://stackoverflow.com/questions/54136085/spring-boot-webclient-builder-bean-usage-in-traditional-servlet-multi-threaded-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!