Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
B
brpc
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
brpc
Commits
f81cc8d4
Commit
f81cc8d4
authored
Jun 08, 2018
by
zhujiashun
Committed by
gejun
Sep 26, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add grpc client directory
parent
5293d5ae
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
317 additions
and
0 deletions
+317
-0
CMakeLists.txt
example/grpc_client/CMakeLists.txt
+122
-0
greeter_client.cpp
example/grpc_client/greeter_client.cpp
+93
-0
helloworld.proto
example/grpc_client/helloworld.proto
+22
-0
http_server.cpp
example/grpc_client/http_server.cpp
+80
-0
No files found.
example/grpc_client/CMakeLists.txt
0 → 100644
View file @
f81cc8d4
cmake_minimum_required
(
VERSION 2.8.10
)
project
(
http_c++ C CXX
)
option
(
EXAMPLE_LINK_SO
"Whether examples are linked dynamically"
OFF
)
execute_process
(
COMMAND bash -c
"find
${
CMAKE_SOURCE_DIR
}
/../.. -type d -regex
\"
.*output/include$
\"
| head -n1 | xargs dirname | tr -d '
\n
'"
OUTPUT_VARIABLE OUTPUT_PATH
)
set
(
CMAKE_PREFIX_PATH
${
OUTPUT_PATH
}
)
include
(
FindThreads
)
include
(
FindProtobuf
)
protobuf_generate_cpp
(
PROTO_SRC PROTO_HEADER helloworld.proto
)
# include PROTO_HEADER
include_directories
(
${
CMAKE_CURRENT_BINARY_DIR
}
)
find_path
(
GPERFTOOLS_INCLUDE_DIR NAMES gperftools/heap-profiler.h
)
find_library
(
GPERFTOOLS_LIBRARIES NAMES tcmalloc_and_profiler
)
include_directories
(
${
GPERFTOOLS_INCLUDE_DIR
}
)
find_path
(
BRPC_INCLUDE_PATH NAMES brpc/server.h
)
if
(
EXAMPLE_LINK_SO
)
find_library
(
BRPC_LIB NAMES brpc
)
else
()
find_library
(
BRPC_LIB NAMES libbrpc.a brpc
)
endif
()
if
((
NOT BRPC_INCLUDE_PATH
)
OR
(
NOT BRPC_LIB
))
message
(
FATAL_ERROR
"Fail to find brpc"
)
endif
()
include_directories
(
${
BRPC_INCLUDE_PATH
}
)
find_path
(
GFLAGS_INCLUDE_PATH gflags/gflags.h
)
find_library
(
GFLAGS_LIBRARY NAMES gflags libgflags
)
if
((
NOT GFLAGS_INCLUDE_PATH
)
OR
(
NOT GFLAGS_LIBRARY
))
message
(
FATAL_ERROR
"Fail to find gflags"
)
endif
()
include_directories
(
${
GFLAGS_INCLUDE_PATH
}
)
execute_process
(
COMMAND bash -c
"grep
\"
namespace [_A-Za-z0-9]
\\
+ {
\"
${
GFLAGS_INCLUDE_PATH
}
/gflags/gflags_declare.h | head -1 | awk '{print $2}' | tr -d '
\n
'"
OUTPUT_VARIABLE GFLAGS_NS
)
if
(
${
GFLAGS_NS
}
STREQUAL
"GFLAGS_NAMESPACE"
)
execute_process
(
COMMAND bash -c
"grep
\"
#define GFLAGS_NAMESPACE [_A-Za-z0-9]
\\
+
\"
${
GFLAGS_INCLUDE_PATH
}
/gflags/gflags_declare.h | head -1 | awk '{print $3}' | tr -d '
\n
'"
OUTPUT_VARIABLE GFLAGS_NS
)
endif
()
if
(
CMAKE_SYSTEM_NAME STREQUAL
"Darwin"
)
include
(
CheckFunctionExists
)
CHECK_FUNCTION_EXISTS
(
clock_gettime HAVE_CLOCK_GETTIME
)
if
(
NOT HAVE_CLOCK_GETTIME
)
set
(
DEFINE_CLOCK_GETTIME
"-DNO_CLOCK_GETTIME_IN_MAC"
)
endif
()
endif
()
set
(
CMAKE_CPP_FLAGS
"
${
DEFINE_CLOCK_GETTIME
}
-DGFLAGS_NS=
${
GFLAGS_NS
}
"
)
set
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CPP_FLAGS
}
-DNDEBUG -O2 -D__const__= -pipe -W -Wall -Wno-unused-parameter -fPIC -fno-omit-frame-pointer"
)
set
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
-DBRPC_ENABLE_CPU_PROFILER"
)
if
(
CMAKE_VERSION VERSION_LESS
"3.1.3"
)
if
(
CMAKE_CXX_COMPILER_ID STREQUAL
"GNU"
)
set
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
-std=c++11"
)
endif
()
if
(
CMAKE_CXX_COMPILER_ID STREQUAL
"Clang"
)
set
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
-std=c++11"
)
endif
()
else
()
set
(
CMAKE_CXX_STANDARD 11
)
set
(
CMAKE_CXX_STANDARD_REQUIRED ON
)
endif
()
find_path
(
LEVELDB_INCLUDE_PATH NAMES leveldb/db.h
)
find_library
(
LEVELDB_LIB NAMES leveldb
)
if
((
NOT LEVELDB_INCLUDE_PATH
)
OR
(
NOT LEVELDB_LIB
))
message
(
FATAL_ERROR
"Fail to find leveldb"
)
endif
()
include_directories
(
${
LEVELDB_INCLUDE_PATH
}
)
find_library
(
SSL_LIB NAMES ssl
)
if
(
NOT SSL_LIB
)
message
(
FATAL_ERROR
"Fail to find ssl"
)
endif
()
find_library
(
CRYPTO_LIB NAMES crypto
)
if
(
NOT CRYPTO_LIB
)
message
(
FATAL_ERROR
"Fail to find crypto"
)
endif
()
set
(
DYNAMIC_LIB
${
CMAKE_THREAD_LIBS_INIT
}
${
GFLAGS_LIBRARY
}
${
PROTOBUF_LIBRARIES
}
${
LEVELDB_LIB
}
${
SSL_LIB
}
${
CRYPTO_LIB
}
dl
)
if
(
CMAKE_SYSTEM_NAME STREQUAL
"Darwin"
)
set
(
DYNAMIC_LIB
${
DYNAMIC_LIB
}
pthread
"-framework CoreFoundation"
"-framework CoreGraphics"
"-framework CoreData"
"-framework CoreText"
"-framework Security"
"-framework Foundation"
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop"
"-Wl,-U,_RegisterThriftProtocol"
)
endif
()
add_executable
(
http_server http_server.cpp
${
PROTO_SRC
}
${
PROTO_HEADER
}
)
#add_executable(greeter_client greeter_client.cpp ${PROTO_SRC} ${PROTO_HEADER})
target_link_libraries
(
http_server
${
BRPC_LIB
}
${
DYNAMIC_LIB
}
${
GPERFTOOLS_LIBRARIES
}
)
#target_link_libraries(greeter_client ${DYNAMIC_LIB} ${GPERFTOOLS_LIBRARIES} "grpc++_reflection")
example/grpc_client/greeter_client.cpp
0 → 100644
View file @
f81cc8d4
/*
*
* Copyright 2015 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <iostream>
#include <memory>
#include <string>
#include <fstream>
#include <grpcpp/grpcpp.h>
#include "helloworld.pb.h"
using
grpc
::
Channel
;
using
grpc
::
ClientContext
;
using
grpc
::
Status
;
using
helloworld
::
HelloRequest
;
using
helloworld
::
HelloReply
;
using
helloworld
::
Greeter
;
class
GreeterClient
{
public
:
GreeterClient
(
std
::
shared_ptr
<
Channel
>
channel
)
:
stub_
(
Greeter
::
NewStub
(
channel
))
{}
// Assembles the client's payload, sends it and presents the response back
// from the server.
std
::
string
SayHello
(
const
std
::
string
&
user
)
{
// Data we are sending to the server.
HelloRequest
request
;
request
.
set_name
(
user
);
request
.
set_sex
(
"1234"
);
std
::
fstream
output
(
"out"
,
std
::
ios
::
out
|
std
::
ios
::
trunc
|
std
::
ios
::
binary
);
request
.
SerializeToOstream
(
&
output
);
//output.close();
std
::
fstream
input
(
"out"
,
std
::
ios
::
in
|
std
::
ios
::
binary
);
HelloRequest
request_in
;
request_in
.
ParseFromIstream
(
&
input
);
std
::
cout
<<
"name="
<<
request_in
.
name
()
<<
", sex="
<<
request_in
.
sex
();
//input.close();
// Container for the data we expect from the server.
HelloReply
reply
;
// Context for the client. It could be used to convey extra information to
// the server and/or tweak certain RPC behaviors.
ClientContext
context
;
// The actual RPC.
Status
status
=
stub_
->
SayHello
(
&
context
,
request
,
&
reply
);
// Act upon its status.
if
(
status
.
ok
())
{
return
reply
.
message
();
}
else
{
std
::
cout
<<
status
.
error_code
()
<<
": "
<<
status
.
error_message
()
<<
std
::
endl
;
return
"RPC failed"
;
}
}
private
:
std
::
unique_ptr
<
Greeter
::
Stub
>
stub_
;
};
int
main
(
int
argc
,
char
**
argv
)
{
// Instantiate the client. It requires a channel, out of which the actual RPCs
// are created. This channel models a connection to an endpoint (in this case,
// localhost at port 50051). We indicate that the channel isn't authenticated
// (use of InsecureChannelCredentials()).
GreeterClient
greeter
(
grpc
::
CreateChannel
(
"localhost:8010"
,
grpc
::
InsecureChannelCredentials
()));
std
::
string
user
(
"world"
);
std
::
string
reply
=
greeter
.
SayHello
(
user
);
std
::
cout
<<
"Greeter received: "
<<
reply
<<
std
::
endl
;
return
0
;
}
example/grpc_client/helloworld.proto
0 → 100644
View file @
f81cc8d4
syntax
=
"proto2"
;
package
helloworld
;
option
cc_generic_services
=
true
;
// The greeting service definition.
service
Greeter
{
// Sends a greeting
rpc
SayHello
(
HelloRequest
)
returns
(
HelloReply
)
{}
}
// The request message containing the user's name.
message
HelloRequest
{
required
string
name
=
1
;
required
string
sex
=
2
;
}
// The response message containing the greetings
message
HelloReply
{
required
string
message
=
1
;
}
example/grpc_client/http_server.cpp
0 → 100644
View file @
f81cc8d4
// Copyright (c) 2014 Baidu, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// A server to receive HttpRequest and send back HttpResponse.
#include <gflags/gflags.h>
#include <butil/logging.h>
#include <brpc/server.h>
#include <brpc/restful.h>
#include "helloworld.pb.h"
DEFINE_int32
(
port
,
8010
,
"TCP Port of this server"
);
DEFINE_int32
(
idle_timeout_s
,
-
1
,
"Connection will be closed if there is no "
"read/write operations during the last `idle_timeout_s'"
);
DEFINE_int32
(
logoff_ms
,
2000
,
"Maximum duration of server's LOGOFF state "
"(waiting for client to close connection before server stops)"
);
// Service with static path.
using
helloworld
::
HelloRequest
;
using
helloworld
::
HelloReply
;
class
HttpServiceImpl
:
public
helloworld
::
Greeter
{
public
:
HttpServiceImpl
()
{};
virtual
~
HttpServiceImpl
()
{};
void
SayHello
(
google
::
protobuf
::
RpcController
*
cntl_base
,
const
HelloRequest
*
req
,
HelloReply
*
res
,
google
::
protobuf
::
Closure
*
done
)
{
brpc
::
ClosureGuard
done_guard
(
done
);
brpc
::
Controller
*
cntl
=
static_cast
<
brpc
::
Controller
*>
(
cntl_base
);
std
::
string
prefix
(
"Hello "
);
LOG
(
INFO
)
<<
"req="
<<
req
->
name
()
<<
" "
<<
req
->
sex
();
cntl
->
http_response
().
set_content_type
(
"application/proto"
);
res
->
set_message
(
prefix
+
req
->
name
());
}
};
int
main
(
int
argc
,
char
*
argv
[])
{
// Parse gflags. We recommend you to use gflags as well.
GFLAGS_NS
::
ParseCommandLineFlags
(
&
argc
,
&
argv
,
true
);
// Generally you only need one Server.
brpc
::
Server
server
;
HttpServiceImpl
http_svc
;
// Add services into server. Notice the second parameter, because the
// service is put on stack, we don't want server to delete it, otherwise
// use brpc::SERVER_OWNS_SERVICE.
if
(
server
.
AddService
(
&
http_svc
,
brpc
::
SERVER_DOESNT_OWN_SERVICE
)
!=
0
)
{
LOG
(
ERROR
)
<<
"Fail to add http_svc"
;
return
-
1
;
}
// Start the server.
brpc
::
ServerOptions
options
;
options
.
idle_timeout_sec
=
FLAGS_idle_timeout_s
;
if
(
server
.
Start
(
FLAGS_port
,
&
options
)
!=
0
)
{
LOG
(
ERROR
)
<<
"Fail to start HttpServer"
;
return
-
1
;
}
// Wait until Ctrl-C is pressed, then Stop() and Join() the server.
server
.
RunUntilAskedToQuit
();
return
0
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment