代码拉取完成,页面将自动刷新
同步操作将从 Windoze/vertx-kotlin-rpc 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
极简的Vertx/Kotlin RPC框架。
Maven包已发布到jCenter,请先按照说明配置maven或gradle的设置。
Maven:
<dependency>
<groupId>codes.unwritten</groupId>
<artifactId>vertx-kotlin-rpc</artifactId>
<version>0.6</version>
<type>pom</type>
</dependency>
Gradle:
compile 'codes.unwritten:vertx-kotlin-rpc:0.6'
import codes.unwritten.vertx.kotlin.rpc.RpcServerVerticle
//...
// 只要方法的签名匹配,服务可以不用实现用于定义接口的interface
class HelloSvcImpl {
// 方法可以是suspend,也可以不是
fun hello(name: String): String = "Hello, $name!"
}
// ...
vertx.deployVerticle(RpcServerVerticle("test-channel")
.register("hello", HelloSvcImpl()))
import codes.unwritten.vertx.kotlin.rpc.getServiceProxy
// ...
// 服务接口定义
interface HelloSvc {
// 成员函数必须是suspend,否则在调用的时候会抛出异常
suspend fun hello(world: String): String
}
// ...
// 创建服务的Proxy对象
val svc: HelloSvc = getServiceProxy(vertx, "test-channel", "hello")
// 调用RPC服务
assertEqual("Hello, world!", svc.hello("world"))
import io.vertx.core.Vertx
import io.vertx.ext.web.Router
import io.vertx.ext.web.handler.BodyHandler
import io.vertx.kotlin.coroutines.CoroutineVerticle
import codes.unwritten.vertx.kotlin.rpc.HttpRpcHandler
// ...
class SomeVerticle: CoroutineVerticle() {
override suspend fun start() {
// ...
val router = Router.router(vertx)
// 必须打开BodyHandler
router.route().handler(BodyHandler.create())
// 只支持POST method
router.post("/some-path").handler(HttpRpcHandler().register("hello", object {
fun hello(name: String): String = "Hello, $name!"
}))
// 启动HTTP server
// ...
}
}
import codes.unwritten.vertx.kotlin.rpc.getHttpServiceProxy
interface HelloSvc {
// 成员函数必须是suspend,否则在调用的时候会抛出异常
suspend fun hello(name: String): String
}
// ...
// 用指定的URL创建服务的Proxy对象
val svc = getHttpServiceProxy<HelloSvc>(vertx, "http://127.0.0.1:8080/some-path", "hello")
// 调用HTTP RPC服务
assertEqual("Hello, world!", svc.hello("world"))
import codes.unwritten.vertx.kotlin.rpc.HttpRequest
import codes.unwritten.vertx.kotlin.rpc.JsonRpcException
import codes.unwritten.vertx.kotlin.rpc.QueryParam
import codes.unwritten.vertx.kotlin.rpc.getHttpJsonRpcServiceProxy
interface DemoSvc {
// 成员函数必须是suspend,否则在调用的时候会抛出异常
// HttpRequest annotation可以用了定制映射
// 缺省的方法是POST,缺省路径是方法名
@HttpRequest(method = HttpMethod.GET, path = "comments")
suspend fun getComments(postId: Int): List<Comment>
}
// ...
// 用指定的URL创建服务的Proxy对象
val svc = getHttpJsonRpcServiceProxy<PostmanSvc>(vertx, "https://postman-echo.com/")
// 调用JSON RPC服务
context.assertTrue(svc.getComments(1).isNotEmpty())
Query Param示例:
interface DemoSvc {
@HttpRequest(method = HttpMethod.POST, path="somepath")
// QueryParam annotation表明这个参数是query parameter而不是body的一部分,也可以指定参数名称
// foo3将会作为body的一部分被发往server
suspend fun someMethod(@QueryParam foo1: String, @QueryParam("foo2") arg2: String, foo3: String): SomeResponse
}
import codes.unwritten.vertx.kotlin.rpc.RpcServerVerticle;
// ...
// 实现类不返回Future<T>,直接返回T
public class HelloSvcImpl {
public String hello(String name) {
return "Hello, " + name + "!";
}
}
// ...
vertx.deployVerticle((new RpcServerVerticle("test-channel"))
.register("hello", new HelloSvcImpl()));
Java没有suspend函数,所以服务接口中的每个方法必须返回Future<T>
而不是T
。
import io.vertx.core.Future;
import static codes.unwritten.vertx.kotlin.rpc.ServiceProxyFactory.getAsyncServiceProxy;
// ...
// 方法必须返回Future<T>而不是T
interface AsyncHelloSvc {
Future<String> hello(String world);
}
// ...
AsyncHelloSvc svc = getAsyncServiceProxy(vertx, "test-channel", "hello", AsyncHelloSvc.class);
svc.hello("world").setHandler(ar -> {
if (ar.succeeded()) {
assertEquals("Hello, world!", ar.result());
} else {
// Error handling
}
});
(略)
Java没有suspend函数,所以服务接口中的每个方法必须返回Future<T>
而不是T
。
import io.vertx.core.Future;
import static codes.unwritten.vertx.kotlin.rpc.AsyncServiceProxyFactory.getAsyncHttpServiceProxy;
// ...
// 方法必须返回Future<T>而不是T
interface AsyncHelloSvc {
Future<String> hello(String world);
}
// ...
AsyncHelloSvc svc = getAsyncHttpServiceProxy(vertx, "http://127.0.0.1:8080/some-path", "hello", AsyncHelloSvc.class);
svc.hello("world").setHandler(ar -> {
if (ar.succeeded()) {
assertEquals("Hello, world!", ar.result());
} else {
// Error handling
}
});
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。