当前位置: 首页 > news >正文

卓越高职院建设网站平湖公司网站建设

卓越高职院建设网站,平湖公司网站建设,南通做百度网站的公司网站,江苏省建设工程质量监督站网站目录 一、netty入门 二、启动方式 三、netty服务启动类 四、handler链 五、具体业务 六、 线程或者非spring管理的bean中获取spring管理的bean 七、效果 一、netty入门 Netty-导学_哔哩哔哩_bilibili 入门视频量比较大#xff0c;最主要是了解netty的架构 netty官网最主要是了解netty的架构 netty官网 Netty.docs: User guide for 4.x 对springboot有入门级别会启动项目设置bean基本就可以完成了 maven依赖 parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.6.7/versionrelativePath//parent propertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdio.netty/groupIdartifactIdnetty-all/artifactIdversion4.1.95.Final/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactId/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdscopeprovided/scope/dependencydependencygroupIdcom.google.code.gson/groupIdartifactIdgson/artifactIdversion2.10.1/version/dependencydependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactId/dependencydependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-generator/artifactIdversion3.5.1/version/dependencydependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-extension/artifactIdversion3.5.3.1/version/dependencydependencygroupIdorg.freemarker/groupIdartifactIdfreemarker/artifactIdversion2.3.31/version/dependencydependencygroupIdcommons-io/groupIdartifactIdcommons-io/artifactIdversion2.11.0/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependencydependencygroupIdcom.github.ben-manes.caffeine/groupIdartifactIdcaffeine/artifactId/dependencydependencygroupIdorg.apache.commons/groupIdartifactIdcommons-pool2/artifactId/dependencydependencygroupIdcommons-codec/groupIdartifactIdcommons-codec/artifactIdversion1.15/version/dependencydependencygroupIdorg.apache.commons/groupIdartifactIdcommons-lang3/artifactIdversion3.12.0/version/dependencydependencygroupIdorg.apache.commons/groupIdartifactIdcommons-collections4/artifactIdversion4.4/version/dependency/dependencies/project 二、启动方式 以web方式启动tomcat占用80端口    netty占用9002端口 import com.xnetty.config.NettyServer; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext;/*** description: 以web的方式进行项目启动*/ SpringBootApplication Slf4j public class XNettyApplication {public static void main(String[] args) throws Exception {ApplicationContext context SpringApplication.run(XNettyApplication.class, args);NettyServer nettyServer context.getBean(NettyServer.class);nettyServer.start();log.info(XNettyApplication finish start....);} } 属性配置文件application.properties中配置 server.port80 netty.server.port9002 三、netty服务启动类 import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.ByteBufAllocator; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelOption; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Component;/*** description:*/ Component Slf4j public class NettyServer {public void start() throws Exception {ThreadPoolTaskExecutor executor new ThreadPoolTaskExecutor();executor.setCorePoolSize(8);executor.setMaxPoolSize(200);executor.setQueueCapacity(10000);executor.setKeepAliveSeconds(60);executor.setRejectedExecutionHandler((r, executor1) - {try {executor1.getQueue().put(r);log.info(执行了拒绝策略.....);} catch (InterruptedException e) {log.error(加入队列异常:{}, e.getMessage());}});executor.setThreadGroupName(netty_request);executor.initialize();NioEventLoopGroup bossGroup new NioEventLoopGroup(16);NioEventLoopGroup workGroup new NioEventLoopGroup(16);try {ChannelFuture channelFuture new ServerBootstrap().group(bossGroup, workGroup).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)).option(ChannelOption.ALLOCATOR, ByteBufAllocator.DEFAULT).childHandler(new ServerInit(executor)).bind(0.0.0.0, port).sync();Channel channel channelFuture.channel();channel.closeFuture().sync();} finally {bossGroup.shutdownGracefully();workGroup.shutdownGracefully();}}Value(${netty.server.port})private int port;} 四、handler链 import com.xnetty.service.ReadRequestHandler; import io.netty.channel.ChannelInitializer; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpServerCodec; import io.netty.handler.timeout.IdleStateHandler; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Service;import java.util.concurrent.TimeUnit;/*** description:*/ Service public class ServerInit extends ChannelInitializerNioSocketChannel {private ThreadPoolTaskExecutor executor;public ServerInit(ThreadPoolTaskExecutor executor) {this.executor executor;}Overrideprotected void initChannel(NioSocketChannel ch) throws Exception {ch.pipeline().addLast(new IdleStateHandler(0, 0, 30 * 3, TimeUnit.SECONDS)).addLast(new HttpServerCodec()).addLast(new HttpObjectAggregator(5 * 1024 * 1024)) //大小为10M.addLast(new MpHandler(executor));} } 五、具体业务 import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.handler.codec.http.*; import io.netty.util.CharsetUtil; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;/*** description:*/ public class MpHandler extends SimpleChannelInboundHandlerFullHttpRequest {ThreadPoolTaskExecutor executor;public MpHandler(ThreadPoolTaskExecutor executor) {this.executor executor;}Overrideprotected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {String uri request.uri();if (uri.equals(/hello)) {executor.execute(()-{FullHttpResponse response new DefaultFullHttpResponse(request.protocolVersion(), HttpResponseStatus.OK, Unpooled.copiedBuffer(hello word, CharsetUtil.UTF_8)); // Unpooled.wrappedBuffer(responseJson)response.headers().set(HttpHeaderNames.CONTENT_TYPE, application/json;charsetUTF-8); // HttpHeaderValues.TEXT_PLAIN.toString()response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());if (HttpUtil.isKeepAlive(request)) {response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);}ctx.writeAndFlush(response);});} else {executor.execute(() - {FullHttpResponse response new DefaultFullHttpResponse(request.protocolVersion(), HttpResponseStatus.OK, Unpooled.copiedBuffer(请求成功...., CharsetUtil.UTF_8)); // Unpooled.wrappedBuffer(responseJson)response.headers().set(HttpHeaderNames.CONTENT_TYPE, application/json;charsetUTF-8); // HttpHeaderValues.TEXT_PLAIN.toString()response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());if (HttpUtil.isKeepAlive(request)) {response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);}ctx.writeAndFlush(response);});}} }六、 线程或者非spring管理的bean中获取spring管理的bean 至于如何在非spring管理的bean中获取spring管理的对象或者在线程中获取spring管理的bean则自定义一个beanutils获取 如下 import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component;Component public class SpringBeanUtils implements ApplicationContextAware {private static ApplicationContext context;Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {context applicationContext;}public static Object getBean(String name) {return context.getBean(name);}public static T T getBean(ClassT clazz) {return context.getBean(clazz);}public static T T getBeanByName(String beanName, ClassT clazz) {return context.getBean(beanName, clazz);}} 七、效果 七http://localhost:9002/hello
http://www.hkea.cn/news/14519395/

相关文章:

  • 潍坊小企业网站建设o2o与网站建设
  • 个人求职网站设计营销型网站策划 pdf
  • 工程公司会计分录外贸seo关键词
  • php中switch做网站公司网站建设价格表
  • 免费建站有哪些wordpress登录页面404
  • php网站整合dz论坛建设网站属于什么费用
  • 最专业的营销网站建设公司排名网站建设要学哪些软件
  • 南京市网站开发建筑工地施工现场视频
  • 网站开发运营维护方案建议注册网站要语音验证码的有哪些
  • 代理充值平台网站乐山住房和城乡建设厅网站
  • 大兴网站开发网站建设创建全国文明城市要求街巷
  • ui网页设计报价seo权重优化软件
  • 泉州教育平台网站建设酒店电子商务网站策划书
  • 做婚恋网站的翻译好吗优化seo系统
  • 沈阳做企业网站哪家好网站建设项目报告书
  • 1150网站建设服务器主板server wordpress
  • 网站如何做微信支付链接微信小程序登录页面
  • 做贺卡的网站专做宝宝的用品网站
  • 如皋企业网站建设外贸多语言网站建设
  • 网站推广公司 优帮云商贸有限公司
  • 先用ps后用dw做网站青海移动网站建设
  • 龙岩做网站公司邵阳隆回今天新闻头条
  • 山东高端网站定制如何做网站卡密
  • 成都网站建设需多少钱wordpress文件存放不同目录下
  • 商城网站建设公司电话WordPress突然全站404
  • 旅游类网站设计模板下载青岛seo整站优化公司
  • 设计网站的企业河南响应式官网建设哪里好
  • 扬州中兴建设有限公司网站网站建设人工智能开发
  • 做棋牌推广网站违法不河北邯郸邮政编码
  • 安徽柱石建设有限公司网站ui培训设计