實(shí)現(xiàn)一個(gè)客戶端調(diào)用go服務(wù)端的簡(jiǎn)單服務(wù)
1.項(xiàng)目結(jié)構(gòu)如下
在lib下面的存在一個(gè)simple.proto文件,我們使用插件protobuf-maven-plugin對(duì)其進(jìn)行編譯。配置如下:
<properties>
<os-maven-plugin.version>1.5.0.Final</os-maven-plugin.version>
<protobuf-maven-plugin.version>0.5.1</protobuf-maven-plugin.version>
<protoc.version>3.5.1-1</protoc.version>
<protobuf.version>3.6.0</protobuf.version>
<grpc.version>1.13.1</grpc.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>${protobuf.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</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>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>${os-maven-plugin.version}</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>${protobuf-maven-plugin.version}</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
利用插件進(jìn)行編譯
后可以獲得對(duì)應(yīng)的文件。
2. Client
在client下創(chuàng)建一個(gè)grpc的包,并將以上兩個(gè)文件放入。最后創(chuàng)建一個(gè)SimpleClient。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-756842.html
package com.iq50.client.grpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.util.concurrent.TimeUnit;
/**
* gRPC客戶端示例
*/
public class SimpleClient {
// gRPC通信通道
private final ManagedChannel channel;
// 自動(dòng)生成的存根
private final SimpleGrpc.SimpleBlockingStub blockingStub;
/**
* 構(gòu)造函數(shù),創(chuàng)建SimpleClient實(shí)例
* @param host 服務(wù)器主機(jī)名
* @param port 服務(wù)器端口
*/
public SimpleClient(String host, int port){
this(ManagedChannelBuilder.forAddress(host, port).usePlaintext());
}
/**
* 私有構(gòu)造函數(shù),接受ManagedChannelBuilder參數(shù)
* @param channelBuilder 通道構(gòu)建器
*/
private SimpleClient(ManagedChannelBuilder<?> channelBuilder) {
// 構(gòu)建通信通道
channel = channelBuilder.build();
// 根據(jù)通道返回的信息創(chuàng)建存根
blockingStub = SimpleGrpc.newBlockingStub(channel);
}
/**
* 關(guān)閉通信通道
* @throws InterruptedException 線程中斷異常
*/
public void shutdown() throws InterruptedException {
// 關(guān)閉通道并等待最多5秒鐘確保關(guān)閉完成
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
/**
* 向gRPC服務(wù)發(fā)送問(wèn)候消息
* @param name 問(wèn)候消息的名稱
* @return 服務(wù)響應(yīng)的消息
*/
public String sayHello(String name) {
// 創(chuàng)建HelloRequest對(duì)象
SimpleOuterClass.HelloRequest req = SimpleOuterClass.HelloRequest.newBuilder().setName(name).build();
// 調(diào)用服務(wù)方法獲取響應(yīng)
SimpleOuterClass.HelloReplay replay = blockingStub.sayHello(req);
// 返回服務(wù)響應(yīng)的消息
return replay.getMessage();
}
}
最后在Application中調(diào)用即可文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-756842.html
package com.iq50.client;
import com.iq50.client.grpc.SimpleClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
SimpleClient client = new SimpleClient("127.0.0.1",50051);
String replay = client.sayHello("Jack");
try {
client.shutdown();
} catch (InterruptedException e) {
System.out.println("channel關(guān)閉異常:"+ e.getMessage());
}
System.out.println("回應(yīng)是"+replay);
}
}
到了這里,關(guān)于【gRPC實(shí)現(xiàn)java端調(diào)用go的服務(wù)】的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!