• Stars
    star
    165
  • Rank 221,185 (Top 5 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created almost 9 years ago
  • Updated about 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A protobuf based high performance rpc framework leveraging full-duplexing and asynchronous io with netty

Navi-pbrpc

Coverage Status

Navi-pbrpc provides a rpc solution for using protocol buffer. This library enables client and server to communicate in a peer-to-peer and full duplexing way.

The server-side is built upon netty which supports asynchronous and non-blocking io functionality, while the client-side provides a wide variety of options to communicate with server, which includes short live connection, keep-alive tcp connection, high availability and failover strategy.

Quick Start

1. Prerequisite

Add the below dependency to pom.xml for a maven enabled project.

<dependency>
	<groupId>com.baidu.beidou</groupId>
	<artifactId>navi-pbrpc</artifactId>
	<version>1.1.1</version>
</dependency>

2. Make a protobuf generated message

Use protoc command to compile an IDL proto file and generate a java source file.

The IDL proto file can define the request and response type. Below is a simple sample:

package com.baidu.beidou.navi.pbrpc.demo.proto;
 
option cc_generic_services = true;

message DemoRequest {
    optional int32 user_id = 1;
}

message DemoResponse {
    optional int32 user_id = 1;
    optional string user_name = 2;
    enum GenderType {
        MALE = 1;
        FEMALE = 2;
    }  
    optional GenderType gender_type = 3;
}

3. Develop server-side service

Develop a server-side service implementation. Below is an example based on the IDL generated java code from the previous step. Note that the method doSmth use the generated class DemoRequest as the parameter and DemoResponse as the return type. The logic here is pretty simple.

public class DemoServiceImpl implements DemoService {

    @Override
    public DemoResponse doSmth(DemoRequest req) {
        DemoResponse.Builder builder = DemoResponse.newBuilder();
        builder.setUserId(1);
        builder.setUserName("name-1");
        builder.setGenderType(DemoResponse.GenderType.MALE);
        return builder.build();
    }

}

4. Expose service and start server

Register the service implementation and set the service id as 100. The id here will be used by the client later as a way of locating one specific service. Then start the server on port 8088.

PbrpcServer server = new PbrpcServer(8088);
server.register(100, new DemoServiceImpl());
server.start();

5. Develop client to invoke remote service

The framework provides many options to communicate with the server in terms of short live connection or keep-alive connection, high availablity and failover strategy. You can check out more on the Tutorials wiki.

Below demostrates how to build a keep-alive, full-duplexing connection pool and make a rpc call.

// 1) Build client with keep-alive, full-duplexing pooled connection, and client read timeout is 60s
PbrpcClient client = PbrpcClientFactory.buildPooledConnection(new PooledConfiguration(),
        "127.0.0.1", 8088, 60000);

// 2) Construct request data by using protobuf
DemoRequest.Builder req = DemoRequest.newBuilder();
req.setUserId(1);
byte[] data = req.build().toByteArray();

// 3) Build message by specifying the service id, and the property provider is used as a client trace sign to tell server who I am.
PbrpcMsg msg = new PbrpcMsg();
msg.setServiceId(100);
msg.setProvider("beidou");
msg.setData(data);

// 4) Asynchronous invocation and return a future for client to hold
CallFuture<DemoResponse> future = client.asyncTransport(DemoResponse.class, msg);

// 5) Wait response to come. Once rpc call is done, the code will stop blocking right away.
DemoResponse res = future.get();

// 6) Print out result.
System.out.println(res);

===

Dependencies

Navi-pbrpc tries to leverage minimum amount of dependency libraries, so that project built upon Navi-pbrpc will not be overwhelmed by other unwanted libraries. The following are the dependencies.

[INFO] +- commons-pool:commons-pool:jar:1.5.7:compile
[INFO] +- com.google.protobuf:protobuf-java:jar:2.5.0:compile
[INFO] +- io.netty:netty-all:jar:4.0.28.Final:compile
[INFO] +- org.javassist:javassist:jar:3.18.1-GA:compile
[INFO] +- org.slf4j:slf4j-api:jar:1.7.7:compile
[INFO] +- org.slf4j:slf4j-log4j12:jar:1.7.7:compile
[INFO] |  \- log4j:log4j:jar:1.2.17:compile

===

More information

Click here to Tutorials wiki.

Supports

More Repositories

1

fluent-validator

A Java validation framework leveraging fluent interface style and JSR 303 specification
Java
1,012
star
2

kraps-rpc

A RPC framework leveraging Spark RPC module
Scala
212
star
3

2018-polar-race

1st AliCloud Database Performance Competition in 2018 - Java rank No.1 source code 阿里云2018年第一届PolarDB数据库性能大赛Java排名第一源码
Java
197
star
4

coddding

My sample code repository
Java
149
star
5

dynamic-proxy

Dynamic proxy library leveraging ASM, CGLIB, ByteBuddy, Javassist and JDKDynamicProxy techniques
Java
116
star
6

easy-mapper

Easy-mapper is a simple, light-weighted, high performance java bean mapping framework
Java
114
star
7

navi

Navi is a distributed service framework that provides cluster management and high performance RPC
Java
93
star
8

fountain

Fountain is a Java based toolkit for syncing MySQL binlog and provide an easy API to process/publish events.
Java
83
star
9

biz-framework

针对复杂业务逻辑的Java实现系统,抽象出一套编程框架,借鉴领域模型的设计方法,使得开发体验更加环保、更加友好,大大提高代码的后期可维护性
Java
23
star
10

app-on-yarn-demo

Demo for service oriented application hosted on Hadoop YARN cluster for HA and scheduling
Java
22
star
11

llama2.java

Inference Llama 2 in one file of pure Java
Java
14
star
12

flume-byday-file-sink

A customized Flume file sink which enables you to persist incrementals to file in a rolling by-day and by-type manner
Java
12
star
13

scala-dsl-rpc-client

Scala
7
star
14

io_benchmark

Maximize the disk IO bandwidth for NVMe SSD by sequential read and write.
C++
4
star
15

kafka-example

Kafka example
Java
2
star
16

maven-plugin-archetype

maven check-style & p3c插件在编译打包时做代码规范检查
Java
2
star
17

rest-api-framework

API framework for building RESTful Web Services following JAX-RS and OPEN-API spec, also leveraging JSR330 implementation - Google Guice
Java
2
star
18

spring-boot-yarn-example

Spring boot YARN example
Java
1
star
19

scala-learn

Scala learning arena
Scala
1
star
20

result-util

Result code and message utility for Java programmer
Java
1
star
21

fast_innochecksum

Faster innochecksum
C++
1
star
22

big-traffic-jam-solver

A algorithm for solving the big traffic jam problem
Java
1
star
23

flink-exercises

Flink exercises
Java
1
star
24

hbase-coprocessor-example

HBase coprocessor example
Java
1
star