zoukankan      html  css  js  c++  java
  • 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文件夹里生成 targetgenerated-sources hriftscroogecom estfinagledemo 下面生成的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 + "	zkPath:" + zkPath+"	port:"+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>
  • 相关阅读:
    iOS-字符串的连接
    [Win32]Win32 SDK编程系列文章——键盘输入消息
    [置顶] eclipse导入svn下载的项目后无法与服务器的svn项目关联
    iOS-时区 日期处理
    数学之路(3)数据分析(5)
    Filter解决中文乱码问题
    Mac OS X 10.8.3搭建Android工程源码的编译环境(解决找不到GCC、GIT、PYTHON的问题)
    paypal租用
    Java通过内部类实现回调功能
    处理9path图片边缘的小黑点
  • 原文地址:https://www.cnblogs.com/rufus-hua/p/4969863.html
Copyright © 2011-2022 走看看