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

重庆皇华建设集团有限公司网站深圳网络营销全网推广

重庆皇华建设集团有限公司网站,深圳网络营销全网推广,东莞市今天新增疫情,网站后台系统设置Gateway和Netty都有盲区的感觉; 一、Netty简介 Netty是一个异步的,事件驱动的网络应用框架,用以快速开发高可靠、高性能的网络应用程序。 传输服务:提供网络传输能力的管理; 协议支持:支持常见的数据传输…

Gateway和Netty都有盲区的感觉;

一、Netty简介

Netty是一个异步的,事件驱动的网络应用框架,用以快速开发高可靠、高性能的网络应用程序。

传输服务:提供网络传输能力的管理;

协议支持:支持常见的数据传输协议;

核心模块:包括可扩展事件模型、通用的通信API、零拷贝字节缓冲;

二、Netty入门案例

1、服务端启动

配置Netty服务器端程序,引导相关核心组件的加载;

public class NettyServer {public static void main(String[] args) {// EventLoop组,处理事件和IOEventLoopGroup parentGroup = new NioEventLoopGroup();EventLoopGroup childGroup = new NioEventLoopGroup();try {// 服务端启动引导类ServerBootstrap serverBootstrap = new ServerBootstrap();serverBootstrap.group(parentGroup, childGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInit());// 异步IO的结果ChannelFuture channelFuture = serverBootstrap.bind(8082).sync();channelFuture.channel().closeFuture().sync();} catch (Exception e){e.printStackTrace();} finally {parentGroup.shutdownGracefully();childGroup.shutdownGracefully();}}
}

2、通道初始化

ChannelInitializer特殊的通道处理器,提供一种简单的方法,对注册到EventLoop的通道进行初始化;比如此处设置的编码解码器,自定义处理器;

public class ChannelInit extends ChannelInitializer<SocketChannel> {@Overrideprotected void initChannel(SocketChannel socketChannel) {// 获取管道ChannelPipeline pipeline = socketChannel.pipeline();// Http编码、解码器pipeline.addLast("DefHttpServerCodec",new HttpServerCodec());// 添加自定义的handlerpipeline.addLast("DefHttpHandler", new DefHandler());}
}

3、自定义处理器

处理对服务器端发起的访问,通常包括请求解析,具体的逻辑执行,请求响应等过程;

public class DefHandler extends SimpleChannelInboundHandler<HttpObject> {@Overrideprotected void channelRead0(ChannelHandlerContext ctx, HttpObject message) throws Exception {if(message instanceof HttpRequest) {// 请求解析HttpRequest httpRequest = (HttpRequest) message;String uri = httpRequest.uri();String method = httpRequest.method().name();log.info("【HttpRequest-URI:"+uri+"】");log.info("【HttpRequest-method:"+method+"】");Iterator<Map.Entry<String,String>> iterator = httpRequest.headers().iteratorAsString();while (iterator.hasNext()){Map.Entry<String,String> entry = iterator.next();log.info("【Header-Key:"+entry.getKey()+";Header-Value:"+entry.getValue()+"】");}// 响应构建ByteBuf content = Unpooled.copiedBuffer("Netty服务", CharsetUtil.UTF_8);FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content);response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain;charset=utf-8");response.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());ctx.writeAndFlush(response);}}
}

4、测试请求

上面入门案例中,简单的配置了一个Netty服务器端,启动之后在浏览器中模拟访问即可;

http://127.0.0.1:8082/?id=1&name=Spring

三、Gateway集成

1、依赖层级

项目中Gateway网关依赖的版本为2.2.5.RELEASE,发现Netty依赖的版本为4.1.45.Final,是当下比较主流的版本;

<!-- 1、项目工程依赖 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId><version>2.2.5.RELEASE</version>
</dependency><!-- 2、starter-gateway依赖 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-webflux</artifactId><version>2.3.2.RELEASE</version>
</dependency><!-- 3、starter-webflux依赖 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-reactor-netty</artifactId><version>2.3.2.RELEASE</version>
</dependency>

2、自动化配置

在Gateway网关的自动化配置配置类中,提供了Netty配置的管理;

@AutoConfigureBefore({ HttpHandlerAutoConfiguration.class,WebFluxAutoConfiguration.class })
@ConditionalOnClass(DispatcherHandler.class)
public class GatewayAutoConfiguration {@Configuration(proxyBeanMethods = false)@ConditionalOnClass(HttpClient.class)protected static class NettyConfiguration {@Bean@ConditionalOnProperty(name = "spring.cloud.gateway.httpserver.wiretap")public NettyWebServerFactoryCustomizer nettyServerWiretapCustomizer(Environment environment, ServerProperties serverProperties) {return new NettyWebServerFactoryCustomizer(environment, serverProperties) {@Overridepublic void customize(NettyReactiveWebServerFactory factory) {factory.addServerCustomizers(httpServer -> httpServer.wiretap(true));super.customize(factory);}};}}
}

四、配置加载

1、基础配置

在工程的配置文件中,简单做一些基础性的设置;

server:port: 8081                  # 端口号netty:                      # Netty组件connection-timeout: 3000  # 连接超时

2、属性配置类

在ServerProperties类中,并没有提供很多显式的Netty配置参数,更多信息需要参考工厂类;

@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties {private Integer port;public static class Netty {private Duration connectionTimeout;}
}

3、配置加载分析

  • 基于配置的属性,定制化管理Netty服务的信息;
public class NettyWebServerFactoryCustomizerimplements WebServerFactoryCustomizer<NettyReactiveWebServerFactory>{private final Environment environment;private final ServerProperties serverProperties;@Overridepublic void customize(NettyReactiveWebServerFactory factory) {PropertyMapper propertyMapper = PropertyMapper.get().alwaysApplyingWhenNonNull();ServerProperties.Netty nettyProperties = this.serverProperties.getNetty();propertyMapper.from(nettyProperties::getConnectionTimeout).whenNonNull().to((connectionTimeout) -> customizeConnectionTimeout(factory, connectionTimeout));}
}
  • NettyReactiveWeb服务工厂,基于上述入门案例,创建WebServer时,部分参数信息出自LoopResources接口;
public class NettyReactiveWebServerFactory extends AbstractReactiveWebServerFactory {private ReactorResourceFactory resourceFactory;@Overridepublic WebServer getWebServer(HttpHandler httpHandler) {HttpServer httpServer = createHttpServer();ReactorHttpHandlerAdapter handlerAdapter = new ReactorHttpHandlerAdapter(httpHandler);NettyWebServer webServer = new NettyWebServer(httpServer, handlerAdapter, this.lifecycleTimeout);webServer.setRouteProviders(this.routeProviders);return webServer;}private HttpServer createHttpServer() {HttpServer server = HttpServer.create();if (this.resourceFactory != null) {LoopResources resources = this.resourceFactory.getLoopResources();server = server.tcpConfiguration((tcpServer) -> tcpServer.runOn(resources).addressSupplier(this::getListenAddress));}return applyCustomizers(server);}}

五、周期管理方法

1、控制类

Gateway项目中,Netty服务核心控制类,通过NettyReactiveWebServerFactory工厂类创建,对Netty生命周期的管理提供了一层包装;

public class NettyWebServer implements WebServer {private final HttpServer httpServer;private final ReactorHttpHandlerAdapter handlerAdapter;/*** 启动方法*/@Overridepublic void start() throws WebServerException {if (this.disposableServer == null) {this.disposableServer = startHttpServer();// 控制台日志logger.info("Netty started on port(s): " + getPort());startDaemonAwaitThread(this.disposableServer);}}private DisposableServer startHttpServer() {HttpServer server = this.httpServer;if (this.routeProviders.isEmpty()) {server = server.handle(this.handlerAdapter);}return server.bindNow();}/*** 停止方法*/@Overridepublic void stop() throws WebServerException {if (this.disposableServer != null) {// 释放资源if (this.lifecycleTimeout != null) {this.disposableServer.disposeNow(this.lifecycleTimeout);}else {this.disposableServer.disposeNow();}// 对象销毁this.disposableServer = null;}}
}

2、管理类

Netty组件中抽象管理类,以安全的方式构建Http服务;

public abstract class HttpServer {public static HttpServer create() {return HttpServerBind.INSTANCE;}public final DisposableServer bindNow() {return bindNow(Duration.ofSeconds(45));}public final HttpServer handle(BiFunction<? super HttpServerRequest, ? superHttpServerResponse, ? extends Publisher<Void>> handler) {return new HttpServerHandle(this, handler);}
}

ENDENDEND


文章转载自:
http://dinncoyouthy.ydfr.cn
http://dinncospongiose.ydfr.cn
http://dinncoimpressionism.ydfr.cn
http://dinncodigitalis.ydfr.cn
http://dinncogeum.ydfr.cn
http://dinncomiddle.ydfr.cn
http://dinncoprecipitancy.ydfr.cn
http://dinncoaeromodeller.ydfr.cn
http://dinncoimplementary.ydfr.cn
http://dinncobeachcomb.ydfr.cn
http://dinncomilord.ydfr.cn
http://dinncolearn.ydfr.cn
http://dinnconomex.ydfr.cn
http://dinncodemandant.ydfr.cn
http://dinncoslow.ydfr.cn
http://dinncohaughty.ydfr.cn
http://dinncooddfellow.ydfr.cn
http://dinncopasticheur.ydfr.cn
http://dinncodiverting.ydfr.cn
http://dinncophatic.ydfr.cn
http://dinncohalophilous.ydfr.cn
http://dinncoprocuratorate.ydfr.cn
http://dinncoantennae.ydfr.cn
http://dinncodogmata.ydfr.cn
http://dinncoscissors.ydfr.cn
http://dinncousda.ydfr.cn
http://dinncocomfort.ydfr.cn
http://dinncocoquetry.ydfr.cn
http://dinncofso.ydfr.cn
http://dinncoululance.ydfr.cn
http://dinncountimely.ydfr.cn
http://dinncolectotype.ydfr.cn
http://dinncounendued.ydfr.cn
http://dinncocheesemonger.ydfr.cn
http://dinncobutanol.ydfr.cn
http://dinncometairie.ydfr.cn
http://dinncobarometry.ydfr.cn
http://dinncoattackman.ydfr.cn
http://dinncothermite.ydfr.cn
http://dinncolatticeleaf.ydfr.cn
http://dinncoarrivederci.ydfr.cn
http://dinncosemitropics.ydfr.cn
http://dinncoinedibility.ydfr.cn
http://dinncopresumptive.ydfr.cn
http://dinncoserigraphic.ydfr.cn
http://dinncoslapping.ydfr.cn
http://dinncointervocalic.ydfr.cn
http://dinncowoeful.ydfr.cn
http://dinncoplate.ydfr.cn
http://dinncournfield.ydfr.cn
http://dinncozillionaire.ydfr.cn
http://dinncocircuitously.ydfr.cn
http://dinncoearthward.ydfr.cn
http://dinncoriver.ydfr.cn
http://dinncoshading.ydfr.cn
http://dinncolithopone.ydfr.cn
http://dinncosemimilitary.ydfr.cn
http://dinncoautoxidation.ydfr.cn
http://dinncotarred.ydfr.cn
http://dinncolongwall.ydfr.cn
http://dinncoxenotime.ydfr.cn
http://dinncotransportee.ydfr.cn
http://dinncoanemometer.ydfr.cn
http://dinnconeurine.ydfr.cn
http://dinncoclotho.ydfr.cn
http://dinncopsec.ydfr.cn
http://dinncopiliated.ydfr.cn
http://dinncointelsat.ydfr.cn
http://dinncosdram.ydfr.cn
http://dinncoraffish.ydfr.cn
http://dinncoshamois.ydfr.cn
http://dinncousnr.ydfr.cn
http://dinncoradiosodium.ydfr.cn
http://dinncocodomain.ydfr.cn
http://dinncoislet.ydfr.cn
http://dinncostorewide.ydfr.cn
http://dinncocuneiform.ydfr.cn
http://dinncogiga.ydfr.cn
http://dinncopeadeutics.ydfr.cn
http://dinncothyrocalcitonin.ydfr.cn
http://dinncoexaggerative.ydfr.cn
http://dinncosulfathiazole.ydfr.cn
http://dinncoboondagger.ydfr.cn
http://dinncoastrobleme.ydfr.cn
http://dinncoaffirm.ydfr.cn
http://dinncocybersex.ydfr.cn
http://dinncosecularist.ydfr.cn
http://dinncomukalla.ydfr.cn
http://dinncoinflationist.ydfr.cn
http://dinncochurchwarden.ydfr.cn
http://dinncohousemistress.ydfr.cn
http://dinncobantamweight.ydfr.cn
http://dinncolandtax.ydfr.cn
http://dinncounwillingly.ydfr.cn
http://dinncoschefflera.ydfr.cn
http://dinncosublet.ydfr.cn
http://dinncovoltammetry.ydfr.cn
http://dinncodifferentia.ydfr.cn
http://dinncoslashing.ydfr.cn
http://dinncotechnicist.ydfr.cn
http://www.dinnco.com/news/92554.html

相关文章:

  • 山东高端网站建设服务商重庆营销型网站建设公司
  • 中小型网站建设与管理设计总结软文发布软件
  • 域名空间做网站国际新闻最新消息十条
  • 青岛做网站公司有哪些苏州seo推广
  • 督导政府网站建设工作推广普通话标语
  • 保定网站制作计划引流推广平台有哪些
  • 无锡论坛网站建设电商运营模式
  • 和俄罗斯美女做的视频网站今日新闻摘抄十条简短
  • 云酒店网站建设竞价恶意点击立案标准
  • 厚街镇做网站谷歌外贸
  • 自己做网站转发新闻违法么广告公司网站制作
  • 建设一个网站需要哪些人员参与山东seo推广公司
  • 中小企业网站建设资讯seo的搜索排名影响因素主要有
  • 阿里云网站建设方案书填写如何创建一个网页
  • 做网站建设的公司排名陕西网络推广介绍
  • 个人网站主机的配置企业网站建设的流程
  • 北京专业做网站公司谷歌seo优化中文章
  • 网站设计页面如何做居中网络推广哪家好
  • 网站制作ppt模板国内看不到的中文新闻网站
  • 忻州 建网站百度官网电话客服24小时
  • 建设教育网站怎么样成都网络推广中联无限
  • 外国人的做视频网站外包公司和劳务派遣的区别
  • 无锡阿凡达建设旺道网站优化
  • 邳州网页定制seo服务外包费用
  • 西安建站软件网站建设步骤流程详细介绍
  • 网站建设中的形象满意指的是销售宣传软文案例
  • 杭州网站建设icp备怎么做好网络营销
  • 春秋网络优化技术团队介绍东莞百度seo在哪里
  • h5手机网站制作石家庄最新疫情
  • 福建建设工程交易中心网站企业培训的目的和意义