跟孙哥学RPC
孙哥B站地址
1. BlockingStub 阻塞 通信方式
2. Stub 异步 通过监听处理的
3. FutureStub 同步/异步 类似NettyFuture
1. FutureStub只能应用 一元RPC
syntax="proto3";
option java_multiple_files=false;
option java_package="com.suns";
option java_outer_classname="TestProto";
message TestRequest{
string name=1;
}
message TestResponse{
string result=1;
}
service TestService{
rpc testSuns(TestRequest) returns(TestResponse){}
}
BlockingStub
/**
* 主函数入口,用于执行程序
*
* @param args 命令行参数
*/
public static void main(String[] args) {
// 创建一个 ManagedChannel 对象,用于连接到指定的地址和端口
ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost", 9000).
usePlaintext().build();
try {
// 创建一个 HelloServerBlockingStub 对象,用于与服务端进行 RPC 调用
HelloServerGrpc.HelloServerBlockingStub helloService = HelloServerGrpc.newBlockingStub(managedChannel);
// 创建一个 HelloRequest.Builder 对象,并设置请求的名称为 "suns",然后构建请求对象
HelloProto.HelloRequest.Builder builder = HelloProto.HelloRequest.newBuilder();
HelloProto.HelloRequest helloRequest = builder.setName("suns").build();
// 发送请求并获取响应
HelloProto.HelloResponse helloResponse = helloService.hello(helloRequest);
// 获取响应结果
String result = helloResponse.getResult();
// 打印结果
System.out.println("result="+result);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
// 关闭 ManagedChannel,释放资源
managedChannel.shutdown();
}
}
Stub
public static void main(String[] args) {
// 创建 ManagedChannel 对象,用于与服务端建立连接
ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost", 9000).
usePlaintext().build();
try {
// 创建 HelloServerGrpc.HelloServerBlockingStub 对象,用于与服务端进行 RPC 调用
HelloServerGrpc.HelloServerStub helloServer= HelloServerGrpc.newStub(managedChannel);
StreamObserver<HelloProto.HelloRequest> helloRequestStreamObserver = helloServer.cs2ss(new StreamObserver<HelloProto.HelloResponse>() {
@Override
public void onNext(HelloProto.HelloResponse helloResponse) {
System.out.println("接受服务端的响应数据"+helloResponse.getResult());
}
@Override
public void onError(Throwable throwable) {
}
@Override
public void onCompleted() {
System.out.println("响应全部结束");
}
});
for (int i = 0; i < 10; i++) {
HelloProto.HelloRequest.Builder builder = HelloProto.HelloRequest.newBuilder();
builder.setName("hello"+i);
HelloProto.HelloRequest helloRequest = builder.build();
TimeUnit.SECONDS.sleep(1);
helloRequestStreamObserver.onNext(helloRequest);
}
helloRequestStreamObserver.onCompleted();
managedChannel.awaitTermination(12,TimeUnit.SECONDS);
}catch (Exception e){
e.printStackTrace();
}finally {
managedChannel.shutdown();
}
}
FutureStub
public class GrpcClient7 {
/**
* 主函数入口,用于执行程序
*
* @param args 命令行参数
*/
public static void main(String[] args) {
// 创建 ManagedChannel 对象,用于与服务端建立连接
ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost", 9000).
usePlaintext().build();
try {
TestServiceGrpc.TestServiceFutureStub testServiceFutureStub = TestServiceGrpc.newFutureStub(managedChannel);
TestProto.TestRequest testRequest = TestProto.TestRequest.newBuilder().setName("blue").build();
ListenableFuture<TestProto.TestResponse> testResponseListenableFuture = testServiceFutureStub.testSuns(testRequest);
// TestProto.TestResponse testResponse = testResponseListenableFuture.get();
// System.out.println("得到的结果"+testResponse.getResult());
/* testResponseListenableFuture.addListener(()->{
System.out.println("收到消息了");
}, Executors.newCachedThreadPool());*/
Futures.addCallback(testResponseListenableFuture, new FutureCallback<TestProto.TestResponse>() {
@Override
public void onSuccess(TestProto.TestResponse testResponse) {
System.out.println("返回的结果:"+testResponse.getResult());
}
@Override
public void onFailure(Throwable throwable) {
}
}, Executors.newCachedThreadPool());
System.out.println("后续的操作");
Thread.sleep(12000);
}catch (Exception e){
e.printStackTrace();
}finally {
managedChannel.shutdown();
}
}
}