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

 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上。

 

 

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

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

 

 

 

你可能感兴趣的:(example)