Skip to content
This repository was archived by the owner on Nov 12, 2024. It is now read-only.

Commit 049f011

Browse files
committed
Blog Post: The versatility of gRPC
1 parent f4a2d1a commit 049f011

18 files changed

+8816
-1
lines changed

.gitignore

+7-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*.out
1515

1616
# Dependency directories (remove the comment below to include it)
17-
# vendor/
17+
vendor/
1818

1919
##### VisualStudioCode #####
2020

@@ -31,3 +31,9 @@
3131
##### goreleaser #####
3232

3333
dist/
34+
35+
##### Python #####
36+
__pycache__/
37+
*.py[cod]
38+
*$py.class
39+

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ gRPC examples and feature references
66

77
* [helloworld](examples/helloworld/) - Forked from [grpc/grpc-go/examples/helloworld](https://github.com/grpc/grpc-go/tree/master/examples/helloworld)
88

9+
## Walkthroughs
10+
11+
* [compute-options](walkthroughs/compute-options/) - Connect a greeter client written in Python running on a Lambda function to a greeter server written in Go running on a Fargate task (container).
12+
913
## Building examples
1014

1115
This project uses [Task](https://taskfile.dev) to build and run examples. Each `Taskfile.yml` can be used to build and run each example or variation.

examples/helloworld/greeter_client_python/helloworld_pb2.py

+137
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
2+
"""Client and server classes corresponding to protobuf-defined services."""
3+
import grpc
4+
5+
import helloworld_pb2 as helloworld__pb2
6+
7+
8+
class GreeterStub(object):
9+
"""The greeting service definition.
10+
"""
11+
12+
def __init__(self, channel):
13+
"""Constructor.
14+
15+
Args:
16+
channel: A grpc.Channel.
17+
"""
18+
self.SayHello = channel.unary_unary(
19+
'/helloworld.Greeter/SayHello',
20+
request_serializer=helloworld__pb2.HelloRequest.SerializeToString,
21+
response_deserializer=helloworld__pb2.HelloReply.FromString,
22+
)
23+
24+
25+
class GreeterServicer(object):
26+
"""The greeting service definition.
27+
"""
28+
29+
def SayHello(self, request, context):
30+
"""Sends a greeting
31+
"""
32+
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
33+
context.set_details('Method not implemented!')
34+
raise NotImplementedError('Method not implemented!')
35+
36+
37+
def add_GreeterServicer_to_server(servicer, server):
38+
rpc_method_handlers = {
39+
'SayHello': grpc.unary_unary_rpc_method_handler(
40+
servicer.SayHello,
41+
request_deserializer=helloworld__pb2.HelloRequest.FromString,
42+
response_serializer=helloworld__pb2.HelloReply.SerializeToString,
43+
),
44+
}
45+
generic_handler = grpc.method_handlers_generic_handler(
46+
'helloworld.Greeter', rpc_method_handlers)
47+
server.add_generic_rpc_handlers((generic_handler,))
48+
49+
50+
# This class is part of an EXPERIMENTAL API.
51+
class Greeter(object):
52+
"""The greeting service definition.
53+
"""
54+
55+
@staticmethod
56+
def SayHello(request,
57+
target,
58+
options=(),
59+
channel_credentials=None,
60+
call_credentials=None,
61+
insecure=False,
62+
compression=None,
63+
wait_for_ready=None,
64+
timeout=None,
65+
metadata=None):
66+
return grpc.experimental.unary_unary(request, target, '/helloworld.Greeter/SayHello',
67+
helloworld__pb2.HelloRequest.SerializeToString,
68+
helloworld__pb2.HelloReply.FromString,
69+
options, channel_credentials,
70+
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2015 gRPC authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""The Python implementation of the GRPC helloworld.Greeter client."""
15+
16+
from __future__ import print_function
17+
import logging
18+
import os
19+
import sys
20+
21+
import grpc
22+
23+
import helloworld_pb2
24+
import helloworld_pb2_grpc
25+
26+
endpoint = os.getenv("GREETER_ENDPOINT", "localhost:50051")
27+
28+
def handler(event, context):
29+
with grpc.insecure_channel(endpoint) as channel:
30+
stub = helloworld_pb2_grpc.GreeterStub(channel)
31+
response = stub.SayHello(helloworld_pb2.HelloRequest(name=event['name']))
32+
print("Received from backend: " + response.message)
33+
34+
if __name__ == "__main__":
35+
event = {}
36+
event['name'] = " ".join(sys.argv[1:])
37+
handler(event, None)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
grpcio==1.32.0
2+
protobuf==3.13.0
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
*.js
2+
!jest.config.js
3+
*.d.ts
4+
node_modules
5+
6+
# CDK asset staging directory
7+
.cdk.staging
8+
cdk.out
9+
10+
# Parcel default cache directory
11+
.parcel-cache
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.ts
2+
!*.d.ts
3+
4+
# CDK asset staging directory
5+
.cdk.staging
6+
cdk.out
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Compute Options Walkthrough
2+
3+
This is a CDK project that deploys a greeter client written in Python running on a Lambda function to a greeter server written in Go running on a Fargate task (container).
4+
5+
This walkthrough is the code used in the AWS Open Source Blog post **The versatility of gRPC, an open source high-performance RPC framework** (link TBD).
6+
7+
## Useful commands
8+
9+
* `npm run build && cdk deploy` compile typescript to js and deploy this stack to your default AWS account/region
10+
* `cdk destroy` removes the deployed CloudFormation stack
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env node
2+
import 'source-map-support/register';
3+
import * as cdk from '@aws-cdk/core';
4+
import { ComputeOptionsStack } from '../lib/compute-options-stack';
5+
6+
const app = new cdk.App();
7+
new ComputeOptionsStack(app, 'ComputeOptionsStack');

walkthroughs/compute-options/cdk.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"app": "npx ts-node bin/compute-options.ts",
3+
"context": {
4+
"@aws-cdk/core:enableStackNameDuplicates": "true",
5+
"aws-cdk:enableDiffNoFail": "true",
6+
"@aws-cdk/core:stackRelativeExports": "true"
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
roots: ['<rootDir>/test'],
3+
testMatch: ['**/*.test.ts'],
4+
transform: {
5+
'^.+\\.tsx?$': 'ts-jest'
6+
}
7+
};

0 commit comments

Comments
 (0)