The previous article was written 1 years ago ashamed ashamed, today a colleague asked me to demo and read the next article well, the main code did not write it, today added
Previous address: http://www.cnblogs.com/rufus-hua/p/4159278.html
Write a new Hello.thrift file in the Thrift folder
As in the following code:
namespace Java com.test.finagle.demoservice hello{ string hellostring (1:string para) i32 helloint (1: i32 para) bool helloboolean (1: BOOL para) void Hellovoid () string
Then click on compile, (if not the error) will see in the target folder generated Target\generated-sources\thrift\scrooge\com\test\finagle\demo generated under the Hello.java file Very good.
Then create a new Java file to inherit the Serveriface interface in the build file
such as code:
Public classHelloimplImplementsHello.serviceiface { PublicFuture<string>hellostring (String para) {returnFuture.value (para); } PublicFuture<integer> Helloint (intpara) { returnFuture.value (para); } PublicFuture<boolean> Helloboolean (Booleanpara) { returnFuture.value (para); } PublicFuture<void>hellovoid () {returnFuture.value (NULL); } PublicFuture<string>Hellonull () {returnFuture.value (NULL); }}
OK the last step of the Main method:
Public classApp {StaticListeningserver server; Public Static voidMain (string[] args) {Integer port= 9801;//This is the finagle real listening address String zkhosts= "127.0.0.1:9000";//This is the address of the ZK server. I only have one here. If multiple units; split String Zkpath= "/soa/test/finagle"; Try{System.out.println ("Zkhosts:" + zkhosts + "\tzkpath:" + zkpath+ "\tport:" +port); Hello.serviceiface Iface=NewHelloimpl (); Server= Thrift.serveiface (Newinetsocketaddress (port), iface); String Zkfullpath= String.Format ("zk!%s!%s!0", Zkhosts, Zkpath); Server.announce (Zkfullpath); System.out.println ("Finagle Server Start"); Runtime.getruntime (). Addshutdownhook (NewThread () {@Override Public voidrun () {app.close (); } }); Await.ready (server); } Catch(Exception e) {e.printstacktrace (); System.exit (-1); } } Public Static voidClose () {System.out.println ("Finagle Server Shutdown"); Server.close (); }}
Below the client-side code: (Note: The client is not concerned about the service side of the real IP only need to ensure that ZK can be used on the line there is a concept called service discovery)
String zkhosts= "127.0.0.1:9000"; String Zkpath= "/soa/test/finagle"; = String.Format ("zk!%s!%s", Zkhosts, Zkpath); Servicefactorybyte[]> factory = thrift.newclient (zkfullpath); New New tbinaryprotocol.factory ()); = helloclient.hellostring ("Test"). get (); Assert.assertequals ("test", ret);
Finally attach demo Http://pan.baidu.com/s/1kTKsYDT
PS: Finally remember to modify the image of Maven setting file
<?XML version= "1.0" encoding= "UTF-8"?><Settingsxmlns= "http://maven.apache.org/SETTINGS/1.0.0"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <plugingroups> </plugingroups> <proxies> <Proxy> <ID>Ss</ID> <Active>False</Active> <Protocol>Socks5</Protocol> <Host>127.0.0.1</Host> <Port>1080</Port> </Proxy> </proxies> <Servers> </Servers> <Mirrors> <Mirror> <ID>Nexus</ID> <name>Nexus</name> <URL>Http://maven.oschina.net/content/groups/public</URL> <mirrorof>*</mirrorof> </Mirror> </Mirrors></Settings>
Java build Finagle (2)