续上篇介绍

Java GRPC proto 编译

现在我们拿到了编译的 Java 文件,其中 User.java 为 rpc 通信,文件名同 .proto 文件名称;CreateAccountGrpc.java 为定义的服务名称,定义几个服务就会编译出几个 Grpc.java 文件

工程中使用

在 src 目录下新建 package grpc.user,将proto 编译得到的 java类文件都复制到目录下

build.gradle环境配置

和 proto 编译配置保持一致即可

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
apply plugin: 'java'
apply plugin: 'com.google.protobuf'
apply plugin: 'idea'
repositories {
    maven { url "https://maven.aliyun.com/repository/central/" }
}
dependencies {
    compile "io.grpc:grpc-netty:1.20.0"
    compile "io.grpc:grpc-protobuf:1.20.0"
    compile "io.grpc:grpc-stub:1.20.0"
}
buildscript {
    repositories {
        maven { url "https://maven.aliyun.com/repository/central/" }
    }
    dependencies {
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.8'
    }
}
protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.7.1'
    }
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.20.0'
        }
    }
    generateProtoTasks {
        ofSourceSet('main')*.plugins {
            grpc { }
        }
    }
}
客户端

在目录下新建 UserClient.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package grpc.user;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.util.concurrent.TimeUnit;
public class UserClient {
    private final ManagedChannel channel;
    private final CreateAccountGrpc.CreateAccountBlockingStub blockingStub;
    private final CreateAccountGrpc.CreateAccountStub Stub;
    public static void main(String[] args) {
        try {
            UserClient service = new UserClient("localhost", 1330);
            System.out.println(service.creatAccount("beihai"));
            service.shutdown();
        } catch (Exception e) {
            System.out.println("出现错误:"+e);
        }
    }
    public UserClient(String host, int port) {
        this(ManagedChannelBuilder.forAddress(host, port).usePlaintext());
    }
    /** Construct client for accessing RouteGuide server using the existing channel. */
    public UserClient(ManagedChannelBuilder<?> channelBuilder) {
        channel = channelBuilder.build();
        blockingStub = CreateAccountGrpc.newBlockingStub(channel);
        Stub = CreateAccountGrpc.newStub(channel);
    }
    public void shutdown() throws InterruptedException{
        channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
    }
    public String creatAccount(String uid){
        User.CreateAccountRequest request = User.CreateAccountRequest.newBuilder().setUid(uid).setService("Register").build();
        User.CreateRequestResponse response = blockingStub.createAccount(request);
        return response.getValue();
    }
}
服务端

在目录下新建 UserServer.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package grpc.user;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
import java.io.IOException;
import org.bcosliteclient.demoDataTransaction;
public class UserServer {
    private Server server;
    public void start() throws IOException {
        /* The port on which the server should run */
        int port = 1330;
        server = ServerBuilder.forPort(port)
                .addService(new UserServer.CreateAccountImpl())
                .build()
                .start();
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            // Use stderr here since the logger may have been reset by its JVM shutdown hook.
            System.err.println("*** shutting down gRPC server since JVM is shutting down");
            UserServer.this.stop();
            System.err.println("*** server shut down");
        }));
    }
    private void stop() {
        if (server != null) {
            server.shutdown();
        }
    }
    /**
     * Await termination on the main thread since the grpc library uses daemon threads.
     */
    public void blockUntilShutdown() throws InterruptedException {
        if (server != null) {
            server.awaitTermination();
        }
    }
    /**
     * Main launches the server from the command line.
     */
    public static void main(String[] args) throws IOException, InterruptedException {
        final UserServer server = new UserServer();
        server.start();
        server.blockUntilShutdown();
    }
    static class CreateAccountImpl extends CreateAccountGrpc.CreateAccountImplBase {
        @Override
        public void createAccount(User.CreateAccountRequest req, StreamObserver<User.CreateRequestResponse> responseObserver) {
            //System.out.println(req.getUid());
            String value ;
            if (req.getService().equals("Register")) {
                try {
                    value = "Register";
                } catch (Exception e) {
                    value = "error";
                    e.printStackTrace();
                }
            } else if (req.getService().equals("TokenQuery")) {
                try {
                    value = "TokenQuery";
                } catch (Exception e) {
                    value = "error";
                    e.printStackTrace();
                }
            } else {
                value = "error";
            }
            User.CreateRequestResponse response = User.CreateRequestResponse.newBuilder().setValue(value).build();
            responseObserver.onNext(response);
            responseObserver.onCompleted();
        }
    }
}

先运行 server 端监听端口,再运行客户端发送信息,可在控制台看到输出。