thrift java示例
thrift java示例
使用IntelliJ IDEA作为开发工具;
增加proto文件夹,里面写上sayHello.proto
syntax = "proto3"; option java_multiple_files = true;
option java_package = "grpc.example";
option java_outer_classname = "HelloProto";
option objc_class_prefix = "HLW"; package service; service HelloService{
rpc SayHello (HelloRequest) returns (HelloResponse){}
} message HelloRequest{
string name = 1;
} message HelloResponse{
string message = 1;
}
pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.stono</groupId>
<artifactId>grpc-2</artifactId>
<version>1.0-SNAPSHOT</version> <properties>
<os.detection.classifierWithLikes>debian,rhel</os.detection.classifierWithLikes>
</properties> <dependencies>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies> <build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.4.1.Final</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.0.0:exe:${os.detected.classifier}}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>os.grpc:protoc-gen-grpc-java:1.0.0:exe:${os.detected.classifier}}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
在Maven Projects中,进行protobuf:compile,protobuf:compile-custom;
生成grpc-java和java源文件;
然后编写客户端、服务端程序:
package com.grpc; import grpc.example.HelloRequest;
import grpc.example.HelloResponse;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import grpc.example.HelloSerivceGrpc;
import io.grpc.stub.StreamObserver; import java.io.IOException; public class HelloServer {
private int port = 50051;
private Server server;
private void start() throws IOException{
server = ServerBuilder.forPort(port)
.addService(new HelloServiceImpl())
.build()
.start();
Runtime.getRuntime().addShutdownHook(new Thread(){
@Override
public void run(){
HelloServer.this.stop();
}
});
}
private void stop(){
if(server!=null){
server.shutdown();
}
}
private void blockUntilShutdown() throws InterruptedException{
if(server != null){
server.awaitTermination();
}
}
private class HelloServiceImpl extends HelloSerivceGrpc.HelloSerivceImplBase{
public void sayHello(HelloRequest req, StreamObserver<HelloResponse> responseObserver){
HelloResponse reply = HelloResponse.newBuilder().setMessage("Hello, "+req.getName()).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
} public static void main(String[] args) throws Exception {
final HelloServer server = new HelloServer();
server.start();
server.blockUntilShutdown();
}
}
package com.grpc; import grpc.example.HelloRequest;
import grpc.example.HelloResponse;
import io.grpc.ManagedChannel;
import grpc.example.HelloSerivceGrpc;
import io.grpc.ManagedChannelBuilder; import java.util.concurrent.TimeUnit; public class HelloClient {
private final ManagedChannel channel;
private final HelloSerivceGrpc.HelloSerivceBlockingStub blockingStub;
public HelloClient(String host,int port){
channel = ManagedChannelBuilder.forAddress(host,port)
.usePlaintext(true)
.build();
blockingStub = HelloSerivceGrpc.newBlockingStub(channel);
}
public void shutdown() throws InterruptedException{
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
public String sayHello(String name){
HelloRequest request = HelloRequest.newBuilder().setName(name).build();
HelloResponse response = blockingStub.sayHello(request);
return response.getMessage();
} public static void main(String[] args) throws Exception {
HelloClient client = new HelloClient("127.0.0.1",50051);
String content = client.sayHello("stono");
System.out.println(content);
client.shutdown();
}
}
注意把生成的代码文件夹作为source folder;
然后就可以运行了;
thrift java示例的更多相关文章
- Python Thrift 简单示例
本文基于Thrift-0.10,使用Python实现服务器端,使用Java实现客户端,演示了Thrift RPC调用示例.Java客户端提供两个字符串参数,Python服务器端计算这两个字符串的相似度 ...
- 课程作业01:模仿JavaAppArguments.java示例,编写一个程序,此程序从命令行接收多个数字,求和之后输出结果。
1.设计思想: 首先是从JavaAppArguments.java示例开始,此示例已打印参数,定义数字 之和和作为存储单位的整型,然后将输入参数的字符串转化为整型,之后求和即可. 2.程序流程图: 3 ...
- 左右JAVA示例代码事件分发和监督机制来实现-绝对原创有用
文章标题:左右JAVA示例代码事件分发和监督机制来实现 文章地址: http://blog.csdn.net/5iasp/article/details/37054171 作者: javaboy201 ...
- Spark 用户自定义函数 Java 示例
Spark UDF Java 示例 在这篇文章中提到了用Spark做用户昵称文本聚类分析,聚类需要选定K个中心点,然后迭代计算其他样本点到中心点的距离.由于中文文字分词之后(n-gram)再加上昵称允 ...
- & 和 && 的区别,与(&)运算符、位移运算符(<< 、>>、>>>)的含义及使用(Java示例)
& 和 && 的区别,与(&)运算符.位移运算符(<< .>>.>>>)的含义及使用(Java示例) 1. & 和 & ...
- HTTP基本认证(Basic Authentication)的JAVA示例
大家在登录网站的时候,大部分时候是通过一个表单提交登录信息.但是有时候浏览器会弹出一个登录验证的对话框,如下图,这就是使用HTTP基本认证.下面来看看一看这个认证的工作过程:第一步: 客户端发送ht ...
- CentOS安装卸载memcache及JAVA示例
原文地址:http://www.cnblogs.com/zhongshengzhen/ 先安装libevent,memcached依赖libevent的lib [root@VM_64_81_c ...
- hbase 0.96 java 示例
import java.util.ArrayList; import java.util.List; import org.apache.hadoop.conf.Configuration; impo ...
- epub、ocf等常用电子书格式浅析----附JAVA示例程序
一. 电子书介绍 转载请注明http://www.cnblogs.com/xckk/p/6020324.html Epub(Electronic Publication)是一个完全开放和免费的电子书标 ...
随机推荐
- bzoj2958: 序列染色&&3269: 序列染色
DP这种东西,考场上就只能看命了.. #include<cstdio> #include<iostream> #include<cstring> #include& ...
- bzoj4247: 挂饰(背包)
4247: 挂饰 题目:传送门 题解: 看完题目很明显的一道二维背包(一开始还推错了) 设f[i][j]表示前i个挂饰选完(可以有不选)之后还剩下j个挂钩的最大值(j最多贡献为n) 那么f[i][j] ...
- 国内物联网平台初探(一) ——百度物接入IoT Hub
物接入IoT Hub - 架构 全托管的云服务,帮助建立设备与云端之间安全可靠的双向连接 支撑海量设备的数据收集.监控.故障预测等各种物联网场景 物接入IoT Hub - 功能 通信协议:支持MQTT ...
- Node.js:Web 模块
ylbtech-Node.js:Web 模块 1.返回顶部 1. Node.js Web 模块 什么是 Web 服务器? Web服务器一般指网站服务器,是指驻留于因特网上某种类型计算机的程序,Web服 ...
- [源码管理] Windows下搭建SVN服务器
前文所述SVN客户端使用的时候,用的SVN服务器通常为外部,例如Google Code的服务器,不过,做为一个程序开发人员,就算自己一个人写程序,也应该有一个SVN版本控制系统,以便对开发代码进行有效 ...
- shp系列(五)——利用C++进行shp文件的写(创建)
之前介绍了shp文件.dbf文件和shx文件的的读取,接下来将分别介绍它们的创建过程.一般来说,读和写的一一对应的,写出的文件就是为了保存数据供以后读取的.写的文件要符合shapefile的标准.之前 ...
- Ajax请求成功但是一直进入error的原因
1.在1.3版本的jQuery以后,严格要求了json格式,如果返回的值不是json格式,他就会执行error函数. 所以如果想让他走success函数的话,还是在后台把数据格式化成json格式吧. ...
- 浅析CLR的GC(垃圾回收器)
文章目录: 了解托管堆和GC GC高效的处理方式—代 特殊类型的清理 手动监控和控制对象生命周期 1.了解托管堆和GC 在面向对象环境中,每一个类型都代表了一种资源.我们要使用这些资源,就要为这些代表 ...
- Redhat/CentOS xfs文件系统及磁盘挂载
#!/bin/sh #DEVICE_LIST=""DEVICE_LIST="/dev/sda /dev/sdb /dev/sdc /dev/sdd /dev/sde /d ...
- 修复lsp,360浏览器可以上网其它软件不行
netsh winsock reset netsh int ip reset 重启电脑