本项目参考了Dubbo部分结构,并参考了一些基于
Netty
的RPC
实现,没有过多分装,实现较为简单,提供大家一个思路,方便理解RPC
相关实现。 适用初学并对RPC
感兴趣的朋友,请在此基础上自由发挥
- 基于
netty
网络通信 - 集成
java
和hessian
序列化 - 基于
zoopker
和redis
两种注册中心 - 提供
Random
的负载均衡,可扩展 - 集成
SpringBoot
快速启动 - 提供
echo
回声探测
public interface HelloService {
String sayHello(String name);
}
@Provider(value = HelloService.class)
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "hello, " + name;
}
}
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
server:
port: 10004
thor:
register:
address: zookeeper://127.0.0.1:2181 //服务注册中心地址,可选 redis://localhost:6379/1,目前仅支持redis单机模式
server:
port: 10003 //RPC服务端口
host: 127.0.0.1 //PRC服务IP,可不填自动取值
serializer: hessian //netty序列化方式
logging:
level:
root: error
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {ConsumeApplication.class})
public class ConsumeTest {
@Consume
HelloService helloService;
@Test
public void test() {
System.out.print(helloService.sayHello("lemon"));
}
}
server:
port: 10002
thor:
register:
address: zookeeper://127.0.0.1:2181 //服务注册中心地址,可选 redis://localhost:6379/1,目前仅支持redis单机模式
consume:
scanPath: org.cxl.thor.rpc.demo.consume //扫描包路径
serializer: hessian //netty序列化方式
logging:
level:
root: error
public interface DemoService {
String sayHello(String something);
}
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String param) {
return String.format("hi, %s !", param);
}
}
@Test
public void testProvider() throws Exception {
ZookeeperRegister zookeeperRegister = new ZookeeperRegister("127.0.0.1:2181");
//实例化实现类
DemoService demoService = new DemoServiceImpl();
//设置服务提供者基本参数
URL url = new URL("thor", InetAddress.getLocalHost().getHostAddress(), 5605, new HashMap<>());
Provider provider = new Provider(DemoService.class.getName(),"v1.0.0", DemoService.class
, demoService, url);
//注册服务
zookeeperRegister.register(provider);
//实例化JDK动态代理类
JDKDynamicProxyHandler requestHandler = new JDKDynamicProxyHandler(zookeeperRegister, new HessianSerializer());
//实例化并启动服务
NettyRpcServer rpcServer = new NettyRpcServer(InetAddress.getLocalHost().getHostAddress() + ":" + url.getPort(),
requestHandler);
rpcServer.start();
System.in.read();
rpcServer.stop();
}
@Test
public void testConsumer() throws Exception {
//负载均衡
LoadBalance loadBalance = new RandomLoadBalance();
//初始化代理工厂
ClientProxyFactory clientProxyFactory = new ClientProxyFactory(
new NettyRpcClient(new HessianSerializer())
, new ZookeeperServiceDiscovery("127.0.0.1:2181",loadBalance));
DemoService demoService = (DemoService) clientProxyFactory.getProxy(DemoService.class);
String hello = demoService.sayHello("world");
System.out.println(hello);
}
-
Thor-RPC 1.0.0 Build 2020/6/16
首版构建,基于
netty
网络框架,提供zk
和redis
两种注册中心,提供java
和hessian
序列化方式 -
Thor-RPC 1.0.0 Build 2020/6/30
Client端新增Netty连接池方式;针对多个Provider,优化Client端调用;加入Echo回声探测
- Author: [email protected]