com.alipay.remoting.rpc.RpcServer Java Examples

The following examples show how to use com.alipay.remoting.rpc.RpcServer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BadServerIpTest.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
public void startServer() {
    server = new RpcServer(ip, 1111);
    server.registerUserProcessor(new SyncUserProcessor<RequestBody>() {
        @Override
        public Object handleRequest(BizContext bizCtx, RequestBody request)
                                                                           throws Exception {
            logger.warn("Request received:" + request);
            return "hello world!";
        }

        @Override
        public String interest() {
            return String.class.getName();
        }

        @Override
        public Executor getExecutor() {
            return null;
        }

    });
    server.start();
}
 
Example #2
Source File: RemotingUtilTest.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
public void startServer() {
    server = new RpcServer(port);
    server.registerUserProcessor(new SyncUserProcessor<RequestBody>() {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 3, 60, TimeUnit.SECONDS,
                                        new ArrayBlockingQueue<Runnable>(4),
                                        new NamedThreadFactory("Request-process-pool"));

        @Override
        public Object handleRequest(BizContext bizCtx, RequestBody request) {
            logger.warn("Request received:" + request);
            return "Hello world!";
        }

        @Override
        public String interest() {
            return RequestBody.class.toString();
        }

        @Override
        public Executor getExecutor() {
            return executor;
        }

    });
    server.start();
}
 
Example #3
Source File: MockServer.java    From sofa-registry with Apache License 2.0 5 votes vote down vote up
/**
 * Start.
 */
public void start() {
    rpcServer = new RpcServer(port);
    rpcServer.registerUserProcessor(new MockSubscriberRegisterProcessor());
    rpcServer.registerUserProcessor(new MockPublisherRegisterProcessor());
    rpcServer.registerUserProcessor(new MockConfiguratorRegisterProcesor());
    rpcServer.start(ip);
}
 
Example #4
Source File: BoltServer.java    From sofa-registry with Apache License 2.0 5 votes vote down vote up
/**
 * start bolt server
 */
public void startServer() {
    if (isStarted.compareAndSet(false, true)) {
        try {
            boltServer = new RpcServer(url.getPort(), true);
            initHandler();
            boltServer.start();

        } catch (Exception e) {
            isStarted.set(false);
            LOGGER.error("Start bolt server error!", e);
            throw new RuntimeException("Start bolt server error!", e);
        }
    }
}
 
Example #5
Source File: BoltServer.java    From sofa-registry with Apache License 2.0 5 votes vote down vote up
/**
 * just init cant start
 */
public void initServer() {
    try {
        boltServer = new RpcServer(url.getPort(), true);
        initHandler();
    } catch (Exception e) {
        LOGGER.error("Init bolt server error!", e);
        throw new RuntimeException("Init bolt server error!", e);
    }
}
 
Example #6
Source File: RaftServer.java    From sofa-registry with Apache License 2.0 5 votes vote down vote up
/**
 * start raft server
 * @param raftServerConfig
 * @throws IOException
 */
public void start(RaftServerConfig raftServerConfig) throws IOException {

    FileUtils.forceMkdir(new File(dataPath));

    serverHandlers.add(new RaftServerHandler(this));
    serverHandlers.add(new RaftServerConnectionHandler());

    boltServer = new BoltServer(new URL(NetUtil.getLocalAddress().getHostAddress(),
        serverId.getPort()), serverHandlers);

    boltServer.initServer();

    RpcServer rpcServer = boltServer.getRpcServer();

    RaftRpcServerFactory.addRaftRequestProcessors(rpcServer);

    this.fsm = ServiceStateMachine.getInstance();
    this.fsm.setLeaderProcessListener(leaderProcessListener);
    this.fsm.setFollowerProcessListener(followerProcessListener);

    NodeOptions nodeOptions = initNodeOptions(raftServerConfig);

    this.raftGroupService = new RaftGroupService(groupId, serverId, nodeOptions, rpcServer);
    //start
    this.node = this.raftGroupService.start();

    if (raftServerConfig.isEnableMetrics()) {
        ReporterUtils.startSlf4jReporter(raftServerConfig.getEnableMetricsReporterPeriod(),
            node.getNodeMetrics().getMetricRegistry(), raftServerConfig.getMetricsLogger());
    }

    RpcClient raftClient = ((AbstractBoltClientService) (((NodeImpl) node).getRpcService()))
        .getRpcClient();

    NotifyLeaderChangeHandler notifyLeaderChangeHandler = new NotifyLeaderChangeHandler(
        groupId, null);
    raftClient.registerUserProcessor(new SyncUserProcessorAdapter(notifyLeaderChangeHandler));
}
 
Example #7
Source File: DmetaServer.java    From distkv with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public DmetaServer(final String dataPath, final String groupId, final PeerId serverId,
                   final NodeOptions nodeOptions) throws IOException {
  // init path
  FileUtils.forceMkdir(new File(dataPath));

  // make raft RPC and work RPC use same RPC server
  final RpcServer rpcServer = new RpcServer(serverId.getPort());
  RaftRpcServerFactory.addRaftRequestProcessors(rpcServer);
  // Registration processor
  rpcServer.registerUserProcessor(new HeartbeatRequestProcessor(this));
  rpcServer.registerUserProcessor(new GetGlobalViewRequestProcessor(this));
  // init StateMachine
  this.fsm = new MetaStateMachine();
  // set StateMachine
  nodeOptions.setFsm(this.fsm);
  // set data path
  // log
  nodeOptions.setLogUri(dataPath + File.separator + "log" + serverId.getPort());
  // meta info
  nodeOptions.setRaftMetaUri(dataPath + File.separator + "raft_meta");
  // snapshot,optional
  nodeOptions.setSnapshotUri(dataPath + File.separator + "counter_snapshot");
  // init raft group framework
  this.raftGroupService = new RaftGroupService(groupId, serverId, nodeOptions, rpcServer);
  // start
  this.node = this.raftGroupService.start();
}
 
Example #8
Source File: RemotingServerTest.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Test
public void testStartRepeatedly() {
    RpcServer rpcServer = new RpcServer(1111);
    rpcServer.start();

    try {
        rpcServer.start();
        Assert.fail("Should not reach here!");
    } catch (Exception e) {
        // expect IllegalStateException
    }
    rpcServer.stop();
}
 
Example #9
Source File: BoltServer.java    From sofa-registry with Apache License 2.0 4 votes vote down vote up
public RpcServer getRpcServer() {
    return boltServer;
}
 
Example #10
Source File: KitRaft.java    From KitDB with Apache License 2.0 4 votes vote down vote up
public KitRaft(GroupConfig groupConfig, NodeConfig nodeConfig, DB db) throws IOException {

        NodeOptions nodeOptions = new NodeOptions();

        RaftOptions raftOptions = new RaftOptions();
        raftOptions.setDisruptorBufferSize(16 * 16384);
        raftOptions.setApplyBatch(128);
        raftOptions.setSync(false);
        nodeOptions.setRaftOptions(raftOptions);

        nodeOptions.setElectionTimeoutMs(groupConfig.getElectionTimeoutMs());
        nodeOptions.setDisableCli(true);
        nodeOptions.setSnapshotIntervalSecs(groupConfig.getSnapshotIntervalSecs());

        PeerId serverId = new PeerId();
        if (!serverId.parse(nodeConfig.getNode())) {
            throw new IllegalArgumentException("Fail to parse serverId:" + nodeConfig.getNode());
        }

        Configuration initConf = new Configuration();
        if (!initConf.parse(groupConfig.getInitNodes())) {
            throw new IllegalArgumentException("Fail to parse initConf:" + groupConfig.getInitNodes());
        }

        nodeOptions.setInitialConf(initConf);

        String raftDir = nodeConfig.getRaftDir();
        FileUtils.forceMkdir(new File(raftDir));

        RpcServer rpcServer = new RpcServer(serverId.getPort());
        RaftRpcServerFactory.addRaftRequestProcessors(rpcServer);

        this.dbsm = new DBStateMachine();
        dbsm.setDbRequestProcessor(new DBRequestProcessor(this));
        dbsm.setDB(db);
        nodeOptions.setFsm(this.dbsm);

        nodeOptions.setLogUri(raftDir + File.separator + "log");
        nodeOptions.setRaftMetaUri(raftDir + File.separator + "raft_meta");
        nodeOptions.setSnapshotUri(raftDir + File.separator + "snapshot");

        this.raftGroupService = new RaftGroupService(groupConfig.getGroup(), serverId, nodeOptions, rpcServer);
        // 启动
        this.node = this.raftGroupService.start();
    }
 
Example #11
Source File: BoltServer.java    From sofa-bolt with Apache License 2.0 4 votes vote down vote up
public BoltServer(int port) {
    this.port = port;
    this.server = new RpcServer(this.port);
}
 
Example #12
Source File: BoltServer.java    From sofa-bolt with Apache License 2.0 4 votes vote down vote up
public BoltServer(int port, boolean manageFeatureEnabled) {
    this.port = port;
    this.server = new RpcServer(this.port, manageFeatureEnabled);
}
 
Example #13
Source File: BoltServer.java    From sofa-bolt with Apache License 2.0 4 votes vote down vote up
public BoltServer(int port, boolean manageFeatureEnabled, boolean syncStop) {
    this.port = port;
    this.server = new RpcServer(this.port, manageFeatureEnabled, syncStop);
}
 
Example #14
Source File: BoltServer.java    From sofa-bolt with Apache License 2.0 4 votes vote down vote up
public RpcServer getRpcServer() {
    return this.server;
}
 
Example #15
Source File: BoltServer.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
protected RemotingServer initRemotingServer() {
    // 绑定到端口
    RemotingServer remotingServer = new RpcServer(serverConfig.getBoundHost(), serverConfig.getPort());
    remotingServer.registerUserProcessor(boltServerProcessor);
    return remotingServer;
}