查看
第一节入门:https://blog.csdn.net/shan165310175/article/details/86618932
第二节C++编译helloworld工程:https://blog.csdn.net/shan165310175/article/details/86619128
0. 建立maven工程(使用Idea ide)
1.maven添加依赖和插件:
io.grpc
grpc-netty-shaded
1.18.0
io.grpc
grpc-protobuf
1.18.0
io.grpc
grpc-stub
1.18.0
kr.motd.maven
os-maven-plugin
1.5.0.Final
org.xolstice.maven.plugins
protobuf-maven-plugin
0.5.1
com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}grpc-javaio.grpc:protoc-gen-grpc-java:1.17.1:exe:${os.detected.classifier}
compile
compile-custom
如果出现插件爆红,下载不下来的,那么将代码贴到dependency中:
org.xolstice.maven.plugins
protobuf-maven-plugin
0.5.1
下载完之后再删除。
此时插件那边显示:
文章图片
2. 建立main/proto目录,将helloworld.proto拷贝到目录中
3.双击上面的编译命令编译proto文件,生成如下文件:
文章图片
4.新建HelloworldClient类,内容如下:
package com.daily;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
import io.grpc.examples.helloworld.GreeterGrpc;
import io.grpc.examples.helloworld.HelloReply;
import io.grpc.examples.helloworld.HelloRequest;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
public class HelloWorldClient {
private static final Logger logger = Logger.getLogger(HelloWorldClient.class.getName());
private final ManagedChannel channel;
private final GreeterGrpc.GreeterBlockingStub blockingStub;
// private final GreeterGrpc.GreeterFutureStub blockingStub;
/** Construct client connecting to HelloWorld server at {@code host:port}. */
public HelloWorldClient(String host, int port) {
this(ManagedChannelBuilder.forAddress(host, port)
// Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
// needing certificates.
.usePlaintext()
.build());
} /** Construct client for accessing HelloWorld server using the existing channel. */
HelloWorldClient(ManagedChannel channel) {
this.channel = channel;
blockingStub = GreeterGrpc.newBlockingStub(channel);
//blockingStub = GreeterGrpc.newFutureStub(channel);
} public void shutdown() throws InterruptedException {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
} private ExecutorService threadPool = Executors.newFixedThreadPool(30);
/** Say hello to server. */
public void greet(String name) {
//logger.info("Will try to greet " + name + " ...");
HelloRequest request = HelloRequest.newBuilder().setName(name).build();
HelloReply response;
try {HelloReply helloReply = blockingStub.sayHello(request);
System.out.println(helloReply.getMessage());
} catch (StatusRuntimeException e) {
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
return;
}
//System.out.println("Greeting: " + response.getMessage());
} /**
* Greet server. If provided, the first element of {@code args} is the name to use in the
* greeting.
*/
public static void main(String[] args) throws Exception {HelloWorldClient client = new HelloWorldClient("localhost", 50051);
try {
for (int i = 0;
i < 5;
++i) {
client.greet("小明"+i);
}
System.out.println("end>>>>>>>>>>>>>>>>>>>>>>>");
} finally {
client.shutdown();
}Thread.sleep(5000);
}
}
main函数中就是主要调用,该调用是同步的。
注释部分有异步的。blockingStub = GreeterGrpc.newFutureStub(channel);
开启c++服务:
文章图片
运行java后输出:
文章图片
此时java调用C++服务成功。
【第三节(grpc1.18.0 在JAVA中调用c++发布的服务)】
推荐阅读
- Java|Java基础——数组
- 人工智能|干货!人体姿态估计与运动预测
- java简介|Java是什么(Java能用来干什么?)
- Java|规范的打印日志
- Linux|109 个实用 shell 脚本
- 程序员|【高级Java架构师系统学习】毕业一年萌新的Java大厂面经,最新整理
- Spring注解驱动第十讲--@Autowired使用
- SqlServer|sql server的UPDLOCK、HOLDLOCK试验
- jvm|【JVM】JVM08(java内存模型解析[JMM])
- 技术|为参加2021年蓝桥杯Java软件开发大学B组细心整理常见基础知识、搜索和常用算法解析例题(持续更新...)