一个基于Netty,Zookeeper和SpringBoot的RPC框架
作者CSDN主页:鲸临于空
- 基于Spring Boot 2的自动发现,添加Starter依赖即可快速集成,开箱即用
- 基于ZooKeeper的服务发现,支持ZooKeeper集群
- 基于Netty的底层通信
1.0版本实现了原型功能,达到初步可用状态,但性能较差,仅作学习交流用
2.0版本针对性能做了优化,并添加一些新功能:
- 增加@RpcReference注解,自动注入服务实现
- 增加负载均衡路由策略引擎(含随机、轮询、哈希等及其带权形式)
- 增加序列化引擎(支持 JDK默认、Hessian、Json、Protostuff、Xml、Avro、ProtocolBuffer、Thrift等序列化方式)
- 服务提供者提供服务时可注解参数指定最大工作线程数来限流
- 服务消费者对服务地址列表进行缓存,并监听变化
- 传输协议修改,使用消息头+消息体的模式以支持新特性并避免粘包半包问题和留下扩展空间
后续将对核心进行改造,使其支持异步模型
以下例子可参考:examples
-
定义公共接口:
public interface HelloService { String hello(String name); }
-
添加对应依赖(注意更新到最新版本)
- 服务提供者:
<dependency> <groupId>com.github.linshenkx</groupId> <artifactId>rpc-netty-server-spring-boot-starter</artifactId> <version>2.0.2.RELEASE</version> </dependency>
- 服务消费者:
<dependency> <groupId>com.github.linshenkx</groupId> <artifactId>rpc-netty-client-spring-boot-starter</artifactId> <version>2.0.2.RELEASE</version> </dependency>
-
在Spring配置文件里配置zookeeper地址列表和rpc端口,如下例,
其中connectTimeOut和sessionTimeOut都有默认值,应根据网络环境配置修改
服务提供者和服务消费者应使用相同 registryPath,一般无需配置,使用默认值即可(如果有多个不同系统则可修改,达到隔离目的)
-
服务提供者 application 文件
zk: address: 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183 connectTimeOut: 10000 sessionTimeOut: 10000 registryPath: "/defaultRegistry" rpc: server: port: 9991 ```
-
服务消费者 application 文件
server: port: 9090 zk: address: 127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183 connectTimeOut: 20000 sessionTimeOut: 10000 registryPath: "/defaultRegistry" rpc: client: routeStrategy: Polling serializeType: ProtoStuff ```
-
-
服务提供方使用@RpcService标注接口实现 : 其中weight代表权重,默认为1。workerThreads代表工作线程数,可用于限流,默认为10.
@RpcService(value = HelloService.class,weight = 2,workerThreads = 3) public class HelloServiceImpl implements HelloService { @Override public String say(String name) { return "hello " + name; } }
-
服务消费者使用@RpcReference标记服务接口,即可注入服务实现类代理
@RestController public class HelloController { @RpcReference private HelloService helloService; @GetMapping("/hello") public String sayHello(@RequestParam(defaultValue = "lin") String name){ return helloService.say(name); } }