Coder Social home page Coder Social logo

brpc-java's Introduction

Build Status license maven

项目名称

brpc-java是baidu rpc的java版本实现,主要用于java系统中的rpc交互,支持baidu rpc、nshead、sofa、hulu、http等协议。

核心功能点

  • 支持baidu rpc标准协议、sofa协议、hulu协议、nshead+protobuf协议、http+protobuf/json协议。
  • 可以灵活自定义任意协议,只需要实现Protocol接口,客户端和服务端可以分开实现。
  • 支持使用POJO替代protobuf生成的类来进行序列化(基于jprotobuf实现)。
  • 支持多种naming服务,比如List、File、DNS等,可以灵活扩展支持zookeeper、etcd、eureka、nacos等。
  • 支持多种负载均衡策略,比如fair、random、round robin、weight等。
  • 支持interceptor功能。
  • 支持server端限流:计数器、令牌桶等算法。
  • 支持snappy、gzip、zlib压缩。
  • 将RPC功能和Spring功能分离,既适用于一些无需spring的场景,也适用于包含Spring的场景。

快速开始

开发环境

java 6+ && netty 4 && protobuf 2.5.0

引入maven依赖

非Spring环境:

<dependency>
    <groupId>com.baidu</groupId>
    <artifactId>brpc-java</artifactId>
    <version>2.0.2</version>
</dependency>

Spring环境:

<dependency>
    <groupId>com.baidu</groupId>
    <artifactId>brpc-java-spring</artifactId>
    <version>2.0.2</version>
</dependency>

Server端使用

Client端使用

与Spring集成

扩展

一些设计

网络模型

采用netty的reactor网络模型,但跟常规用法有些不同:

  • 没有使用netty的ByteToMessageDecoder去解析协议,因为ByteToMessageDecoder内部会对buffer进行拷贝。
  • 为了提高并发,我们尽量少在IO线程中执行业务逻辑,所以在io线程中只会去解析协议的header部分,并把body的buffer retain出来,然后丢给工作线程去处理。
  • 工作线程会decode body,并执行具体业务逻辑。
  • 由于粘包/拆包问题,可能一次socket读操作会包含多个包,所以我们支持了批量往工作线程中submit任务。

零拷贝Buffer

线程池ThreadPool

  • 我们调研过JDK的ThreadPoolExecutor、ConcurrentLinkedQueue以及Disruptor,最后还是使用了更高性能的ThreadPool
  • ThreadPool内部把生产者队列、消费者队列分开,用两个锁去控制同步,当consumer queue为空时,且producer queue不为空条件满足时,会交换两个队列。

比ConcurrentHashMap更快的FastFutureStore

压力测试数据

部署环境:

  • Client/Server机器配置:cpu 12核,内存132G,千兆网卡。
  • 压测代码

压力测试结果:

数据量 5 byte 1k byte 2k byte 4k byte
qps 22w 10w 5.3w 2.7w

测试

mvn clean install

brpc-java's People

Contributors

wenweihu86 avatar wjyheropk avatar wanghongfei avatar wy223170 avatar raptium avatar

Watchers

James Cloos avatar Jieyu Zeng avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.