# bird_rpc **Repository Path**: wangyuyucd/bird_rpc ## Basic Information - **Project Name**: bird_rpc - **Description**: 轻量级rpc框架, - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-03-01 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # bird_rpc #### 介绍 bird-rpc,轻量级rpc框架,基于netty和zookeeper,后期会支持consul,nacos等发现注册中心。 该框架默认是基于springboot,注解的方式引入。非dubbo需要扫描配置文件加载。 #### 调用示例 ![a](architecture.jpg) #### 引入pom ~~~ com.bird br-register-zk 1.0-SNAPSHOT com.bird br-remote-netty 1.0-SNAPSHOT com.bird br-springboot-starter 1.0-SNAPSHOT ~~~ #### 使用说明 1. 服务提供方 @EnableBrRpcServer注解代表该应用提供rpc服务,ComponentScan确保将rpc的实现类能够被容器加载 ~~~ @SpringBootApplication @ComponentScan({"com.product.interfaces.impl"}) @EnableBrRpcServer public class ProductSpringBootStrap { public static void main(String[] args) { new SpringApplication(ProductSpringBootStrap.class).run(args); } } ~~~ 2. 服务消费方 @EnableBrRpcClient注解代表该应用能够调用远程rpc服务,且能够扫描到声明的远程rpc接口 可以配置多个 ~~~ @EnableBrRpcClient({"com.trade.interfaces", "com.product.interfaces"}) @SpringBootApplication @RestController public class ConsumerSpringBootStrap { public static void main(String[] args) { new SpringApplication(ConsumerSpringBootStrap.class).run(args); } } ~~~ 3. rpc接口定义 建议独立成单独的项目,调用方和消费方都能引入。 @BrInterface代表该接口为rpc接口,product代表服务提供方的服务名称。 ~~~ @BrInterface("product") public interface ProductInterface { List getProductInfo(List productIds); } ~~~ 4. 既是服务方,也是消费方 则需要2个注解,如下代表trade应用,既作为trade的rpc提供方,也能调用其他rpc接口(product) ~~~ @SpringBootApplication @EnableBrRpcClient("com.product.interfaces") @ComponentScan({"com.trade.interfaces.impl"}) @EnableBrRpcServer public class TradeSpringBootStrap { public static void main(String[] args) { new SpringApplication(TradeSpringBootStrap.class).run(args); } } ~~~ 5. 异步调用支持 参考阿里dubbo的实现方式,提供返回值为CompletableFuture类型的异步调用。 ~~~ 接口声明 返回类型为CompletableFuture,接口实现类保持一致 @BrInterface("product") public interface ProductInterface { CompletableFuture> asynGetProductInfo(List productIds); } 调用方式 CompletableFuture> listCompletableFuture = productInterface.asynGetProductInfo(Arrays.asList(1)); listCompletableFuture.whenComplete((result, throwable) -> { if (throwable != null) { log.info("返回异常:" + throwable); } else { log.info("返回成功:" + result); } }); ~~~