【橘子分布式】gRPC(编程篇-中)

一、简介

我们之前已经完成了对于api模块的开发,也就是已经生成了基础的类和对应的接口,现在我们需要完成的是client和server端的开发。其实如同thrift一样,现在要做的就是实现我们之前定义的service里面的hello方法,里面写我们的业务逻辑,然后通过grpc的server发布暴露出去给客户端使用。

// 定义服务
service HelloService{
 /* 简单rpc,参数为HelloRequest类型,返回类型为HelloResponse */
 rpc hello(HelloRequest) returns (HelloResponse){}
}

ok,我们就先来实现serve模块,然后再实现client模块。

二、server模块

我们创建一个新的模块叫做rpc-grpc-service,并且在其pom依赖中引入api这个公共模块。

<?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>
    <parent>
        <groupId>com.levi</groupId>
        <artifactId>rpc</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>rpc-grpc-service</artifactId>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.levi</groupId>
            <artifactId>rpc-grpc-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

1、业务实现

ok,我们前面说过了,我们其实要实现的业务接口或者说重写的接口实现方法是HelloServiceGrpc.HelloServiceImplBase这个Base的内部类,在这个里面我们覆盖我们的业务方法。
我们重新来回顾一下我们的这个接口和方法。

// 定义请求接口参数
message HelloRequest{
  string name = 1;
}

// 定义接口响应参数
message HelloResponse{
  string result = 1;
}

// 定义服务
service HelloService{
  /* 简单rpc,参数为HelloRequest类型,返回类型为HelloResponse */
  rpc hello(HelloRequest) returns (HelloResponse){}
}

我们看到我们的请求类里面是一个参数name,响应类里面是一个参数result,并且接口的方法叫做hello。ok,我们就来实现覆盖这个方法。

/ 服务端实现类
public class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase {
   
   

    /*
      1. 接受client提交的参数  request.getParameter()
      2. 业务处理 service+dao 调用对应的业务功能。
      3. 提供返回值
     */
    @Override
    public void hello(HelloProto.HelloRequest request, StreamObserver<HelloProto.HelloResponse> responseObserver) {
   
   
       
    }
}
因为HelloServiceGrpc.HelloServiceImplBase不是一个接口,所以我们要继承然后覆盖方法。并且我们看到这个方法和我们当初定义的略有不同
第一个参数没毛病就是HelloProto.HelloRequest,但是这个方法没有返回值,他是个void。这就是grpc的规范,他的返回是通过第二个参数
StreamObserver<HelloProto.HelloResponse> responseObserver来给客户端返回的,因为grpc有流式的返回,所以它是通过这个返回的,
如果弄成返回值就不方便以流的形式不断的推给客户端了。而且responseObserver的泛型就是我们定义的返回类型HelloProto.HelloResponse

于是我们就来实现这个方法。

/ 服务端实现类
public class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase {
   
   

    /*
      1. 接受client提交的参数  request.getParameter()
      2. 业务处理 service+dao 调用对应的业务功能。
      3. 提供返回值
     */
    @Override
    public void hello(HelloProto.HelloRequest request, StreamObserver<HelloProto.HelloResponse> responseObserver) {
   
   
        //1.接受client的请求参数,获取我们定义的那个属性name
        String name = request.getName();
        //2.业务处理
        System.out.println("name parameter "+name);
<think>我们遇到了一个Maven依赖冲突问题,具体是io.grpc:grpc-core的版本冲突。错误信息显示有多个依赖要求不同版本的grpc-core(1.42.1和1.42.2)。为了解决这个问题,我们需要在pom.xml中显式指定一个统一的版本。 解决方案: 1. 在<properties>中定义grpc.version属性,并设置为1.42.2(因为从错误看,多数依赖要求1.42.2)。 2. 确保所有grpc相关的依赖都使用这个属性定义的版本。 3. 使用Maven的依赖管理(dependencyManagement)来统一管理版本。 但是,由于我们使用的是Spring Boot父pom,它已经管理了一些依赖版本,但可能没有管理grpc。因此,我们可以通过dependencyManagement来覆盖。 具体步骤: 修改pom.xml: - 在<properties>中设置grpc.version为1.42.2(或者1.42.1,但需要看哪个版本与其他依赖兼容,根据错误信息,建议使用1.42.2,因为多个传递依赖要求1.42.2)。 - 在<dependencyManagement>中添加grpc相关依赖的版本管理。 注意:由于我们使用了grpc-spring-boot-starter,它可能也管理了grpc版本,但错误显示它内部依赖了1.42.2和1.42.1,所以我们需要显式统一。 示例配置: 在<dependencyManagement>中添加如下依赖管理: ```xml <dependencyManagement> <dependencies> <!-- 统一管理grpc依赖版本 --> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-bom</artifactId> <version>${grpc.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ``` 同时,确保在<properties>中设置了grpc.version: ```xml <properties> <grpc.version>1.42.2</grpc.version> </properties> ``` 这样,所有来自grpc-bom的依赖都会使用1.42.2版本。 完整示例: 修改后的pom.xml部分: ```xml <project> <!-- ... 其他部分 ... --> <properties> <java.version>1.8</java.version> <!-- 设置grpc版本为1.42.2 --> <grpc.version>1.42.2</grpc.version> <protobuf.version>3.19.4</protobuf.version> <grpc-spring-boot.version>2.13.1.RELEASE</grpc-spring-boot.version> <os-maven-plugin.version>1.7.0</os-maven-plugin.version> <protobuf-maven-plugin.version>0.6.1</protobuf-maven-plugin.version> </properties> <dependencyManagement> <dependencies> <!-- 导入grpc的BOM以统一版本 --> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-bom</artifactId> <version>${grpc.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- ... 其他依赖 ... --> <!-- grpc依赖不再指定版本,由BOM管理 --> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-stub</artifactId> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-protobuf</artifactId> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-netty-shaded</artifactId> </dependency> <!-- ... 其他依赖 ... --> </dependencies> <!-- ... 构建配置 ... --> </project> ``` 解释: 1. 我们通过导入grpc-bom(Bill of Materials)来管理所有grpc相关依赖的版本,这样所有grpc依赖的版本都会统一为我们在<properties>中设置的${grpc.version}(即1.42.2)。 2. 在<dependencies>中,我们不再为grpc依赖指定版本,因为BOM会提供版本。 3. 这样,Maven在解析依赖时就会使用统一的版本,避免冲突。 注意:如果项目中还有其他依赖引入了不同版本的grpc,现在也会被强制升级或降级到1.42.2。如果这个版本与其他依赖不兼容,可能需要调整版本号(例如,尝试1.42.1)或者排除冲突的传递依赖。 如果问题仍然存在,可以尝试使用mvn dependency:tree命令查看依赖树,确认是否还有其他依赖引入了不同版本的grpc-core。如果有,可以在相应的依赖中排除: ```xml <dependency> <groupId>some.group</groupId> <artifactId>some-artifact</artifactId> <version>1.0</version> <exclusions> <exclusion> <groupId>io.grpc</groupId> <artifactId>grpc-core</artifactId> </exclusion> </exclusions> </dependency> ``` 重新构建项目,应该可以解决版本冲突问题。 §§相关问题§§: 1. 如何通过Maven命令查看项目的依赖树? 2. 什么是Maven的BOM(Bill of Materials)?它是如何工作的? 3. 除了使用BOM,还有哪些方法解决Maven依赖冲突? 4. 在Spring Boot项目中,如何覆盖父pom管理的依赖版本?</think>这个错误表明 Maven 依赖解析中存在版本冲突,特别是 `io.grpc:grpc-core` 的不同版本要求(1.42.1 和 1.42.2)。以下是完整的解决方案: ### 解决方案:统一 gRPC 版本 #### 1. 修改 `pom.xml` 添加依赖管理 ```xml <properties> <!-- 统一所有 gRPC 相关依赖版本 --> <grpc.version>1.42.2</grpc.version> <protobuf.version>3.19.4</protobuf.version> <grpc-spring-boot.version>2.13.1.RELEASE</grpc-spring-boot.version> </properties> <dependencyManagement> <dependencies> <!-- 导入 gRPC BOM 统一管理版本 --> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-bom</artifactId> <version>${grpc.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- 统一管理 grpc-spring-boot-starter 相关依赖 --> <dependency> <groupId>net.devh</groupId> <artifactId>grpc-spring-boot-starter</artifactId> <version>${grpc-spring-boot.version}</version> </dependency> </dependencies> </dependencyManagement> ``` #### 2. 更新依赖声明 ```xml <dependencies> <!-- gRPC 依赖 --> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-stub</artifactId> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-protobuf</artifactId> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-netty-shaded</artifactId> </dependency> <!-- Spring Boot gRPC 集成 --> <dependency> <groupId>net.devh</groupId> <artifactId>grpc-spring-boot-starter</artifactId> </dependency> </dependencies> ``` #### 3. 完整 `pom.xml` 示例 ```xml <?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.example</groupId> <artifactId>grpc-spring-demo</artifactId> <version>1.0.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.14.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> <grpc.version>1.42.2</grpc.version> <protobuf.version>3.19.4</protobuf.version> <grpc-spring-boot.version>2.13.1.RELEASE</grpc-spring-boot.version> <os-maven-plugin.version>1.7.0</os-maven-plugin.version> <protobuf-maven-plugin.version>0.6.1</protobuf-maven-plugin.version> </properties> <dependencyManagement> <dependencies> <!-- 统一 gRPC 核心库版本 --> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-bom</artifactId> <version>${grpc.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- 统一 grpc-spring-boot 相关库版本 --> <dependency> <groupId>net.devh</groupId> <artifactId>grpc-spring-boot-starter</artifactId> <version>${grpc-spring-boot.version}</version> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- Spring Boot Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- gRPC 依赖 --> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-stub</artifactId> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-protobuf</artifactId> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-netty-shaded</artifactId> </dependency> <!-- Spring Boot gRPC 集成 --> <dependency> <groupId>net.devh</groupId> <artifactId>grpc-spring-boot-starter</artifactId> </dependency> </dependencies> <build> <extensions> <extension> <groupId>kr.motd.maven</groupId> <artifactId>os-maven-plugin</artifactId> <version>${os-maven-plugin.version}</version> </extension> </extensions> <plugins> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>${protobuf-maven-plugin.version}</version> <configuration> <protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact> <pluginId>grpc-java</pluginId> <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>compile-custom</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ``` ### 问题分析 1. **版本冲突原因**: - `grpc-netty-shaded:1.42.1` 要求 `grpc-core:1.42.1` - `grpc-spring-boot-starter:2.13.1.RELEASE` 要求 `grpc-core:1.42.2` - 两者版本不兼容导致冲突 2. **解决方案核心**: - 使用 `<dependencyManagement>` 统一所有 gRPC 相关依赖版本 - 导入 `grpc-bom` 管理所有 gRPC 组件的版本 - 强制使用较新的 `1.42.2` 版本(兼容两个依赖) 3. **为什么选择 1.42.2**: - 向后兼容 1.42.1 的功能 - `grpc-spring-boot-starter` 明确要求 1.42.2 - 包含更多错误修复和安全更新 ### 验证解决方案 1. 运行 Maven 依赖树分析: ```bash mvn dependency:tree -Dincludes=io.grpc:grpc-core ``` 输出应显示统一版本: ``` [INFO] +- io.grpc:grpc-netty-shaded:jar:1.42.2:compile [INFO] | \- io.grpc:grpc-core:jar:1.42.2:compile [INFO] \- net.devh:grpc-spring-boot-starter:jar:2.13.1.RELEASE:compile [INFO] \- io.grpc:grpc-services:jar:1.42.2:compile [INFO] \- io.grpc:grpc-core:jar:1.42.2:compile ``` 2. 清理并重新构建项目: ```bash mvn clean install ``` ### 备选方案:显式排除冲突依赖 如果上述方案无效,可以显式排除冲突依赖: ```xml <dependency> <groupId>net.devh</groupId> <artifactId>grpc-spring-boot-starter</artifactId> <version>${grpc-spring-boot.version}</version> <exclusions> <exclusion> <groupId>io.grpc</groupId> <artifactId>grpc-core</artifactId> </exclusion> <exclusion> <groupId>io.grpc</groupId> <artifactId>grpc-services</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-services</artifactId> <version>${grpc.version}</version> </dependency> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值