提交 341ed955 编写于 作者: G guide

[refractor]zk utils

上级 31101914
...@@ -115,7 +115,7 @@ fork 项目到自己的仓库,然后克隆项目到自己的本地:`git clon ...@@ -115,7 +115,7 @@ fork 项目到自己的仓库,然后克隆项目到自己的本地:`git clon
### CheckStyle 插件下载和配置 ### CheckStyle 插件下载和配置
IntelliJ IDEA-> Preferences->Plugins->搜索下载 CheckStyle 插件,然后按照如下方式进行配置。 `IntelliJ IDEA-> Preferences->Plugins->搜索下载 CheckStyle 插件`,然后按照如下方式进行配置。
![CheckStyle 插件下载和配置](./images/setting-check-style.png) ![CheckStyle 插件下载和配置](./images/setting-check-style.png)
......
package github.javaguide.extension; package github.javaguide.extension;
import github.javaguide.utils.Holder;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader; import java.io.BufferedReader;
...@@ -88,7 +87,6 @@ public final class ExtensionLoader<T> { ...@@ -88,7 +87,6 @@ public final class ExtensionLoader<T> {
instance = (T) EXTENSION_INSTANCES.get(clazz); instance = (T) EXTENSION_INSTANCES.get(clazz);
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage()); log.error(e.getMessage());
throw new RuntimeException("Fail to create an instance of the extension class " + clazz);
} }
} }
return instance; return instance;
......
package github.javaguide.utils; package github.javaguide.extension;
public class Holder<T> { public class Holder<T> {
......
package github.javaguide.utils.file; package github.javaguide.utils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -14,8 +14,8 @@ import java.util.Properties; ...@@ -14,8 +14,8 @@ import java.util.Properties;
* @createTime 2020年07月21日 14:25:00 * @createTime 2020年07月21日 14:25:00
**/ **/
@Slf4j @Slf4j
public final class PropertiesFileUtils { public final class PropertiesFileUtil {
private PropertiesFileUtils() { private PropertiesFileUtil() {
} }
public static Properties readPropertiesFile(String fileName) { public static Properties readPropertiesFile(String fileName) {
......
...@@ -30,7 +30,7 @@ public class ZkServiceDiscovery implements ServiceDiscovery { ...@@ -30,7 +30,7 @@ public class ZkServiceDiscovery implements ServiceDiscovery {
public InetSocketAddress lookupService(String rpcServiceName) { public InetSocketAddress lookupService(String rpcServiceName) {
CuratorFramework zkClient = CuratorUtils.getZkClient(); CuratorFramework zkClient = CuratorUtils.getZkClient();
List<String> serviceUrlList = CuratorUtils.getChildrenNodes(zkClient, rpcServiceName); List<String> serviceUrlList = CuratorUtils.getChildrenNodes(zkClient, rpcServiceName);
if (serviceUrlList.size() == 0) { if (serviceUrlList == null || serviceUrlList.size() == 0) {
throw new RpcException(RpcErrorMessageEnum.SERVICE_CAN_NOT_BE_FOUND, rpcServiceName); throw new RpcException(RpcErrorMessageEnum.SERVICE_CAN_NOT_BE_FOUND, rpcServiceName);
} }
// load balancing // load balancing
......
package github.javaguide.registry.zk.util; package github.javaguide.registry.zk.util;
import github.javaguide.enums.RpcConfigEnum; import github.javaguide.enums.RpcConfigEnum;
import github.javaguide.exception.RpcException; import github.javaguide.utils.PropertiesFileUtil;
import github.javaguide.utils.file.PropertiesFileUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.curator.RetryPolicy; import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
...@@ -55,7 +54,7 @@ public final class CuratorUtils { ...@@ -55,7 +54,7 @@ public final class CuratorUtils {
} }
REGISTERED_PATH_SET.add(path); REGISTERED_PATH_SET.add(path);
} catch (Exception e) { } catch (Exception e) {
throw new RpcException(e.getMessage(), e.getCause()); log.error("create persistent node for path [{}] fail", path);
} }
} }
...@@ -69,14 +68,14 @@ public final class CuratorUtils { ...@@ -69,14 +68,14 @@ public final class CuratorUtils {
if (SERVICE_ADDRESS_MAP.containsKey(rpcServiceName)) { if (SERVICE_ADDRESS_MAP.containsKey(rpcServiceName)) {
return SERVICE_ADDRESS_MAP.get(rpcServiceName); return SERVICE_ADDRESS_MAP.get(rpcServiceName);
} }
List<String> result; List<String> result = null;
String servicePath = ZK_REGISTER_ROOT_PATH + "/" + rpcServiceName; String servicePath = ZK_REGISTER_ROOT_PATH + "/" + rpcServiceName;
try { try {
result = zkClient.getChildren().forPath(servicePath); result = zkClient.getChildren().forPath(servicePath);
SERVICE_ADDRESS_MAP.put(rpcServiceName, result); SERVICE_ADDRESS_MAP.put(rpcServiceName, result);
registerWatcher(rpcServiceName, zkClient); registerWatcher(rpcServiceName, zkClient);
} catch (Exception e) { } catch (Exception e) {
throw new RpcException(e.getMessage(), e.getCause()); log.error("get children nodes for path [{}] fail", servicePath);
} }
return result; return result;
} }
...@@ -89,7 +88,7 @@ public final class CuratorUtils { ...@@ -89,7 +88,7 @@ public final class CuratorUtils {
try { try {
zkClient.delete().forPath(p); zkClient.delete().forPath(p);
} catch (Exception e) { } catch (Exception e) {
throw new RpcException(e.getMessage(), e.getCause()); log.error("clear registry for path [{}] fail", p);
} }
}); });
log.info("All registered services on the server are cleared:[{}]", REGISTERED_PATH_SET.toString()); log.info("All registered services on the server are cleared:[{}]", REGISTERED_PATH_SET.toString());
...@@ -97,7 +96,7 @@ public final class CuratorUtils { ...@@ -97,7 +96,7 @@ public final class CuratorUtils {
public static CuratorFramework getZkClient() { public static CuratorFramework getZkClient() {
// check if user has set zk address // check if user has set zk address
Properties properties = PropertiesFileUtils.readPropertiesFile(RpcConfigEnum.RPC_CONFIG_PATH.getPropertyValue()); Properties properties = PropertiesFileUtil.readPropertiesFile(RpcConfigEnum.RPC_CONFIG_PATH.getPropertyValue());
if (properties != null) { if (properties != null) {
defaultZookeeperAddress = properties.getProperty(RpcConfigEnum.ZK_ADDRESS.getPropertyValue()); defaultZookeeperAddress = properties.getProperty(RpcConfigEnum.ZK_ADDRESS.getPropertyValue());
} }
...@@ -121,7 +120,7 @@ public final class CuratorUtils { ...@@ -121,7 +120,7 @@ public final class CuratorUtils {
* *
* @param rpcServiceName rpc service name eg:github.javaguide.HelloServicetest2version * @param rpcServiceName rpc service name eg:github.javaguide.HelloServicetest2version
*/ */
private static void registerWatcher(String rpcServiceName, CuratorFramework zkClient) { private static void registerWatcher(String rpcServiceName, CuratorFramework zkClient) throws Exception {
String servicePath = ZK_REGISTER_ROOT_PATH + "/" + rpcServiceName; String servicePath = ZK_REGISTER_ROOT_PATH + "/" + rpcServiceName;
PathChildrenCache pathChildrenCache = new PathChildrenCache(zkClient, servicePath, true); PathChildrenCache pathChildrenCache = new PathChildrenCache(zkClient, servicePath, true);
PathChildrenCacheListener pathChildrenCacheListener = (curatorFramework, pathChildrenCacheEvent) -> { PathChildrenCacheListener pathChildrenCacheListener = (curatorFramework, pathChildrenCacheEvent) -> {
...@@ -129,11 +128,7 @@ public final class CuratorUtils { ...@@ -129,11 +128,7 @@ public final class CuratorUtils {
SERVICE_ADDRESS_MAP.put(rpcServiceName, serviceAddresses); SERVICE_ADDRESS_MAP.put(rpcServiceName, serviceAddresses);
}; };
pathChildrenCache.getListenable().addListener(pathChildrenCacheListener); pathChildrenCache.getListenable().addListener(pathChildrenCacheListener);
try { pathChildrenCache.start();
pathChildrenCache.start();
} catch (Exception e) {
throw new RpcException(e.getMessage(), e.getCause());
}
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册