RPC和服務注冊框架實現
simple-rpc
RPC BASED ON NETTY Running in some business online.
內部結構
- IO netty
- Serialize with protostuff, kryo
- Transparent service discovery and connection management
現有功能
- 基本的客戶端、服務端交互
- 提供代理實現接口
- spring 集成, xml配置和Java Config配置方式
- 服務發布訂閱 DONE
- 斷線重連 DONE
RoadMap
- 服務心跳檢測
- 連接池
- 服務注冊發布功能
- 服務管理、監控
- 服務調用日志鏈路跟蹤
- 集成swagger功能,提供文檔、測試、客戶端生成
消息協議
當前采用簡單的在消息體前加上4byte的消息長度值
使用示例
// 需要先啟動一個zookeeper作為服務注冊發現中心
// 服務接口
public interface IHello {
`
String say(String hello);
int sum(int a, int b);
int sum(Integer a, Integer b);
}
// 服務實現
public class HelloImpl implements IHello {
public String say(String hello) {
return "return " + hello;
}
public int sum(int a, int b) {
return a + b;
}
public int sum(Integer a, Integer b) {
return a + b * 3;
}
}
// 客戶端代碼
// beanJavaConfig方式
@Bean
public CountService countService() {
RpcClientWithLB rpcClientWithLB = new RpcClientWithLB("fyes-counter");
rpcClientWithLB.setZkConn("127.0.0.1:2181");
rpcClientWithLB.init();
CountService countService = rpcClientWithLB.newProxy(CountService.class);
return countService;
}
// 服務端發布
// xml配置方式
<bean class="com.github.liuzhengyang.simplerpc.ServerFactoryBean" init-method="start">
<property name="serviceInterface" value="com.test.liuzhengyang.CountService"/>
<property name="port" value="8888"/>
<property name="serviceName" value="fyes-counter"/>
<property name="serviceImpl" ref="countServiceImpl"/>
<property name="zkConn" value="127.0.0.1:2181"/></code></pre>