msa-rpc4j是一款轻量级rpc nio通讯框架,底层通讯采用了netty nio通讯框架,rpc调用速度更快,序列化采用protostuff,序列化速度性能更优,为了保持高可用,采用zookeeper作为 服务注册中心,所以msa-rpc4j是一款具有高性能、高可用的通讯框架。
maven:
<dependency>
<groupId>com.github.microcmpt</groupId>
<artifactId>msa-rpc-server</artifactId>
<version>1.0.1</version>
</dependency>
gradle:
compile group: 'com.github.microcmpt', name: 'msa-rpc-server', version: '1.0.1'
public interface HelloRpc4jService {
/**
* Hello string.
*
* @param str the str
* @return the string
*/
String hello(String str);
}
@RpcService(value = HelloRpc4jService.class)
public class HelloRpc4jServiceImpl implements HelloRpc4jService {
/**
* Hello string.
*
* @param str the str
* @return the string
*/
public String hello(String str) {
return "Hello, " + str;
}
}
maven:
<dependency>
<groupId>com.github.microcmpt</groupId>
<artifactId>msa-rpc-client</artifactId>
<version>1.0.1</version>
</dependency>
gradle:
compile group: 'com.github.microcmpt', name: 'msa-rpc-client', version: '1.0.1'
ZkServiceDiscovery discovery = new ZkServiceDiscovery();
discovery.setZkAddress("localhost:2181");
RpcClient client = new RpcClient(discovery);
RpcClientFactory factory = new RpcClientFactory(new DefaultInvocationProxy(rpcClient));
HelloRpc4jService client = factory.newClient(HelloRpc4jService.class);
String resp = client.hello("rpc4j");
System.out.println("返回结果:" + resp);
@RpcService用于标记该服务为暴露出去的API服务,服务端启动时自动注册到注册中心