Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
matrix-sample
Overview
Overview
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mall
arch
matrix-sample
Commits
011f2f08
Commit
011f2f08
authored
Nov 20, 2019
by
QIANGLU
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add grpc demo
parent
6fe7031f
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
194 additions
and
2 deletions
+194
-2
pom.xml
spring-boot-grpc/pom.xml
+70
-0
GreeterImpl.java
spring-boot-grpc/src/main/java/com/matrix/grpc/GreeterImpl.java
+43
-0
HelloWordClient.java
spring-boot-grpc/src/main/java/com/matrix/grpc/HelloWordClient.java
+37
-0
hello.proto
spring-boot-grpc/src/main/proto/hello.proto
+36
-0
application.yml
spring-boot-grpc/src/main/resources/application.yml
+6
-0
pom.xml
spring-boot-pure/pom.xml
+1
-1
PureStartup.java
spring-boot-pure/src/main/java/com/matrix/pure/PureStartup.java
+1
-1
No files found.
spring-boot-grpc/pom.xml
0 → 100644
View file @
011f2f08
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
com.nova
</groupId>
<artifactId>
spring-boot-grpc
</artifactId>
<version>
1.0-SNAPSHOT
</version>
<parent>
<artifactId>
matrix
</artifactId>
<groupId>
com.secoo.mall
</groupId>
<version>
1.1.8.RELEASE
</version>
</parent>
<properties>
<java.version>
1.8
</java.version>
</properties>
<dependencies>
<dependency>
<groupId>
io.grpc
</groupId>
<artifactId>
grpc-netty
</artifactId>
<version>
1.25.0
</version>
</dependency>
<dependency>
<groupId>
io.grpc
</groupId>
<artifactId>
grpc-protobuf
</artifactId>
<version>
1.25.0
</version>
</dependency>
<dependency>
<groupId>
io.grpc
</groupId>
<artifactId>
grpc-stub
</artifactId>
<version>
1.25.0
</version>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>
kr.motd.maven
</groupId>
<artifactId>
os-maven-plugin
</artifactId>
<version>
1.5.0.Final
</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>
org.xolstice.maven.plugins
</groupId>
<artifactId>
protobuf-maven-plugin
</artifactId>
<version>
0.5.1
</version>
<configuration>
<protocArtifact>
com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}
</protocArtifact>
<pluginId>
grpc-java
</pluginId>
<pluginArtifact>
io.grpc:protoc-gen-grpc-java:1.25.0:exe:${os.detected.classifier}
</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>
compile
</goal>
<goal>
compile-custom
</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
spring-boot-grpc/src/main/java/com/matrix/grpc/GreeterImpl.java
0 → 100644
View file @
011f2f08
package
com
.
matrix
.
grpc
;
import
com.takumiCX.greeter.GreeterGrpc
;
import
com.takumiCX.greeter.HelloReply
;
import
com.takumiCX.greeter.HelloRequest
;
import
io.grpc.Server
;
import
io.grpc.ServerBuilder
;
import
io.grpc.stub.StreamObserver
;
import
java.io.IOException
;
import
java.util.logging.Logger
;
/**
* @ClassName GreeterImpl
* @Author QIANGLU
* @Date 2019/11/20 11:12 上午
* @Version 1.0
*/
public
class
GreeterImpl
extends
GreeterGrpc
.
GreeterImplBase
{
private
static
final
Logger
log
=
Logger
.
getLogger
(
GreeterImpl
.
class
.
getName
());
@Override
public
void
sayHello
(
HelloRequest
request
,
StreamObserver
<
HelloReply
>
responseObserver
)
{
//构建响应消息,从请求消息中获取姓名,在前面拼接上"Hello "
HelloReply
reply
=
HelloReply
.
newBuilder
().
setMessage
(
"Hello "
+
request
.
getName
()).
build
();
//在流关闭或抛出异常前可以调用多次
responseObserver
.
onNext
(
reply
);
//关闭流
responseObserver
.
onCompleted
();
}
public
static
void
main
(
String
[]
args
)
throws
IOException
,
InterruptedException
{
int
port
=
50051
;
Server
server
=
ServerBuilder
.
forPort
(
port
).
addService
(
new
GreeterImpl
()).
build
().
start
();
log
.
info
(
"Server started,listening on "
+
port
);
server
.
awaitTermination
();
}
}
spring-boot-grpc/src/main/java/com/matrix/grpc/HelloWordClient.java
0 → 100644
View file @
011f2f08
package
com
.
matrix
.
grpc
;
import
com.takumiCX.greeter.GreeterGrpc
;
import
com.takumiCX.greeter.HelloReply
;
import
com.takumiCX.greeter.HelloRequest
;
import
io.grpc.ManagedChannel
;
import
io.grpc.ManagedChannelBuilder
;
import
java.util.Scanner
;
import
java.util.logging.Logger
;
/**
* @ClassName HelloWordClient
* @Author QIANGLU
* @Date 2019/11/20 11:23 上午
* @Version 1.0
*/
public
class
HelloWordClient
{
private
static
final
Logger
log
=
Logger
.
getLogger
(
HelloWordClient
.
class
.
getName
());
public
static
void
main
(
String
[]
args
)
{
int
port
=
50051
;
ManagedChannel
channel
=
ManagedChannelBuilder
.
forAddress
(
"localhost"
,
port
).
usePlaintext
().
build
();
GreeterGrpc
.
GreeterBlockingStub
stub
=
GreeterGrpc
.
newBlockingStub
(
channel
);
Scanner
scanner
=
new
Scanner
(
System
.
in
);
while
(
true
){
String
name
=
scanner
.
nextLine
().
trim
();
HelloRequest
helloRequest
=
HelloRequest
.
newBuilder
().
setName
(
name
).
build
();
HelloReply
helloReply
=
stub
.
sayHello
(
helloRequest
);
log
.
info
(
"message"
+
helloReply
.
getMessage
());
}
}
}
spring-boot-grpc/src/main/proto/hello.proto
0 → 100644
View file @
011f2f08
//Protocal Buffers的版本有v2和v3之分,语法有较多变化,且相互不兼容
//这里使用的v3版本的
syntax
=
"proto3"
;
//编译后生成的消息类HelloRequest和HelloReply是否分别放在单独的class文件中
option
java_multiple_files
=
true
;
//生成代码的包路径
option
java_package
=
"com.takumiCX.greeter"
;
//最外层的类名称
option
java_outer_classname
=
"HelloWorldProto"
;
//包命名空间
package
helloworld
;
// 服务接口
service
Greeter
{
// 一个简单的rpc方法
rpc
SayHello
(
HelloRequest
)
returns
(
HelloReply
)
{}
}
service
Hello
{
rpc
hello
(
HelloRequest
)
returns
(
HelloReply
){}
}
// 请求消息
message
HelloRequest
{
string
name
=
1
;
}
// 响应消息
message
HelloReply
{
string
message
=
1
;
}
\ No newline at end of file
spring-boot-grpc/src/main/resources/application.yml
0 → 100644
View file @
011f2f08
spring
:
application
:
name
:
learn-grpc
server
:
port
:
6080
spring-boot-pure/pom.xml
View file @
011f2f08
...
@@ -21,7 +21,7 @@
...
@@ -21,7 +21,7 @@
<dependencies>
<dependencies>
<dependency>
<dependency>
<groupId>
com.secoo.mall
</groupId>
<groupId>
com.secoo.mall
</groupId>
<artifactId>
secoo-log
-starter
</artifactId>
<artifactId>
logger
-starter
</artifactId>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<groupId>
org.springframework.boot
</groupId>
...
...
spring-boot-pure/src/main/java/com/matrix/pure/PureStartup.java
View file @
011f2f08
package
com
.
matrix
.
pure
;
package
com
.
matrix
.
grpc
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.SpringApplication
;
...
...
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