使用工具
idea + gradle
项目构建
创建空的父项目
注意此处使用gradle进行项目构建, 选择war,选择gradle,此处我们创建多模块项目,所以父项目并不需要src等目录,因为是笔者刚开始使用gradle,所以几乎每一步都是小心翼翼
此时完成了一个空项目,里面具有两个文件, settings.gradle和build.gradle
配置子项目的仓库地址, 在build.gradle中添加如下代码
subprojects {
repositories {
maven {
url "https://maven.aliyun.com/repository/central"
}
}
}
创建lib子项目
上一篇文章我基于gradle构建了一个java项目并且根据proto生成了java文件,此处需要引用上篇中生成的文件
新建grpc-lib子项目
同理创建grpc-server和grpc-client,都是springboot-web项目,项目建成以后的目录如下图:
项目结构搭建完成,接下来完成基于springboot的grpc实现
1:springboot-grpc-demo/settings.gradle中配置,完成整合,表示这三个Module是属于同一个项目
rootProject.name = 'sprignboot-grpc-demo'
include 'grpc-lib',"grpc-server","grpc-client"
2:springboot-grpc-demo/build.gradle中为子项目做一些配置,此处指定maven仓库,也可以选择不指定
group 'com.example'
version '1.0-SNAPSHOT'
subprojects {
repositories {
mavenLocal()
maven {
url "https://maven.aliyun.com/repository/central"
}
mavenCentral()
}
}
3:grpc-lib中引入所需依赖
plugins {
id 'java'
}
group 'com.example.springboot-grpc-demo'
version '1.0-SNAPSHOT'
archivesBaseName="grpc-lib"
sourceCompatibility = 1.8
// 引入依赖
dependencies {
compile "io.grpc:grpc-netty:1.10.0"
compile "io.grpc:grpc-protobuf:1.10.0"
compile "io.grpc:grpc-stub:1.10.0"
}
4:配置grpc-server的项目依赖等(grpc-server/build.gradle)
buildscript {
ext {
springBootVersion = '2.0.5.RELEASE'
}
repositories {
mavenLocal()
maven{
url "https://maven.aliyun.com/repository/central"
}
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'
group 'com.example.springboot-grpc-demo'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
archivesBaseName="grpc-server"
repositories {
mavenLocal()
maven{
url "https://maven.aliyun.com/repository/central"
}
mavenCentral()
}
configurations {
providedRuntime
}
dependencies {
//引入grpc-lib模块
compile project(":grpc-lib")
//引入grpc-server所需依赖,springboot集成的
compile 'net.devh:grpc-server-spring-boot-starter:1.4.0.RELEASE'
compile('org.springframework.boot:spring-boot-starter-web')
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
}
5:grpc-client配置(grpc-client/build.gradle)
buildscript {
ext {
springBootVersion = '2.0.5.RELEASE'
}
repositories {
mavenLocal()
maven{
url "https://maven.aliyun.com/repository/central"
}
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: "idea"
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'
group 'com.example.springboot-grpc-demo'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
archivesBaseName="grpc-client"
configurations {
providedRuntime
}
repositories {
mavenLocal()
maven{
url "https://maven.aliyun.com/repository/central"
}
mavenCentral()
}
dependencies {
// 引入grpc-lib
compile project(":grpc-lib")
// 此处和上面server配置中不一样哦
compile 'net.devh:grpc-client-spring-boot-starter:1.4.0.RELEASE'
compile('org.springframework.boot:spring-boot-starter-web')
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
}
项目搭建及配置完成,终于要开始写代码了,吼吼
grpc实现
1:按照上篇文章中的内容,命令行输入 gradle compileJava完成对hello.proto的编译
2:编写grpc-server,新建service/HelloService.java (interface) 、service/impl/HelloServiceImpl.java
HelloServiceImpl代码
@GrpcService(HelloServiceGrpc.class)
public class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase implements HelloService {
private static final Logger LOG = LoggerFactory.getLogger(HelloServiceImpl.class);
@Override
public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
String name = request.getName();
LOG.info("received client name is " + name);
HelloResponse response = HelloResponse.newBuilder().setCode("0000").setMessage("hello "+ name).build();
responseObserver.onNext(response);
responseObserver.onCompleted();
}
}
3:编写grpc-client,新建service/HelloService.java (interface) 、service/impl/HelloServiceImpl.java 、controller\HelloController
因为client也需要提供接口给前端访问,所以就需要controller
(1)HelloService.java代码
public interface HelloService {
//此处没有深入做封装,简单演示
Map getGreeting (String name);
}
(2)HelloServiceImpl代码
//方便大家观察,把导入的包也放上来
import com.example.grpcclient.service.HelloService;
import com.geek.grpc.lib.hello.HelloRequest;
import com.geek.grpc.lib.hello.HelloResponse;
import com.geek.grpc.lib.hello.HelloServiceGrpc;
import io.grpc.Channel;
@Service
public class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase implements HelloService {
@GrpcClient("grpc_local_server")
private Channel channel;
@Override
public Map getGreeting(String name) {
HelloServiceGrpc.HelloServiceBlockingStub blockingStub = HelloServiceGrpc.newBlockingStub(channel);
HelloResponse response = blockingStub.sayHello(HelloRequest.newBuilder().setName(name).build());
Map map = new HashMap();
map.put("code", response.getCode());
map.put("msg", response.getMessage());
return map;
}
}
(3)HelloController代码
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@GetMapping("/hello")
public Map sayHello(String name){
return helloService.sayHello(name);
}
}
完成以上步骤,代码编写完成,本文不做代码详细释义,具体参考官网API文档
项目启动和准备调用配置
grpc-server/src/main/resources/application.properties(熟悉springboot的猿和媛就可以用自己的配置了)
server.port=8081
spring.application.name=grpc-local-server
grpc.server.port=9898
grpc-client/src/main/resources/application.properties
server.port=8082
spring.application.name=grpc-local-client
grpc.client.grpc_local_server.host=127.0.0.1
grpc.client.grpc_local_server.port=9898
grpc.client.grpc_local_server.enableKeepAlive=true
grpc.client.grpc_local_server.keepAliveWithoutCalls=true
启动调用
启动grpc-client和grpc-server,然后浏览器中访问http://localhost:8082/hello?name=World
同时,我们查看grpc-server控制台