A remote process call (RPC) framework based on Netty and Zookeeper.
- Netty for networking.
- Zookeeper as service registration center.
- Spring Framework as Ioc context provider.
- FastJson as default serializer.
- Service registration.
- Service auto discovery / update through Zookeeper.
- Call methods through interfaces with transparency as if the whole progress happens at local environment. Implemented by introducing proxy objects which do the actual Rpc call for us.
- Exception is thrown at local instead of remote side.
- Complex parameter type transimission is supported.
FastJson
is introduced as the default serializer. Serializer can be customized usingSerializer
interface.- Auto-routing to remote server with
LoadBalancer
introduced. They can be customized as well. - Heart-beat keep alive between Rpc server and Rpc client.
There's already a show case provided within the project.
- Boot up Zookeeper server on port 12181.
- Open
com.galaxyzeta.test.ServerAppContext.java
and boot it. - Open
com.galaxyzeta.test.ClientAppContext.java
and boot it.
What you will see:
RpcServer
begins to push services to Zookeeper.RpcClient
does service fetching from Zookeeper.- Demo RPC calls was made between
RpcServer
andRpcClient
. RpcClient
is shutdown becauseclose()
is invoked.
If you are going to write your own test cases, please read the following stuff:
- Create interfaces for Rpc calls.
public interface MyService {
String echo(String data);
}
-
Add service implementations.
The
@RpcServiceComponent
indicates a service to be called in Rpc. It will be registered into Spring Ioc container.
@RpcServiceComponent(interfaceClass = MyService.class, version = "1.0")
public class MyServiceImpl implements MyService {
public String echo(String data) {
return data;
}
}
-
Before booting the server, make sure Zookeeper is installed and running.
-
Configure the server through
rpc.properties
.
zookeeper.address = localhost
zookeeper.port = 12181
zookeeper.sessionTimeout = 5000
zookeeper.connectionTimeout = 5000
server.address = localhost
server.port = 18080
-
A spring xml configuration is needed. Just use the one I provided in
com/galaxyzeta/resources
. -
Boot up the server. You can start up a server using application context like this:
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-server.xml");
RpcServer server = (RpcServer)context.getBean("rpcServer");
server.start();
- Prepare a class that contains all the services to be used in Rpc call. In this example, the class is named
Foo
.
public class Foo {
@RpcAutowired(version = "1.0")
private HelloService helloService;
public HelloService getHelloService() {
return helloService;
}
}
@RpcAutowired
is a bit similar to @Autowired
in Spring framework, which automatically injects an instance into the given field when the application is started.
- Boot up the client like this:
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-client.xml");
- Do remote progress call just like calling a normal local method.
Foo foo = (Foo) context.getBean("foo");
foo.getHelloService().hello();