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

来源:互联网 发布:moment.js 时间差 编辑:程序博客网 时间:2024/05/17 01:30

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

    博客分类:
  • norbert
  • LinkedIn
  • 分布式
  • senseidb
 

Norbert简介
      norbert是一个提供分布式集群服务的开发框架,具备集群管理功能,是一个开发简单的通信架构,易扩展能承受高吞吐量的框架。
     norbert 应该是用scala来实现,对Netty, Protocol Buffers and Zookeeper的一个封装。 其中集群管理的功能利用了zookeeper来进行node状态的感知,通讯采用了nio的netty server,序列化采用的是Protocol Buffers。虽然是用scala来写的,但是对java有良好的兼容性。linkedin在他们的架构里面都用到了这个框架。

    norbert的最早版本应该在 https://github.com/rhavyn/norbert, 这个工程是可以用maven编译的,但是好久没有人维护了;最新的应该在https://github.com/jhartman/norbert 或者https://github.com/linkedin/norbert下,用scala的sbt编译的。

   

    norbert的体系结构:

   

 

 linkedin发布的分布式搜索senseidb使用的就是norbert,因此想要了解senseidb,最好还是先了解一下norbert的example。senseidb的体系结构:



 

    我参考norbert examples目录下的例子做了个工程,来说一下norbert 分布式搭建:

   目标:建立两个server节点(Node),算个加法,可以在不同的Node上实现;norbert 自带的examples Ping也可以实现,主要的程序名称:

  

 

 

1. 下载zookeeper,安装

2. Git下载norbert  或者直接下载程序包

    我下载的是norbert_2.8.1  version:0.6.12

3. 建立maven工程,pom文件内容

    

Xml代码  收藏代码
  1. <dependency>  
  2.     <groupId>com.linkedin</groupId>  
  3.     <artifactId>norbert_2.8.1</artifactId>  
  4.     <version>0.6.12</version>  
  5. </dependency>  
  6. <dependency>  
  7.     <groupId>org.scala-lang</groupId>  
  8.     <artifactId>scala-library</artifactId>  
  9.     <version>2.8.1</version>  
  10. </dependency>  
  11. <dependency>  
  12.     <groupId>org.jboss.netty</groupId>  
  13.     <artifactId>netty</artifactId>  
  14.     <version>3.2.3.Final</version>  
  15.     <scope>compile</scope>  
  16. </dependency>  
  17. <dependency>  
  18.     <groupId>com.google.protobuf</groupId>  
  19.     <artifactId>protobuf-java</artifactId>  
  20.     <version>2.4.0a</version> <!--  2.3.0 2.4.0a -->  
  21.     <scope>compile</scope>  
  22. </dependency>  
  23. <dependency>  
  24.     <groupId>org.apache.zookeeper</groupId>  
  25.     <artifactId>zookeeper</artifactId>  
  26.     <version>3.3.3</version>  
  27.     <scope>compile</scope>  
  28. </dependency>  

 

   4.定义客户端和服务器端的消息

   可以采用examples下的Ping.

   自己添加了Request和Response消息类型。

   Request下有public int num;

   Response下有public int sum; 存放Request消息中计算加和。

 

   Request和Response转换为google protocol buf消息

   编写request.proto文件: 

  

Protobuf代码  收藏代码
  1. package norbert.exam.protobuf;  
  2.   
  3. option java_package = "norbert.exam.model";  
  4. option java_outer_classname = "RequestBPO";  
  5.   
  6. option optimize_for = SPEED;  
  7.   
  8. message RequestMsg{  
  9.   required bytes val = 1;  
  10. }  

 

 

  执行 protoc.exe --java_out=..\src\main\java\ request.proto  自动生成RequestBPO.java (protoc.exe程序去protocol buf里下载,我用的版本是2.4.1 )

  同理生成ReponseBPO.java

 

  定义消息完后,需要实现import com.linkedin.norbert.network.Serializer;

    

  不过在本例中没有怎么使用protocol buf中的序列化来转换消息。  对里面的Serializer<Request,Reponse> 没有深入理解,看不懂scala呀  :(

 

  前面部分参考http://tech.optify.net/building-a-server-cluster-with-linkedins-norbert/

  后面的部分参考examples.

 

  未完,待续......

0 0