.Net Grpc Client调用Java Grpc Server
1. Java Grpc Server
参考地址:
1.新建SpringBoot项目[略]
2.POM引入相关依赖
<dependencies>
<dependency>
<groupId>io.github.lognet</groupId>
<artifactId>grpc-spring-boot-starter</artifactId>
<version>5.1.1</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.23.0</version>
</dependency>
</dependencies>
<build>
<extensions>
<!-- 检测当前系统信息的工具-->
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.7.1</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<configuration>
<!-- 通过系统信息的工具变量获取操作系统的版本 -->
<protocArtifact>com.google.protobuf:protoc:3.23.0:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.55.1:exe:${os.detected.classifier}</pluginArtifact>
<!--默认值,proto源文件路径-->
<protoSourceRoot>${project.basedir}/src/main/java/cn/coreqi/dapr_server/proto</protoSourceRoot>
<!--默认值,proto目标java文件路径-->
<outputDirectory>${project.basedir}/src/main/java</outputDirectory>
<!--设置是否在生成java文件之前清空outputDirectory的文件,默认值为true,设置为false时也会覆盖同名文件-->
<clearOutputDirectory>false</clearOutputDirectory>
</configuration>
<executions>
<execution>
<!--在执行mvn compile的时候会执行以下操作-->
<phase>compile</phase>
<goals>
<!--生成OuterClass类-->
<goal>compile</goal>
<!--生成Grpc类-->
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
3.编写proto文件
syntax = "proto3";
option java_multiple_files = true;
option java_package = "cn.coreqi.dapr_server.protobuf";
option java_outer_classname = "HelloWorldProto";
service HelloWorld {
rpc Say (SayRequest) returns (SayResponse) {}
}
message SayRequest {
string message = 1;
}
message SayResponse {
string message = 1;
string timestamp = 2;
}
4.使用插件生成相关文件
5.编写服务的实现
package cn.coreqi.dapr_server.services;
import cn.coreqi.dapr_server.protobuf.HelloWorldGrpc;
import cn.coreqi.dapr_server.protobuf.SayRequest;
import cn.coreqi.dapr_server.protobuf.SayResponse;
import io.grpc.stub.StreamObserver;
import org.lognet.springboot.grpc.GRpcService;
@GRpcService
public class HelloWorldServiceImpl extends HelloWorldGrpc.HelloWorldImplBase {
@Override
public void say(SayRequest request, StreamObserver<SayResponse> responseObserver) {
SayResponse response = SayResponse.newBuilder()
.setMessage("你好," + request.getMessage())
.setTimestamp(Long.valueOf(System.currentTimeMillis()).toString())
.build();
responseObserver.onNext(response);
responseObserver.onCompleted();
}
}
6.修改相关配置
grpc.port=8081
2..NET Grpc Client
1.新建控制台项目[略]
2.引入相关依赖
dotnet add dapr_client.csproj package Google.Protobuf
dotnet add dapr_client.csproj package Grpc.Core
dotnet add dapr_client.csproj package Grpc.Tools
3.复制proto文件,生成方式选择服务端和客户端[略]
4.编写客户端文件
using Grpc.Core;
namespace dapr_client.client;
public class HelloServiceClient
{
private static Channel _channel;
private static HelloWorld.HelloWorldClient _client;
static HelloServiceClient()
{
_channel = new Channel("127.0.0.1:8081", ChannelCredentials.Insecure);
_client = new HelloWorld.HelloWorldClient(_channel);
}
public static SayResponse Say(SayRequest sayRequest)
{
return _client.Say(sayRequest);
}
}
5.在启动类中调用
SayResponse? result = HelloServiceClient.Say(new SayRequest()
{
Message = "coreqi"
});