Merge pull request #22 from tbetbetbe/grpc_samples_update_java_helloworld_code_and_docs

Grpc samples update java helloworld code and docs
pull/3109/head
Tim Emiola 10 years ago
commit 381faf3dec
  1. 61
      README.md
  2. 4
      java/run_greeter_client.sh
  3. 4
      java/run_greeter_server.sh
  4. 14
      java/src/main/java/ex/grpc/GreeterClient.java
  5. 126
      java/src/main/java/ex/grpc/GreeterGrpc.java
  6. 4
      java/src/main/java/ex/grpc/GreeterImpl.java
  7. 8
      java/src/main/java/ex/grpc/GreeterServer.java
  8. 12
      java/src/main/java/ex/grpc/Helloworld.java

@ -247,26 +247,26 @@ we need to create our example:
- [`Helloworld.java`](java/src/main/java/ex/grpc/Helloworld.java), which
has all the protocol buffer code to populate, serialize, and retrieve our
`HelloRequest` and `HelloReply` message types
- [`GreetingsGrpc.java`](java/src/main/java/ex/grpc/GreetingsGrpc.java),
- [`GreeterGrpc.java`](java/src/main/java/ex/grpc/GreeterGrpc.java),
which contains (along with some other useful code):
- an interface for `Greetings` servers to implement
- an interface for `Greeter` servers to implement
```java
public static interface Greetings {
public static interface Greeter {
public void hello(ex.grpc.Helloworld.HelloRequest request,
public void sayHello(ex.grpc.Helloworld.HelloRequest request,
com.google.net.stubby.stub.StreamObserver<ex.grpc.Helloworld.HelloReply>
responseObserver);
}
```
- _stub_ classes that clients can use to talk to a `Greetings` server. As you can see, they also implement the `Greetings` interface.
- _stub_ classes that clients can use to talk to a `Greeter` server. As you can see, they also implement the `Greeter` interface.
```java
public static class GreetingsStub extends
com.google.net.stubby.stub.AbstractStub<GreetingsStub,
GreetingsServiceDescriptor>
implements Greetings {
public static class GreeterStub extends
com.google.net.stubby.stub.AbstractStub<GreeterStub,
GreeterServiceDescriptor>
implements Greeter {
...
}
```
@ -282,18 +282,18 @@ tutorial for your chosen language (coming soon).
Our server application has two classes:
- a simple service implementation
[GreetingsImpl.java](java/src/main/java/ex/grpc/GreetingsImpl.java).
[GreeterImpl.java](java/src/main/java/ex/grpc/GreeterImpl.java).
- a server that hosts the service implementation and allows access over the
network: [GreetingsServer.java](java/src/main/java/ex/grpc/GreetingsServer.java).
network: [GreeterServer.java](java/src/main/java/ex/grpc/GreeterServer.java).
#### Service implementation
[GreetingsImpl.java](java/src/main/java/ex/grpc/GreetingsImpl.java)
[GreeterImpl.java](java/src/main/java/ex/grpc/GreeterImpl.java)
actually implements our GreetingService's required behaviour.
As you can see, the class `GreetingsImpl` implements the interface
`GreetingsGrpc.Greetings` that we [generated](#generating) from our proto
As you can see, the class `GreeterImpl` implements the interface
`GreeterGrpc.Greeter` that we [generated](#generating) from our proto
[IDL](java/src/main/proto/helloworld.proto) by implementing the method `hello`:
```java
@ -325,7 +325,7 @@ finished dealing with this RPC.
#### Server implementation
[GreetingsServer.java](java/src/main/java/ex/grpc/GreetingsServer.java)
[GreeterServer.java](java/src/main/java/ex/grpc/GreeterServer.java)
shows the other main feature required to provide a gRPC service; making the service
implementation available from the network.
@ -334,7 +334,7 @@ implementation available from the network.
...
private void start() throws Exception {
server = NettyServerBuilder.forPort(port)
.addService(GreetingsGrpc.bindService(new GreetingsImpl()))
.addService(GreeterGrpc.bindService(new GreeterImpl()))
.build();
server.startAsync();
server.awaitRunning(5, TimeUnit.SECONDS);
@ -342,15 +342,15 @@ implementation available from the network.
```
The `GreetingsServer` class has a `ServerImpl` member that actually runs the
The `GreeterServer` class has a `ServerImpl` member that actually runs the
server. To create an appropriate `ServerImpl`, we use a special `ServerBuilder`
class (in this case a `NettyServerBuilder`) in the `GreetingsServer`'s `start`
method, binding the `GreetingsService` implementation that we created to a
class (in this case a `NettyServerBuilder`) in the `GreeterServer`'s `start`
method, binding the `GreeterService` implementation that we created to a
port. Then we start the server running: the server is now ready to receive
requests from `Greetings` service clients on our specified port. We'll cover
requests from `Greeter` service clients on our specified port. We'll cover
how all this works in a bit more detail in our language-specific documentation.
`GreetingsServer` also has a `stop` method that takes care of shutting down
`GreeterServer` also has a `stop` method that takes care of shutting down
the service and cleaning up when the program exits.
#### Build it
@ -367,9 +367,9 @@ We'll look at using a client to access the server in the next section.
### Writing a client
Client-side gRPC is pretty simple. In this step, we'll use the generated code
to write a simple client that can access the `Greetings` server we created
to write a simple client that can access the `Greeter` server we created
in the [previous section](#server). You can see the complete client code in
[GreetingsClient.java](java/src/main/java/ex/grpc/GreetingsClient.java).
[GreeterClient.java](java/src/main/java/ex/grpc/GreeterClient.java).
Again, we're not going to go into much detail about how to implement a client
- we'll leave that for the tutorial.
@ -386,13 +386,13 @@ service. The channel in turn is used to construct the stub instance.
```java
private final ChannelImpl channel;
private final GreetingGrpc.GreetingBlockingStub blockingStub;
private final GreeterGrpc.GreeterBlockingStub blockingStub;
public HelloClient(String host, int port) {
channel = NettyChannelBuilder.forAddress(host, port)
.negotiationType(NegotiationType.PLAINTEXT)
.build();
blockingStub = GreetingGrpc.newBlockingStub(channel);
blockingStub = GreeterGrpc.newBlockingStub(channel);
}
```
@ -419,7 +419,7 @@ from which we can get our greeting.
try {
Helloworld.HelloRequest request =
Helloworld.HelloRequest.newBuilder().setName(name).build();
Helloworld.HelloReply reply = blockingStub.hello(request);
Helloworld.HelloReply reply = blockingStub.sayHello(request);
logger.info("Greeting: " + reply.getMessage());
} catch (RuntimeException e) {
logger.log(Level.WARNING, "RPC failed", e);
@ -462,20 +462,15 @@ We've added simple shell scripts to simplifying running the examples. Now
that they are built, you can run the server with:
```sh
$ ./run_greetings_server.sh
$ ./run_greeter_server.sh
```
and in another terminal window confirm that it receives a message.
```sh
$ ./run_greetings_client.sh
$ ./run_greeter_client.sh
```
### Adding another client
###TODO: Section on Go client for same server

@ -1,6 +1,6 @@
#!/bin/bash -e
TARGET='Greetings Client'
TARGET_CLASS='ex.grpc.GreetingsClient'
TARGET='Greeter Client'
TARGET_CLASS='ex.grpc.GreeterClient'
TARGET_ARGS="$@"
cd "$(dirname "$0")"

@ -1,6 +1,6 @@
#!/bin/bash -e
TARGET='Greetings Server'
TARGET_CLASS='ex.grpc.GreetingsServer'
TARGET='Greeter Server'
TARGET_CLASS='ex.grpc.GreeterServer'
cd "$(dirname "$0")"
mvn -q -nsu -am package -Dcheckstyle.skip=true -DskipTests

@ -9,17 +9,17 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.concurrent.TimeUnit;
public class GreetingsClient {
public class GreeterClient {
private final Logger logger = Logger.getLogger(
GreetingsClient.class.getName());
GreeterClient.class.getName());
private final ChannelImpl channel;
private final GreetingsGrpc.GreetingsBlockingStub blockingStub;
private final GreeterGrpc.GreeterBlockingStub blockingStub;
public GreetingsClient(String host, int port) {
public GreeterClient(String host, int port) {
channel = NettyChannelBuilder.forAddress(host, port)
.negotiationType(NegotiationType.PLAINTEXT)
.build();
blockingStub = GreetingsGrpc.newBlockingStub(channel);
blockingStub = GreeterGrpc.newBlockingStub(channel);
}
public void shutdown() throws InterruptedException {
@ -31,7 +31,7 @@ public class GreetingsClient {
logger.fine("Will try to greet " + name + " ...");
Helloworld.HelloRequest req =
Helloworld.HelloRequest.newBuilder().setName(name).build();
Helloworld.HelloReply reply = blockingStub.hello(req);
Helloworld.HelloReply reply = blockingStub.sayHello(req);
logger.info("Greeting: " + reply.getMessage());
} catch (RuntimeException e) {
logger.log(Level.WARNING, "RPC failed", e);
@ -40,7 +40,7 @@ public class GreetingsClient {
}
public static void main(String[] args) throws Exception {
GreetingsClient client = new GreetingsClient("localhost", 50051);
GreeterClient client = new GreeterClient("localhost", 50051);
try {
/* Access a service running on the local machine on port 50051 */
String user = "world";

@ -13,150 +13,150 @@ import static com.google.net.stubby.stub.ServerCalls.asyncUnaryRequestCall;
import static com.google.net.stubby.stub.ServerCalls.asyncStreamingRequestCall;
@javax.annotation.Generated("by gRPC proto compiler")
public class GreetingsGrpc {
public class GreeterGrpc {
private static final com.google.net.stubby.stub.Method<ex.grpc.Helloworld.HelloRequest,
ex.grpc.Helloworld.HelloReply> METHOD_HELLO =
ex.grpc.Helloworld.HelloReply> METHOD_SAY_HELLO =
com.google.net.stubby.stub.Method.create(
com.google.net.stubby.MethodType.UNARY, "hello",
com.google.net.stubby.MethodType.UNARY, "sayHello",
com.google.net.stubby.proto.ProtoUtils.marshaller(ex.grpc.Helloworld.HelloRequest.PARSER),
com.google.net.stubby.proto.ProtoUtils.marshaller(ex.grpc.Helloworld.HelloReply.PARSER));
public static GreetingsStub newStub(com.google.net.stubby.Channel channel) {
return new GreetingsStub(channel, CONFIG);
public static GreeterStub newStub(com.google.net.stubby.Channel channel) {
return new GreeterStub(channel, CONFIG);
}
public static GreetingsBlockingStub newBlockingStub(
public static GreeterBlockingStub newBlockingStub(
com.google.net.stubby.Channel channel) {
return new GreetingsBlockingStub(channel, CONFIG);
return new GreeterBlockingStub(channel, CONFIG);
}
public static GreetingsFutureStub newFutureStub(
public static GreeterFutureStub newFutureStub(
com.google.net.stubby.Channel channel) {
return new GreetingsFutureStub(channel, CONFIG);
return new GreeterFutureStub(channel, CONFIG);
}
public static final GreetingsServiceDescriptor CONFIG =
new GreetingsServiceDescriptor();
public static final GreeterServiceDescriptor CONFIG =
new GreeterServiceDescriptor();
@javax.annotation.concurrent.Immutable
public static class GreetingsServiceDescriptor extends
com.google.net.stubby.stub.AbstractServiceDescriptor<GreetingsServiceDescriptor> {
public static class GreeterServiceDescriptor extends
com.google.net.stubby.stub.AbstractServiceDescriptor<GreeterServiceDescriptor> {
public final com.google.net.stubby.MethodDescriptor<ex.grpc.Helloworld.HelloRequest,
ex.grpc.Helloworld.HelloReply> hello;
ex.grpc.Helloworld.HelloReply> sayHello;
private GreetingsServiceDescriptor() {
hello = createMethodDescriptor(
"helloworld.Greetings", METHOD_HELLO);
private GreeterServiceDescriptor() {
sayHello = createMethodDescriptor(
"helloworld.Greeter", METHOD_SAY_HELLO);
}
private GreetingsServiceDescriptor(
private GreeterServiceDescriptor(
java.util.Map<java.lang.String, com.google.net.stubby.MethodDescriptor<?, ?>> methodMap) {
hello = (com.google.net.stubby.MethodDescriptor<ex.grpc.Helloworld.HelloRequest,
sayHello = (com.google.net.stubby.MethodDescriptor<ex.grpc.Helloworld.HelloRequest,
ex.grpc.Helloworld.HelloReply>) methodMap.get(
CONFIG.hello.getName());
CONFIG.sayHello.getName());
}
@java.lang.Override
protected GreetingsServiceDescriptor build(
protected GreeterServiceDescriptor build(
java.util.Map<java.lang.String, com.google.net.stubby.MethodDescriptor<?, ?>> methodMap) {
return new GreetingsServiceDescriptor(methodMap);
return new GreeterServiceDescriptor(methodMap);
}
@java.lang.Override
public com.google.common.collect.ImmutableList<com.google.net.stubby.MethodDescriptor<?, ?>> methods() {
return com.google.common.collect.ImmutableList.<com.google.net.stubby.MethodDescriptor<?, ?>>of(
hello);
sayHello);
}
}
public static interface Greetings {
public static interface Greeter {
public void hello(ex.grpc.Helloworld.HelloRequest request,
public void sayHello(ex.grpc.Helloworld.HelloRequest request,
com.google.net.stubby.stub.StreamObserver<ex.grpc.Helloworld.HelloReply> responseObserver);
}
public static interface GreetingsBlockingClient {
public static interface GreeterBlockingClient {
public ex.grpc.Helloworld.HelloReply hello(ex.grpc.Helloworld.HelloRequest request);
public ex.grpc.Helloworld.HelloReply sayHello(ex.grpc.Helloworld.HelloRequest request);
}
public static interface GreetingsFutureClient {
public static interface GreeterFutureClient {
public com.google.common.util.concurrent.ListenableFuture<ex.grpc.Helloworld.HelloReply> hello(
public com.google.common.util.concurrent.ListenableFuture<ex.grpc.Helloworld.HelloReply> sayHello(
ex.grpc.Helloworld.HelloRequest request);
}
public static class GreetingsStub extends
com.google.net.stubby.stub.AbstractStub<GreetingsStub, GreetingsServiceDescriptor>
implements Greetings {
private GreetingsStub(com.google.net.stubby.Channel channel,
GreetingsServiceDescriptor config) {
public static class GreeterStub extends
com.google.net.stubby.stub.AbstractStub<GreeterStub, GreeterServiceDescriptor>
implements Greeter {
private GreeterStub(com.google.net.stubby.Channel channel,
GreeterServiceDescriptor config) {
super(channel, config);
}
@java.lang.Override
protected GreetingsStub build(com.google.net.stubby.Channel channel,
GreetingsServiceDescriptor config) {
return new GreetingsStub(channel, config);
protected GreeterStub build(com.google.net.stubby.Channel channel,
GreeterServiceDescriptor config) {
return new GreeterStub(channel, config);
}
@java.lang.Override
public void hello(ex.grpc.Helloworld.HelloRequest request,
public void sayHello(ex.grpc.Helloworld.HelloRequest request,
com.google.net.stubby.stub.StreamObserver<ex.grpc.Helloworld.HelloReply> responseObserver) {
asyncUnaryCall(
channel.newCall(config.hello), request, responseObserver);
channel.newCall(config.sayHello), request, responseObserver);
}
}
public static class GreetingsBlockingStub extends
com.google.net.stubby.stub.AbstractStub<GreetingsBlockingStub, GreetingsServiceDescriptor>
implements GreetingsBlockingClient {
private GreetingsBlockingStub(com.google.net.stubby.Channel channel,
GreetingsServiceDescriptor config) {
public static class GreeterBlockingStub extends
com.google.net.stubby.stub.AbstractStub<GreeterBlockingStub, GreeterServiceDescriptor>
implements GreeterBlockingClient {
private GreeterBlockingStub(com.google.net.stubby.Channel channel,
GreeterServiceDescriptor config) {
super(channel, config);
}
@java.lang.Override
protected GreetingsBlockingStub build(com.google.net.stubby.Channel channel,
GreetingsServiceDescriptor config) {
return new GreetingsBlockingStub(channel, config);
protected GreeterBlockingStub build(com.google.net.stubby.Channel channel,
GreeterServiceDescriptor config) {
return new GreeterBlockingStub(channel, config);
}
@java.lang.Override
public ex.grpc.Helloworld.HelloReply hello(ex.grpc.Helloworld.HelloRequest request) {
public ex.grpc.Helloworld.HelloReply sayHello(ex.grpc.Helloworld.HelloRequest request) {
return blockingUnaryCall(
channel.newCall(config.hello), request);
channel.newCall(config.sayHello), request);
}
}
public static class GreetingsFutureStub extends
com.google.net.stubby.stub.AbstractStub<GreetingsFutureStub, GreetingsServiceDescriptor>
implements GreetingsFutureClient {
private GreetingsFutureStub(com.google.net.stubby.Channel channel,
GreetingsServiceDescriptor config) {
public static class GreeterFutureStub extends
com.google.net.stubby.stub.AbstractStub<GreeterFutureStub, GreeterServiceDescriptor>
implements GreeterFutureClient {
private GreeterFutureStub(com.google.net.stubby.Channel channel,
GreeterServiceDescriptor config) {
super(channel, config);
}
@java.lang.Override
protected GreetingsFutureStub build(com.google.net.stubby.Channel channel,
GreetingsServiceDescriptor config) {
return new GreetingsFutureStub(channel, config);
protected GreeterFutureStub build(com.google.net.stubby.Channel channel,
GreeterServiceDescriptor config) {
return new GreeterFutureStub(channel, config);
}
@java.lang.Override
public com.google.common.util.concurrent.ListenableFuture<ex.grpc.Helloworld.HelloReply> hello(
public com.google.common.util.concurrent.ListenableFuture<ex.grpc.Helloworld.HelloReply> sayHello(
ex.grpc.Helloworld.HelloRequest request) {
return unaryFutureCall(
channel.newCall(config.hello), request);
channel.newCall(config.sayHello), request);
}
}
public static com.google.net.stubby.ServerServiceDefinition bindService(
final Greetings serviceImpl) {
return com.google.net.stubby.ServerServiceDefinition.builder("helloworld.Greetings")
final Greeter serviceImpl) {
return com.google.net.stubby.ServerServiceDefinition.builder("helloworld.Greeter")
.addMethod(createMethodDefinition(
METHOD_HELLO,
METHOD_SAY_HELLO,
asyncUnaryRequestCall(
new com.google.net.stubby.stub.ServerCalls.UnaryRequestMethod<
ex.grpc.Helloworld.HelloRequest,
@ -165,7 +165,7 @@ public class GreetingsGrpc {
public void invoke(
ex.grpc.Helloworld.HelloRequest request,
com.google.net.stubby.stub.StreamObserver<ex.grpc.Helloworld.HelloReply> responseObserver) {
serviceImpl.hello(request, responseObserver);
serviceImpl.sayHello(request, responseObserver);
}
}))).build();
}

@ -2,10 +2,10 @@ package ex.grpc;
import com.google.net.stubby.stub.StreamObserver;
public class GreetingsImpl implements GreetingsGrpc.Greetings {
public class GreeterImpl implements GreeterGrpc.Greeter {
@Override
public void hello(Helloworld.HelloRequest req,
public void sayHello(Helloworld.HelloRequest req,
StreamObserver<Helloworld.HelloReply> responseObserver) {
Helloworld.HelloReply reply = Helloworld.HelloReply.newBuilder().setMessage(
"Hello " + req.getName()).build();

@ -7,16 +7,16 @@ import com.google.net.stubby.transport.netty.NettyServerBuilder;
import java.util.concurrent.TimeUnit;
/**
* Server that manages startup/shutdown of a {@code Greetings} server.
* Server that manages startup/shutdown of a {@code Greeter} server.
*/
public class GreetingsServer {
public class GreeterServer {
/* The port on which the server should run */
private int port = 50051;
private ServerImpl server;
private void start() throws Exception {
server = NettyServerBuilder.forPort(port)
.addService(GreetingsGrpc.bindService(new GreetingsImpl()))
.addService(GreeterGrpc.bindService(new GreeterImpl()))
.build();
server.startAsync();
server.awaitRunning(5, TimeUnit.SECONDS);
@ -33,7 +33,7 @@ public class GreetingsServer {
* Main launches the server from the command line.
*/
public static void main(String[] args) throws Exception {
final GreetingsServer server = new GreetingsServer();
final GreeterServer server = new GreeterServer();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override

@ -1,5 +1,5 @@
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: src/main/proto/helloworld.proto
// source: helloworld.proto
package ex.grpc;
@ -915,11 +915,11 @@ public final class Helloworld {
descriptor;
static {
java.lang.String[] descriptorData = {
"\n\037src/main/proto/helloworld.proto\022\nhello" +
"world\"\034\n\014HelloRequest\022\014\n\004name\030\001 \001(\t\"\035\n\nH" +
"elloReply\022\017\n\007message\030\001 \001(\t2H\n\tGreetings\022" +
";\n\005hello\022\030.helloworld.HelloRequest\032\026.hel" +
"loworld.HelloReply\"\000B\t\n\007ex.grpcb\006proto3"
"\n\020helloworld.proto\022\nhelloworld\"\034\n\014HelloR" +
"equest\022\014\n\004name\030\001 \001(\t\"\035\n\nHelloReply\022\017\n\007me" +
"ssage\030\001 \001(\t2I\n\007Greeter\022>\n\010sayHello\022\030.hel" +
"loworld.HelloRequest\032\026.helloworld.HelloR" +
"eply\"\000B\t\n\007ex.grpcb\006proto3"
};
com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner =
new com.google.protobuf.Descriptors.FileDescriptor. InternalDescriptorAssigner() {

Loading…
Cancel
Save