FlexRPC is an annotation-driven, high-performance RPC framework built on top of Springboot. It uses Zookeeper as a registry center and Netty as the underlying transport layer, providing both NIO and BIO options for sending RPC requests.
To use FlexRPC, you first need to add the framework as a dependency in your project. Then, you can define an interface for the remote service. For provider side:
<dependency>
<groupId>com.cliffe.flex</groupId>
<artifactId>flex-server</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
For client side:
<dependency>
<groupId>com.cliffe.flex</groupId>
<artifactId>flex-client</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
Provider side:
flex:
server:
zk:
# zookeeper registry root
root: /rpc
# Zookeeper server address
addr: localhost:2181
# connection timeout
connect-timeout: 10000
# rpc port (Netty server port)
rpc-port: 28889
Client side:
flex:
server:
zk:
# zookeeper registry root
root: /rpc
# Zookeeper server address
addr: localhost:2181
# connection timeout
connect-timeout: 10000
# rpc port (Netty server port)
rpc-port: 28889
Then on the provider side, add the @RpcService
annotation to the services implementations you want to provide.
This will register the service in the Zookeeper registry, making it available for clients to access.
public interface HelloService {
String sayHello(String name);
}
@FlexService(interfaceClass = OrderService.class)
public class HelloService {
String sayHello(String name){
return "hello, "+name;
}
}
On the client side, you can use the @FlexRpc
annotation to inject a proxy for the remote service.
@FlexRpc
private HelloService helloService;
You can now use the helloService object to invoke the remote sayHello() method, just like a local method.
String result = helloService.sayHello("John");
FlexRPC also supports for nio type of rpc request. To enable this, configure on your client side as the folloing example:
@FlexService
public interface HelloService {
@NIO(nioHandler = HelloNioHandler.class)
String sayHello(String name);
}
You will need a NioHandler by implementing com.cliffe.flex.client.io.NIOHandler
for handling the callback once client receives the response. The object parameter will be the response object.
public class HelloNioHandler implements NIOHandler {
@Override
public void handleResponse(Object o) {
// your callback logic
}
}
You can customize Serialization type by extendscom.cliffe.flex.core.serialize.Serialization
class and implements deserialize
and serialize
methods.
For example,
@Component
public class MySerialization extends Serialization{
public static final int serializationType = 3;
public MySerialization() {
super(serializationType, MySerialization.class);
}
@Override
public <T> T deserialize(Class<T> clazz, byte[] bytes) {
return null;
}
@Override
public <T> byte[] serialize(T object) {
return new byte[0];
}
}
then define your class in configuration file:
flex:
client:
serialization-type: path.to.config.MySerialization
On the Client side, you can configure threadPool based on your requirement.
# this is for @NIO ThreadPool
flex:
client:
nio:
thread:
...
# This is the configuration for the threadPool that receives RPC responses on the client side
flex:
client:
thread:
...
On the server side: TODO
You can also configure the Load Balance strategy on Client side. Default is random
.
@FlexRpc(lbStrategy="hash")
private HelloService helloService;
You can also customize your own load balance strategy by extends com.cliffe.flex.client.cluster.lb.LoadBalanceSelector
@Component
public class ExampleLoadBalanceSelector implements LoadBalanceSelector{
@Override
public ServiceProvider select(List<ServiceProvider> serviceProviderList, String interfaceName) {
// your load balance strategy logic
}
@Override
public String strategy() {
return "example";
}
}
Default support is CGlib. TODO
Default support "FLEX" protocol. TODO: "HTTP" ...