协议
协议简介
什么是协议
Client(Consumer端)与Server(Provider端)在传输数据时双方的约定。
Dubbo3中常见的协议
1.dubbo协议[前面文章中使用的都是dubbo协议]
2.rest协议
3.triple协议
4.grpc协议
5.thirft协议
6.webservice协议
7.rocketmq协议
8.redis协议
9.memacached协议
...等等
不同协议的通信效率对比
dubbo官方提供的不同协议之间通信效率的对比如下:
Dubbo协议
1.Dubbo的默认协议,自己定义的私有协议,基于TCP层,默认的通信方式是Netty4;
2.Dubbo采用单一长连接和NIO异步通讯,适合于小数据量大并发的服务调用,反之,Dubbo缺省协议不适合传送大数据量的服务,比如传文件、传视频等。
官方提供的Dubbo协议结构图:
官方提供的Dubbo协议的帧组成图:
协议构成详见Dubbo2 协议规范 | Apache Dubbo
Dubbo协议的使用
准备——zookeeper的安装
https://archive.apache.org/dist/zookeeper/
如果使用windows,可以7z解压缩软件解压缩tar包的形式
https://sparanoid.com/lab/7z/
修改conf/zoo.cfg配置⽂件
dataDir=D:\\zookeeper\\apache-zookeeper-3.6.1\\data
启动服务端
启动客户端
https://github.com/vran-dev/PrettyZoo/releases
图形化zk的客户端
创建项目
父项目maven文件中对依赖版本进行管理
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<dubbo.version>3.2.0dubbo.version>
<spring-boot.version>2.7.12spring-boot.version>
<spring.version>5.3.25spring.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-bomartifactId>
<version>${dubbo.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring-boot.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
引入相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-dependencies-zookeeper-curator5artifactId>
<version>${dubbo.version}version>
<type>pomtype>
<exclusions>
<exclusion>
<artifactId>zookeeperartifactId>
<groupId>org.apache.zookeepergroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.8.1version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.22version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-expressionartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jclartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
dependencies>
创建api、provider、consumer包,并编写代码。
provider的application.yml配置文件如下:
spring:
application:
name: DUBBO-PROTOCOL-PROVIDER
dubbo:
protocol:
name: dubbo
port: -1
registry:
address: zookeeper://127.0.0.1:2181
consumer的application.yml的配置如下:
spring:
application:
name: DUBBO-PROTOCOL-CONSUMER
dubbo:
registry:
address: zookeeper://127.0.0.1:2181
application:
qos-enable: false
REST协议
Rest协议就是我们平时说的Restful,本质上把他称为协议不准确。因为 Restful他是基于Http1.x协议的。 使⽤Rest协议这种⽅式可以让我们的Dubbo服务直接通过url进⾏访问,同时也可以更好的与SpringCloud技术栈进⾏整合。
搭建开发环境
父项目的pom文件核心如下:
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<dubbo.version>3.2.0dubbo.version>
<spring-boot.version>2.6.11spring-boot.version>
<jackson-version>1.9.13jackson-version>
<resteasy-version>3.15.6.Finalresteasy-version>
<tomcat.version>9.0.75tomcat.version>
<grpc.version>1.54.1grpc.version>
<spring.version>5.3.25spring.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-dependencies-zookeeper-curator5artifactId>
<version>${dubbo.version}version>
<type>pomtype>
<exclusions>
<exclusion>
<artifactId>zookeeperartifactId>
<groupId>org.apache.zookeepergroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.8.1version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.22version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-expressionartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jclartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-rpc-restartifactId>
<version>${dubbo.version}version>
<exclusions>
<exclusion>
<artifactId>jackson-core-aslartifactId>
<groupId>org.codehaus.jacksongroupId>
exclusion>
<exclusion>
<artifactId>jackson-jaxrsartifactId>
<groupId>org.codehaus.jacksongroupId>
exclusion>
<exclusion>
<artifactId>jackson-mapper-aslartifactId>
<groupId>org.codehaus.jacksongroupId>
exclusion>
<exclusion>
<artifactId>jackson-xcartifactId>
<groupId>org.codehaus.jacksongroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.jboss.resteasygroupId>
<artifactId>resteasy-jackson-providerartifactId>
<version>${resteasy-version}version>
<exclusions>
<exclusion>
<artifactId>jackson-core-aslartifactId>
<groupId>org.codehaus.jacksongroupId>
exclusion>
<exclusion>
<artifactId>jackson-jaxrsartifactId>
<groupId>org.codehaus.jacksongroupId>
exclusion>
<exclusion>
<artifactId>jackson-mapper-aslartifactId>
<groupId>org.codehaus.jacksongroupId>
exclusion>
<exclusion>
<artifactId>jackson-xcartifactId>
<groupId>org.codehaus.jacksongroupId>
exclusion>
exclusions>
dependency>
<dependency>
<artifactId>jackson-core-aslartifactId>
<groupId>org.codehaus.jacksongroupId>
<version>${jackson-version}version>
dependency>
<dependency>
<artifactId>jackson-jaxrsartifactId>
<groupId>org.codehaus.jacksongroupId>
<version>${jackson-version}version>
dependency>
<dependency>
<artifactId>jackson-mapper-aslartifactId>
<groupId>org.codehaus.jacksongroupId>
<version>${jackson-version}version>
dependency>
<dependency>
<artifactId>jackson-xcartifactId>
<groupId>org.codehaus.jacksongroupId>
<version>${jackson-version}version>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-bomartifactId>
<version>${dubbo.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring-boot.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
provide pom文件引入的依赖:
<dependencies>
<dependency>
<groupId>com.sjdwzgroupId>
<artifactId>dubbo-protocol-apiartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<exclusions>
<exclusion>
<artifactId>tomcat-embed-coreartifactId>
<groupId>org.apache.tomcat.embedgroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.tomcat.embedgroupId>
<artifactId>tomcat-embed-coreartifactId>
<version>${tomcat.version}version>
dependency>
dependencies>
consumer只需要引入api模块即可。
api模块
java编码
@Path("orders")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({ContentType.APPLICATION_JSON_UTF_8})
public interface OrderService {
@GET
@Path("{id}")
Order getOrder(@PathParam("id") Long id);
}
provider模块
application.yml文件
spring:
application:
name: DUBBO-PROTOCOL-PROVIDER
dubbo:
protocol:
name: rest
port: 9005
server: tomcat
contextpath: sjdwz
registry:
address: zookeeper://127.0.0.1:2181
server:
port: 8090
java代码
@DubboService
public class OrderServiceImpl implements OrderService {
@Override
public Order getOrder(Long id) {
System.out.println("OrderServiceImpl.getOrder id " + id);
return new Order("1001", 100.0);
}
}
运行起来
http://localhost:9005/sjdwz/orders/1
在浏览器输入上述url即可访问到。
consumer模块
java代码
@DubboReference(protocol = "rest")
private OrderService orderService;
@Test
public void test2(){
orderService.getOrder(2L);
}
gRPC协议
-
gRPC简介
Dubbo中的gRPC协议就是应⽤gRPC来替换Dubbo的RPC调⽤。本质上把它称为协议不准确,因为gRPC他是Google开源⼀种异构语⾔通信的RPC框 架。Dubbo引⼊gRPC的⽬的是为了更好的支持云原生的开发。
-
gRPC底层协议
gRPC底层基于Http2.x协议。Http2.x是双工协议,可以支持流式通信(反应式编程)。所以在通信效率上更高。
-
gRPC开发过程
1. 通过Protobuf的IDL定义通信数据,及操作。 2. 通过Maven的插件,根据不同的编程语言生成成对应的代码 3. 服务端发布RPC服务 3.1. ⼀元调⽤ 3.2. 服务端流式RPC 3.3. 客户端流式RPC 3.4. 双向流式RPC 4. 客户端进⾏RPC服务的调⽤ 4.1. 通过stub代理进⾏远端RPC的调⽤
-
Dubbo中gRPC协议的开发
本质上Dubbo对于gRPC协议的⽀持,就是在原有gRPC客户端代码上进行了封装。
-
父项目依赖设置
<properties> <maven.compiler.source>8maven.compiler.source> <maven.compiler.target>8maven.compiler.target> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <dubbo.version>3.1.10dubbo.version> <spring-boot.version>2.7.12spring-boot.version> <grpc.version>1.54.1grpc.version> <os.detected.classifier>windows-x86_64os.detected.classifier> <spring.version>5.3.25spring.version> properties> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starterartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope> dependency> <dependency> <groupId>org.apache.dubbogroupId> <artifactId>dubbo-spring-boot-starterartifactId> dependency> <dependency> <groupId>org.apache.dubbogroupId> <artifactId>dubbo-dependencies-zookeeper-curator5artifactId> <version>${dubbo.version}version> <type>pomtype> <exclusions> <exclusion> <artifactId>zookeeperartifactId> <groupId>org.apache.zookeepergroupId> exclusion> exclusions> dependency> <dependency> <groupId>org.apache.zookeepergroupId> <artifactId>zookeeperartifactId> <version>3.8.1version> dependency> <dependency> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> <version>1.18.22version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-aopartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-beansartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-contextartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-coreartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-expressionartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-jclartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-testartifactId> <version>${spring.version}version> dependency> dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.apache.dubbogroupId> <artifactId>dubbo-bomartifactId> <version>${dubbo.version}version> <type>pomtype> <scope>importscope> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-dependenciesartifactId> <version>${spring-boot.version}version> <type>pomtype> <scope>importscope> dependency> <dependency> <groupId>io.grpcgroupId> <artifactId>grpc-bomartifactId> <version>${grpc.version}version> <type>pomtype> <scope>importscope> dependency> dependencies> dependencyManagement>
api模块的开发
pom文件修改
<dependencies>
<dependency>
<groupId>io.grpcgroupId>
<artifactId>grpc-nettyartifactId>
<exclusions>
<exclusion>
<groupId>io.nettygroupId>
<artifactId>netty-codec-http2artifactId>
exclusion>
<exclusion>
<groupId>io.nettygroupId>
<artifactId>netty-handler-proxyartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>io.grpcgroupId>
<artifactId>grpc-netty-shadedartifactId>
dependency>
<dependency>
<groupId>io.grpcgroupId>
<artifactId>grpc-protobufartifactId>
<exclusions>
<exclusion>
<groupId>com.google.protobufgroupId>
<artifactId>protobuf-javaartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>io.grpcgroupId>
<artifactId>grpc-stubartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-commonartifactId>
dependency>
<dependency>
<groupId>com.google.protobufgroupId>
<artifactId>protobuf-javaartifactId>
<version>3.21.7version>
dependency>
<dependency>
<groupId>com.google.protobufgroupId>
<artifactId>protobuf-java-utilartifactId>
<version>3.21.7version>
<exclusions>
<exclusion>
<groupId>com.google.protobufgroupId>
<artifactId>protobuf-javaartifactId>
exclusion>
exclusions>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>kr.motd.mavengroupId>
<artifactId>os-maven-pluginartifactId>
<version>1.7.1version>
plugin>
<plugin>
<groupId>org.xolstice.maven.pluginsgroupId>
<artifactId>protobuf-maven-pluginartifactId>
<version>0.6.1version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.21.7:exe:${os.detected.classifier}protocArtifact>
<pluginId>grpc-javapluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
pluginArtifact>
<outputDirectory>${basedir}/src/main/javaoutputDirectory>
<clearOutputDirectory>falseclearOutputDirectory>
<protocPlugins>
<protocPlugin>
<id>dubbo-grpcid>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-compilerartifactId>
<version>${dubbo.version}version>
<mainClass>org.apache.dubbo.gen.grpc.DubboGrpcGeneratormainClass>
protocPlugin>
protocPlugins>
configuration>
<executions>
<execution>
<id>protobuf-maven-pluginid>
<goals>
<goal>compilegoal>
<goal>compile-customgoal>
goals>
execution>
executions>
plugin>
plugins>
build>
provider模块的开发
application.yml文件
spring:
application:
name: DUBBO-PROTOCOL-PROVIDER
dubbo:
protocol:
name: grpc
port: -1
registry:
address: zookeeper://127.0.0.1:2181
# port: 8081
Java类
@DubboService
public class DemoServiceImpl extends DubboDemoServiceGrpc.DemoServiceImplBase {
@Override
public void demo(RequestData request, StreamObserver responseObserver) {
System.out.println("DemoServiceImpl.demo " + request.getName());
responseObserver.onNext(ResponseData.newBuilder().setResult("this is result").build());
responseObserver.onCompleted();
}
}
consumer模块的开发
测试Java方法
@DubboReference(protocol = "grpc")
private DubboDemoServiceGrpc.IDemoService iDemoService;
@Test
void test3() {
ResponseData ret = iDemoService.demo(RequestData.newBuilder().setName("xiaohei").build());
System.out.println("ret.getResult() = " + ret.getResult());
}
Triple协议
-
Triple协议是什么
在阿里巴巴,Triple 协议广泛用于跨环境、跨语言、跨生态互通,已有数十万容器生产级使用。 Triple 协议是 Dubbo3设计的基于HTTP的RPC通信协议规范,它完全兼容 gRPC 协议,支持 Request-Response、Streaming流式等通信模型,可同时运行在HTTP/1和 HTTP/2之上。 核心: 1.Dubbo3全新设计的基于Http的RPC协议 2.完全兼容gRPC协议 3.支持Streaming流式通信【与gRPC类似】 4.同时支持HTTP1和HTTP2 5.对比内置的Dubbo协议,更好的支持异构语言通信,且更加适应云原生。
-
Dubbo原有已经支持了gRPC协议,为什么还要做Triple协议?
生态建设︰ 1.如果只支持gRPC协议,不能够体现Dubbo的价值,毕竟gRPC是Google制定的。 Triple的优势: 1.不绑定IDL,支持Java lnterface 定义服务。(gRPC需要使用Protobuf,而Triple可以通过Java接口的方式进行服务的) 2.同时支持Http1和Http2协议,而gRPC只支持Http2协议。 3.100%兼容gRPC。从Dubbo3.2.x开始,已经废弃原有的gRPC协议使用Triple协议进行替代。
Triple协议的开发
父项目中pom文件,dubbo版本切换到3.2.0
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<dubbo.version>3.2.0dubbo.version>
<grpc.version>1.54.1grpc.version>
<spring-boot.version>2.7.12spring-boot.version>
<protoc.version>3.21.7protoc.version>
<os.detected.classifier>windows-x86_64os.detected.classifier>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubboartifactId>
dependency>
<dependency>
<groupId>io.grpcgroupId>
<artifactId>grpc-allartifactId>
<version>${grpc.version}version>
<exclusions>
<exclusion>
<artifactId>grpc-nettyartifactId>
<groupId>io.grpcgroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>com.google.protobufgroupId>
<artifactId>protobuf-javaartifactId>
<version>${protoc.version}version>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-dependencies-zookeeper-curator5artifactId>
<version>${dubbo.version}version>
<type>pomtype>
<exclusions>
<exclusion>
<artifactId>zookeeperartifactId>
<groupId>org.apache.zookeepergroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.8.1version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.22version>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-bomartifactId>
<version>${dubbo.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-dependenciesartifactId>
<version>${spring-boot.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>io.grpcgroupId>
<artifactId>grpc-bomartifactId>
<version>${grpc.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<extensions>
<extension>
<groupId>kr.motd.mavengroupId>
<artifactId>os-maven-pluginartifactId>
<version>1.7.1version>
extension>
extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.pluginsgroupId>
<artifactId>protobuf-maven-pluginartifactId>
<version>0.6.1version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.21.7:exe:${os.detected.classifier}
protocArtifact>
<pluginId>grpc-javapluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
pluginArtifact>
<outputDirectory>${basedir}/src/main/javaoutputDirectory>
<clearOutputDirectory>falseclearOutputDirectory>
<protocPlugins>
<protocPlugin>
<id>dubboid>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-compilerartifactId>
<version>${dubbo.version}version>
<mainClass>org.apache.dubbo.gen.tri.Dubbo3TripleGeneratormainClass>
protocPlugin>
protocPlugins>
configuration>
<executions>
<execution>
<goals>
<goal>compilegoal>
<goal>test-compilegoal>
<goal>compile-customgoal>
<goal>test-compile-customgoal>
goals>
execution>
executions>
plugin>
plugins>
build>
api模块的开发
syntax = "proto3";
option java_multiple_files = true;
option java_package = "com.sjdwz.tri";
package com.sjdwz.tri;
// The request message containing the user's name.
message GreeterRequest {
string name = 1;
}
// The response message containing the greetings
message GreeterReply {
string message = 1;
}
service Greeter{
// unary
rpc greet(GreeterRequest) returns (GreeterReply);
// serverStream
rpc greetServerStream(GreeterRequest) returns (stream GreeterReply);
// bi streaming
rpc greetStream(stream GreeterRequest) returns (stream GreeterReply);
}
在父项目执行上图maven命令。
provider模块的开发
application.yml
spring:
application:
name: DUBBO-TRIPLE-PROVIDER
dubbo:
protocol:
name: tri
port: -1
registry:
address: zookeeper://127.0.0.1:2181
服务接口的开发
@DubboService
public class GreeterImpl extends DubboGreeterTriple.GreeterImplBase {
@Override
public GreeterReply greet(GreeterRequest request) {
System.out.println("GreeterImpl.greet " + request.getName());
GreeterReply greeterReply = GreeterReply.newBuilder().setMessage("this is on method").build();
return greeterReply;
}
@Override
public StreamObserver greetStream(StreamObserver responseObserver) {
System.out.println("GreeterImpl.greetStream");
responseObserver.onNext(GreeterReply.newBuilder().setMessage("bi stream result").build());
responseObserver.onCompleted();
return new StreamObserver() {
@Override
public void onNext(GreeterRequest data) {
System.out.println(" recive request param = " + data.getName());
}
@Override
public void onError(Throwable throwable) {
}
@Override
public void onCompleted() {
System.out.println("GreeterImpl.onCompleted");
}
};
}
@Override
public void greetServerStream(GreeterRequest request, StreamObserver responseObserver) {
System.out.println("GreeterImpl.greetServerStream name " + request.getName());
responseObserver.onNext(GreeterReply.newBuilder().setMessage("this is result").build());
responseObserver.onCompleted();
}
}
consumer模块的开发
测试类的开发
@DubboReference(protocol = "tri")
private Greeter greeter;
@Test
void test2() {
GreeterRequest request = GreeterRequest.newBuilder().setName("xiaowb").build();
GreeterReply greeterReply = greeter.greet(request);
System.out.println("greeterReply.getMessage() = " + greeterReply.getMessage());
}
triple协议——grpc方式的开发
api模块
在api模块的pom文件执行如下命令;
consumer模块
gRPC adaptor类
package com.sjdwz;
import io.grpc.stub.StreamObserver;
public class GrpcStreamObserverAdapter implements StreamObserver {
private final org.apache.dubbo.common.stream.StreamObserver delegate;
public GrpcStreamObserverAdapter(org.apache.dubbo.common.stream.StreamObserver delegate) {
this.delegate = delegate;
}
@Override
public void onNext(T value) {
delegate.onNext(value);
}
@Override
public void onError(Throwable t) {
delegate.onError(t);
}
@Override
public void onCompleted() {
delegate.onCompleted();
}
}
Test类
@Test
void test4() {
ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost", 50051).usePlaintext().build();
try {
//一元操作
GreeterGrpc.GreeterBlockingStub greeterBlockingStub = GreeterGrpc.newBlockingStub(managedChannel);
//流式处理
GreeterGrpc.GreeterStub greeterStub = GreeterGrpc.newStub(managedChannel);
GreeterRequest request = GreeterRequest.newBuilder().setName("grpc").build();
greeterStub.greetServerStream(request, new GrpcStreamObserverAdapter<>(new StreamObserver() {
@Override
public void onNext(GreeterReply data) {
System.out.println("TestTripleDemo.onNext " + data.getMessage());
}
@Override
public void onError(Throwable throwable) {
}
@Override
public void onCompleted() {
System.out.println("TestTripleDemo.onCompleted");
}
}));
System.in.read();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
managedChannel.shutdown();
}
}
triple协议——Java Interface的方式
需要记住两个规律:
1. message替换成Java类型
2. StreamObserver也用StreamObserver处理