ITeye博客

norbert 高并发分布式服务例子 examples (二)

rabbit9898 · 2012-05-04 11:05 · norbert · 分布式 · senseidb · 开源软件 · Markdown

iteye编辑器在IE8下太不好用了,写了保存到草稿里结果再编辑格式就都没了,晕

接上面的 http://rabbit9898.iteye.com/blog/1508968 ,继续

 5. 写服务器端代码

servernode1.java

package norbert.exam.server;
import norbert.exam.util.ServerUtil;
import com.linkedin.norbert.javacompat.cluster.ClusterClient;

public class ServerNode1 {
    public static void main(String[] args)
    {
    	//创建clusterClient
        ClusterClient cc = ServerUtil.configCluster(ServerUtil.clusterName, ServerUtil.zkConnectStr);
        //添加node1,端口绑定5001
        cc.removeNode(1);
        cc.addNode(1, "localhost:5001");
        //启动node1
        ServerUtil.startServer(ServerUtil.clusterName, 1, ServerUtil.zkConnectStr);
    }
}

 其中在ServerUtil.startServer中有:

   public static void startServer(String serviceName, int nodeId, String zkConnectStr)
     {
         NetworkServerConfig config = new NetworkServerConfig();
         config.setServiceName(serviceName);
         ...

         NetworkServer ns = new NettyNetworkServer(config);

         //server中绑定需要处理的消息类型和功能

         ns.registerHandler(new NodePingRequestHandler(), new PingSerializer());
         ns.registerHandler(new NodeSumRequestHandler(), new ReqProtoSerializer());

         ns.bind(nodeId);
     }

 

NodeSumRequestHandler.java 代码,实现加和: 

 

public class NodeSumRequestHandler implements RequestHandler<Request,Reponse>{

	@Override
	public Reponse handleRequest(Request request) throws Exception {
		Reponse r= new Reponse();
		if(request!=null){
			r.setSum(request.num + request.num);
		}
		System.out.println("receive request=" + request.num +" sum="+ r.getSum());
		return r;
	}
}

 

同样创建servernode2.java

 

 6. 写客户端测试调用代码

ClientSum.java

 

public class ClientSum {
     public static void main(String[] args)
    {
        NetworkClientConfig config = new NetworkClientConfig();
        config.setServiceName(ServerUtil.clusterName);
        config.setZooKeeperConnectString(ServerUtil.zkConnectStr);
       //....
       
      //采用roundrobin策略 随机调用node上的功能
        NetworkClient nc = new NettyNetworkClient(config, new RoundRobinLoadBalancerFactory());

        int num =3;
        final Request request = new Request(num);
        Future<Reponse> responseFuture = nc.sendRequest(request, new ReqProtoSerializer());

        try
        {
            final Reponse sumResp = responseFuture.get();
            System.out.println("num=" + num +" ; got sum resp: " + sumResp.getSum());
        }
        catch( InterruptedException e )
        {...   }
}

 7. 客户端测试Server调用

  启动servernode1和servernode2,可以看到:

 servernode1的控制台输出
 

connected to cluster: []
nodes changed,current node size= 0
nodes changed,current node size= 0
nodes changed,current node size= 0
nodes changed,current node size= 0
nodes changed,current node size= 1 node: JavaNode(1,localhost:5001,true,[]); 
nodes changed,current node size= 1 node: JavaNode(1,localhost:5001,true,[]); 
nodes changed,current node size= 1 node: JavaNode(1,localhost:5001,true,[]); 
nodes changed,current node size= 2 node: JavaNode(2,localhost:5002,true,[]);  node: JavaNode(1,localhost:5001,true,[]); 

 启动ClientSum.java 控制台输出:

 

num=3 ; got sum resp: 6

 在servernode1 或者servernode2下可以看到多了一行输出:

 

receive request=3 sum=6

 

多启动几次ClientSum.java ,可以看到会分发到不同的node上。

 

 

转载请标明出处。上程序附件。

==================完=====================

评论(4)

  1. #4 楼 · baso4233 · 2014-03-17 15:27

    非常感谢,我跑通了。 途中出现了, java.lang.UnsupportedOperationException: This is supposed to be overridden by subclasses. 错误,主要是我用了 protobuf 2.5.0 太高了,大伙在使用本例子的时候最好还是安装 2.4.1 的。

  2. #3 楼 · a14 · 2012-11-07 17:19

    好厉害,你好,看到你的文章,自己按着做了,出现了写问题,我是个新手,资料也挺少的,不知道能不到加一个QQ:790115315,到时候联系联系,谢谢了,我是杭州的,有机会到这边玩也可以联系我哈

  3. #2 楼 · a14 · 2012-11-07 15:02

    a14 writes:

    你好,这个运行的时候报错:2012-11-07 14:37:40,886 Opening socket connection to server 10.10.10.89/10.10.10.89:2181 2012-11-07 14:37:41,886 Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect java.net.ConnectException: Connection refused: no further information at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:567) at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1119) 2012-11-07 14:37:43,568 Opening socket connection to server 10.10.10.89/10.10.10.89:2181 大概是什么原因,知道吗?新手,很多不知道啊

    这个知道了,是zookeper没启动

  4. #1 楼 · a14 · 2012-11-07 14:40

    你好,这个运行的时候报错:2012-11-07 14:37:40,886 Opening socket connection to server 10.10.10.89/10.10.10.89:2181 2012-11-07 14:37:41,886 Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect java.net.ConnectException: Connection refused: no further information at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:567) at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1119) 2012-11-07 14:37:43,568 Opening socket connection to server 10.10.10.89/10.10.10.89:2181 大概是什么原因,知道吗?新手,很多不知道啊

评论功能维护中,当前仅展示历史评论。