Grpc+Dubbo+Spring Boot整合终于打通
最近看到了一篇好文<Dubbo 在跨语言和协议穿透性方向的探索:支持 HTTP/2 gRPC>
文章地址如下:https://mp.weixin.qq.com/s?__biz=MzU4NzU0MDIzOQ==&mid=2247488245&idx=2&sn=b59960420b4e7ae65bb813d4215601a2&chksm=fdeb2095ca9ca983a8da5379b29191c7cba256aaf287966d419d25a9606187805249c8988d63&scene=27#wechat_redirect
于是就想到了将Grpc、Dubbo、以及Spring Boot整合。这样就可以通过Java就可以完成RPC协议是Grpc服务的治理。结合Sring Boot 也能方便开发。
官方的Demo是基于Spring的,看遍了sample包里面的所有项目,也没有找到合适的。于是决定自己试一试,研究2天终于打通。特来分享
Demo项目结构如下:
dubbo-api:用于编译proto文件以及Dubbo的服务接口,项目所需依赖也在这个项目里面。
dubbo-consumer:Dubbo服务订阅
dubbo-provider:Dubbo生产者
项目依赖
api依赖如下:
<java.version>1.8</java.version>
<dubbo.version>2.7.7</dubbo.version>
<grpc.version>1.25.0</grpc.version>
<source.level>1.8</source.level>
<target.level>1.8</target.level>
<dubbo.compiler.version>0.0.1</dubbo.compiler.version>
<dependencies>
<!--
Grpc相关依赖
-->
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>${grpc.version}</version>
</dependency>
<!--
apache官方Demo的probuf工具类
-->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>3.6.1</version>
</dependency>
<!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.7</version>
</dependency>
</dependencies>
<!--
apache官方Demo提供的支持Dubbo的proto文件生成工具插件
-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.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>
<outputDirectory>src/main/java</outputDirectory>
<clearOutputDirectory>false</clearOutputDirectory>
<protocPlugins>
<protocPlugin>
<id>dubbo-grpc</id>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-compiler</artifactId>
<version>${dubbo.compiler.version}</version>
<mainClass>org.apache.dubbo.gen.grpc.DubboGrpcGenerator</mainClass>
</protocPlugin>
</protocPlugins>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
<goal>test-compile</goal>
<goal>test-compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.1</version>
</extension>
</extensions>
</build>
dubbo-consumer、dubbo-provider项目只需要把dubbo-api项目依赖进去就可以了。
proto文件
需要在main目录下新建proto目录,创建hello.proto文件,内容如下:
//声明proto语法版本
syntax = “proto3”;
option java_multiple_files = true;
//声明生成文件包路径
option java_package = “com.kd.wyq.api.service”;
option java_outer_classname = “HelloWorldProto”;
option objc_class_prefix = “WYQ”;
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user’s name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
根据proto文件生成java相关grpc接口文件
依次点击compile、compile-custom即可。
生成文件列表如下:
有了Grpc相关接口文件,需要编写接口实现类。
dubbo-provider配置文件内容如下:
dubbo:
protocol:
id: grpc
name: grpc
registry:
address: zookeeper://localhost:2181
scan:
basePackages: com.kd.wyq.dubboprovider.service
application:
id: dubbo-provider
name: dubbo-provider
Grpc接口实现代码如下:
package com.kd.wyq.dubboprovider.service.impl;
import com.kd.wyq.api.DubboGreeterGrpc;
import com.kd.wyq.api.HelloReply;
import com.kd.wyq.api.HelloRequest;
import io.grpc.stub.StreamObserver;
import org.apache.dubbo.config.annotation.DubboService;
//通过DubboService注解声明为Dubbo的服务生产者,该类需要继承 DubboGreeterGrpc.GreeterImplBase并实现
//DubboGreeterGrpc.IGreeter,否则会报ClassNotFoundMethods异常。
@DubboService
public class HelloServiceImpl extends DubboGreeterGrpc.GreeterImplBase implements DubboGreeterGrpc.IGreeter {
@Override
public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
System.out.println("Received request from client.");
System.out.println("Executing thread is " + Thread.currentThread().getName());
HelloReply reply = HelloReply.newBuilder()
.setMessage("Hello " + request.getName()).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}
直接启动spring boot启动类即可完成Dubbo服务注册。
通过Dubbo-Consumer模块,完成服务订阅
配置文件内容如下:
dubbo:
application:
id: dubbo-consumer
name: dubbo-consumer
registry:
address: zookeeper://localhost:2181
scan:
basePackages: com.kd.wyq.dubboconsumer.controller
protocol:
id: grpc
name: grpc
server:
port: 8888
代码如下:
package com.kd.wyq.dubboconsumer.controller;
import com.kd.wyq.api.DubboGreeterGrpc;
import com.kd.wyq.api.HelloReply;
import com.kd.wyq.api.HelloRequest;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
//通过DubboReference注解声明为服务的订阅者,需要注意的是注入的是DubboGreeterGrpc.IGreeter接口。
@DubboReference
DubboGreeterGrpc.IGreeter greeter;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String hello(){
HelloReply reply = greeter.sayHello(HelloRequest.newBuilder().setName("world!").build());
System.out.println("Result: " + reply.getMessage());
return reply.getMessage();
}
}
通过Spring Boot启动类启动,即可完成服务的订阅。
访问localhost:8888/hello即可查看。
项目整体架构是基于Spring Boot的,通过Dubbo完成服务的注册,订阅。
并且使用的rpc框架为GRPC框架。
这样就方便完成对使用GRPC框架的Java服务的治理,为以后的负载均衡,服务熔断,限流降级等提供方便的条件。
作为刚步入工作没多久的小白,虽然目的简单粗暴,但是完成的那刻还是很兴奋的。
希望大家看后可以一起讨论,虚心请教。