Skip to content

Commit 8012c8c

Browse files
author
quding
committed
增加feature存根,该存根为同步非阻塞
1 parent 43ece38 commit 8012c8c

File tree

4 files changed

+16
-15
lines changed

4 files changed

+16
-15
lines changed

gRPC-Demo/src/main/java/cn/mrdear/grpc/clientProcess/forwardExample/InterceptTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* @date 2017/2/4
77
*/
88
public class InterceptTest {
9+
910
public static void main(String[] args) {
1011
Client client = new ClientImp();//主要想执行的方法
1112
//构造第一个拦截器

gRPC-Demo/src/main/java/cn/mrdear/grpc/hello/HelloWorldClient.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,25 @@ public class HelloWorldClient {
1717

1818
private final ManagedChannel channel; //一个gRPC信道
1919
private final GreeterGrpc.GreeterBlockingStub blockingStub;//阻塞/同步 存根
20+
private GreeterGrpc.GreeterFutureStub futureStub;//非阻塞/同步 存根
21+
private GreeterGrpc.GreeterStub asyncStub;//非阻塞/异步 存根
2022

2123
//初始化信道和存根
2224
public HelloWorldClient(String host,int port){
2325
this(ManagedChannelBuilder.forAddress(host, port)
24-
// Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
25-
// needing certificates.
2626
.usePlaintext(true));
2727
}
2828

29-
/** Construct client for accessing RouteGuide server using the existing channel. */
3029
private HelloWorldClient(ManagedChannelBuilder<?> channelBuilder) {
30+
//信道应该是长连接,存根则每次调用创建一个
3131
channel = channelBuilder.build();
3232
//使用gzip压缩
3333
blockingStub = GreeterGrpc.newBlockingStub(channel).withCompression("gzip");
34+
// futureStub = GreeterGrpc.newFutureStub(channel).withCompression("gzip");
35+
// asyncStub = GreeterGrpc.newStub(channel).withCompression("gzip");
3436
}
3537

38+
3639
public void shutdown() throws InterruptedException {
3740
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
3841
}

gRPC-Demo/src/main/java/cn/mrdear/grpc/hello/HelloWorldServer.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,22 @@ public class HelloWorldServer {
2020

2121
/**
2222
* 启动服务
23-
* @throws IOException
2423
*/
2524
private void start() throws IOException {
25+
26+
System.out.println("service start...");
2627
server = ServerBuilder.forPort(port)
2728
.addService(new GreeterImpl())
2829
.build()
2930
.start();
30-
31-
System.out.println("service start...");
31+
System.out.println("service started");
3232

3333
//程序正常退出,系统调用 System.exit方法或虚拟机被关闭时执行该调用
34-
Runtime.getRuntime().addShutdownHook(new Thread() {
35-
36-
@Override
37-
public void run() {
38-
System.err.println("*** shutting down gRPC server since JVM is shutting down");
39-
HelloWorldServer.this.stop();
40-
System.err.println("*** server shut down");
41-
}
42-
});
34+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
35+
System.err.println("*** shutting down gRPC server since JVM is shutting down");
36+
HelloWorldServer.this.stop();
37+
System.err.println("*** server shut down");
38+
}));
4339
}
4440

4541
private void stop() {

gRPC-Demo/src/main/java/cn/mrdear/grpc/route/RouteGuideClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* @date 2017/2/2
2424
*/
2525
public class RouteGuideClient {
26+
2627
private final ManagedChannel channel;//grpc信道,需要指定端口和地址
2728
private final RouteGuideGrpc.RouteGuideBlockingStub blockingStub;//阻塞/同步存根
2829
private final RouteGuideGrpc.RouteGuideStub asyncStub;//非阻塞,异步存根

0 commit comments

Comments
 (0)