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示例的更多相关文章

  1. Python Thrift 简单示例

    本文基于Thrift-0.10,使用Python实现服务器端,使用Java实现客户端,演示了Thrift RPC调用示例.Java客户端提供两个字符串参数,Python服务器端计算这两个字符串的相似度 ...

  2. 课程作业01:模仿JavaAppArguments.java示例,编写一个程序,此程序从命令行接收多个数字,求和之后输出结果。

    1.设计思想: 首先是从JavaAppArguments.java示例开始,此示例已打印参数,定义数字 之和和作为存储单位的整型,然后将输入参数的字符串转化为整型,之后求和即可. 2.程序流程图: 3 ...

  3. 左右JAVA示例代码事件分发和监督机制来实现-绝对原创有用

    文章标题:左右JAVA示例代码事件分发和监督机制来实现 文章地址: http://blog.csdn.net/5iasp/article/details/37054171 作者: javaboy201 ...

  4. Spark 用户自定义函数 Java 示例

    Spark UDF Java 示例 在这篇文章中提到了用Spark做用户昵称文本聚类分析,聚类需要选定K个中心点,然后迭代计算其他样本点到中心点的距离.由于中文文字分词之后(n-gram)再加上昵称允 ...

  5. & 和 && 的区别,与(&)运算符、位移运算符(<< 、>>、>>>)的含义及使用(Java示例)

    & 和 && 的区别,与(&)运算符.位移运算符(<< .>>.>>>)的含义及使用(Java示例) 1. & 和 & ...

  6. HTTP基本认证(Basic Authentication)的JAVA示例

    大家在登录网站的时候,大部分时候是通过一个表单提交登录信息.但是有时候浏览器会弹出一个登录验证的对话框,如下图,这就是使用HTTP基本认证.下面来看看一看这个认证的工作过程:第一步:  客户端发送ht ...

  7. CentOS安装卸载memcache及JAVA示例

      原文地址:http://www.cnblogs.com/zhongshengzhen/   先安装libevent,memcached依赖libevent的lib [root@VM_64_81_c ...

  8. hbase 0.96 java 示例

    import java.util.ArrayList; import java.util.List; import org.apache.hadoop.conf.Configuration; impo ...

  9. epub、ocf等常用电子书格式浅析----附JAVA示例程序

    一. 电子书介绍 转载请注明http://www.cnblogs.com/xckk/p/6020324.html Epub(Electronic Publication)是一个完全开放和免费的电子书标 ...

随机推荐

  1. BZOJ 1005 [HNOI2008]明明的烦恼 purfer序列,排列组合

    1005: [HNOI2008]明明的烦恼 Description 自从明明学了树的结构,就对奇怪的树产生了兴趣......给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连线,可产生多少 ...

  2. zjnu 1181 石子合并(区间DP)

    Description 在操场上沿一直线排列着 n堆石子. 现要将石子有次序地合并成一堆.规定每次仅仅能选相邻的两堆石子合并成新的一堆, 并将新的一堆石子数记为该次合并的得分.同意在第一次合并前对调一 ...

  3. [POI 2007] 堆积木

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1109 [算法] DP [代码] #include<bits/stdc++.h& ...

  4. java javax.annotation.Resource注解的详解

    转自:https://www.jb51.net/article/95456.htm java 注解:java javax.annotation.Resource  当我们在xml里面为类配置注入对象时 ...

  5. putty和xshell使用和免密登录

    putty和xshell使用和免密登录 XSHELL的设置 事前:我们先去关闭防火墙和selinux 关闭防火墙:   ufw disable 再去看看selinux 一.查看SELinux状态命令: ...

  6. Linux下JDK Tomcat MySQL基本环境搭建

    1. 安装JDK wget http://download.oracle.com/otn-pub/java/jdk/8u181-b13/96a7b8442fe848ef90c96a2fad6ed6d1 ...

  7. 更换WordPress编辑器为TinyMCE Advanced

    WordPress自带的编辑器功能很少,连更换字体样式大小都不行,没关系WordPress的插件中心插件非常多 在插件中心搜索TinyMCE Advanced 安装启用 还没完 点击设置 里面有丰富的 ...

  8. 使用光盘作为yum源安装ifconfig等网络命令

    # mkdir -p /mnt/cdrom# 如果是光驱:mount -t iso9660 /dev/cdrom /mnt/cdrom/# 如果是ISO:mount -o loop /usr/loca ...

  9. 杭电2053 WA

    #include<stdio.h> int main() { ]; while(scanf("%d",&n)!=EOF) { ;i<=;i++) { a[ ...

  10. 【技术累积】【点】【java】【2】聊一聊似曾相识的switch语句

    闲聊 有些东西并不能像爱因斯坦老先生说的那样,书上查的到就不用去记住... 开始 java使用了C的所有流程控制语句: java中同样有switch语句: 大多数情况下,switch都可以用if替换: ...