Skip to content

Commit 211fab1

Browse files
author
YunaiV
committed
增加 cloud + grpc 入门
1 parent 5c41b74 commit 211fab1

File tree

13 files changed

+364
-0
lines changed

13 files changed

+364
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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>labx-30-grpc-cloud</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>labx-30-grpc-cloud-application</artifactId>
13+
14+
<properties>
15+
<!-- 依赖相关配置 -->
16+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
17+
<!-- 插件相关配置 -->
18+
<maven.compiler.target>1.8</maven.compiler.target>
19+
<maven.compiler.source>1.8</maven.compiler.source>
20+
</properties>
21+
22+
<dependencyManagement>
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-parent</artifactId>
27+
<version>${spring.boot.version}</version>
28+
<type>pom</type>
29+
<scope>import</scope>
30+
</dependency>
31+
</dependencies>
32+
</dependencyManagement>
33+
34+
<dependencies>
35+
<!-- 引入 API 项目 -->
36+
<dependency>
37+
<groupId>cn.iocoder.springboot.labs</groupId>
38+
<artifactId>labx-30-grpc-cloud-user-service-api</artifactId>
39+
<version>1.0-SNAPSHOT</version>
40+
</dependency>
41+
42+
<!-- 实现对 SpringMVC 的自动化配置 -->
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-web</artifactId>
46+
</dependency>
47+
48+
<!-- 引入 gRPC Client Starter 依赖,实现对 gRPC 的自动配置 -->
49+
<dependency>
50+
<groupId>net.devh</groupId>
51+
<artifactId>grpc-client-spring-boot-starter</artifactId>
52+
<version>2.8.0.RELEASE</version>
53+
</dependency>
54+
</dependencies>
55+
56+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cn.iocoder.springcloud.labx30.demo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class DemoApplication {
8+
9+
public static void main(String[] args) {
10+
// 启动 Spring Boot 应用
11+
SpringApplication.run(DemoApplication.class, args);
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package cn.iocoder.springcloud.labx30.demo.controller;
2+
3+
import cn.iocoder.springcloud.labx30.userservice.api.*;
4+
import net.devh.boot.grpc.client.inject.GrpcClient;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestParam;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
@RestController
11+
@RequestMapping("/demo")
12+
public class DemoController {
13+
14+
@GrpcClient("userService")
15+
private UserServiceGrpc.UserServiceBlockingStub userServiceGrpc;
16+
17+
@GetMapping("/get")
18+
public String get(@RequestParam("id") Integer id) {
19+
// 创建请求
20+
UserGetRequest request = UserGetRequest.newBuilder().setId(id).build();
21+
// 执行 gRPC 请求
22+
UserGetResponse response = userServiceGrpc.get(request);
23+
// 响应
24+
return response.getName();
25+
}
26+
27+
@GetMapping("/create") // 为了方便测试,实际使用 @PostMapping
28+
public Integer create(@RequestParam("name") String name,
29+
@RequestParam("gender") Integer gender) {
30+
// 创建请求
31+
UserCreateRequest request = UserCreateRequest.newBuilder()
32+
.setName(name).setGender(gender).build();
33+
// 执行 gRPC 请求
34+
UserCreateResponse response = userServiceGrpc.create(request);
35+
// 响应
36+
return response.getId();
37+
}
38+
39+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
grpc:
2+
# gRPC 客户端配置,对应 GrpcChannelsProperties 配置类的映射
3+
client:
4+
userService:
5+
address: 'static://127.0.0.1:8888' # 用户服务的地址
6+
negotiation-type: plaintext
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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>labx-30-grpc-cloud</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>labx-30-grpc-cloud-user-service-api</artifactId>
13+
14+
<properties>
15+
<!-- 依赖相关配置 -->
16+
<io.grpc.version>1.30.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+
</properties>
23+
24+
<dependencies>
25+
<!-- 引入 gRPC Protobuf 依赖,因为使用它作为序列化库 -->
26+
<dependency>
27+
<groupId>io.grpc</groupId>
28+
<artifactId>grpc-protobuf</artifactId>
29+
<version>${io.grpc.version}</version>
30+
</dependency>
31+
<!-- 引入 gRPC Stub 依赖,因为使用它作为 gRPC 客户端库 -->
32+
<dependency>
33+
<groupId>io.grpc</groupId>
34+
<artifactId>grpc-stub</artifactId>
35+
<version>${io.grpc.version}</version>
36+
</dependency>
37+
</dependencies>
38+
39+
<build>
40+
<extensions>
41+
<!-- os-maven-plugin 插件,从 OS 系统中获取参数 -->
42+
<extension>
43+
<groupId>kr.motd.maven</groupId>
44+
<artifactId>os-maven-plugin</artifactId>
45+
<version>${os-maven-plugin.version}</version>
46+
</extension>
47+
</extensions>
48+
<plugins>
49+
<!-- protobuf-maven-plugin 插件,通过 protobuf 文件,生成 Service 和 Message 类 -->
50+
<plugin>
51+
<groupId>org.xolstice.maven.plugins</groupId>
52+
<artifactId>protobuf-maven-plugin</artifactId>
53+
<version>${protobuf-maven-plugin.version}</version>
54+
<configuration>
55+
<pluginId>grpc-java</pluginId>
56+
<protocArtifact>com.google.protobuf:protoc:3.9.1:exe:${os.detected.classifier}</protocArtifact>
57+
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${io.grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
58+
</configuration>
59+
<executions>
60+
<execution>
61+
<goals>
62+
<goal>compile</goal>
63+
<goal>compile-custom</goal>
64+
</goals>
65+
</execution>
66+
</executions>
67+
</plugin>
68+
</plugins>
69+
</build>
70+
71+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
syntax = "proto3";
2+
option java_multiple_files = true;
3+
4+
package cn.iocoder.springcloud.labx30.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+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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>labx-30-grpc-cloud</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>labx-30-grpc-cloud-user-service</artifactId>
13+
14+
<properties>
15+
<!-- 依赖相关配置 -->
16+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
17+
<!-- 插件相关配置 -->
18+
<maven.compiler.target>1.8</maven.compiler.target>
19+
<maven.compiler.source>1.8</maven.compiler.source>
20+
</properties>
21+
22+
<dependencyManagement>
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-parent</artifactId>
27+
<version>${spring.boot.version}</version>
28+
<type>pom</type>
29+
<scope>import</scope>
30+
</dependency>
31+
</dependencies>
32+
</dependencyManagement>
33+
34+
<dependencies>
35+
<!-- 引入 API 项目 -->
36+
<dependency>
37+
<groupId>cn.iocoder.springboot.labs</groupId>
38+
<artifactId>labx-30-grpc-cloud-user-service-api</artifactId>
39+
<version>1.0-SNAPSHOT</version>
40+
</dependency>
41+
42+
<!-- 引入 Spring Boot 基础 Starter 依赖 -->
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter</artifactId>
46+
</dependency>
47+
48+
<!-- 引入 gRPC Server Starter 依赖,实现对 gRPC 的自动配置 -->
49+
<dependency>
50+
<groupId>net.devh</groupId>
51+
<artifactId>grpc-server-spring-boot-starter</artifactId>
52+
<version>2.8.0.RELEASE</version>
53+
</dependency>
54+
</dependencies>
55+
56+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cn.iocoder.springcloud.labx30.userservice;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class UserServiceApplication {
8+
9+
public static void main(String[] args) {
10+
// 启动 Spring Boot 应用
11+
SpringApplication.run(UserServiceApplication.class, args);
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cn.iocoder.springcloud.labx30.userservice.rpc;
2+
3+
import cn.iocoder.springcloud.labx30.userservice.api.*;
4+
import io.grpc.stub.StreamObserver;
5+
import net.devh.boot.grpc.server.service.GrpcService;
6+
7+
@GrpcService
8+
public class UserServiceGrpcImpl extends UserServiceGrpc.UserServiceImplBase {
9+
10+
@Override
11+
public void get(UserGetRequest request, StreamObserver<UserGetResponse> responseObserver) {
12+
// 创建响应对象
13+
UserGetResponse.Builder builder = UserGetResponse.newBuilder();
14+
builder.setId(request.getId())
15+
.setName("没有昵称:" + request.getId())
16+
.setGender(request.getId() % 2 + 1);
17+
// 返回响应
18+
responseObserver.onNext(builder.build());
19+
responseObserver.onCompleted();
20+
}
21+
22+
@Override
23+
public void create(UserCreateRequest request, StreamObserver<UserCreateResponse> responseObserver) {
24+
// 创建响应对象
25+
UserCreateResponse.Builder builder = UserCreateResponse.newBuilder();
26+
builder.setId((int) (System.currentTimeMillis() / 1000));
27+
// 返回响应
28+
responseObserver.onNext(builder.build());
29+
responseObserver.onCompleted();
30+
}
31+
32+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
grpc:
2+
# gRPC 服务器配置,对应 GrpcServerProperties 配置类
3+
server:
4+
port: 8888

0 commit comments

Comments
 (0)