1 使用zookeeper
作为服务注册中心,服务注册并动态发现。
1.1 zk服务注册
package com.xxl.rpc.registry;
import com.xxl.rpc.util.Environment;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
/**
* zookeeper service registry
* @author xuxueli 2015-10-29 14:43:46
*/
public class ZkServiceRegistry {
private static final Logger logger = LoggerFactory.getLogger(ZkServiceRegistry.class);
// ------------------------------ zookeeper client ------------------------------
private static ZooKeeper zooKeeper;
private static ReentrantLock INSTANCE_INIT_LOCK = new ReentrantLock(true);
private static ZooKeeper getInstance(){
if (zooKeeper==null) {
try {
if (INSTANCE_INIT_LOCK.tryLock(2, TimeUnit.SECONDS)) {
try {
/*final CountDownLatch countDownLatch = new CountDownLatch(1);
countDownLatch.countDown();
countDownLatch.await();*/
zooKeeper = new ZooKeeper(Environment.ZK_ADDRESS, 30000, new Watcher() {
@Override
public void process(WatchedEvent event) {
// session expire, close old and create new
if (event.getState() == Event.KeeperState.Expired) {
try {
zooKeeper.close();
} catch (InterruptedException e) {
logger.error("", e);
}
zooKeeper = null;
}
// add One-time trigger, ZooKeeper的Watcher是一次性的,用过了需要再注册
try {
String znodePath = event.getPath();
if (znodePath != null) {
zooKeeper.exists(znodePath, true);
}
} catch (KeeperException e) {
logger.error("", e);
} catch (InterruptedException e) {
logger.error("", e);
}
}
});
logger.info(">>>>>>>>> xxl-rpc zookeeper connnect success.");
} finally {
INSTANCE_INIT_LOCK.unlock();
}
}
} catch (InterruptedException e) {
logger.error("", e);
} catch (IOException e) {
logger.error("", e);
}
}
if (zooKeeper == null) {
throw new NullPointerException(">>>>>>>>>>> xxl-rpc, zookeeper connect fail.");
}
return zooKeeper;
}
public static void destory(){
if (zooKeeper != null) {
try {
zooKeeper.close();
} catch (InterruptedException e) {
logger.error("", e);
}
}
}
// ------------------------------ register service ------------------------------
/**
* register service
*/
public static void registerServices(int port, Set<String> serviceList) throws KeeperException, InterruptedException {
// valid
if (port < 1 || (serviceList==null || serviceList.size()==0)) {
return;
}
// init address: ip : port
String ip = null;
try {
ip = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
if (ip == null) {
return;
}
String serverAddress = ip + ":" + port;
// base path
Stat stat = getInstance().exists(Environment.ZK_SERVICES_PATH, true);
if (stat == null) {
getInstance().create(Environment.ZK_SERVICES_PATH, new byte[]{}, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
// register
for (String interfaceName : serviceList) {
// init servicePath prefix : servicePath : xxl-rpc/interfaceName/serverAddress(ip01:port9999)
String ifacePath = Environment.ZK_SERVICES_PATH.concat("/").concat(interfaceName);
String addressPath = Environment.ZK_SERVICES_PATH.concat("/").concat(interfaceName).concat("/").concat(serverAddress);
// ifacePath(parent) path must be PERSISTENT
Stat ifacePathStat = getInstance().exists(ifacePath, true);
if (ifacePathStat == null) {
getInstance().create(ifacePath, new byte[]{}, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
// register service path must be EPHEMERAL
Stat addreddStat = getInstance().exists(addressPath, true);
if (addreddStat != null) {
getInstance().delete(addressPath, -1);
}
String path = getInstance().create(addressPath, serverAddress.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
logger.info(">>>>>>>>>>> xxl-rpc register service on zookeeper success, interfaceName:{}, serverAddress:{}, addressPath:{}", interfaceName, serverAddress, addressPath);
}
}
}
1.2 zk服务发现
package com.xxl.rpc.registry;
import com.xxl.rpc.util.Environment;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;
/**
* zookeeper service discovery
* @author xuxueli 2015-10-29 17:29:32
*/
public class ZkServiceDiscovery {
private static final Logger logger = LoggerFactory.getLogger(ZkServiceDiscovery.class);
// ------------------------------ zookeeper client ------------------------------
private static ZooKeeper zooKeeper;
private static ReentrantLock INSTANCE_INIT_LOCK = new ReentrantLock(true);
private static ZooKeeper getInstance(){
if (zooKeeper==null) {
try {
if (INSTANCE_INIT_LOCK.tryLock(2, TimeUnit.SECONDS)) {
try {
/*final CountDownLatch countDownLatch = new CountDownLatch(1);
countDownLatch.countDown();
countDownLatch.await();*/
zooKeeper = new ZooKeeper(Environment.ZK_ADDRESS, 30000, new Watcher() {
@Override
public void process(WatchedEvent event) {
// session expire, close old and create new
if (event.getState() == Event.KeeperState.Expired) {
try {
zooKeeper.close();
} catch (InterruptedException e) {
logger.error("", e);
}
zooKeeper = null;
}
// add One-time trigger, ZooKeeper的Watcher是一次性的,用过了需要再注册
try {
String znodePath = event.getPath();
if (znodePath != null) {
zooKeeper.exists(znodePath, true);
}
} catch (KeeperException e) {
logger.error("", e);
} catch (InterruptedException e) {
logger.error("", e);
}
// refresh service address
if (event.getType() == Event.EventType.NodeChildrenChanged || event.getState() == Event.KeeperState.SyncConnected) {
freshServiceAddress();
}
}
});
logger.info(">>>>>>>>> xxl-rpc zookeeper connnect success.");
} finally {
INSTANCE_INIT_LOCK.unlock();
}
}
} catch (InterruptedException e) {
logger.error("", e);
} catch (IOException e) {
logger.error("", e);
}
}
if (zooKeeper == null) {
throw new NullPointerException(">>>>>>>>>>> xxl-rpc, zookeeper connect fail.");
}
return zooKeeper;
}
// ------------------------------ discover service ------------------------------
private static Executor executor = Executors.newCachedThreadPool();
static {
executor.execute(new Runnable() {
@Override
public void run() {
while (true) {
freshServiceAddress();
try {
TimeUnit.SECONDS.sleep(10L);
} catch (InterruptedException e) {
logger.error("", e);
}
}
}
});
}
/**
* /xxl-rpc/iface1/address1
* /xxl-rpc/iface1/address2
* /xxl-rpc/iface1/address3
* /xxl-rpc/iface2/address1
*/
private static volatile ConcurrentMap<String, Set<String>> serviceAddress = new ConcurrentHashMap<String, Set<String>>();
public static void freshServiceAddress(){
ConcurrentMap<String, Set<String>> tempMap = new ConcurrentHashMap<String, Set<String>>();
try {
// iface list
List<String> interfaceNameList = getInstance().getChildren(Environment.ZK_SERVICES_PATH, true);
if (interfaceNameList!=null && interfaceNameList.size()>0) {
for (String interfaceName : interfaceNameList) {
// address list
String ifacePath = Environment.ZK_SERVICES_PATH.concat("/").concat(interfaceName);
List<String> addressList = getInstance().getChildren(ifacePath, true);
if (addressList!=null && addressList.size() > 0) {
Set<String> addressSet = new HashSet<String>();
for (String address : addressList) {
// data from address
String addressPath = ifacePath.concat("/").concat(address);
byte[] bytes = getInstance().getData(addressPath, false, null);
addressSet.add(new String(bytes));
}
tempMap.put(interfaceName, addressSet);
}
}
serviceAddress = tempMap;
logger.info(">>>>>>>>>>> xxl-rpc fresh serviceAddress success: {}", serviceAddress);
}
} catch (KeeperException e) {
logger.error("", e);
} catch (InterruptedException e) {
logger.error("", e);
}
}
public static String discover(String interfaceName) {
logger.debug(">>>>>>>>>>>> discover:{}", serviceAddress);
freshServiceAddress();
Set<String> addressSet = serviceAddress.get(interfaceName);
if (addressSet==null || addressSet.size()==0) {
return null;
}
String address;
List<String> addressArr = new ArrayList<String>(addressSet);
int size = addressSet.toArray().length;
if (size == 1) {
address = addressArr.get(0);
} else {
address = addressArr.get(new Random().nextInt(size));
}
return address;
}
}
改进建议:ZooKeeper的Watcher是一次性的,用过了需要再注册。我们可以不直接使用原生的zk API改用curator框架,curator之nodeCache一次注册N次监听。
1.3 改进:zk curator一次注册N次监听
package com.imooc.curator;
import java.util.List;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCache.StartMode;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.curator.retry.RetryNTimes;
public class CuratorOperator {
public CuratorFramework client = null;
public static final String zkServerPath = "192.168.1.110:2181";
/**
* 实例化zk客户端
*/
public CuratorOperator() {
/**
* 同步创建zk示例,原生api是异步的
*
* curator链接zookeeper的策略:ExponentialBackoffRetry
* baseSleepTimeMs:初始sleep的时间
* maxRetries:最大重试次数
* maxSleepMs:最大重试时间
*/
// RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 5);
/**
* curator链接zookeeper的策略:RetryNTimes
* n:重试的次数
* sleepMsBetweenRetries:每次重试间隔的时间
*/
RetryPolicy retryPolicy = new RetryNTimes(3, 5000);
/**
* curator链接zookeeper的策略:RetryOneTime
* sleepMsBetweenRetry:每次重试间隔的时间
*/
// RetryPolicy retryPolicy2 = new RetryOneTime(3000);
/**
* 永远重试,不推荐使用
*/
// RetryPolicy retryPolicy3 = new RetryForever(retryIntervalMs)
/**
* curator链接zookeeper的策略:RetryUntilElapsed
* maxElapsedTimeMs:最大重试时间
* sleepMsBetweenRetries:每次重试间隔
* 重试时间超过maxElapsedTimeMs后,就不再重试
*/
// RetryPolicy retryPolicy4 = new RetryUntilElapsed(2000, 3000);
client = CuratorFrameworkFactory.builder()
.connectString(zkServerPath)
.sessionTimeoutMs(10000).retryPolicy(retryPolicy)
.namespace("workspace").build();
client.start();
}
/**
*
* @Description: 关闭zk客户端连接
*/
public void closeZKClient() {
if (client != null) {
this.client.close();
}
}
public static void main(String[] args) throws Exception {
// 实例化
CuratorOperator cto = new CuratorOperator();
boolean isZkCuratorStarted = cto.client.isStarted();
System.out.println("当前客户的状态:" + (isZkCuratorStarted ? "连接中" : "已关闭"));
// 创建节点
String nodePath = "/super/imooc";
// byte[] data = "superme".getBytes();
// cto.client.create().creatingParentsIfNeeded()
// .withMode(CreateMode.PERSISTENT)
// .withACL(Ids.OPEN_ACL_UNSAFE)
// .forPath(nodePath, data);
// 更新节点数据
// byte[] newData = "batman".getBytes();
// cto.client.setData().withVersion(0).forPath(nodePath, newData);
// 删除节点
// cto.client.delete()
// .guaranteed() // 如果删除失败,那么在后端还是继续会删除,直到成功
// .deletingChildrenIfNeeded() // 如果有子节点,就删除
// .withVersion(0)
// .forPath(nodePath);
// 读取节点数据
// Stat stat = new Stat();
// byte[] data = cto.client.getData().storingStatIn(stat).forPath(nodePath);
// System.out.println("节点" + nodePath + "的数据为: " + new String(data));
// System.out.println("该节点的版本号为: " + stat.getVersion());
// 查询子节点
// List<String> childNodes = cto.client.getChildren()
// .forPath(nodePath);
// System.out.println("开始打印子节点:");
// for (String s : childNodes) {
// System.out.println(s);
// }
// 判断节点是否存在,如果不存在则为空
// Stat statExist = cto.client.checkExists().forPath(nodePath + "/abc");
// System.out.println(statExist);
// watcher 事件 当使用usingWatcher的时候,监听只会触发一次,监听完毕后就销毁
// cto.client.getData().usingWatcher(new MyCuratorWatcher()).forPath(nodePath);
// cto.client.getData().usingWatcher(new MyWatcher()).forPath(nodePath);
// 为节点添加watcher
// NodeCache: 监听数据节点的变更,会触发事件
// final NodeCache nodeCache = new NodeCache(cto.client, nodePath);
// // buildInitial : 初始化的时候获取node的值并且缓存
// nodeCache.start(true);
// if (nodeCache.getCurrentData() != null) {
// System.out.println("节点初始化数据为:" + new String(nodeCache.getCurrentData().getData()));
// } else {
// System.out.println("节点初始化数据为空...");
// }
// nodeCache.getListenable().addListener(new NodeCacheListener() {
// public void nodeChanged() throws Exception {
// if (nodeCache.getCurrentData() == null) {
// System.out.println("空");
// return;
// }
// String data = new String(nodeCache.getCurrentData().getData());
// System.out.println("节点路径:" + nodeCache.getCurrentData().getPath() + "数据:" + data);
// }
// });
// 为子节点添加watcher
// PathChildrenCache: 监听数据节点的增删改,会触发事件
String childNodePathCache = nodePath;
// cacheData: 设置缓存节点的数据状态
final PathChildrenCache childrenCache = new PathChildrenCache(cto.client, childNodePathCache, true);
/**
* StartMode: 初始化方式
* POST_INITIALIZED_EVENT:异步初始化,初始化之后会触发事件
* NORMAL:异步初始化
* BUILD_INITIAL_CACHE:同步初始化
*/
childrenCache.start(StartMode.POST_INITIALIZED_EVENT);
List<ChildData> childDataList = childrenCache.getCurrentData();
System.out.println("当前数据节点的子节点数据列表:");
for (ChildData cd : childDataList) {
String childData = new String(cd.getData());
System.out.println(childData);
}
childrenCache.getListenable().addListener(new PathChildrenCacheListener() {
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
if(event.getType().equals(PathChildrenCacheEvent.Type.INITIALIZED)){
System.out.println("子节点初始化ok...");
}
else if(event.getType().equals(PathChildrenCacheEvent.Type.CHILD_ADDED)){
String path = event.getData().getPath();
if (path.equals(ADD_PATH)) {
System.out.println("添加子节点:" + event.getData().getPath());
System.out.println("子节点数据:" + new String(event.getData().getData()));
} else if (path.equals("/super/imooc/e")) {
System.out.println("添加不正确...");
}
}else if(event.getType().equals(PathChildrenCacheEvent.Type.CHILD_REMOVED)){
System.out.println("删除子节点:" + event.getData().getPath());
}else if(event.getType().equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)){
System.out.println("修改子节点路径:" + event.getData().getPath());
System.out.println("修改子节点数据:" + new String(event.getData().getData()));
}
}
});
Thread.sleep(100000);
cto.closeZKClient();
boolean isZkCuratorStarted2 = cto.client.isStarted();
System.out.println("当前客户的状态:" + (isZkCuratorStarted2 ? "连接中" : "已关闭"));
}
public final static String ADD_PATH = "/super/imooc/d";
}
2 TCP/HTTP通讯
TCP通讯可以执行NETTY或MINA作为可选通讯方案,以提供高效的服务通讯支持。
2.1 作者提供了四种备选方案
TCP通讯:netty、mina
HTTP通讯:jetty、servlet
2.2 改进:不关心大而全,只关注性能和安全
改进建议:完全没有必要,既然我们是代理的,就应该把最好用的netty给我们的调用方;既然我们是屏蔽了底层,就应该把性能最好的netty给我们的实现方。
3 序列化
3.1 jvm序列化性能测试
https://github.com/eishay/jvm-serializers/wiki
3.2 支持hessian、protobuf和jackson等多种序列化方案
3.2.1 Serializer模板方法模式
package com.xxl.rpc.serialize;
import com.xxl.rpc.serialize.impl.HessianSerializer;
import com.xxl.rpc.serialize.impl.JacksonSerializer;
import com.xxl.rpc.serialize.impl.ProtostuffSerializer;
import java.util.HashMap;
import java.util.Map;
/**
* 序列化器
* Tips:模板方法模式:
* 定义:定义一个操作中算法的骨架(或称为顶级逻辑),将一些步骤(或称为基本方法)的执行延迟到其子类中;
* 基本方法:抽象方法 + 具体方法final + 钩子方法;
* @author xuxueli 2015-10-30 21:02:55
*/
public abstract class Serializer {
public abstract <T> byte[] serialize(T obj);
public abstract <T> Object deserialize(byte[] bytes, Class<T> clazz);
public enum SerializeEnum {
HESSIAN(new HessianSerializer()),
PROTOSTUFF(new ProtostuffSerializer()),
JSON(new JacksonSerializer());
public final Serializer serializer;
private SerializeEnum (Serializer serializer) {
this.serializer = serializer;
}
public static SerializeEnum match(String name, SerializeEnum defaultSerializer){
for (SerializeEnum item : SerializeEnum.values()) {
if (item.name().equals(name)) {
return item;
}
}
return defaultSerializer;
}
}
public static void main(String[] args) {
Serializer serializer = SerializeEnum.match("HESSIAN", null).serializer;
System.out.println(serializer);
try {
Map<String, String> map = new HashMap<String, String>();
map.put("aaa", "111");
map.put("bbb", "222");
System.out.println(serializer.deserialize(serializer.serialize("ddddddd"), String.class));
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.2.2 HessianSerializer
package com.xxl.rpc.serialize.impl;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;
import com.xxl.rpc.serialize.Serializer;
/**
* hessian serialize
* @author xuxueli 2015-9-26 02:53:29
*/
public class HessianSerializer extends Serializer {
@Override
public <T> byte[] serialize(T obj){
ByteArrayOutputStream os = new ByteArrayOutputStream();
HessianOutput ho = new HessianOutput(os);
try {
ho.writeObject(obj);
} catch (IOException e) {
throw new IllegalStateException(e.getMessage(), e);
}
return os.toByteArray();
}
@Override
public <T> Object deserialize(byte[] bytes, Class<T> clazz) {
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
HessianInput hi = new HessianInput(is);
try {
return hi.readObject();
} catch (IOException e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
}
3.2.3 JacksonSerializer
package com.xxl.rpc.serialize.impl;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xxl.rpc.serialize.Serializer;
/**
* Jackson工具类
* 1、obj need private and set/get;2、do not support inner class;
* @author xuxueli 2015-9-25 18:02:56
*/
public class JacksonSerializer extends Serializer {
private final static ObjectMapper objectMapper = new ObjectMapper();
/** bean、array、List、Map --> json
* @param <T>*/
@Override
public <T> byte[] serialize(T obj) {
try {
return objectMapper.writeValueAsBytes(obj);
} catch (JsonProcessingException e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
/** string --> bean、Map、List(array) */
@Override
public <T> Object deserialize(byte[] bytes, Class<T> clazz) {
try {
return objectMapper.readValue(bytes, clazz);
} catch (JsonParseException e) {
throw new IllegalStateException(e.getMessage(), e);
} catch (JsonMappingException e) {
throw new IllegalStateException(e.getMessage(), e);
} catch (IOException e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
}
3.2.4 ProtostuffSerializer
package com.xxl.rpc.serialize.impl;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.objenesis.Objenesis;
import org.objenesis.ObjenesisStd;
import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.Schema;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
import com.xxl.rpc.serialize.Serializer;
/**
* Protostuff util
* xuxueli 2015-10-29 18:53:43
*/
public class ProtostuffSerializer extends Serializer {
private static Objenesis objenesis = new ObjenesisStd(true);
private static Map<Class<?>, Schema<?>> cachedSchema = new ConcurrentHashMap<Class<?>, Schema<?>>();
private static <T> Schema<T> getSchema(Class<T> cls) {
@SuppressWarnings("unchecked")
Schema<T> schema = (Schema<T>) cachedSchema.get(cls);
if (schema == null) {
schema = RuntimeSchema.createFrom(cls);
if (schema != null) {
cachedSchema.put(cls, schema);
}
}
return schema;
}
@Override
public <T> byte[] serialize(T obj) {
@SuppressWarnings("unchecked")
Class<T> cls = (Class<T>) obj.getClass();
LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
try {
Schema<T> schema = getSchema(cls);
return ProtostuffIOUtil.toByteArray(obj, schema, buffer);
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
} finally {
buffer.clear();
}
}
@Override
public <T> Object deserialize(byte[] bytes, Class<T> clazz) {
try {
T message = (T) objenesis.newInstance(clazz);
Schema<T> schema = getSchema(clazz);
ProtostuffIOUtil.mergeFrom(bytes, message, schema);
return message;
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
}
3.3 改进:不关心大而全,只关注序列化和反序列化时间、空间大小
改进建议:通过上面的在Github上的测试结果,综合比较还是protostuff完胜。
<!-- protostuff -->
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>1.1.3</version>
</dependency>
<!-- objenesis(support protostuff) -->
<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>2.6</version>
</dependency>