- 浏览: 2540995 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Thrift(4)Java Server and Client
1. First, I did a Implementation class for BlogService Interface
package com.sillycat.easytalker.plugins.thrift.business;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import com.sillycat.easytalker.plugins.thrift.gen.code.Blog;
import com.sillycat.easytalker.plugins.thrift.gen.code.BlogService;
public class BlogServiceImpl implements BlogService.Iface {
private Logger logger = Logger.getLogger(this.getClass());
public String createBlog(Blog blog) throws TException {
logger.debug("Method createBlog is invoked! Parameters blog = " + blog
+ " topic = " + blog.getTopic());
return "1";
}
public List<String> batchCreateBlog(List<Blog> blogs) throws TException {
logger.debug("Method batchCreateBlog is invoked! Parameters blogs = "
+ blogs);
List<String> ids = new ArrayList<String>();
ids.add("1");
ids.add("2");
ids.add("3");
return ids;
}
public String deleteBlog(String id) throws TException {
logger.debug("Method deleteBlog is invoked! Parameters id = " + id);
return "1";
}
public List<Blog> listAll() throws TException {
logger.debug("Method listAll is invoked!");
List<Blog> blogs = new ArrayList<Blog>();
Blog b1 = new Blog();
Blog b2 = new Blog();
b1.setContent("test1 content".getBytes());
b1.setTopic("topic1");
b1.setCreatedTime(new Date().getTime());
b1.setIpAddress("127.0.0.1");
b1.setId("1");
b2.setContent("test2 content".getBytes());
b2.setTopic("topic2");
b2.setCreatedTime(new Date().getTime());
b2.setIpAddress("127.0.0.1");
b2.setId("2");
blogs.add(b1);
blogs.add(b2);
return blogs;
}
public Blog getOne(String id) throws TException {
logger.debug("Method getOne is invoked! Parameters id = " + id);
Blog b1 = new Blog();
b1.setContent("test1 content".getBytes());
b1.setTopic("topic1");
b1.setCreatedTime(new Date().getTime());
b1.setIpAddress("127.0.0.1");
b1.setId("1");
return b1;
}
public String updateBlog(Blog blog) throws TException {
logger.debug("Method updateBlog is invoked! Parameters blog = " + blog
+ " topic = " + blog.getTopic());
return "1";
}
}
2. Then I try to create the ThriftJavaServer
package com.sillycat.easytalker.plugins.thrift.server;
import java.net.InetSocketAddress;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.server.TThreadPoolServer.Args;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransportFactory;
import com.sillycat.easytalker.plugins.thrift.business.BlogServiceImpl;
import com.sillycat.easytalker.plugins.thrift.gen.code.BlogService;
public class ThriftJavaServer {
public static void main(String[] args) {
BlogService.Processor<BlogServiceImpl> processor = new BlogService.Processor<BlogServiceImpl>(
new BlogServiceImpl());
try {
TServerTransport serverTransport = new TServerSocket(
new InetSocketAddress("0.0.0.0", 9813));
Args trArgs = new Args(serverTransport);
trArgs.processor(processor);
trArgs.protocolFactory(new TBinaryProtocol.Factory(true, true));
trArgs.transportFactory(new TTransportFactory());
TServer server = new TThreadPoolServer(trArgs);
System.out.println("server begin ......................");
server.serve();
System.out.println("---------------------------------------");
server.stop();
} catch (Exception e) {
throw new RuntimeException("thrift server start failed!!" + "/n"
+ e.getMessage());
}
}
}
3. Our Test Java Client class
package com.sillycat.easytalker.plugins.thrift.client;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import com.sillycat.easytalker.plugins.thrift.gen.code.Blog;
import com.sillycat.easytalker.plugins.thrift.gen.code.BlogService;
public class ThriftJavaClient {
public static void main(String[] args) throws TException {
long start = System.currentTimeMillis();
TTransport transport = new TSocket("127.0.0.1", 9813);
TProtocol protocol = new TBinaryProtocol(transport);
BlogService.Client client = new BlogService.Client(
protocol);
transport.open();
//createBlog
Blog b1 = new Blog();
b1.setContent("test1 content".getBytes());
b1.setTopic("topic1");
b1.setCreatedTime(new Date().getTime());
b1.setIpAddress("127.0.0.1");
String result_CreateBlog = client.createBlog(b1);
System.out.println("result_CreateBlog = " + result_CreateBlog);
//batchCreateBlog
List<Blog> blogs = new ArrayList<Blog>();
Blog b2 = new Blog();
b2.setContent("test2 content".getBytes());
b2.setTopic("topic2");
b2.setCreatedTime(new Date().getTime());
b2.setIpAddress("127.0.0.1");
b2.setId("2");
blogs.add(b1);
blogs.add(b2);
List<String> result_ids = client.batchCreateBlog(blogs);
System.out.println("result_ids = " + result_ids);
//deleteBlog
String result_deleteBlog = client.deleteBlog("1");
System.out.println("result_deleteBlog = " + result_deleteBlog);
//getOne
Blog result_GetOne = client.getOne("id");
System.out.println("result_GetOne = " + result_GetOne);
//listAll
List<Blog> result_ListAll = client.listAll();
System.out.println("result_ListAll = " + result_ListAll);
//updateBlog
String result_UpdateBlog = client.updateBlog(b1);
System.out.println("result_UpdateBlog = " + result_UpdateBlog);
transport.close();
System.out.println((System.currentTimeMillis() - start));
System.out.println("client sucess!");
}
}
references:
http://li3huo.com/2011/10/creating-a-thrift-service-step-by-step/
http://gemantic.iteye.com/blog/1199214
1. First, I did a Implementation class for BlogService Interface
package com.sillycat.easytalker.plugins.thrift.business;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import com.sillycat.easytalker.plugins.thrift.gen.code.Blog;
import com.sillycat.easytalker.plugins.thrift.gen.code.BlogService;
public class BlogServiceImpl implements BlogService.Iface {
private Logger logger = Logger.getLogger(this.getClass());
public String createBlog(Blog blog) throws TException {
logger.debug("Method createBlog is invoked! Parameters blog = " + blog
+ " topic = " + blog.getTopic());
return "1";
}
public List<String> batchCreateBlog(List<Blog> blogs) throws TException {
logger.debug("Method batchCreateBlog is invoked! Parameters blogs = "
+ blogs);
List<String> ids = new ArrayList<String>();
ids.add("1");
ids.add("2");
ids.add("3");
return ids;
}
public String deleteBlog(String id) throws TException {
logger.debug("Method deleteBlog is invoked! Parameters id = " + id);
return "1";
}
public List<Blog> listAll() throws TException {
logger.debug("Method listAll is invoked!");
List<Blog> blogs = new ArrayList<Blog>();
Blog b1 = new Blog();
Blog b2 = new Blog();
b1.setContent("test1 content".getBytes());
b1.setTopic("topic1");
b1.setCreatedTime(new Date().getTime());
b1.setIpAddress("127.0.0.1");
b1.setId("1");
b2.setContent("test2 content".getBytes());
b2.setTopic("topic2");
b2.setCreatedTime(new Date().getTime());
b2.setIpAddress("127.0.0.1");
b2.setId("2");
blogs.add(b1);
blogs.add(b2);
return blogs;
}
public Blog getOne(String id) throws TException {
logger.debug("Method getOne is invoked! Parameters id = " + id);
Blog b1 = new Blog();
b1.setContent("test1 content".getBytes());
b1.setTopic("topic1");
b1.setCreatedTime(new Date().getTime());
b1.setIpAddress("127.0.0.1");
b1.setId("1");
return b1;
}
public String updateBlog(Blog blog) throws TException {
logger.debug("Method updateBlog is invoked! Parameters blog = " + blog
+ " topic = " + blog.getTopic());
return "1";
}
}
2. Then I try to create the ThriftJavaServer
package com.sillycat.easytalker.plugins.thrift.server;
import java.net.InetSocketAddress;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.server.TThreadPoolServer.Args;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransportFactory;
import com.sillycat.easytalker.plugins.thrift.business.BlogServiceImpl;
import com.sillycat.easytalker.plugins.thrift.gen.code.BlogService;
public class ThriftJavaServer {
public static void main(String[] args) {
BlogService.Processor<BlogServiceImpl> processor = new BlogService.Processor<BlogServiceImpl>(
new BlogServiceImpl());
try {
TServerTransport serverTransport = new TServerSocket(
new InetSocketAddress("0.0.0.0", 9813));
Args trArgs = new Args(serverTransport);
trArgs.processor(processor);
trArgs.protocolFactory(new TBinaryProtocol.Factory(true, true));
trArgs.transportFactory(new TTransportFactory());
TServer server = new TThreadPoolServer(trArgs);
System.out.println("server begin ......................");
server.serve();
System.out.println("---------------------------------------");
server.stop();
} catch (Exception e) {
throw new RuntimeException("thrift server start failed!!" + "/n"
+ e.getMessage());
}
}
}
3. Our Test Java Client class
package com.sillycat.easytalker.plugins.thrift.client;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import com.sillycat.easytalker.plugins.thrift.gen.code.Blog;
import com.sillycat.easytalker.plugins.thrift.gen.code.BlogService;
public class ThriftJavaClient {
public static void main(String[] args) throws TException {
long start = System.currentTimeMillis();
TTransport transport = new TSocket("127.0.0.1", 9813);
TProtocol protocol = new TBinaryProtocol(transport);
BlogService.Client client = new BlogService.Client(
protocol);
transport.open();
//createBlog
Blog b1 = new Blog();
b1.setContent("test1 content".getBytes());
b1.setTopic("topic1");
b1.setCreatedTime(new Date().getTime());
b1.setIpAddress("127.0.0.1");
String result_CreateBlog = client.createBlog(b1);
System.out.println("result_CreateBlog = " + result_CreateBlog);
//batchCreateBlog
List<Blog> blogs = new ArrayList<Blog>();
Blog b2 = new Blog();
b2.setContent("test2 content".getBytes());
b2.setTopic("topic2");
b2.setCreatedTime(new Date().getTime());
b2.setIpAddress("127.0.0.1");
b2.setId("2");
blogs.add(b1);
blogs.add(b2);
List<String> result_ids = client.batchCreateBlog(blogs);
System.out.println("result_ids = " + result_ids);
//deleteBlog
String result_deleteBlog = client.deleteBlog("1");
System.out.println("result_deleteBlog = " + result_deleteBlog);
//getOne
Blog result_GetOne = client.getOne("id");
System.out.println("result_GetOne = " + result_GetOne);
//listAll
List<Blog> result_ListAll = client.listAll();
System.out.println("result_ListAll = " + result_ListAll);
//updateBlog
String result_UpdateBlog = client.updateBlog(b1);
System.out.println("result_UpdateBlog = " + result_UpdateBlog);
transport.close();
System.out.println((System.currentTimeMillis() - start));
System.out.println("client sucess!");
}
}
references:
http://li3huo.com/2011/10/creating-a-thrift-service-step-by-step/
http://gemantic.iteye.com/blog/1199214
发表评论
-
Update Site will come soon
2021-06-02 04:10 1672I am still keep notes my tech n ... -
Hadoop Docker 2019 Version 3.2.1
2019-12-10 07:39 288Hadoop Docker 2019 Version 3.2. ... -
Nginx and Proxy 2019(1)Nginx Enable Lua and Parse JSON
2019-12-03 04:17 439Nginx and Proxy 2019(1)Nginx En ... -
Data Solution 2019(13)Docker Zeppelin Notebook and Memory Configuration
2019-11-09 07:15 279Data Solution 2019(13)Docker Ze ... -
Data Solution 2019(10)Spark Cluster Solution with Zeppelin
2019-10-29 08:37 243Data Solution 2019(10)Spark Clu ... -
AMAZON Kinesis Firehose 2019(1)Firehose Buffer to S3
2019-10-01 10:15 313AMAZON Kinesis Firehose 2019(1) ... -
Rancher and k8s 2019(3)Clean Installation on CentOS7
2019-09-19 23:25 302Rancher and k8s 2019(3)Clean In ... -
Pacemaker 2019(1)Introduction and Installation on CentOS7
2019-09-11 05:48 333Pacemaker 2019(1)Introduction a ... -
Crontab-UI installation and Introduction
2019-08-30 05:54 442Crontab-UI installation and Int ... -
Spiderkeeper 2019(1)Installation and Introduction
2019-08-29 06:49 493Spiderkeeper 2019(1)Installatio ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 363Supervisor 2019(2)Ubuntu and Mu ... -
Supervisor 2019(1)CentOS 7
2019-08-19 09:33 322Supervisor 2019(1)CentOS 7 Ins ... -
Redis Cluster 2019(3)Redis Cluster on CentOS
2019-08-17 04:07 365Redis Cluster 2019(3)Redis Clus ... -
Amazon Lambda and Version Limit
2019-08-02 01:42 432Amazon Lambda and Version Limit ... -
MySQL HA Solution 2019(1)Master Slave on MySQL 5.7
2019-07-27 22:26 512MySQL HA Solution 2019(1)Master ... -
RabbitMQ Cluster 2019(2)Cluster HA and Proxy
2019-07-11 12:41 456RabbitMQ Cluster 2019(2)Cluster ... -
Running Zeppelin with Nginx Authentication
2019-05-25 21:35 315Running Zeppelin with Nginx Aut ... -
Running Zeppelin with Nginx Authentication
2019-05-25 21:34 316Running Zeppelin with Nginx Aut ... -
ElasticSearch(3)Version Upgrade and Cluster
2019-05-20 05:00 321ElasticSearch(3)Version Upgrade ... -
Jetty Server and Cookie Domain Name
2019-04-28 23:59 389Jetty Server and Cookie Domain ...
相关推荐
本篇将详细讲解如何将Spring与Thrift整合,构建一个Server和Client,以实现高效的数据通信。 首先,我们需要了解Thrift的基本原理。Thrift通过定义一种中间描述文件(.thrift),来生成不同语言的客户端和服务端...
在这个"thrift的java和python结合例子"中,我们将探讨如何使用Thrift在Java和Python之间建立通信。 首先,Thrift通过定义接口描述文件(.thrift)来规范服务的接口。这个文件使用Thrift IDL(Interface Description...
本文将基于Thrift的Java实现,总结学习过程中的一些关键知识点,旨在帮助理解Thrift的工作原理以及如何在Java环境中应用。 一、Thrift简介 Thrift是一种远程过程调用(RPC)框架,它通过定义一种中间描述文件(....
一组用thrift写的Java RPC框架,是两个maven项目,直接可以用,可以传字符串。 方便初学者了解thrift RPC各部分的原理,如果想自己加功能,就要新建XX.thrift文件,定义好接口,用用thrift生成
客户端代码同样基于Thrift生成的代码,创建一个`Client.java`文件,用于调用服务器上的服务: ```java import org.apache.thrift.TException; public class Client { public static void main(String[] args) { ...
4. 创建Thrift服务器并启动服务。 5. 创建Thrift客户端,连接到服务器并调用服务。 通过这种方式,Thrift简化了分布式系统之间的通信,并提供了高性能、可扩展的解决方案。对于Java开发者来说,理解并掌握Thrift的...
在"Thrift双向通讯java代码"这个主题中,我们主要讨论如何使用Thrift在Java环境中实现客户端和服务器端的双向通信。双向通信意味着服务器和客户端都可以向对方发送请求并接收响应,这对于构建复杂的分布式系统至关...
4. **启动服务器**:使用生成的`ThriftServer.java`(或者自定义服务器),创建一个Thrift服务器实例,监听特定端口并处理客户端请求: ```java public class ThriftServer { public static void main(String[] ...
在本示例中,我们将探讨如何使用Thrift在Java和Python之间实现RPC(Remote Procedure Call)的互相调用。 首先,我们需要了解Thrift IDL。在Thrift IDL文件中,我们可以定义服务接口、数据结构(如struct)和常量。...
javac -cp .:`thrift-<version>-lib.jar`:./* Server.java Client.java ``` 确保`thrift-<version>-lib.jar`是Thrift库的正确版本。 4.2 运行 首先启动服务器,然后运行客户端: ```bash java -cp .:`thrift-...
在完成上述步骤后,你需要将所有相关的 Java 类编译成字节码,并确保运行时的 CLASSPATH 包含了 `libthrift.jar` 及其依赖(如 `slf4j-api.jar` 和 `slf4j-simple.jar`)。使用 `javac` 编译 Java 代码,并运行...
《Thrift下Java服务器与客户端开发指南》 Thrift是一种高性能、可扩展的跨语言服务开发框架,由Facebook开发并开源。它通过定义一个中间表示(IDL,Interface Description Language)来描述服务,允许开发者在不同...
- **运行**:先运行服务器端的`Server.java`,然后启动客户端的`Client.java`。客户端将连接到服务器,调用`ping`方法,并显示结果。 通过以上步骤,你可以在Thrift框架下成功地开发Java服务器和客户端。Thrift的...
在客户端,同样使用生成的Java代码,创建`ThriftService.Client`实例,通过`TTransport`连接到服务器,调用服务方法。例如: ```java public static void main(String[] args) throws Exception { TTransport ...
javac -cp .:lib/*:./*:./*/*:./*/*/* Server.java Client.java ``` 确保正确设置了类路径(classpath)以包含Thrift生成的代码和依赖库。 4.2 运行 首先运行服务器: ```bash java -cp .:lib/*:./*:./*/*:./*/*/...
5. **跨平台通信**:可能涉及如何在Web应用中使用Thrift JSClient与后端服务(可能是Java、Python或其他语言)进行通信。 6. **示例代码**:通常会提供实际的JavaScript代码片段,展示如何创建Thrift客户端对象,...
《Thrift下Java服务器与客户端开发指引》是指导开发者如何使用Thrift框架进行Java应用程序的服务器和客户端构建的文档。Thrift是一种开源的跨语言服务开发框架,它通过定义一个中间描述文件(如testJava.thrift),...