Thrift Notes (unfinished)

Source: Internet
Author: User

Thrift Tutorial Demo python server and client
The development environment for this article is Windows 7 + python2.7.3
Thrift Official Homepage: http://thrift.apache.org/
First come here to download thrift-0.9.1.tar.gz and thrift Compiler for Windows (Thrift-0.9.1.exe). Thrift-0.9.1.exe is compiled for you and can be run under Windows.
Decompression thrift-0.9.1.tar.gz
1. Installing the Thrift Library
First to install thrift-0.9.1\lib\py in the library, Python setup.py install note, installation in Windows is not successful, need to compile with VC, but also to make corresponding changes to the code, very troublesome, do not say, directly download the compiled version here. Http://www.lfd.uci.edu/~gohlke/pythonlibs/(This is a really good place to thank the great God)
2.Thrift Generating Code
Thrift can help you generate a portion of your code to focus more on your business, rather than on how it communicates. Thrift-0.7.0\tutorial, is an official tutorial. Tutorial.thrift, which is the syntax defined by thrift, is to generate various kinds of code through it.

Execute commands in the Thrift-0.7.0\tutorial directory using the previously downloaded Thrift-0.7.0.exe
Thrift-0.7.0.exe-r-gen Py-gen Java tutorial.thrift
In the current folder is generated two folders, respectively, Gen-java and Gen-py. Gen-java We do not need, generated to play:) If you want to generate more other types of code,-gen XXX can be.
3. Running
Run pythonserver.py under the Thrift-0.7.0\tutorial\py directory. Seeing starting the server proves that you have successfully run the service side!
Next, run the client pythonclient.py, then you can see the service side has information output can see the server has information output ~ ~ Success! ~ ~ Success!

Thrift Tutorial Demo Java server and client end with Jdk,ant and Tomcat (unrelated configuration) configuration instructions
Thrift Java compilation requires Ant. and a special package slf4j is used. And in the process of using ant to compile Thrit, you also need to download some dependent Java libraries. So it's best to have Internet access on the installed machine. Otherwise it would be very troublesome. It is recommended to install the latest version of Ant, otherwise, there may be some errors in compiling, resulting in failure to compile. Here, I'm taking the latest 1.8.2.

1. Install Ant
Download apache-ant-1.8.2-bin.tar.gz
Unzip apache-ant-1.8.2-bin.tar.gz to/usr/local
A directory will appear under/usr/local apache-ant-1.8.2
Set environment variable Ant_home
ant_home=/usr/local/apache-ant-1.8.2
Add the path/usr/local/apache-ant-1.8.2 to the system's environment variable path.


2. Installing SLF4J
Download slf4j-1.6.1.tar.gz
Unzip slf4j-1.6.1.tar.gz to/usr/local
A directory will appear under/usr/local slf4j-1.6.1
Add/usr/local/src/slf4j-1.6.1/slf4j-api-1.6.1.jar and/USR/LOCAL/SLF4J-1.6.1/to the system's environment variable Classpath Slf4j-simple-1.6.1.jar. The contents of my classpath are as follows:
./:/usr/local/slf4j-1.6.1/slf4j-api-1.6.1.jar:/usr/local/slf4j-1.6.1/slf4j-simple-1.6.1.jar

3.ANT configuration using a proxy server
My server does not have direct access to the Internet and needs to be configured to have ant use the proxy to connect to the Internet (which is actually the parameter that ant passes to the Java Virtual machine at boot time). You can skip this step if your machine does not need to configure the agent to connect directly to the Internet.

Ant uses proxies, which are implemented by configuring the environment variable ant_opts, for example:
Export ant_opts= "-dhttp.proxyhost=192.168.0.105-dhttp.proxyport=3128"
This setting tells Ant to use the proxy server 192.168.0.105 when connecting to the Internet, port 3128

5. Compiling thrift
CD Thrift-0.2.0/lib/java
Ant
A libthrift.jar will appear in the current directory after compilation is complete
Copy the Libthrift.jar to/usr/local/lib (this position is because in thrift's tutor, Java sample Code build.xml indicates that the jar file will be found in/usr/local/lib).
Then the/usr/local/lib/libthrift.jar is added to the classpath.

6. Compiling tutorial
CD thrift-0.2.0/tutorial
Thrift-r--gen Java Tutorial.thrift
This time the Java source code corresponding to the Tutorial.thrift will be generated.
CD Java
Ant

At compile time, if Java version is newer, there may be some errors, for example, on my machine, I reported some grammatical errors, my is Java 1.6.0

In my machine reported error, there is a case Operation.add error, because in the new version of Java, when using the enumeration value in the cases statement, do not need to write the name of the class before enumeration constants, directly with the Add.

Another mistake is not allowed. The enumeration value is copied to an int value, for example io.what = Work.op. Can be changed to Io.what = Work.op.ordinal ().

After compilation, Javaserver.class and Javaclient.class will be generated in the current directory in the build directory, to execute, to enter this directory, first run Java JavaServer, and then run Java on the other terminal Javaclient, you should see javaclient successfully displaying the response.

Configuring Tomcat and the Java environment


The following error has been not successfully configured

After resolving the success shown below:

The reason for the error, the configured environment is followed by a semicolon. This is not true.
Deploying Tomcat in Eclipse
Runtime Environment from Windows, Preferences, Server, and Add.
Choose your Tomcat path-----Next to your Tomcat version--
Installed JREs, choose your JDK--Finish. You can then find servers-A, from Show View--
Server so you can see the tomcat you just set up. If you need to change Tomcat's settings, simply double-click Tomcat to see the configured GUI. Right-click Tomcat to start Tomcat.

Ant Introduction and Ant Configuration
1 What is ant
Ant is the build tool


2 What is Build
The concept can be found everywhere, in the image, you have to take the code from somewhere, compile, and then copy it to a place and so on, of course, not only with this, but mainly to do this


Benefits of 3.ant
Cross-platform-because Ant is implemented in Java, it's cross-platform
Simple to use--compared to Ant's brother make
The syntax is clear-it's also compared to make
Powerful--ant can do a lot of things, you may have spent a long time, you still do not know how much it can function. When you develop some ant plugins yourself, you will find it more functional.


4.ant's brother make
Many of the things that Ant does, most of which are once made, have been done by make, but the objects are different, making is more applied to C + +, and ant is more in Java. Of course it's not certain, but most people do.

5 Building an ant environment
To use ant to build an ant environment first, the steps are simple:
1), install JDK, set Java_home, PATH, Class_path (these should be known to the people who read this article)
2), download ant address http://www.apache.org/Find a version you like, or simply the latest version
3), unzip ant you get a compressed package, unzip it, and put it in a directory as simple as possible, such as D:\ant-1.6 although you don't have to do this, it's good to do so.
4), set Ant_home,
Add the Bin directory under the Ant_home directory in PATH (I set: Ant_home:d:\apache-ant-1.8.2,path:%ant_home%\bin)
5), test your setup, start-and-run-->cmd into command line-and type ant carriage return if you see
Buildfile:build.xml does not exist!
Build failed
Well, congratulations, you've done your ant setup.


when to use ant
Maybe you heard someone talk about ant, on impulse to learn ant, when you read the first instance above, perhaps you feel ant is really good, perhaps you feel ant is so much, to come to these conclusions can not say wrong, although ant is very useful,
But it's not always the best option in any case, such as having more simple, easy-to-use tools on Windows like Eclipse+myeclipse ECLIPSE+WTP and so on, whether it's compiling, deploying, running, using more than Ant.
Easy, convenient but in some cases it's a good place for ant to play:
1, when deploying on the server
When your program is developed and deployed to the server, it is not always possible to configure a eclipse+myeclipse because of the installation of a program, Ant is a good choice because it is small and easy to match.
You take your build.xml to any server, just make simple changes (some settings, such as directories), and then one or two commands to complete, isn't that a good thing.
2,linux, most of the time, program development is under Windows, but programs run on Linux or UNIX, on Linux or
Deployment on Unix (especially Unix) is a hassle, and this time the ant feature comes out because Ant is cross-platform and you can use it on most operating systems in Build.xml, with minimal modification.

3, when the server maintainer does not understand the programming
A lot of people have had this experience, people who use your program do not know how to write programs. You have to update the program because it needs to be re-deployed again and again because of bug fixes. This time you will find it difficult to teach a person. But
After having ant, you just need to tell him to enter one or two commands such as Ant xxx, everything OK.
These are some of the things I have encountered.
After reading the above situation, think about whether you need to use ant, if it is to continue.
Learn a little bit more complex ant
In the actual work process may have some of the following situations, a project into a number of modules, each team or department is responsible for a module, in order to test, they wrote a build.xml, and you are responsible for these modules to the
Used together, write a build.xml

You have two options at this time:
1, to write myself a build.xml, which would be a troublesome thing to
2, try to use the build.xml they have written to reduce their work
As an example:
Let's say you have three groups below, each with one section, one src and one written build.xml.
This time you get their src, what you need to do is build three folders Src1, SRC2, src3 respectively put their src and build.xml in, and then write a build.xml
<?xml version= ' 1.0 ' encoding= ' UTF-8 '?>
<project name= ' main ' default= ' build '
Basedir= '. ' >
<property name= ' bin ' value= ' ${basedir}\bin '/>
<property name= ' src1 ' value= ' ${basedir}\src1 '
/>
<property name= ' src2 ' value= ' ${basedir}\src2 '
/>
<property name= ' src3 ' value= ' ${BASEDIR}\SRC3 '
/>
<target name= ' init ' >
<mkdir dir= ' ${bin} '/>
</target>
<target name= ' Run ' >
<ant dir= ' ${src1} ' target= ' Run '/>
<ant dir= ' ${src2} ' target= ' Run '/>
<ant dir= ' ${src3} ' target= ' Run '/>
</target>
<target name= ' clean ' >
<ant dir= ' ${src1} ' target= ' Clean '/>
<ant dir= ' ${src2} ' target= ' Clean '/>
<ant dir= ' ${src3} ' target= ' Clean '/>
</target>
<target name= ' build ' depends= ' Init,call ' >
<copy todir= ' ${bin} ' >
<fileset dir= ' ${src1} ' >
<include name= ' *.jar '/>
</fileset>
<fileset dir= ' ${src2} ' >
<include name= ' *.jar '/>
</fileset>
<fileset dir= ' ${src3} ' >
<include name= ' *.jar '/>
</fileset>
</copy>
</target>
<target name= ' rebuild ' depends= ' Build,clean ' >
<ant target= ' clean '/>
<ant target= ' Build '/>
</target>
</project>
OK, your job is done.

Thrift Notes (unfinished)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.