赞
踩
创建一个基础的spring boot项目
<properties>
<java.version>1.8</java.version>
<os-maven-plugin.version>1.6.1</os-maven-plugin.version>
<protobuf-maven-plugin.version>0.6.1</protobuf-maven-plugin.version>
<grpc-client-spring-boot-starter>2.4.0.RELEASE</grpc-client-spring-boot-starter>
</properties>
<dependencies>
<dependency>
<groupId>net.devh</groupId>
<artifactId>grpc-client-spring-boot-starter</artifactId>
<version>${grpc-client-spring-boot-starter}</version>
</dependency>
<!-- 方便测试 搞一个web服务-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<!--grpc proto文件编译插件-->
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>${os-maven-plugin.version}</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<!--grpc proto文件编译插件-->
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>${protobuf-maven-plugin.version}</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.16.1:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
grpc:
client:
GLOBAL:
negotiation-type: plaintext
# 修改客户端端默认入参最大大小,默认值为4M ,这里修改为20M 20*1024*1024
max-inbound-message-size: 20971520
# 客户端指定连接服务端地址
address: 'static://127.0.0.1:8989'
在main文件夹下创建proto文件。
syntax = "proto3";
package com.example.grpc;
option java_multiple_files = true;
option java_package = "com.example.grpc.lib";
option java_outer_classname = "HelloWorldProto";
// The greeting service definition.
service MyService {
// 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;
}
如果出现编译异常问题。有可能是yaml文件编码问题,更改成utf-8编码就可以了。
/**
* grpc 客户端的名称
*/
@GrpcClient(value = “test-client”)
private MyServiceGrpc.MyServiceBlockingStub simpleStub;
其中Mysercie是你.proto中的service 。
/**
* @author dzp
* @date 2022/5/29
*/
@Service
public class MyGrpcClientService {
/**
* grpc 客户端的名称
*/
@GrpcClient(value = "test-client")
private MyServiceGrpc.MyServiceBlockingStub simpleStub;
public String sendMessage(final String name) {
try {
final HelloReply response = simpleStub.sayHello(HelloRequest.newBuilder().setName(name).build());
return response.getMessage();
} catch (final StatusRuntimeException e) {
return "FAILED with " + e.getStatus().getCode().name();
}
}
}
在grpc-client中编写一个简单接口。
创建一个controller文件夹,编写一个TestController.java
@RestController
public class TestController {
@Resource
private MyGrpcClientService testService;
@GetMapping("/test")
public Object test(String name){
return testService.sendMessage(name);
}
}
分别启动server端和client端,在使用postman进行调用测试。
调用 成功
到了这一步,grpc简单使用已经完成。最后我们只需要让甲方提供给 我们proto文件夹 进行编译下, 然后编写我们的业务代码就可以了。
java交流群: 868794080
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。