forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YunaiV
committed
Jun 14, 2020
1 parent
5dc20cd
commit 08e5388
Showing
19 changed files
with
363 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,4 +29,3 @@ service UserService { | |
rpc create(UserCreateRequest) returns (UserCreateResponse); | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
lab-64/lab-64-grpc-starter/lab-64-grpc-starter-application/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?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"> | ||
<parent> | ||
<artifactId>lab-64-grpc-starter</artifactId> | ||
<groupId>cn.iocoder.springboot.labs</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>lab-64-grpc-starter-application</artifactId> | ||
|
||
<properties> | ||
<!-- 依赖相关配置 --> | ||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version> | ||
<!-- 插件相关配置 --> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
</properties> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>${spring.boot.version}</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<dependencies> | ||
<!-- 引入 API 项目 --> | ||
<dependency> | ||
<groupId>cn.iocoder.springboot.labs</groupId> | ||
<artifactId>lab-64-grpc-starter-user-service-api</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
</dependency> | ||
|
||
<!-- 实现对 SpringMVC 的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<!-- 引入 gRPC Client Starter 依赖,实现对 gRPC 的自动配置 --> | ||
<dependency> | ||
<groupId>net.devh</groupId> | ||
<artifactId>grpc-client-spring-boot-starter</artifactId> | ||
<version>2.8.0.RELEASE</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
14 changes: 14 additions & 0 deletions
14
...c-starter-application/src/main/java/cn/iocoder/springboot/lab64/demo/DemoApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package cn.iocoder.springboot.lab64.demo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class DemoApplication { | ||
|
||
public static void main(String[] args) { | ||
// 启动 Spring Boot 应用 | ||
SpringApplication.run(DemoApplication.class, args); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...application/src/main/java/cn/iocoder/springboot/lab64/demo/controller/DemoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package cn.iocoder.springboot.lab64.demo.controller; | ||
|
||
import cn.iocoder.springboot.lab64.userservice.api.*; | ||
import net.devh.boot.grpc.client.inject.GrpcClient; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/demo") | ||
public class DemoController { | ||
|
||
@GrpcClient("userService") | ||
private UserServiceGrpc.UserServiceBlockingStub userServiceGrpc; | ||
|
||
@GetMapping("/get") | ||
public String get(@RequestParam("id") Integer id) { | ||
// 创建请求 | ||
UserGetRequest request = UserGetRequest.newBuilder().setId(id).build(); | ||
// 执行 gRPC 请求 | ||
UserGetResponse response = userServiceGrpc.get(request); | ||
// 响应 | ||
return response.getName(); | ||
} | ||
|
||
@GetMapping("/create") // 为了方便测试,实际使用 @PostMapping | ||
public Integer create(@RequestParam("name") String name, | ||
@RequestParam("gender") Integer gender) { | ||
// 创建请求 | ||
UserCreateRequest request = UserCreateRequest.newBuilder() | ||
.setName(name).setGender(gender).build(); | ||
// 执行 gRPC 请求 | ||
UserCreateResponse response = userServiceGrpc.create(request); | ||
// 响应 | ||
return response.getId(); | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
...64/lab-64-grpc-starter/lab-64-grpc-starter-application/src/main/resources/application.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
grpc: | ||
# gRPC 客户端配置,对应 GrpcChannelsProperties 配置类的映射 | ||
client: | ||
userService: | ||
address: 'static://127.0.0.1:8888' # 用户服务的地址 | ||
negotiation-type: plaintext |
71 changes: 71 additions & 0 deletions
71
lab-64/lab-64-grpc-starter/lab-64-grpc-starter-user-service-api/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?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"> | ||
<parent> | ||
<artifactId>lab-64-grpc-starter</artifactId> | ||
<groupId>cn.iocoder.springboot.labs</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>lab-64-grpc-starter-user-service-api</artifactId> | ||
|
||
<properties> | ||
<!-- 依赖相关配置 --> | ||
<io.grpc.version>1.30.0</io.grpc.version> | ||
<!-- 插件相关配置 --> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<os-maven-plugin.version>1.6.2</os-maven-plugin.version> | ||
<protobuf-maven-plugin.version>0.6.1</protobuf-maven-plugin.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<!-- 引入 gRPC Protobuf 依赖,因为使用它作为序列化库 --> | ||
<dependency> | ||
<groupId>io.grpc</groupId> | ||
<artifactId>grpc-protobuf</artifactId> | ||
<version>${io.grpc.version}</version> | ||
</dependency> | ||
<!-- 引入 gRPC Stub 依赖,因为使用它作为 gRPC 客户端库 --> | ||
<dependency> | ||
<groupId>io.grpc</groupId> | ||
<artifactId>grpc-stub</artifactId> | ||
<version>${io.grpc.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<extensions> | ||
<!-- os-maven-plugin 插件,从 OS 系统中获取参数 --> | ||
<extension> | ||
<groupId>kr.motd.maven</groupId> | ||
<artifactId>os-maven-plugin</artifactId> | ||
<version>${os-maven-plugin.version}</version> | ||
</extension> | ||
</extensions> | ||
<plugins> | ||
<!-- protobuf-maven-plugin 插件,通过 protobuf 文件,生成 Service 和 Message 类 --> | ||
<plugin> | ||
<groupId>org.xolstice.maven.plugins</groupId> | ||
<artifactId>protobuf-maven-plugin</artifactId> | ||
<version>${protobuf-maven-plugin.version}</version> | ||
<configuration> | ||
<pluginId>grpc-java</pluginId> | ||
<protocArtifact>com.google.protobuf:protoc:3.9.1:exe:${os.detected.classifier}</protocArtifact> | ||
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${io.grpc.version}:exe:${os.detected.classifier}</pluginArtifact> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>compile</goal> | ||
<goal>compile-custom</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
31 changes: 31 additions & 0 deletions
31
...lab-64-grpc-starter/lab-64-grpc-starter-user-service-api/src/main/proto/UserService.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
syntax = "proto3"; | ||
option java_multiple_files = true; | ||
|
||
package cn.iocoder.springboot.lab64.userservice.api; | ||
|
||
message UserGetRequest { | ||
int32 id = 1; | ||
} | ||
|
||
message UserGetResponse { | ||
int32 id = 1; | ||
string name = 2; | ||
int32 gender = 3; | ||
} | ||
|
||
message UserCreateRequest { | ||
string name = 1; | ||
int32 gender = 2; | ||
} | ||
|
||
message UserCreateResponse { | ||
int32 id = 1; | ||
} | ||
|
||
service UserService { | ||
|
||
rpc get(UserGetRequest) returns (UserGetResponse); | ||
|
||
rpc create(UserCreateRequest) returns (UserCreateResponse); | ||
|
||
} |
Oops, something went wrong.