2015年12月4日,風輕雲淡html
參考這篇文檔《Forked Tomcat Native》。java
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <dependencies> <dependency> <groupId>io.netty</groupId> <artifactId>netty-tcnative</artifactId> <version>1.1.33.Fork6</version> <classifier>${os.detected.classifier}</classifier> </dependency> </dependencies> <build> <plugins> </plugins> <extensions> <extension> <groupId>kr.motd.maven</groupId> <artifactId>os-maven-plugin</artifactId> <version>1.4.1.Final</version> </extension> </extensions> </build> </project>
public class ProxyInitializer extends ChannelInitializer<SocketChannel> { private static SslContext sslContext; @Override public void initChannel(SocketChannel ch) throws SSLException, CertificateException { ChannelPipeline pipeline = ch.pipeline(); if (sslContext != null) pipeline.addLast(new SslHandler(sslContext.newEngine(ch.alloc()))); pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new HttpObjectAggregator(1024 * 1024 + 2048)); pipeline.addLast(new Dispatcher()); } private void initNetty() throws InterruptedException, SSLException, CertificateException { InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory()); LOGGER.info("OpenSsl.isAvailable(): {}", OpenSsl.isAvailable()); if (OpenSsl.isAvailable()) { SelfSignedCertificate ssc = new SelfSignedCertificate(); SslContextBuilder sslContextBuilder = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).sslProvider(SslProvider.OPENSSL); sslContext = sslContextBuilder.build(); } // Configure the bootstrap. EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(this) .childOption(ChannelOption.TCP_NODELAY, true) .bind(7000).sync().channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } public static void main(String[] args) { ProxyInitializer initializer = new ProxyInitializer(); initializer.initialize(); } }