Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
Grpc
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tci-gateway-module
Grpc
Commits
860ae5a0
Commit
860ae5a0
authored
10 years ago
by
Tim Emiola
Browse files
Options
Downloads
Patches
Plain Diff
Adds: Step_2 - adds a command line client app
parent
783a8613
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Step_2.md
+85
-0
85 additions, 0 deletions
Step_2.md
src/main/java/ex/grpc/GreetingsClient.java
+55
-0
55 additions, 0 deletions
src/main/java/ex/grpc/GreetingsClient.java
with
140 additions
and
0 deletions
Step_2.md
0 → 100644
+
85
−
0
View file @
860ae5a0
# Step-2: Write a service client.
This step uses the generated code to write a simple client to access the hello
service. The full client is in
[
GreetingsClient.java
](
src/main/java/ex/grpc/GreetingsClient.java
)
.
## Configuring the service to connect to.
The client contains uses a Stub to contact the service. The internet address
is configured in the client constructor. gRPC Channel is the abstraction over
transport handling; its constructor accepts the host name and port of the
service. The channel in turn is used to construct the Stub.
```
private final ChannelImpl channel;
private final GreetingGrpc.GreetingBlockingStub blockingStub;
public HelloClient(String host, int port) {
channel = NettyChannelBuilder.forAddress(host, port)
.negotiationType(NegotiationType.PLAINTEXT)
.build();
blockingStub = GreetingGrpc.newBlockingStub(channel);
}
```
## Obtaining a greeting
The greet method uses the stub to contact the service and obtain a greeting.
It:
-
constructs a request
-
obtains a reply from the stub
-
prints out the greeting
```
public void greet(String name) {
logger.debug("Will try to greet " + name + " ...");
try {
Helloworld.HelloRequest request = Helloworld.HelloRequest.newBuilder().setName(name).build();
Helloworld.HelloReply reply = blockingStub.hello(request);
logger.info("Greeting: " + reply.getMessage());
} catch (RuntimeException e) {
logger.log(Level.WARNING, "RPC failed", e);
return;
}
}
```
## Running from the command line
The main method puts together the example so that it can be run from a command
line.
```
/* Access a service running on the local machine on port 50051 */
HelloClient client = new HelloClient("localhost", 50051);
String user = "world";
if (args.length > 1) {
user = args[1];
}
client.greet(user);
```
It can be built as follows.
```
$ mvn package
```
It can also be run, but doing so now would end up a with a failure as there is
no server available yet. The
[
next step
](
Step-3.md
)
, describes how to
implement, build and run a server that supports the service description.
## Notes
-
the client uses a blocking stub. This means that the RPC call waits for the
server to respond, and will either return a response or raise an exception.
-
gRPC Java has other kinds of stubs that make non-blocking calls to the
server, where the response is returned asynchronously. Usage of these stubs
is a more advanced topic and will be described in later steps.
This diff is collapsed.
Click to expand it.
src/main/java/ex/grpc/GreetingsClient.java
0 → 100644
+
55
−
0
View file @
860ae5a0
package
ex.grpc
;
import
com.google.net.stubby.ChannelImpl
;
import
com.google.net.stubby.stub.StreamObserver
;
import
com.google.net.stubby.transport.netty.NegotiationType
;
import
com.google.net.stubby.transport.netty.NettyChannelBuilder
;
import
java.util.logging.Level
;
import
java.util.logging.Logger
;
import
java.util.concurrent.TimeUnit
;
public
class
GreetingsClient
{
private
final
Logger
logger
=
Logger
.
getLogger
(
GreetingsClient
.
class
.
getName
());
private
final
ChannelImpl
channel
;
private
final
GreetingsGrpc
.
GreetingsBlockingStub
blockingStub
;
public
GreetingsClient
(
String
host
,
int
port
)
{
channel
=
NettyChannelBuilder
.
forAddress
(
host
,
port
)
.
negotiationType
(
NegotiationType
.
PLAINTEXT
)
.
build
();
blockingStub
=
GreetingsGrpc
.
newBlockingStub
(
channel
);
}
public
void
shutdown
()
throws
InterruptedException
{
channel
.
shutdown
().
awaitTerminated
(
5
,
TimeUnit
.
SECONDS
);
}
public
void
greet
(
String
name
)
{
try
{
logger
.
fine
(
"Will try to greet "
+
name
+
" ..."
);
Helloworld
.
HelloRequest
req
=
Helloworld
.
HelloRequest
.
newBuilder
().
setName
(
name
).
build
();
Helloworld
.
HelloReply
reply
=
blockingStub
.
hello
(
req
);
logger
.
info
(
"Greeting: "
+
reply
.
getMessage
());
}
catch
(
RuntimeException
e
)
{
logger
.
log
(
Level
.
WARNING
,
"RPC failed"
,
e
);
return
;
}
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
GreetingsClient
client
=
new
GreetingsClient
(
"localhost"
,
50051
);
try
{
/* Access a service running on the local machine on port 50051 */
String
user
=
"world"
;
if
(
args
.
length
>
0
)
{
user
=
args
[
0
];
/* Use the arg as the name to greet if provided */
}
client
.
greet
(
user
);
}
finally
{
client
.
shutdown
();
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment