Thrift notes (unfinished), thrift notes incomplete
Thrift tutorial demonstrates the python server and client
The development environment in this article is windows 7 + python2.7.3
Thrift official homepage: http://thrift.apache.org/
First download thrift-0.9.1.tar.gz and Thrift compiler for Windows (thrift-0.9.1.exe ). Thrift-0.9.1.exe is to help you compile, can run in windows.
Decompress thrift-0.9.1.tar.gz
1. Install the thrift Library
First install the library in thrift-0.9.1 \ lib \ py, python setup. py install note: the installation fails in windows. You need to use VC to compile the code and modify the code accordingly. If you do not need to, download the compiled version directly here. Http://www.lfd.uci.edu /~ Gohlke/pythonlibs/(this is really a good place, thank you)
2. Code generated by Thrift
Thrift can help you generate a part of the Code, so that you can pay more attention to your business part, rather than how it communicates. Thrift-0.7.0 \ tutorial is an official tutorial. Tutorial. thrift is the syntax defined by thrift. It is used to generate various codes.
Run the preceding thrift-0.7.0.exe command in the thrift-0.7.0 \ tutorial directory.
Thrift-0.7.0.exe-r-gen py-gen java tutorial. thrift
Two folders are generated in the current folder, namely gen-java and gen-py. Gen-java is not needed for the moment. Generate and play :) if you want to generate more other types of code,-gen xxx is enough.
3. Run
Run PythonServer. py in the thrift-0.7.0 \ tutorial \ py directory. Seeing Starting the server proves that you have successfully run the server!
Next, run the client PythonClient. py. Then you can see that the server has output information ~~ Successful! Yes ~~ Successful!
Thrift tutorial demonstrates the java server and client [jdk, ant, and tomcat (unrelated configuration) configuration instructions are provided at the end of this article]
The compilation of Thrift Java requires Ant. In addition, a special package slf4j is used. In addition, you also need to download some dependent Java libraries when using ant to compile thrit. Therefore, it is recommended that the installed machine can access the Internet. Otherwise, it will be very troublesome. We recommend that you install the latest Ant version. Otherwise, compilation may fail due to some errors. Here, I take the latest 1.8.2.
1. Install Ant
Download apache-ant-1.8.2-bin.tar.gz
Decompress apache-ant-1.8.2-bin.tar.gz to/usr/local
An apache-ant-1.8.2 directory appears under/usr/local.
Set the 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 PATH of the system's environment variable.
2. Install slf4j
Download slf4j-1.6.1.tar.gz
Decompress slf4j-1.6.1.tar.gz to/usr/local
A directory slf4j-1.6.1 appears under/usr/local
Add/usr/local/src/slf4j-1.6.1/slf4j-api-1.6.1.jar and/usr/local/slf4j-1.6.1/slf4j-simple-1.6.1.jar in the system's environment variable CLASSPATH. The content of my CLASSPATH is 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 use proxy server configuration
My server cannot directly access the Internet. You need to configure ant to use the proxy to connect to the internet (in fact, the parameter that ant passed to the Java Virtual Machine at startup ). Skip this step if your machine does not need to configure a proxy and you can directly connect to the internet.
Ant uses a proxy to configure 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 and port 3128 when connecting to the internet.
5. Compile thrift
Cd thrift-0.2.0/lib/java
Ant
After compilation, A libthrift. jar file will appear in the current directory.
Put libthrift. jar copy to/usr/local/lib (this location is because the java sample code build is in thrift's tutor. xml indicates to find the jar file in/usr/local/lib ).
Then add/usr/local/lib/libthrift. jar to CLASSPATH.
6. Compile tutorial
Cd thrift-0.2.0/tutorial
Thrift-r -- gen java tutorial. thrift
At this time, the Java source code corresponding to tutorial. thrift will be generated.
Cd java
Ant
During compilation, if the java version is newer, some errors may occur. For example, some syntax errors are reported on my machine.
Errors reported on my machine include case Operation. the ADD error occurs because, in the new java version, when the case statement uses enumeration values, you do not need to write the class name before the enumeration constant. You can simply use ADD.
Another error is that you cannot copy the enumerated values to an int value, for example, io. what = work. op. You can change it to io. what = work. op. ordinal ().
After the compilation is complete, the assumerver. class and JavaClient. the class will be generated in the build directory under the current directory. To execute the class, you need to enter this directory, first run java JavaServer, and then run java JavaClient on another terminal, the JavaClient returns a successful response.
Configure tomcat and java Environments
The following error occurs when the configuration fails.
After the resolution is successful, the following information is displayed:
Cause of error: a semicolon is added after the configured environment. This is incorrect.
Deploy tomcat in eclipse
Choose Window> Preferences> Server> Runtime Environment, and then Add>
Select your Tomcat version-> Next-> Browse-> select your Tomcat path->
Installed JREs-> select your JDK-> Finish. Then you can find Servers from Window> Show View> Other.
Server, so that you can see the Tomcat you just set up. To change the Tomcat settings, double-click Tomcat to view the configured GUI. Right-click Tomcat to start Tomcat.
Ant introduction and ant Configuration
1. What is ant?
Ant is a build tool
2. What is building?
The concept can be found everywhere. For the image, you need to take the code from somewhere, compile it, copy it to somewhere, and so on. Of course, this is not only the same, but it is mainly used to do this.
3. ant benefits
Cross-platform-Because ant is implemented in java, It is cross-platform
Easy to use-compared with ant's brother make
Clear syntax-same as make
Powerful functions-ant can do a lot of things. You may have used it for a long time and still don't know how many features it can have. When you develop some ant plug-ins, you will find more functions.
4. ant's brother make
Ant has done many things, most of which were once done by make. However, unlike objects, make applies more to c/c ++ and ant applies more to Java. Of course this is not certain, but most people do.
5. Build an ant Environment
To use ant, you must first build an ant environment. The procedure is simple:
1) install jdk and set JAVA_HOME, PATH, and CLASS_PATH (these should be known to those who read this article)
2) download the ant address http://www.apache.org/to find your latest version.
3) decompress ant. What you get is a compressed package, decompress it, and put it in a directory as simple as possible, such as D: \ ant-1.6. Although you do not have to do this, but doing so is advantageous.
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 settings and start --> Run --> cmd to enter the command line --> enter ant and press Enter. If you see
Buildfile: build. xml does not exist!
Build failed
Congratulations, you have completed ant settings.
When to use ant
Maybe you heard about ant and are eager to learn ant. When you finish reading the first instance above, you may feel like ant, maybe you feel like ant, we can't make any mistakes when drawing these conclusions, although ant is very useful,
However, it is not always the best choice in any situation. For example, there are more simple and easy-to-use tools on windows, such as eclipse + myeclipse eclipse + wtp, deployment, run more than ant
Easy and convenient, but in some cases it is a good place for ant to play:
1. During server deployment
When your program development is complete and deployment personnel need to deploy it on the server, you cannot configure an eclipse + myeclipse because of installing a program, ant is a good choice at this time, because it is small and easy to configure
You have taken your build. xml to any server, you only need to make simple modifications (some settings, such as directories), and then one or two commands are completed. Isn't that a good thing.
2. in linux, This is often the case. program development is in windows, but the program must run on linux or unix.
Deploying on unix (especially unix) is troublesome. At this time, ant features come out again, because ant is cross-platform and you are building. xml can be used in most operating systems and does not need to be modified.
3. When the server maintainer does not understand Programming
Many people have had such experiences. People who use your programs do not know how to write programs. Because of version updates, You need to redeploy the program again and again to fix bugs. At this time, you will find it so difficult to teach a person. However
If there is ant, you just need to tell him to input one or two commands such as ant xxx. Everything is OK.
The above are some situations I have encountered.
After reading the above information, consider whether you need to use ant.
Learn more about ant with a little complexity
In actual work, the following situations may occur: A project is divided into multiple modules, and each group or department is responsible for one module. To test, they write a build. xml, and you are responsible for combining these modules
Write a build. xml file.
At this time, you have two options:
1. re-write a build. xml file by yourself, which will be troublesome.
2. Try to use the build. xml they have already written to reduce your work.
For example:
Suppose you have three groups, each of which is responsible for one part. Each of them has a src and a well-written build. xml.
At this time, you get their src. What you need to do is to create three folders: src1, src2, and src3. Put their src and build. xml respectively, and then write a build. xml file.
<? 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 = 'built'/>
</Target>
</Project>
OK, your task is finished.