/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.remoting.rpc;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.security.KeyStore;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.TrustManagerFactory;
import com.alipay.remoting.*;
import com.alipay.remoting.config.BoltGenericOption;
import com.alipay.remoting.config.BoltServerOption;
import org.slf4j.Logger;
import com.alipay.remoting.codec.Codec;
import com.alipay.remoting.config.ConfigManager;
import com.alipay.remoting.constant.Constants;
import com.alipay.remoting.exception.RemotingException;
import com.alipay.remoting.log.BoltLoggerFactory;
import com.alipay.remoting.rpc.protocol.UserProcessor;
import com.alipay.remoting.rpc.protocol.UserProcessorRegisterHelper;
import com.alipay.remoting.util.IoUtils;
import com.alipay.remoting.util.NettyEventLoopUtil;
import com.alipay.remoting.util.RemotingUtil;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.WriteBufferWaterMark;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.flush.FlushConsolidationHandler;
import io.netty.handler.timeout.IdleStateHandler;
/**
* Server for Rpc.
*
* Usage:
* You can initialize RpcServer with one of the three constructors:
* {@link #RpcServer(int)}, {@link #RpcServer(int, boolean)}, {@link #RpcServer(int, boolean, boolean)}
* Then call start() to start a rpc server, and call stop() to stop a rpc server.
*
* Notice:
* Once rpc server has been stopped, it can never be start again. You should init another instance of RpcServer to use.
*
* @author jiangping
* @version $Id: RpcServer.java, v 0.1 2015-8-31 PM5:22:22 tao Exp $
*/
public class RpcServer extends AbstractRemotingServer {
/** logger */
private static final Logger logger = BoltLoggerFactory
.getLogger("RpcRemoting");
/** server bootstrap */
private ServerBootstrap bootstrap;
/** channelFuture */
private ChannelFuture channelFuture;
/** connection event handler */
private ConnectionEventHandler connectionEventHandler;
/** connection event listener */
private ConnectionEventListener connectionEventListener = new ConnectionEventListener();
/** user processors of rpc server */
private ConcurrentHashMap<String, UserProcessor<?>> userProcessors = new ConcurrentHashMap<String, UserProcessor<?>>(
4);
/** boss event loop group, boss group should not be daemon, need shutdown manually*/
private final EventLoopGroup bossGroup = NettyEventLoopUtil
.newEventLoopGroup(
1,
new NamedThreadFactory(
"Rpc-netty-server-boss",
false));
/** worker event loop group. Reuse I/O worker threads between rpc servers. */
private static final EventLoopGroup workerGroup = NettyEventLoopUtil
.newEventLoopGroup(
Runtime
.getRuntime()
.availableProcessors() * 2,
new NamedThreadFactory(
"Rpc-netty-server-worker",
true));
/** address parser to get custom args */
private RemotingAddressParser addressParser;
/** connection manager */
private DefaultServerConnectionManager connectionManager;
/** rpc remoting */
protected RpcRemoting rpcRemoting;
/** rpc codec */
private Codec codec = new RpcCodec();
static {
if (workerGroup instanceof NioEventLoopGroup) {
((NioEventLoopGroup) workerGroup).setIoRatio(ConfigManager.netty_io_ratio());
} else if (workerGroup instanceof EpollEventLoopGroup) {
((EpollEventLoopGroup) workerGroup).setIoRatio(ConfigManager.netty_io_ratio());
}
}
/**
* Construct a rpc server with random port <br>
* the random port will determined after server startup
* Note:<br>
* You can only use invoke methods with params {@link Connection}, for example {@link #invokeSync(Connection, Object, int)} <br>
* Otherwise {@link UnsupportedOperationException} will be thrown.
*/
public RpcServer() {
this(false);
}
/**
* Construct a rpc server with random port <br>
* the random port will determined after server startup
* Note:<br>
* You can only use invoke methods with params {@link Connection}, for example {@link #invokeSync(Connection, Object, int)} <br>
* Otherwise {@link UnsupportedOperationException} will be thrown.
*
* @param manageConnection true to enable connection management feature
*/
public RpcServer(boolean manageConnection) {
this(0, manageConnection);
}
/**
* Construct a rpc server. <br>
*
* Note:<br>
* You can only use invoke methods with params {@link Connection}, for example {@link #invokeSync(Connection, Object, int)} <br>
* Otherwise {@link UnsupportedOperationException} will be thrown.
*/
public RpcServer(int port) {
this(port, false);
}
/**
* Construct a rpc server. <br>
*
* Note:<br
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
SOFABolt 是蚂蚁金融服务集团开发的一套基于 Netty 实现的网络通信框架。为了让 Java 程序员能将更多的精力放在基于网络通信的业务逻辑实现上,而不是过多的纠结于网络底层 NIO 的实现以及处理难以调试的网络问题,Netty 应运而生。为了让中间件开发者能将更多的精力放在产品功能特性实现上,而不是重复地一遍遍制造通信框架的轮子,SOFABolt 应运而生
资源推荐
资源详情
资源评论






















收起资源包目录





































































































共 324 条
- 1
- 2
- 3
- 4
资源评论


Java程序员-张凯
- 粉丝: 1w+
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 人力资源信息化管理x.docx
- 数据仓库和数据挖掘的OLAP技术[1].ppt
- 注塑机上下料机械手机构及自动控制系统PLC的设计.docx
- 项目管理亮点及经验总结.pdf
- 公司项目管理办法(可编辑修改word版).docx
- 基于网络计划技术的生产调度设计与实现.doc
- 最新毕业设计(基于单片机控制的智能电源的设计)整稿.doc
- 小学生网络使用情况调查问卷.doc
- 计算机控制技术实验.doc
- 医院信息化及电子政务实施建设的几个热点话题PPT课件.ppt
- 物联网十二五发展纲要.docx
- 基于JAVA的餐饮管理系统设计说明书.doc
- 高三生物复习基因工程练习题.doc
- (源码)基于STM32F1xx系列微控制器的USART DMA通信项目.zip
- 广东省干部培训网络学院2类关于干部教育目标和课程体系的思考考试答案100分.doc
- 吉林大学人工智能学院2023级程序设计导论课程(python)期末大作业
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
