Skip to content

Commit 53698a5

Browse files
author
YunaiV
committed
增加 grpc 入门
1 parent 43d33e3 commit 53698a5

File tree

8 files changed

+198
-0
lines changed

8 files changed

+198
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>lab-64-grpc-demo</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-64-grpc-demo-user-service-api</artifactId>
13+
14+
<properties>
15+
<!-- 依赖相关配置 -->
16+
<io.grpc.version>1.23.0</io.grpc.version>
17+
<!-- 插件相关配置 -->
18+
<maven.compiler.target>1.8</maven.compiler.target>
19+
<maven.compiler.source>1.8</maven.compiler.source>
20+
<os-maven-plugin.version>1.6.2</os-maven-plugin.version>
21+
<protobuf-maven-plugin.version>0.6.1</protobuf-maven-plugin.version>
22+
<grpc-starter-version>3.4.1</grpc-starter-version>
23+
</properties>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>io.grpc</groupId>
28+
<artifactId>grpc-protobuf</artifactId>
29+
<version>${io.grpc.version}</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>io.grpc</groupId>
33+
<artifactId>grpc-stub</artifactId>
34+
<version>${io.grpc.version}</version>
35+
</dependency>
36+
</dependencies>
37+
38+
<build>
39+
<extensions>
40+
<extension>
41+
<groupId>kr.motd.maven</groupId>
42+
<artifactId>os-maven-plugin</artifactId>
43+
<version>${os-maven-plugin.version}</version>
44+
</extension>
45+
</extensions>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.xolstice.maven.plugins</groupId>
49+
<artifactId>protobuf-maven-plugin</artifactId>
50+
<version>${protobuf-maven-plugin.version}</version>
51+
<configuration>
52+
<protocArtifact>
53+
com.google.protobuf:protoc:3.9.1:exe:${os.detected.classifier}
54+
</protocArtifact>
55+
<pluginId>grpc-java</pluginId>
56+
<pluginArtifact>
57+
io.grpc:protoc-gen-grpc-java:1.23.0:exe:${os.detected.classifier}
58+
</pluginArtifact>
59+
</configuration>
60+
<executions>
61+
<execution>
62+
<goals>
63+
<goal>compile</goal>
64+
<goal>compile-custom</goal>
65+
</goals>
66+
</execution>
67+
</executions>
68+
</plugin>
69+
</plugins>
70+
</build>
71+
72+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
syntax = "proto3";
2+
option java_multiple_files = true;
3+
4+
package cn.iocoder.springboot.lab64.userservice.api;
5+
6+
message UserGetRequest {
7+
int32 id = 1;
8+
}
9+
10+
message UserGetResponse {
11+
int32 id = 1;
12+
string name = 2;
13+
int32 gender = 3;
14+
}
15+
16+
message UserCreateRequest {
17+
string name = 1;
18+
int32 gender = 2;
19+
}
20+
21+
message UserCreateResponse {
22+
int32 id = 1;
23+
}
24+
25+
service UserService {
26+
27+
rpc get(UserGetRequest) returns (UserGetResponse);
28+
29+
rpc create(UserCreateRequest) returns (UserCreateResponse);
30+
31+
}
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>lab-64-grpc-demo</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-64-grpc-demo-user-service</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>cn.iocoder.springboot.labs</groupId>
17+
<artifactId>lab-64-grpc-demo-user-service-api</artifactId>
18+
<version>1.0-SNAPSHOT</version>
19+
</dependency>
20+
</dependencies>
21+
22+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package cn.iocoder.springboot.lab64.userservice;
2+
3+
public class UserServiceApplication {
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cn.iocoder.springboot.lab64.userservice.rpc;
2+
3+
import cn.iocoder.springboot.lab64.userservice.api.*;
4+
import io.grpc.stub.StreamObserver;
5+
6+
public class UserServiceGrpcImpl extends UserServiceGrpc.UserServiceImplBase {
7+
8+
@Override
9+
public void get(UserGetRequest request, StreamObserver<UserGetResponse> responseObserver) {
10+
// 创建响应对象
11+
UserGetResponse.Builder builder = UserGetResponse.newBuilder();
12+
builder.setId(request.getId())
13+
.setName("没有昵称:" + request.getId())
14+
.setGender(request.getId() % 2 + 1);
15+
// 返回响应
16+
responseObserver.onNext(builder.build());
17+
}
18+
19+
@Override
20+
public void create(UserCreateRequest request, StreamObserver<UserCreateResponse> responseObserver) {
21+
// 创建响应对象
22+
UserCreateResponse.Builder builder = UserCreateResponse.newBuilder();
23+
builder.setId((int) (System.currentTimeMillis() / 1000));
24+
// 返回响应
25+
responseObserver.onNext(builder.build());
26+
}
27+
28+
}

lab-64/lab-64-grpc-demo/pom.xml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>lab-64</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-64-grpc-demo</artifactId>
13+
<packaging>pom</packaging>
14+
<modules>
15+
<module>lab-64-grpc-demo-user-service</module>
16+
<module>lab-64-grpc-demo-user-service-api</module>
17+
</modules>
18+
19+
20+
</project>

lab-64/pom.xml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>labs-parent</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-64</artifactId>
13+
14+
<packaging>pom</packaging>
15+
<modules>
16+
<module>lab-64-grpc-demo</module>
17+
</modules>
18+
19+
</project>

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
<!-- <module>labx-28</module>-->
107107
<!-- <module>labx-28</module>-->
108108
<!-- <module>labx-29</module>-->
109+
<module>lab-64</module>
109110
</modules>
110111

111112
</project>

0 commit comments

Comments
 (0)