---
title: "norbert 高并发分布式服务例子 examples （一）"
author: "rabbit9898"
date: "2012-05-04T10:32:18Z"
tags: []
categories: ["开源软件"]
slug: "norbert-examples"
draft: false
lastmod: "2012-05-04T10:34:40Z"
id: 1508968
url: "https://rabbit9898.iteye.com/blog/1508968"
---

**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](https://github.com/rhavyn/norbert)， 这个工程是可以用maven编译的，但是好久没有人维护了；最新的应该在[https://github.com/jhartman/norbert](https://github.com/jhartman/norbert) 或者[https://github.com/linkedin/norbert](https://github.com/linkedin/norbert)下，用scala的sbt编译的。

   

    norbert的体系结构：

     
![](http://dl.iteye.com/upload/attachment/0067/5104/88ab9ee0-3315-32d4-a5a1-5632cfaca0b2.png)  
 

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

![](http://dl.iteye.com/upload/attachment/0067/5128/318f536c-c44d-3a06-b3bd-9a74274c349a.jpg)  
 

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

   目标：建立两个server节点（Node），算个加法，可以在不同的Node上实现；norbert 自带的examples Ping也可以实现，主要的程序名称：

    
![](http://dl.iteye.com/upload/attachment/0067/5137/fee27669-6782-3552-9c8c-d0ea7ff3a718.png)  
 

 

**1. 下载zookeeper，安装**

**2. Git下载[norbert](https://github.com/linkedin/norbert)  或者直接下载程序包**

    我下载的是norbert\_2.8.1  version:0.6.12

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

    

```
		<dependency>
			<groupId>com.linkedin</groupId>
			<artifactId>norbert_2.8.1</artifactId>
			<version>0.6.12</version>
		</dependency>
		<dependency>
			<groupId>org.scala-lang</groupId>
			<artifactId>scala-library</artifactId>
			<version>2.8.1</version>
		</dependency>
		<dependency>
			<groupId>org.jboss.netty</groupId>
			<artifactId>netty</artifactId>
			<version>3.2.3.Final</version>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>com.google.protobuf</groupId>
			<artifactId>protobuf-java</artifactId>
			<version>2.4.0a</version> <!--  2.3.0 2.4.0a -->
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.3.3</version>
			<scope>compile</scope>
		</dependency>
```

 

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

   可以采用examples下的Ping.

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

   Request下有public int num;

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

 

   Request和Response转换为google protocol buf消息

   编写request.proto文件： 

  

```
package norbert.exam.protobuf;

option java_package = "norbert.exam.model";
option java_outer_classname = "RequestBPO";

option optimize_for = SPEED;

message RequestMsg{
  required bytes val = 1;
}
```

 

 

  执行 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&lt;Request,Reponse&gt; 没有深入理解,看不懂scala呀  :(

 

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

  后面的部分参考examples.

 

  未完，待续......