是否可以使用spring-boot-starter-web实现redis反应功能?

时间:2019-07-08 00:05:35

标签: spring-mvc reactive-programming spring-webflux spring-data-redis-reactive

我将在Spring Boot中使用redis-active。但是,所有现有代码都是在webmvc模式下工作的代码。尝试对webmvc使用redis-active而不是使用webflux是否有问题? 从Servlet 3.1开始,即使不使用webflux,它似乎也可以异步工作。

这是示例代码。 请让我指出是否有我不熟悉的异步开发方法。如果您将active-redis与webmvc结合使用而不是webflux,请让我知道问题出在哪里。

弹簧依赖性

compile('org.springframework.boot:spring-boot-starter-data-redis-reactive')
compile("org.springframework.boot:spring-boot-starter-web")

示例代码

@RestController
public class CoffeeController {
    private final ReactiveRedisOperations<String, Coffee> coffeeOps;
    private final ReactiveRedisConnectionFactory factory;

    WebClient client = WebClient.create();

    public CoffeeController(ReactiveRedisConnectionFactory factory, ReactiveRedisOperations<String, Coffee> coffeeOps) {
        this.factory = factory;
        this.coffeeOps = coffeeOps;
    }

    @GetMapping("/coffees")
    public Flux<Coffee> all() {
return coffeeOps.keys("*")
                .flatMap(coffeeOps.opsForValue()::get);
    }


}

结果日志 请求运行线程-> nio-8080-exec-3

redis辅助线程-> ioEventLoop-4-1

响应线程-> nio-8080-exec-4

2019-07-05 13:31:32.788 TRACE 38376 --- [nio-8080-exec-3] o.s.b.w.s.f.OrderedRequestContextFilter  : Bound request context to thread: org.apache.catalina.connector.RequestFacade@1f500e3e
2019-07-05 13:31:32.788 TRACE 38376 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : GET "/coffees", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
2019-07-05 13:31:32.789 TRACE 38376 --- [nio-8080-exec-3] o.s.b.f.s.DefaultListableBeanFactory     : Returning cached instance of singleton bean 'coffeeController'
2019-07-05 13:31:32.789 TRACE 38376 --- [nio-8080-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public reactor.core.publisher.Flux<hello.Coffee> hello.CoffeeController.all()
2019-07-05 13:31:32.789 TRACE 38376 --- [nio-8080-exec-3] .w.s.m.m.a.ServletInvocableHandlerMethod : Arguments: []
2019-07-05 13:31:32.789 DEBUG 38376 --- [nio-8080-exec-3] o.s.w.c.request.async.WebAsyncManager    : Started async request
2019-07-05 13:31:32.790 DEBUG 38376 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : Exiting but response remains open for further handling
2019-07-05 13:31:32.790 TRACE 38376 --- [nio-8080-exec-3] o.s.b.w.s.f.OrderedRequestContextFilter  : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@1f500e3e
2019-07-05 13:31:32.792 DEBUG 38376 --- [ioEventLoop-4-1] o.s.w.c.request.async.WebAsyncManager    : Async result set, dispatch to /coffees
2019-07-05 13:31:32.793 TRACE 38376 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : "ASYNC" dispatch for GET "/coffees", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
2019-07-05 13:31:32.793 TRACE 38376 --- [nio-8080-exec-4] o.s.b.f.s.DefaultListableBeanFactory     : Returning cached instance of singleton bean 'coffeeController'
2019-07-05 13:31:32.793 TRACE 38376 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to public reactor.core.publisher.Flux<hello.Coffee> hello.CoffeeController.all()
2019-07-05 13:31:32.793 TRACE 38376 --- [nio-8080-exec-4] s.w.s.m.m.a.RequestMappingHandlerAdapter : Resume with async result [[Coffee(id=44d68449-4d3b-44f2-b117-7018f7eb434b, name=Black Alert Redis), Coffee(id=4e9f48a0-4cd4-418f-8336-6a638f501ba7, name=Darth Redis), Coffee(id=71019626-6ad2-47d0-a1a5-f619f06f9f2c, name=Jet Black Redis)]]
2019-07-05 13:31:32.793 TRACE 38376 --- [nio-8080-exec-4] dlerMethod$ConcurrentResultHandlerMethod : Arguments: []
2019-07-05 13:31:32.793 DEBUG 38376 --- [nio-8080-exec-4] m.m.a.RequestResponseBodyMethodProcessor : Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]
2019-07-05 13:31:32.793 TRACE 38376 --- [nio-8080-exec-4] m.m.a.RequestResponseBodyMethodProcessor : Writing [[Coffee(id=44d68449-4d3b-44f2-b117-7018f7eb434b, name=Black Alert Redis), Coffee(id=4e9f48a0-4cd4-418f-8336-6a638f501ba7, name=Darth Redis), Coffee(id=71019626-6ad2-47d0-a1a5-f619f06f9f2c, name=Jet Black Redis)]]
2019-07-05 13:31:32.794 TRACE 38376 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : No view rendering, null ModelAndView returned.
2019-07-05 13:31:32.794 DEBUG 38376 --- [nio-8080-exec-4] o.s.web.servlet.DispatcherServlet        : Exiting from "ASYNC" dispatch, status 200, headers={masked}

0 个答案:

没有答案