project-reactor

How to limit request payload size using Spring Webflux?

99封情书 提交于 2019-12-04 18:34:37
Using Spring Webflux, I need to limit the payload size of incoming HTTP requests (e.g. to say 5MB). Is this something configurable out-of-the-box? If not, which APIs can we use to implement this behavior (e.g. return a 413 if the payload is too big). I guess you want to do this to prevent Denial Of Service attacks by malicious or misbehaving clients. The usual recommendation is to not directly connect HTTP application servers to the Internet, but instead via an HTTP reverse proxy server (such as a suitably configured Apache HTTPD or nginx). These proxy servers provide means for blocking large

Wrapping blocking I/O in project reactor

柔情痞子 提交于 2019-12-04 11:57:37
问题 I have a spring-webflux API which, at a service layer, needs to read from an existing repository which uses JDBC. Having done some reading on the subject, I would like to keep the execution of the blocking database call separate from the rest of my non-blocking async code. I have defined a dedicated jdbcScheduler: @Bean public Scheduler jdbcScheduler() { return Schedulers.fromExecutor(Executors.newFixedThreadPool(maxPoolSize)); } And an AsyncWrapper utility to use it: @Component public class

How to correctly use slf4j MDC in spring-webflux WebFilter

ぃ、小莉子 提交于 2019-12-04 08:25:19
I referenced with the blog post Contextual Logging with Reactor Context and MDC but I don't know how to access reactor context in WebFilter. @Component public class RequestIdFilter implements WebFilter { @Override public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { List<String> myHeader = exchange.getRequest().getHeaders().get("X-My-Header"); if (myHeader != null && !myHeader.isEmpty()) { MDC.put("myHeader", myHeader.get(0)); } return chain.filter(exchange); } } You can do something similar to below, You can set the context with any class you like, for this example I

Readable debug logging for http requests with spring webclient

人盡茶涼 提交于 2019-12-04 07:36:09
I'm using Spring reactive WebClient for sending requests to a http server. Inorder to view the underlying request & response that's being sent, I enabled debug logging for reactor.ipc.netty package. The headers for the outgoing requests can be viewed normally. Tho I'm sending & receiving plain text over http, the log contains the request & responses in the below format (is it hex?) I'm not sure how to view the logged data in a easy to understand way. Better yet log the request & response in a understandable way Here is a snippet of the logged data +---------------------------------------------

Spring Web Reactive Framework Multipart File Issue

不羁的心 提交于 2019-12-04 06:54:35
I'm trying to implement and image upload using Spring's Reactive Framework by trying the following: @RestController @RequestMapping("/images") public class ImageController { @Autowired private IImageService imageService; @PostMapping(value = "", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) Mono<ImageEntity> saveImage(@RequestBody Mono<FilePart> part) throws Exception{ return part.flatMap(file -> imageService.saveImage(file)); } } But I keep getting a 415 with the following error message: Response status 415 with reason "Content type 'multipart

Reactor Kafka: Exactly Once Processing Sample

人走茶凉 提交于 2019-12-04 06:46:18
问题 I've read many articles where there are many different configurations to achieve exactly once processing. Here is my producer config: final Map<String, Object> props = Maps.newConcurrentMap(); props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class); props.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, "true"); props.put

compose() vs. transform() vs. as() vs. map() in Flux and Mono

蓝咒 提交于 2019-12-04 03:20:24
Recently, I decided to try spring 5 with projectreactor.io (io.projectreactor:3.1.1). Does anyone know what the best case of using this functions? What cons and pros of using each of them and where they should be used? Good examples will be helpful. You have two broadly different categories of operators here: Operators that work on the Flux itself transform and compose are for code mutualization When you compose chains of operators regularly and you have common operator usage patterns in your application, you can mutualize this code or give it a more descriptive name by using compose and

Is really necessary to use Hystrix with reactive spring boot 2 application?

爱⌒轻易说出口 提交于 2019-12-03 21:13:19
I'm working in a project in which we are moving some of ours microservices from Spring-MVC to Spring-Webflux to test the reactive paradigm. Looking for some help in the github repository of hystrix we've noted that the project have no commits since a year ago, and it's based in RxJava, so there are some incompatibilities with project-reactor. We're having some issues using Hystrix, particulary that the annotations from "Javanica" doesn't work and our developers need to use HystrixCommands from Spring-Cloud instead. And the fact that Hystrix, obviously, creates his own pool of threads aside

JDBC with Webflux - how to dispatch to container thread

谁说我不能喝 提交于 2019-12-03 20:44:00
I am working on a small proof-of-concept with webflux. In one part of my application I would like to communicate with a database (via JDBC) which is blocking and is not a good fit for reactor. Nevertheless for this proof-of-concept, I am thinking of following trick: Define a dedicated thread pool (let's call it DBThreadPool ) as ExecutorService with a fixed number of threads that equal JDBC connection pool size. Wrap that pool in Reactor Scheduler (named dbScheduller ) Wrap blocking call (like described in Reactor doc) and schedule it on dbScheduller : public Flux<DbUser> allUsers() { return

Using both publishOn and subscribeOn on a flux results in nothing happening

旧时模样 提交于 2019-12-03 15:20:24
Whenever i use both subscribeOn and publishOn nothing is printed. If I use only one it will print. If I use subscribeOn(Schedulers.immediate()) or elastic it works. Any ideea why that is? It was my understanding that publishOn affects on what thread it gets published and subscribe on on what thread the subscriber runs. Could you please point me in the right direction? fun test() { val testPublisher = EmitterProcessor.create<String>().connect() testPublisher .publishOn(Schedulers.elastic()) .map { it -> println("map on ${Thread.currentThread().name}") it } .subscribeOn(Schedulers.parallel())