v1.6
热补丁,nio目前来看最后的完善,使用Curator简化zookeeper的操作,优化调用体验
-
使用Curator创建服务注册和服务发现类(是看快速开始速成的)
-
服务注册类实现代码
package provider.service_registry; import constants.RpcConstants; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.retry.ExponentialBackoffRetry; import java.nio.charset.StandardCharsets; //通过curator简化 zookeeper对相应的服务端服务注册的流程 更轻松的看懂 public class ZkCuratorRegistry { private static String connectString = RpcConstants.ZOOKEEPER_ADDRESS; public static void registerMethod(String RpcServiceName, String hostname, int port) throws Exception { //创建连接 然后将对应的对象注册进去即可 //BackoffRetry 退避策略,决定失败后如何确定补偿值。 //ExponentialBackOffPolicy //指数退避策略,需设置参数sleeper、initialInterval、 // maxInterval和multiplier,initialInterval指定初始休眠时间,默认100毫秒, // maxInterval指定最大休眠时间,默认30秒,multiplier指定乘数,即下一次休眠时间为当前休眠时间*multiplier; CuratorFramework client = CuratorFrameworkFactory.newClient(connectString, new ExponentialBackoffRetry(1000, 3)); //需要启动 当注册完毕后记得关闭 不然会浪费系统资源 client.start(); //进行创建 首先先判断是否创建过service还有对应的方法路径 是这样判断 但是我可能还没有完全玩透curator //多线程问题 一定要进行加锁 synchronized (ZkCuratorRegistry.class) { if (client.checkExists().forPath("/service")==null) { client.create().forPath("/service","".getBytes(StandardCharsets.UTF_8)); } if (client.checkExists().forPath("/service/"+RpcServiceName)==null) { client.create().forPath("/service/"+RpcServiceName,"".getBytes(StandardCharsets.UTF_8)); } } String date = hostname+":"+port; client.create().forPath("/service/"+RpcServiceName+"/"+date,"0".getBytes(StandardCharsets.UTF_8)); client.close(); System.out.println("服务端:"+hostname+":"+port+":"+RpcServiceName+"方法在zkCurator中注册完毕"); } }
-
服务发现类实现代码
服务发现端没有强行改,因为要保证我们负载均衡算法的完整性,不强行的进行改动了,读者如需改动可以自己尝试,curator并不是很难 主要就加了下面这段,后面的逻辑还是和之前的一样。CuratorFramework client = CuratorFrameworkFactory.newClient(connectString, new ExponentialBackoffRetry(1000, 3)); //需要启动 当获取完毕后需要关闭相应的客户端 client.start(); //首先获取的时候 要负载均衡 ZooKeeper zooKeeper = client.getZookeeperClient().getZooKeeper();
-
-
优化调用体验
-
将部分改动代码上传
-
用户启动类
//通用启动类 将启动的逻辑藏在ClientBootStrap中 public class ClientCall { public static void main(String[] args) throws IOException, RpcException { Customer customer = ClientBootStrap.start(); //实现调用 System.out.println(customer.Hello("success")); System.out.println(customer.Bye("fail")); System.out.println(customer.Hello("fail")); } }
-
实际引导启动类
//1.2版本之前都是键盘输入 所以不是根据代理对象来进行调用的 暂时注释掉 // case "1.0": // NIOConsumerBootStrap10.main(null); // break; // case "1.1": // NIOConsumerBootStrap11.main(null); // break; case "1.2": return NIOConsumerBootStrap12.main(null); case "1.4": return NIOConsumerBootStrap14.main(null); case "1.5": return NIOConsumerBootStrap15.main(null); default: throw new RpcException("太着急了兄弟,这个版本还没出呢!要不你给我提个PR");
-
nio实际启动端
public class NIOConsumerBootStrap12 { public static Customer main(String[] args) throws IOException { RpcClientProxy clientProxy = new RpcClientProxy(); return (Customer) clientProxy.getBean(Customer.class); } }
-
-
-
总结
改动不大,用curator实现zookeepper注册中心提供更清晰的代码,效率上是没有什么差别的,降低了使用的复杂性,修改了下 几个启动类 直接让用户自己进行方法的选取调用