java搭建finagle(2)

上篇文章是1年前写的惭愧惭愧,今天一个同事问我要demo然后看了下文章 好吧主要的代码 没写出来,今天补充下

上篇地址:http://www.cnblogs.com/rufus-hua/p/4159278.html

上篇写到 在 thrift文件夹里 新建 hello.thrift文件

如下面代码:

namespace java com.test.finagle.demo
service Hello{

    string helloString(1:string para)
    i32 helloInt(1:i32 para)
    bool helloBoolean(1:bool para)
    void helloVoid()
    string helloNull() 

} 

然后 点击编译 ,(如果不报错的)会看到在 target文件夹里生成 target\generated-sources\thrift\scrooge\com\test\finagle\demo 下面生成的hello.java文件 很好.

然后新建java文件 继承生成文件里的ServerIface接口

如代码:

public class HelloImpl implements Hello.ServiceIface {
    public Future<String> helloString(String para) {
        return Future.value(para);
    }

    public Future<Integer> helloInt(int para) {
        return Future.value(para);
    }

    public Future<Boolean> helloBoolean(boolean para) {
        return Future.value(para);
    }

    public Future<Void> helloVoid() {
        return Future.value(null);
    }

    public Future<String> helloNull() {
        return Future.value(null);
    }
}

ok 最后一步main 方法:

public class App {

    static ListeningServer server;
    public static void main(String[] args) {
        Integer port = 9801;//这是 finagle 真正的监听地址
        String zkHosts="127.0.0.1:9000";//这是zk服务器的地址 我这里只有一台 如果多台;分割
        String zkPath="/soa/test/finagle";
        try {
            System.out.println("zkHosts:" + zkHosts + "\tzkPath:" + zkPath+"\tport:"+port);
            Hello.ServiceIface iface=new HelloImpl();
            server = Thrift.serveIface(new InetSocketAddress(port), iface);
            String zkFullPath = String.format("zk!%s!%s!0", zkHosts, zkPath);
            server.announce(zkFullPath);
            System.out.println("finagle server start");
            Runtime.getRuntime().addShutdownHook(new Thread() {
                @Override
                public void run() {
                    App.close();
                }
            });
            Await.ready(server);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    public static void close() {
        System.out.println("finagle server shutdown");
        server.close();
    }
}

 

下面上 client 端代码:(注:客户端是不用关心服务端真正的ip只需要确保zk能用就行  有个概念叫服务发现)

 String zkHosts="127.0.0.1:9000";
        String zkPath="/soa/test/finagle";
        String zkFullPath = String.format("zk!%s!%s", zkHosts, zkPath);
        ServiceFactory<ThriftClientRequest, byte[]> factory = Thrift.newClient(zkFullPath);
        Hello.ServiceIface helloClient = new Hello.ServiceToClient(factory.toService(), new TBinaryProtocol.Factory());
        String ret = helloClient.helloString("test").get();
        Assert.assertEquals("test", ret);

 最后附上 demo下载地址 http://pan.baidu.com/s/1kTKsYDT

 

ps: 最后 记得修改 maven的setting文件 的镜像

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="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>

 

posted @ 2015-11-16 19:51  沐松  阅读(1850)  评论(4编辑  收藏  举报