Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,29 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 通过 maven 插件把.proto文件生成 Java 类 -->
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<!--默认值-->
<outputDirectory>${project.basedir}/src/main/java</outputDirectory>
<!--设置是否在生成java文件之前清空outputDirectory的文件,默认值为true,设置为false时也会覆盖同名文件-->
<clearOutputDirectory>false</clearOutputDirectory>
<protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
68 changes: 68 additions & 0 deletions src/main/java/io/grpc/examples/client/GrpcClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package io.grpc.examples.client;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.examples.user.*;
import io.grpc.stub.StreamObserver;

import java.util.Iterator;
import java.util.concurrent.TimeUnit;

/**
* @Author: zjfan
* @Description:
* @Date: Created in 21:23 2021/2/5
*/
public class GrpcClient {
public static void main(String[] args) throws InterruptedException {
ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost",8888)
.usePlaintext().build();
UserServiceGrpc.UserServiceBlockingStub blockingStub = UserServiceGrpc.newBlockingStub(managedChannel);

// 获取单个用户
GetUserResponse response = blockingStub.getUser(GetUserRequest.newBuilder().setId(3).build());
System.out.println(response.getUser().getName());

// 获取用户列表
Iterator<User> allUser = blockingStub.getUserList(GetUserListRequest.newBuilder().setPage(1).setSize(5).build());
while (allUser.hasNext()){
User user = allUser.next();
System.out.println("name: " + user.getName());
}

// 间隔一定时间获取用户
UserServiceGrpc.UserServiceStub stub = UserServiceGrpc.newStub(managedChannel);

StreamObserver<GetUserRequest> requestStreamObserver = stub.getUserOneByOne(new StreamObserver<User>() {
@Override
public void onNext(User value) {
// 模拟间断请求
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("name: " + value.getName());
}

@Override
public void onError(Throwable t) {

}

@Override
public void onCompleted() {
System.out.println("Completed!");
}
});

for(int i=5; i<=10; i++) {
requestStreamObserver.onNext(GetUserRequest.newBuilder().setId(i).build());
}

requestStreamObserver.onCompleted();

TimeUnit.SECONDS.sleep(30);

}
}
72 changes: 36 additions & 36 deletions src/main/java/io/grpc/examples/helloworld/GreeterGrpc.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,29 @@ private GreeterGrpc() {}
public static final String SERVICE_NAME = "helloworld.Greeter";

// Static method descriptors that strictly reflect the proto.
private static volatile io.grpc.MethodDescriptor<HelloRequest,
HelloReply> getSayHelloMethod;
private static volatile io.grpc.MethodDescriptor<io.grpc.examples.helloworld.HelloRequest,
io.grpc.examples.helloworld.HelloReply> getSayHelloMethod;

@io.grpc.stub.annotations.RpcMethod(
fullMethodName = SERVICE_NAME + '/' + "SayHello",
requestType = HelloRequest.class,
responseType = HelloReply.class,
requestType = io.grpc.examples.helloworld.HelloRequest.class,
responseType = io.grpc.examples.helloworld.HelloReply.class,
methodType = io.grpc.MethodDescriptor.MethodType.UNARY)
public static io.grpc.MethodDescriptor<HelloRequest,
HelloReply> getSayHelloMethod() {
io.grpc.MethodDescriptor<HelloRequest, HelloReply> getSayHelloMethod;
public static io.grpc.MethodDescriptor<io.grpc.examples.helloworld.HelloRequest,
io.grpc.examples.helloworld.HelloReply> getSayHelloMethod() {
io.grpc.MethodDescriptor<io.grpc.examples.helloworld.HelloRequest, io.grpc.examples.helloworld.HelloReply> getSayHelloMethod;
if ((getSayHelloMethod = GreeterGrpc.getSayHelloMethod) == null) {
synchronized (GreeterGrpc.class) {
if ((getSayHelloMethod = GreeterGrpc.getSayHelloMethod) == null) {
GreeterGrpc.getSayHelloMethod = getSayHelloMethod =
io.grpc.MethodDescriptor.<HelloRequest, HelloReply>newBuilder()
io.grpc.MethodDescriptor.<io.grpc.examples.helloworld.HelloRequest, io.grpc.examples.helloworld.HelloReply>newBuilder()
.setType(io.grpc.MethodDescriptor.MethodType.UNARY)
.setFullMethodName(generateFullMethodName(SERVICE_NAME, "SayHello"))
.setSampledToLocalTracing(true)
.setRequestMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
HelloRequest.getDefaultInstance()))
io.grpc.examples.helloworld.HelloRequest.getDefaultInstance()))
.setResponseMarshaller(io.grpc.protobuf.ProtoUtils.marshaller(
HelloReply.getDefaultInstance()))
io.grpc.examples.helloworld.HelloReply.getDefaultInstance()))
.setSchemaDescriptor(new GreeterMethodDescriptorSupplier("SayHello"))
.build();
}
Expand All @@ -54,7 +54,7 @@ HelloReply> getSayHelloMethod() {
public static GreeterStub newStub(io.grpc.Channel channel) {
io.grpc.stub.AbstractStub.StubFactory<GreeterStub> factory =
new io.grpc.stub.AbstractStub.StubFactory<GreeterStub>() {
@Override
@java.lang.Override
public GreeterStub newStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
return new GreeterStub(channel, callOptions);
}
Expand All @@ -69,7 +69,7 @@ public static GreeterBlockingStub newBlockingStub(
io.grpc.Channel channel) {
io.grpc.stub.AbstractStub.StubFactory<GreeterBlockingStub> factory =
new io.grpc.stub.AbstractStub.StubFactory<GreeterBlockingStub>() {
@Override
@java.lang.Override
public GreeterBlockingStub newStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
return new GreeterBlockingStub(channel, callOptions);
}
Expand All @@ -84,7 +84,7 @@ public static GreeterFutureStub newFutureStub(
io.grpc.Channel channel) {
io.grpc.stub.AbstractStub.StubFactory<GreeterFutureStub> factory =
new io.grpc.stub.AbstractStub.StubFactory<GreeterFutureStub>() {
@Override
@java.lang.Override
public GreeterFutureStub newStub(io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
return new GreeterFutureStub(channel, callOptions);
}
Expand All @@ -104,19 +104,19 @@ public static abstract class GreeterImplBase implements io.grpc.BindableService
* Sends a greeting
* </pre>
*/
public void sayHello(HelloRequest request,
io.grpc.stub.StreamObserver<HelloReply> responseObserver) {
public void sayHello(io.grpc.examples.helloworld.HelloRequest request,
io.grpc.stub.StreamObserver<io.grpc.examples.helloworld.HelloReply> responseObserver) {
io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall(getSayHelloMethod(), responseObserver);
}

@Override public final io.grpc.ServerServiceDefinition bindService() {
@java.lang.Override public final io.grpc.ServerServiceDefinition bindService() {
return io.grpc.ServerServiceDefinition.builder(getServiceDescriptor())
.addMethod(
getSayHelloMethod(),
io.grpc.stub.ServerCalls.asyncUnaryCall(
new MethodHandlers<
HelloRequest,
HelloReply>(
io.grpc.examples.helloworld.HelloRequest,
io.grpc.examples.helloworld.HelloReply>(
this, METHODID_SAY_HELLO)))
.build();
}
Expand All @@ -133,7 +133,7 @@ private GreeterStub(
super(channel, callOptions);
}

@Override
@java.lang.Override
protected GreeterStub build(
io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
return new GreeterStub(channel, callOptions);
Expand All @@ -144,8 +144,8 @@ protected GreeterStub build(
* Sends a greeting
* </pre>
*/
public void sayHello(HelloRequest request,
io.grpc.stub.StreamObserver<HelloReply> responseObserver) {
public void sayHello(io.grpc.examples.helloworld.HelloRequest request,
io.grpc.stub.StreamObserver<io.grpc.examples.helloworld.HelloReply> responseObserver) {
io.grpc.stub.ClientCalls.asyncUnaryCall(
getChannel().newCall(getSayHelloMethod(), getCallOptions()), request, responseObserver);
}
Expand All @@ -162,7 +162,7 @@ private GreeterBlockingStub(
super(channel, callOptions);
}

@Override
@java.lang.Override
protected GreeterBlockingStub build(
io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
return new GreeterBlockingStub(channel, callOptions);
Expand All @@ -173,7 +173,7 @@ protected GreeterBlockingStub build(
* Sends a greeting
* </pre>
*/
public HelloReply sayHello(HelloRequest request) {
public io.grpc.examples.helloworld.HelloReply sayHello(io.grpc.examples.helloworld.HelloRequest request) {
return io.grpc.stub.ClientCalls.blockingUnaryCall(
getChannel(), getSayHelloMethod(), getCallOptions(), request);
}
Expand All @@ -190,7 +190,7 @@ private GreeterFutureStub(
super(channel, callOptions);
}

@Override
@java.lang.Override
protected GreeterFutureStub build(
io.grpc.Channel channel, io.grpc.CallOptions callOptions) {
return new GreeterFutureStub(channel, callOptions);
Expand All @@ -201,8 +201,8 @@ protected GreeterFutureStub build(
* Sends a greeting
* </pre>
*/
public com.google.common.util.concurrent.ListenableFuture<HelloReply> sayHello(
HelloRequest request) {
public com.google.common.util.concurrent.ListenableFuture<io.grpc.examples.helloworld.HelloReply> sayHello(
io.grpc.examples.helloworld.HelloRequest request) {
return io.grpc.stub.ClientCalls.futureUnaryCall(
getChannel().newCall(getSayHelloMethod(), getCallOptions()), request);
}
Expand All @@ -223,21 +223,21 @@ private static final class MethodHandlers<Req, Resp> implements
this.methodId = methodId;
}

@Override
@SuppressWarnings("unchecked")
@java.lang.Override
@java.lang.SuppressWarnings("unchecked")
public void invoke(Req request, io.grpc.stub.StreamObserver<Resp> responseObserver) {
switch (methodId) {
case METHODID_SAY_HELLO:
serviceImpl.sayHello((HelloRequest) request,
(io.grpc.stub.StreamObserver<HelloReply>) responseObserver);
serviceImpl.sayHello((io.grpc.examples.helloworld.HelloRequest) request,
(io.grpc.stub.StreamObserver<io.grpc.examples.helloworld.HelloReply>) responseObserver);
break;
default:
throw new AssertionError();
}
}

@Override
@SuppressWarnings("unchecked")
@java.lang.Override
@java.lang.SuppressWarnings("unchecked")
public io.grpc.stub.StreamObserver<Req> invoke(
io.grpc.stub.StreamObserver<Resp> responseObserver) {
switch (methodId) {
Expand All @@ -251,12 +251,12 @@ private static abstract class GreeterBaseDescriptorSupplier
implements io.grpc.protobuf.ProtoFileDescriptorSupplier, io.grpc.protobuf.ProtoServiceDescriptorSupplier {
GreeterBaseDescriptorSupplier() {}

@Override
@java.lang.Override
public com.google.protobuf.Descriptors.FileDescriptor getFileDescriptor() {
return HelloWorld.getDescriptor();
return io.grpc.examples.helloworld.HelloWorld.getDescriptor();
}

@Override
@java.lang.Override
public com.google.protobuf.Descriptors.ServiceDescriptor getServiceDescriptor() {
return getFileDescriptor().findServiceByName("Greeter");
}
Expand All @@ -276,7 +276,7 @@ private static final class GreeterMethodDescriptorSupplier
this.methodName = methodName;
}

@Override
@java.lang.Override
public com.google.protobuf.Descriptors.MethodDescriptor getMethodDescriptor() {
return getServiceDescriptor().findMethodByName(methodName);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/grpc/examples/helloworld/HelloReply.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions src/main/java/io/grpc/examples/helloworld/HelloWorld.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading