2015年12月4日:让 Netty 使用 openssl

2015年12月4日,风轻云淡

参考这篇文档《Forked Tomcat Native》。

两点注意

  1. netty-tcnative 的版本最高只能是 1.1.33.Fork6,否则会有 libnetty-tcnative<random_number>.so 无法加载的问题。这个问题在 Netty 的 Github 上已经有记录。
  2. 参考这篇文章([HOW TO INSTALL AND UPDATE OPENSSL ON CENTOS 6 / CENTOS 7]()手工编译部署 1.0.2 版本的 openssl。

代码参考

pom.xml 参考配置

<?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>

Channel 初始化参考

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();
    }
}

转载于:https://my.oschina.net/lifany/blog/539444

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值