SpringBoot 集成 Dubbo 启用 gRPC 协议

本文记录下SpringBoot集成Dubbo启用gRPC协议,以及与原生 gRPC 在代码编写过程中的区别。

前言

Dubbo 在 2.7.5 版本开始支持原生 gRPC 协议,对于计划使用 HTTP/2 通信或者期望 gRPC 协议支持服务治理能力的,都可以考虑接入 Dubbo 体系启用 gRPC 协议。

由于官网给的 代码示例 是基于 spring,现在基本上都是基于SpringBoot开发,所以本文提供一下 SpringBoot 的代码示例。

此外还会简单说明 Dubbo 支持的原生 gRPC 协议与原生 gRPC 协议在代码开发时的区别。

如果对gRPC协议不了解的,后续文章会有更新,请持续关注。

项目结构

根据现在微服务开发的常见方式,先搭建一个项目,结构如下

2e30cfc4cb8addbd729250e45f948c47.png

这样的项目结构可以将服务的声明和实现隔离开,如果有 client 调用,直接添加api module 的依赖即可。

代码示例

项目结构确定好后需要做三件事

  • 在项目中需要用到 grpc 和 dubbo 相关依赖,所以在父工程中的 pom.xml 文件添加两者的 BOM。

  • gRPC 支持的序列化协议为 protobuf,我们在 api module 下添加 gRPC 所需依赖、插件以及 proto IDL文件。

  • 在 service module 添加相关配置并进行 api service 的实现。

详细代码如下:

父工程

父工程中的 pom.xml 文件添加 grpc 和 dubbo 的 BOM。

pom.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.demo</groupId>
    <artifactId>nava</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>nava</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <dubbo.version>3.1.7</dubbo.version>
        <grpc.version>1.44.1</grpc.version>
        <spring-boot.version>2.6.11</spring-boot.version>
    </properties>

    <modules>
        <module>nava-api</module>
        <module>nava-service</module>
    </modules>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.grpc</groupId>
                <artifactId>grpc-bom</artifactId>
                <version>${grpc.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-bom</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.demo</groupId>
                <artifactId>nava-api</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.demo</groupId>
                <artifactId>nava-service</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

api module

在 api module 中的 pom.xml 文件添加 dubbo 、gRPC 所需依赖、插件。

pom.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.demo</groupId>
        <artifactId>nava</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <artifactId>nava-api</artifactId>
    <name>nava-api</name>
    <description>api 模块,对外提供的 API</description>
    <dependencies>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-netty</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.netty</groupId>
                    <artifactId>netty-codec-http2</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>io.netty</groupId>
                    <artifactId>netty-handler-proxy</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-netty-shaded</artifactId>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-protobuf</artifactId>
        </dependency>
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-stub</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-common</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.7.1</version>
                <executions>
                    <execution>
                        <id>os-maven-plugin</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>detect</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.6.1</version>
                <configuration>
                    <protocArtifact>com.google.protobuf:protoc:3.7.1:exe:${os.detected.classifier}</protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
                    <protocPlugins>
                        <protocPlugin>
                            <id>dubbo-grpc</id>
                            <groupId>org.apache.dubbo</groupId>
                            <artifactId>dubbo-compiler</artifactId>
                            <version>0.0.1</version>
                            <mainClass>org.apache.dubbo.gen.grpc.DubboGrpcGenerator</mainClass>
                        </protocPlugin>
                    </protocPlugins>
                </configuration>
                <executions>
                    <execution>
                        <id>protobuf-maven-plugin</id>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

在main文件夹下面创建proto文件夹,以及 DemoService.proto 文件。

9922671f18cd3d0fa0b2ec23915a494f.png

DemoService.proto

syntax = "proto3";

option java_multiple_files = true;
option java_package = "com.demo.nava";
option java_outer_classname = "DemoServiceProto";
option objc_class_prefix = "DSP";

// The greeting service definition.
service DemoService {
  // Sends a greeting
  rpc service (RequestData) returns (ResponseData) {}
}

// The request message containing the user's name.
message RequestData {
  string name = 1;
}

// The response message containing the greetings
message ResponseData {
  string message = 1;
}

service module

在 service module 中的 pom.xml 文件添加 api module 的依赖以及 dubbo 其他依赖。

pom.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.demo</groupId>
        <artifactId>nava</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <artifactId>nava-service</artifactId>
    <name>nava-service</name>
    <description>service 模块,存放核心业务逻辑代码</description>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.demo</groupId>
            <artifactId>nava-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-nacos</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.6.11</version>
                <configuration>
                    <mainClass>com.demo.nava.NavaApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

在 application.properties 文件中添加 dubbo 相关配置

application.properties

# 设置dubbo传输协议
dubbo.protocol.name=grpc
dubbo.protocol.port=-1
# dubbo nacos注册中心说明 https://cn.dubbo.apache.org/zh-cn/overview/mannual/java-sdk/reference-manual/registry/nacos/
dubbo.registry.address: nacos://nacos:nacos@${nacos.address:127.0.0.1}:8848

在 SpringBoot 启动类添加 @EnableDubbo 注解

45e015014def6590d05c3f0c9370ee68.png

添加 DemoServiceImpl 实现类进行业务编码。

DemoServiceImpl.java

package com.demo.nava.service;

import com.demo.nava.DubboDemoServiceGrpc;
import com.demo.nava.RequestData;
import com.demo.nava.ResponseData;
import io.grpc.stub.StreamObserver;
import org.apache.dubbo.config.annotation.DubboService;

@DubboService
public class DemoServiceImpl extends DubboDemoServiceGrpc.DemoServiceImplBase implements DubboDemoServiceGrpc.IDemoService {

    @Override
    public void service(RequestData request, StreamObserver<ResponseData> responseObserver) {
        ResponseData reply = ResponseData.newBuilder().setMessage("Hello " + request.getName()).build();
        responseObserver.onNext(reply);
        responseObserver.onCompleted();
    }
}

注意事项

经过以上的步骤,一个简单的 SpringBoot 集成 Dubbo 启用 gRPC 协议的示例就完成了。这个时候直接启动项目是会报错的,因为protobuf相关的代码还没生成,我们需要对项目进行 maven install 以及 maven reload 操作。

maven install 的目的是为了生成protobuf相关代码,这个时候我们可以在 target 中看到

c0b3324f0509c33268549250147a9011.png

maven reload 的目的是为了更新加载 pom.xml 文件,从而将 api module 中生成的代码加载到 service module

操作后就可以成功启动项目了。

区别

在项目启动成功后可以回头看下 Dubbo 支持的原生 gRPC 与原生 gRPC 在代码编写过程中的区别

1.maven plugin 的区别,dubbo 在原先的基础上添加了 dubbo-grpc 的 plugin,目的是生成扩展的代码做到对 grpc 的支持。

0fbbb4c0cf56ea6de85684172863426c.png

对应生成的代码如下

cb1f03d0e5f637bca736e0910b4abf42.png

2.service 实现区别,dubbo-grpc 的 plugin 生成了 dubbo 相关的 protobuf 的代码,所以在实现上有所区别。

9c63e938b3619be33874f8835572caf9.png

3.RPC 调用区别,因为 grpc 接入了 dubbo 体系,所以使用 Dubbo 风格,基于接口的编程来定义和使用远程服务。

1f543d7302a477793997d36e052ea634.png

来源:blog.csdn.net/qq_28314431/

article/details/130090121

热门内容:
美团动态线程池,真香!

Controller层代码就该这么写,简洁又优雅!
 
 

c832cc62023cc823524b502643f7f4de.jpeg

 
 
最近面试BAT,整理一份面试资料《Java面试BAT通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。
获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。

明天见(。・ω・。)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值