# Swufer-RPC **Repository Path**: weizhu66/swufer-rpc ## Basic Information - **Project Name**: Swufer-RPC - **Description**: 基于grpc的轻量级、高性能、极易用的rpc框架,支持SpringBoot,包含负载均衡、限流、降级等服务治理功能,使grpc变得方便易用。 - **Primary Language**: Java - **License**: GPL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 5 - **Forks**: 1 - **Created**: 2022-02-22 - **Last Updated**: 2023-09-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: RPC开发框架 ## README # Swufer-RPC #### 介绍 使用grpc进行通信的轻量级、高性能、极易用的rpc框架,支持SpringBoot,包含服务注册与发现、负载均衡、限流、降级等服务治理功能,使grpc变得方便易用。 grpc是一款高性能的rpc框架,但在编写grpc存在一定的复杂性,需要编写大量proto文件,虽然grpc支持跨语言使用,但对于java开发者来言,使用起来较为不方便。 本项目致力于编写一个帮助Java开发者获得**开发体验接近于本地服务**并且具有**高性能的轻量级**rpc框架。 #### 项目说明 1. 无需编写protobuf文件即可使用grpc,api简单易用,必需额外配置少,开发体验接近于开发单体项目 2. 使用grpc进行网络通信,protobuf作为序列化协议,性能高效 3. 模块抽象,具备强扩展性 4. 使用zookeeper作为注册中心,具备服务注册、发现功能,支持客户端缓存服务地址,实时监听服务的上线与离线。 5. 基于SpringBoot进行便捷的注解式开发,并且也可以也可不依赖框架纯原生开发。 6. 支持同步调用、异步调用、泛化调用。 7. 支持客户端负载均衡,已实现带权重的随机负载均衡、一致性哈希算法,可通过接口自定义负载均衡算法。可指定调用服务地址,绕过注册中心。 8. 客户端最长调用等待时间、超时重试。 9. 服务端支持接口级、方法级的qps限流 10. 服务端、客户端均支持责任链过滤器机制,自定义过滤器可插拔,任意扩展 11. 提供集群容错机制,含快速失败、重试、降级等策略,可自定义容错策略 12. 客户端调用失败后使用本地mock方法 13. 提供上下文机制,可在服务提供者与使用者的任何地方传递参数。 14. 支持token机制,客户端凭借token调用服务 15. 通过traceId进行调用链路追踪分享 16. 服务优雅启动,服务提供者上线后延迟注册到注册中心,防止突然流入大量流量。 17. 服务优雅下线,服务提供者下线或者重启时,拒绝新的请求接入,处理完当前尚未处理完的请求再关闭服务器,防止正在处理的请求响应失败。 18. 消费者自动剔除缓存的失败请求达到阈值的远程服务地址。 #### 安装教程 1. git clone https://gitee.com/weizhu66/swufer-rpc.git 2. mvn compile 3. mvn install 4. 在自己的项目的pom.xml中导入 ``` com.weizhu swufer-core 0.0.1-SNAPSHOT ``` #### 使用说明 ##### 1. 通过SpringBoot项目使用 服务提供者 ``` @SwuferService //实现类上添加注解 注册服务 public class DemoServiceImpl implements DemoService { @Override public String hello(String name) { return "hello from " + name; } } ``` 服务提供者项目启动类 ``` @SpringBootApplication @EnableSwuferAutoStart //添加此注解直接开启RPC服务端 public class App { public static void main(String[] args) throws InterruptedException { SpringApplication.run(App.class, args); new CountDownLatch(1).await(); } } ``` 服务提供者配置application.yml ``` swufer: rpc: registryType: zookeeper registryAddress: 127.0.0.1:2181 #zookeeper地址 port: 8001 #grpc服务器端口 weight: 100 #服务权重 ``` 服务调用者 ``` @RestController public class ConsumerController { @SwuferReference //使用此注解直接注入代理对象 DemoService demoService; @SwuferReference //注入泛化调用服务 GenericService genericService; private static final int nThread = 30; private static final ExecutorService pool = Executors.newFixedThreadPool(nThread); @RequestMapping("test/hello") public String testSync(@RequestParam("name") String name) throws ExecutionException, InterruptedException { return demoService.hello(name); } @RequestMapping("test/generic") public String testGeneric(@RequestParam("name") String name){ try { return (String) genericService.$invoke("com.weizhu.swufer.api.demo.DemoService","hello", "generic_test"); }catch (RpcException e){ return e.getCode() + e.getMessage(); } } } ``` 服务调用者 配置application.yml ``` swufer: rpc: registryType: zookeeper registryAddress: 127.0.0.1:2181 # cluster: FailBackMockCluster # loadBalancer: RandomLoadBalancer server: port: 8080 ``` **仅使用几个注解和配置** 就能在SpringBoot上使用grpc服务 ##### 2. Java无框架调用 服务端: ``` public static void testSerive() throws IOException { //registry:zookeeper地址 //port:grpc通信端口 //scanRpcService:扫描带有@SwuferService的类的包路径 SwuferBootstrap bootstrap = SwuferBootstrap.newBuilder().registry("127.0.0.1:2181") .port(8001).scanRpcService("com.weizhu.swufer.testprovider.demo").buildBootstrap(); //开启服务端 bootstrap.start(); new CountDownLatch(1).await(); } ``` 客户端: ``` public class UnitTest { private static SwuferBootstrap bootstrap = SwuferBootstrap.newBuilder().registry("127.0.0.1:2181") .buildBootstrap(); public static void main(String[] args) { testProxy(); } public static void testProxy(){ ReferenceConfig config = ReferenceConfig.ref(DemoService.class); DemoService reference = (DemoService) bootstrap.getReference(config); String result = reference.hello("hello from swufer"); System.out.println(result); } } ``` 3. xxxx #### 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request #### 特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)