基于 jProtobuf 以及 Protobuf RPC协议 实现的 简单响应式RPC框架
参考自 baidu/Jprotobuf-rpc-socket
使用详见
- reactor-registry-spring-boot-starter
RPC框架注册中心支持 - reactor-rpc4j-spring-boot-starter
RPC框架
RPC
<dependency>
<groupId>com.github.759434091</groupId>
<artifactId>reactor-rpc4j-spring-boot-starter</artifactId>
<version>1.0.1.RELEASE</version>
</dependency>
@Data
@Builder
@ProtobufClass
@NoArgsConstructor
@AllArgsConstructor
public class TestRequest {
private int id;
}
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ProtobufClass
public class TestResponse {
private int id;
private String value;
}
import reactor.core.publisher.Mono;
/**
* 接口参数支持
* {@link Mono} / {@link Object}
* 两种格式
*/
@ProtobufService("testService")
public interface TestService {
@ProtobufMethod
Mono<TestResponse> testMethod(Mono<TestRequest> testRequestMono);
@ProtobufMethod
Mono<TestResponse> testMethod(TestRequest testRequest);
}
这里并没有选择暴露和发现分开注解操作, 因为注册接口的时候就已经注定这是暴露还是发现.
public class TestServiceImpl implements TestService {
@Override
public Mono<TestResponse> testMethod(Mono<TestRequest> testRequestMono) {
return testRequestMono
.map(testRequest -> TestResponse.builder()
.id(testRequest.getId())
.value("hello")
.build())
.log();
}
}
或者@Component\@Service
@Configuration
public class ServerConfiguration {
@Bean
TestServiceImpl testService() {
return new TestServiceImpl();
}
}
application.properties
reactor-rpc4j.server.enabled=true
reactor-rpc4j.server.port=8080
怎么简单怎么来..
TestService testService = RpcServiceClientRegister.register(
TestService.class,
"127.0.0.1",
8000
);
服务端启动服务.
使用CountDownLatch
避免提前退出.
CountDownLatch countDownLatch = new CountDownLatch(1);
TestRequest testRequest = TestRequest.builder()
.id(1)
.build();
testService.testMethod(Mono.just(testRequest))
.subscribe(testResponse -> {
log.info(JSON.toJSONString(testResponse));
countDownLatch.countDown();
});
countDownLatch.await();
使用 etcd 作为注册中心
<dependency>
<groupId>com.github.759434091</groupId>
<artifactId>reactor-registry-spring-boot-starter</artifactId>
<version>1.0.1.RELEASE</version>
</dependency>
开启配置
reactor-rpc4j.server.register.enabled=true
reactor-rpc4j.server.register.projectName=projectName
reactor-rpc4j.server.register.host=host
#如 http://127.0.0.1:8081
reactor-rpc4j.server.register.port=port
# 对外port
reactor-rpc4j.server.register.endpoints=
# 如 http://127.0.0.1:8081
@see RpcServiceClientRegister#etcdRegister
贡献patch流程、质量要求