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

[refractor]zk utils

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