File tree Expand file tree Collapse file tree 4 files changed +16
-15
lines changed
gRPC-Demo/src/main/java/cn/mrdear/grpc
clientProcess/forwardExample Expand file tree Collapse file tree 4 files changed +16
-15
lines changed Original file line number Diff line number Diff line change 6
6
* @date 2017/2/4
7
7
*/
8
8
public class InterceptTest {
9
+
9
10
public static void main (String [] args ) {
10
11
Client client = new ClientImp ();//主要想执行的方法
11
12
//构造第一个拦截器
Original file line number Diff line number Diff line change @@ -17,22 +17,25 @@ public class HelloWorldClient {
17
17
18
18
private final ManagedChannel channel ; //一个gRPC信道
19
19
private final GreeterGrpc .GreeterBlockingStub blockingStub ;//阻塞/同步 存根
20
+ private GreeterGrpc .GreeterFutureStub futureStub ;//非阻塞/同步 存根
21
+ private GreeterGrpc .GreeterStub asyncStub ;//非阻塞/异步 存根
20
22
21
23
//初始化信道和存根
22
24
public HelloWorldClient (String host ,int port ){
23
25
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.
26
26
.usePlaintext (true ));
27
27
}
28
28
29
- /** Construct client for accessing RouteGuide server using the existing channel. */
30
29
private HelloWorldClient (ManagedChannelBuilder <?> channelBuilder ) {
30
+ //信道应该是长连接,存根则每次调用创建一个
31
31
channel = channelBuilder .build ();
32
32
//使用gzip压缩
33
33
blockingStub = GreeterGrpc .newBlockingStub (channel ).withCompression ("gzip" );
34
+ // futureStub = GreeterGrpc.newFutureStub(channel).withCompression("gzip");
35
+ // asyncStub = GreeterGrpc.newStub(channel).withCompression("gzip");
34
36
}
35
37
38
+
36
39
public void shutdown () throws InterruptedException {
37
40
channel .shutdown ().awaitTermination (5 , TimeUnit .SECONDS );
38
41
}
Original file line number Diff line number Diff line change @@ -20,26 +20,22 @@ public class HelloWorldServer {
20
20
21
21
/**
22
22
* 启动服务
23
- * @throws IOException
24
23
*/
25
24
private void start () throws IOException {
25
+
26
+ System .out .println ("service start..." );
26
27
server = ServerBuilder .forPort (port )
27
28
.addService (new GreeterImpl ())
28
29
.build ()
29
30
.start ();
30
-
31
- System .out .println ("service start..." );
31
+ System .out .println ("service started" );
32
32
33
33
//程序正常退出,系统调用 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
+ }));
43
39
}
44
40
45
41
private void stop () {
Original file line number Diff line number Diff line change 23
23
* @date 2017/2/2
24
24
*/
25
25
public class RouteGuideClient {
26
+
26
27
private final ManagedChannel channel ;//grpc信道,需要指定端口和地址
27
28
private final RouteGuideGrpc .RouteGuideBlockingStub blockingStub ;//阻塞/同步存根
28
29
private final RouteGuideGrpc .RouteGuideStub asyncStub ;//非阻塞,异步存根
You can’t perform that action at this time.
0 commit comments