zoukankan      html  css  js  c++  java
  • SpringBoot-Webflux-Netty(四)

    SpringBoot Netty 配置

    配置启动端口

    @Component
    public class NettyWebServerFactoryPortCustomizer
          implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {
       @Override
       public void customize(NettyReactiveWebServerFactory serverFactory) {
          serverFactory.setPort(8089);
       }
    }
    

    配置 EventLoopGroup

    EventLoopNettyCustomizer 配置类

    public class EventLoopNettyCustomizer implements NettyServerCustomizer {
    
        @Override
        public HttpServer apply(HttpServer httpServer) {
            EventLoopGroup bossGroup = new NioEventLoopGroup(1);
            EventLoopGroup workGroup = new NioEventLoopGroup();
            return httpServer.tcpConfiguration(tcpServer -> tcpServer
                    .bootstrap(serverBootstrap -> serverBootstrap
                            .group(bossGroup, workGroup)
                            .handler(new LoggingHandler(LogLevel.DEBUG))
                            .option(ChannelOption.SO_BACKLOG, 128)
                            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000)
                            .channel(NioServerSocketChannel.class)));
        }
    }
    

    NettyReactiveWebServerFactory 工厂类

    @Bean
    public NettyReactiveWebServerFactory nettyReactiveWebServerFactory() {
       NettyReactiveWebServerFactory webServerFactory = new NettyReactiveWebServerFactory();
       // 同时可以扩展 SSL
       webServerFactory.addServerCustomizers(new EventLoopNettyCustomizer());
       return webServerFactory;
    }
    

    查看日志

    要启用Netty访问日志记录,实用配置参数-Dreactor.netty.http.server.accessLogEnabled = true

    新建两个 SpringBoot 项目

    使用 Flux 写法

    @GetMapping("flux")
    public Mono<String> reactor() {
       return Mono.just("hello world");
    }
    

    使用 Mvc 写法

    @GetMapping("mvc")
    public String hello() {
       return "hello world";
    }
    

    压测看结果

    Flux 截图

    image-20210109130724515

    Mvc 截图

    image-20210109130900111

    网关代理

    直接代理(默认参数)

    image-20210109134614688

    自定义 EvenLoopGroup

    image-20210109132310751

    修改参数

    image-20210109132654663

    参考链接

  • 相关阅读:
    集合使用技巧
    集合总结
    Eclipse快捷键大全
    集合去掉重复元素的两种方式
    Collection集合的三种遍历方式
    win基本流程
    url
    StringBuffer7
    StringBuffer8
    StringBuffer6
  • 原文地址:https://www.cnblogs.com/holddie/p/springbootwebfluxnetty-si.html
Copyright © 2011-2022 走看看