ikasoa高性能輕量級 RPC 框架
ikasoa-rpc是一款高性能輕量級的RPC框架,基于apache thrift開發(fā).
拋棄了thrift框架中的idl,客戶端可以像調(diào)用本地接口一樣調(diào)用遠程接口,減少了idl的學(xué)習(xí)成本和生成代碼的過程.
超輕量級和簡單的依賴關(guān)系,并不會強依賴某些框架(比如spring).
繼承了thrift優(yōu)秀的性能和跨語言能力,提供了負(fù)載均衡和多種序列化實現(xiàn).并補充了socket連接池,服務(wù)發(fā)現(xiàn),監(jiān)控,數(shù)據(jù)加密,等配套功能.
包含一個spring-boot-starter,使其在spring-boot上使用更方便.
”helloworld”示例
創(chuàng)建接口和實現(xiàn)
新建例子接口(ExampleService.java),對象(ExampleVO.java)和實現(xiàn) (ExampleServiceImpl.java)類:
ExampleService.java
public interface ExampleService {
// 查詢對象
public ExampleVO findVO(int id);
}
ExampleServiceImpl.java
public class ExampleServiceImpl implements ExampleService {
@Override
public ExampleVO findVO(int id) {
return new ExampleVO(id, “helloworld”);
}
}
ExampleVO.java
public class ExampleVO {
private int id;
private String string;
public ExampleVO() {
}
public ExampleVO(int id, String string) {
this.id = id;
this.string = string;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
}
服務(wù)端
Server.java
import com.ikasoa.rpc.DefaultIkasoaFactory;
import com.ikasoa.rpc.IkasoaException;
import com.ikasoa.rpc.IkasoaServer;
public class Server {
private static IkasoaServer ikasoaServer;
public static void start() {
try {
if (ikasoaServer == null) {
ikasoaServer = new DefaultIkasoaFactory().getIkasoaServer(ExampleServiceImpl.class, 9999);
}
// 啟動服務(wù)
ikasoaServer.run();
} catch (IkasoaException e) {
e.printStackTrace();
}
}
public static void stop() {
if (ikasoaServer != null && ikasoaServer.isServing()) {
// 停止服務(wù)
ikasoaServer.stop();
}
}
}
客戶端
Client.java
import com.ikasoa.rpc.DefaultIkasoaFactory;
import com.ikasoa.rpc.ServerInfoWrapper;
public class Client {
public static void call() {
// 客戶端獲取遠程接口實現(xiàn)
ExampleService es = new DefaultIkasoaFactory().getInstance(ExampleService.class, new ServerInfoWrapper("localhost", 9999));
// 客戶端輸出結(jié)果
System.out.println(es.findVO(1).getString());
}
}
執(zhí)行類
Main.java
public class Main {
public static void main(String[] args) {
try {
// 啟動服務(wù)
Server.start();
Thread.sleep(100);
// 客戶端調(diào)用
Client.call();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 停止服務(wù)
Server.stop();
}
}
}
執(zhí)行Main.java,或單獨調(diào)用Server.start()啟動服務(wù)后再調(diào)用Client.call()執(zhí)行.
如輸出“helloworld”則表示執(zhí)行成功.
評論
圖片
表情
