Skip to content

Commit

Permalink
增加 grpc 入门
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Jun 14, 2020
1 parent 5dc20cd commit 08e5388
Show file tree
Hide file tree
Showing 19 changed files with 363 additions and 16 deletions.
2 changes: 2 additions & 0 deletions lab-64/lab-64-grpc-demo/lab-64-grpc-demo-application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
</dependencyManagement>

<dependencies>
<!-- 引入 API 项目 -->
<dependency>
<groupId>cn.iocoder.springboot.labs</groupId>
<artifactId>lab-64-grpc-demo-user-service-api</artifactId>
Expand All @@ -45,6 +46,7 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- 引入 gRPC Netty 依赖,因为使用它作为网络库 -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@

import cn.iocoder.springboot.lab64.userservice.api.UserServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.netty.NettyChannelBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.grpc.ManagedChannelBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class GrpcConfig {

private final Logger logger = LoggerFactory.getLogger(getClass());

private static final Integer GRPC_PORT = 8888;

@Bean
public ManagedChannel userGrpcManagedChannel() {
return NettyChannelBuilder.forAddress("127.0.0.1", GRPC_PORT).usePlaintext().build();
return ManagedChannelBuilder.forAddress("127.0.0.1", GRPC_PORT).usePlaintext().build();
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public String get(@RequestParam("id") Integer id) {

@GetMapping("/create") // 为了方便测试,实际使用 @PostMapping
public Integer create(@RequestParam("name") String name,
@RequestParam("gender") Integer gender) {
@RequestParam("gender") Integer gender) {
// 创建请求
UserCreateRequest request = UserCreateRequest.newBuilder()
.setName(name).setGender(gender).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<properties>
<!-- 依赖相关配置 -->
<io.grpc.version>1.23.0</io.grpc.version>
<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>
Expand All @@ -22,11 +22,13 @@
</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>
Expand All @@ -36,25 +38,23 @@

<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>
<protocArtifact>
com.google.protobuf:protoc:3.9.1:exe:${os.detected.classifier}
</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>
io.grpc:protoc-gen-grpc-java:1.23.0:exe:${os.detected.classifier}
</pluginArtifact>
<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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,3 @@ service UserService {
rpc create(UserCreateRequest) returns (UserCreateResponse);

}

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<properties>
<!-- 依赖相关配置 -->
<io.grpc.version>1.23.0</io.grpc.version>
<io.grpc.version>1.30.0</io.grpc.version>
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
<!-- 插件相关配置 -->
<maven.compiler.target>1.8</maven.compiler.target>
Expand All @@ -33,6 +33,7 @@
</dependencyManagement>

<dependencies>
<!-- 引入 API 项目 -->
<dependency>
<groupId>cn.iocoder.springboot.labs</groupId>
<artifactId>lab-64-grpc-demo-user-service-api</artifactId>
Expand All @@ -45,6 +46,7 @@
<artifactId>spring-boot-starter</artifactId>
</dependency>

<!-- 引入 gRPC Netty 依赖,因为使用它作为网络库 -->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class GrpcConfig {

private final Logger logger = LoggerFactory.getLogger(getClass());

/**
* gRPC Server 端口
*/
private static final Integer GRPC_PORT = 8888;

@Bean
Expand Down
56 changes: 56 additions & 0 deletions lab-64/lab-64-grpc-starter/lab-64-grpc-starter-application/pom.xml
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>
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);
}

}
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();
}

}
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
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>
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);

}
Loading

0 comments on commit 08e5388

Please sign in to comment.