com.alipay.sofa.rpc.common.utils.CommonUtils Java Examples

The following examples show how to use com.alipay.sofa.rpc.common.utils.CommonUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConsulRegistry.java    From sofa-rpc with Apache License 2.0 7 votes vote down vote up
private List<NewService> buildNewServices(ProviderConfig<?> config) {
    List<ServerConfig> servers = config.getServer();
    if (CommonUtils.isEmpty(servers)) {
        return Collections.emptyList();
    }
    return servers.stream().map(server -> {
        NewService service = new NewService();
        service.setId(buildServiceId(config, server));
        service.setName(buildServiceName(config));

        String host = getServerHost(server);
        int port = server.getPort();
        service.setAddress(host);
        service.setPort(port);

        Map<String, String> metaData = RegistryUtils.convertProviderToMap(config, server).entrySet().stream()
                .filter(e -> ConsulUtils.isValidMetaKey(e.getKey()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
        service.setMeta(metaData);
        service.setTags(Collections.singletonList(buildUniqueName(config, server.getProtocol())));

        service.setCheck(buildCheck(host, port));
        return service;
    }).collect(Collectors.toList());
}
 
Example #2
Source File: ZookeeperConfigObserver.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
/**
 * 接口配置修改子节点Data列表
 *
 * @param config      接口配置
 * @param configPath  配置Path
 * @param currentData 子节点Data列表
 */
public void updateConfigAll(AbstractInterfaceConfig config, String configPath, List<ChildData> currentData) {
    if (CommonUtils.isEmpty(currentData)) {
        if (LOGGER.isInfoEnabled(config.getAppName())) {
            LOGGER.infoWithApp(config.getAppName(), "Receive updateAll data is null");
        }
    } else {
        if (LOGGER.isInfoEnabled(config.getAppName())) {
            for (ChildData data : currentData) {
                LOGGER.infoWithApp(config.getAppName(), "Receive updateAll data: path=["
                    + data.getPath() + "], data=[" + StringSerializer.decode(data.getData()) + "]"
                    + ", stat=[" + data.getStat() + "]");
            }
        }
        List<ConfigListener> configListeners = configListenerMap.get(config);
        if (CommonUtils.isNotEmpty(configListeners)) {
            List<Map<String, String>> attributes = ZookeeperRegistryHelper.convertConfigToAttributes(configPath,
                currentData);
            for (ConfigListener listener : configListeners) {
                for (Map<String, String> attribute : attributes) {
                    listener.configChanged(attribute);
                }
            }
        }
    }
}
 
Example #3
Source File: MockTestRegistry.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public void unRegister(ProviderConfig config) {
    String key = buildKey(config);
    ProviderGroup group = memoryCache.get(key);
    if (group != null) {
        List<ServerConfig> serverConfigs = config.getServer();
        if (CommonUtils.isNotEmpty(serverConfigs)) {
            for (ServerConfig server : serverConfigs) {
                group.remove(ProviderHelper.toProviderInfo(convertProviderToUrls(config, server)));
            }
        }
    }
    Map<ConsumerConfig, ProviderInfoListener> listeners = notifyListeners.get(key);
    if (listeners != null) {
        for (ProviderInfoListener listener : listeners.values()) {
            listener.updateProviders(group);
        }
    }
}
 
Example #4
Source File: ReaderTest.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Test
public void testRead() {
    Map<Class<?>, Object> interfaceMapRef = new HashMap<>();
    interfaceMapRef.put(TestSwaggerService.class, new TestSwaggerServiceImpl());
    Swagger swagger = new Swagger();
    swagger.setBasePath("/rest/");
    Reader.read(swagger, interfaceMapRef, "");

    Assert.assertEquals("2.0", swagger.getSwagger());
    Assert.assertEquals("/rest/", swagger.getBasePath());
    Map<String, Path> paths = swagger.getPaths();
    Assert.assertEquals(TestSwaggerService.class.getMethods().length, paths.size());
    Assert.assertTrue(paths.containsKey(COMPLEX));
    List<Parameter> parameters = paths.get(COMPLEX).getPost().getParameters();
    Assert.assertTrue(CommonUtils.isNotEmpty(parameters));
    Parameter parameter = parameters.get(0);
    Assert.assertTrue(parameter instanceof BodyParameter);
    Model schema = ((BodyParameter) parameter).getSchema();
    Assert.assertTrue(schema instanceof RefModel);
    String ref = ((RefModel) schema).get$ref();
    Assert.assertEquals("#/definitions/ComplexPojo", ref);

}
 
Example #5
Source File: SofaResteasyClientBuilder.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
/**
 * 注册jaxrs Provider
 *
 * @return SofaResteasyClientBuilder
 */
public SofaResteasyClientBuilder registerProvider() {
    ResteasyProviderFactory providerFactory = getProviderFactory();
    // 注册内置
    Set<Class> internalProviderClasses = JAXRSProviderManager.getInternalProviderClasses();
    if (CommonUtils.isNotEmpty(internalProviderClasses)) {
        for (Class providerClass : internalProviderClasses) {
            providerFactory.register(providerClass);
        }
    }
    // 注册自定义
    Set<Object> customProviderInstances = JAXRSProviderManager.getCustomProviderInstances();
    if (CommonUtils.isNotEmpty(customProviderInstances)) {
        for (Object provider : customProviderInstances) {
            PropertyInjector propertyInjector = providerFactory.getInjectorFactory()
                .createPropertyInjector(
                    JAXRSProviderManager.getTargetClass(provider), providerFactory);
            propertyInjector.inject(provider);
            providerFactory.registerProviderInstance(provider);
        }
    }

    return this;
}
 
Example #6
Source File: DefaultConsumerBootstrap.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public List<ProviderGroup> subscribe() {
    List<ProviderGroup> result = null;
    String directUrl = consumerConfig.getDirectUrl();
    if (StringUtils.isNotEmpty(directUrl)) {
        // 如果走直连
        result = subscribeFromDirectUrl(directUrl);
    } else {
        // 没有配置url直连
        List<RegistryConfig> registryConfigs = consumerConfig.getRegistry();
        if (CommonUtils.isNotEmpty(registryConfigs)) {
            // 从多个注册中心订阅服务列表
            result = subscribeFromRegistries();
        }
    }
    return result;
}
 
Example #7
Source File: ServerFactory.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
/**
 * 关闭全部服务端
 */
public static void destroyAll() {
    if (CommonUtils.isEmpty(SERVER_MAP)) {
        return;
    }
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("Destroy all server now!");
    }
    for (Map.Entry<String, Server> entry : SERVER_MAP.entrySet()) {
        String key = entry.getKey();
        Server server = entry.getValue();
        try {
            server.destroy();
        } catch (Exception e) {
            LOGGER.error(LogCodes.getLog(LogCodes.ERROR_DESTROY_SERVER, key), e);
        }
    }
    SERVER_MAP.clear();
}
 
Example #8
Source File: ZookeeperRegistryDataCacheImpl.java    From sofa-dashboard with Apache License 2.0 6 votes vote down vote up
@Override
public List<RpcProvider> fetchProvidersByService(String serviceName) {
    List<RpcProvider> result = new ArrayList<>();
    if (StringUtils.isEmpty(serviceName)) {
        return result;
    }
    RpcService rpcService = services.get(serviceName);

    if (rpcService != null) {
        result = providers.get(rpcService);
        if (CommonUtils.isNotEmpty(result)) {
            return result;
        }
    }
    return result;
}
 
Example #9
Source File: MulticastRegistry.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
private void clean() {
    Map<String, ProviderGroup> allProviderCache = this.allProviderCache;
    for (Map.Entry<String, ProviderGroup> entry : allProviderCache.entrySet()) {
        List<ProviderInfo> providerInfos = entry.getValue().getProviderInfos();
        if (CommonUtils.isNotEmpty(providerInfos)) {
            for (ProviderInfo providerInfo : providerInfos) {
                if (isExpired(providerInfo)) {
                    if (LOGGER.isWarnEnabled()) {
                        LOGGER.warn("Clean expired provider " + providerInfos);
                    }
                    doUnRegister(entry.getKey(), providerInfo);
                }
            }

        }
    }
}
 
Example #10
Source File: MockTestRegistry.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public void register(ProviderConfig config) {
    String key = buildKey(config);
    ProviderGroup group = memoryCache.get(key);
    if (group == null) {
        group = buildProviderGroup();
        ProviderGroup old = memoryCache.putIfAbsent(key, group);
        if (old != null) {
            group = old;
        }
    }
    List<ServerConfig> serverConfigs = config.getServer();
    if (CommonUtils.isNotEmpty(serverConfigs)) {
        for (ServerConfig server : serverConfigs) {
            group.add(ProviderHelper.toProviderInfo(convertProviderToUrls(config, server)));
        }
    }
    Map<ConsumerConfig, ProviderInfoListener> listeners = notifyListeners.get(key);
    if (listeners != null) {
        for (ProviderInfoListener listener : listeners.values()) {
            listener.updateProviders(group);
        }
    }
}
 
Example #11
Source File: ConsumerConfig.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
/**
 * 是否有并发控制需求,有就打开过滤器
 * 配置-1关闭并发过滤器,等于0表示开启过滤但是不限制
 *
 * @return 是否配置了concurrents boolean
 */
@Override
public boolean hasConcurrents() {
    if (concurrents > 0) {
        return true;
    }
    if (CommonUtils.isNotEmpty(methods)) {
        for (MethodConfig methodConfig : methods.values()) {
            if (methodConfig.getConcurrents() != null
                && methodConfig.getConcurrents() > 0) {
                return true;
            }
        }
    }
    return false;
}
 
Example #12
Source File: RegistryRouter.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public List<ProviderInfo> route(SofaRequest request, List<ProviderInfo> providerInfos) {

    //has  address. FIXME
    if (CommonUtils.isNotEmpty(providerInfos)) {
        return providerInfos;
    }

    AddressHolder addressHolder = consumerBootstrap.getCluster().getAddressHolder();
    if (addressHolder != null) {
        List<ProviderInfo> current = addressHolder.getProviderInfos(RpcConstants.ADDRESS_DEFAULT_GROUP);
        if (providerInfos != null) {
            providerInfos.addAll(current);
        } else {
            providerInfos = current;
        }
    }
    recordRouterWay(RPC_REGISTRY_ROUTER);
    return providerInfos;
}
 
Example #13
Source File: MeshRegistry.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
protected String fillProtocolAndVersion(SubscribeServiceResult subscribeServiceResult, String targetURL,
                                        String serviceName, String protocol) {

    String meshPort = judgeMeshPort(protocol);

    final List<String> datas = subscribeServiceResult.getDatas();

    if (CommonUtils.isEmpty(datas)) {
        targetURL = targetURL + ":" + meshPort;
    } else {
        for (String data : subscribeServiceResult.getDatas()) {
            final int indexOfParam = data.indexOf("?");
            if (indexOfParam != -1) {
                String param = data.substring(indexOfParam + 1);
                targetURL = targetURL + ":" + meshPort;
                targetURL = targetURL + "?" + param;
            } else {
                targetURL = targetURL + ":" + meshPort;
            }
            break;
        }
    }
    return targetURL;
}
 
Example #14
Source File: ZookeeperProviderObserver.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
private void notifyListeners(ConsumerConfig config, String providerPath, List<ChildData> currentData, boolean add)
    throws UnsupportedEncodingException {
    List<ProviderInfoListener> providerInfoListeners = providerListenerMap.get(config);
    if (CommonUtils.isNotEmpty(providerInfoListeners)) {
        List<ProviderInfo> providerInfos = ZookeeperRegistryHelper.convertUrlsToProviders(providerPath,
            currentData);
        List<ProviderInfo> providerInfosForProtocol = RegistryUtils.matchProviderInfos(config, providerInfos);
        for (ProviderInfoListener listener : providerInfoListeners) {
            if (add) {
                listener.addProvider(new ProviderGroup(providerInfosForProtocol));
            } else {
                listener.updateProviders(new ProviderGroup(providerInfosForProtocol));
            }
        }
    }
}
 
Example #15
Source File: ZookeeperRegistryHelper.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
/**
 * Convert child data to attribute list.
 *
 * @param config       the interface config
 * @param overridePath the override path
 * @param currentData  the current data
 * @return the attribute list
 * @throws UnsupportedEncodingException decode exception
 */
static List<Map<String, String>> convertOverrideToAttributes(AbstractInterfaceConfig config,
                                                             String overridePath,
                                                             List<ChildData> currentData)
    throws UnsupportedEncodingException {
    List<Map<String, String>> attributes = new ArrayList<Map<String, String>>();
    if (CommonUtils.isEmpty(currentData)) {
        return attributes;
    }

    for (ChildData childData : currentData) {
        String url = URLDecoder.decode(childData.getPath().substring(overridePath.length() + 1),
            "UTF-8");
        if (config instanceof ConsumerConfig) {
            //If child data contains system local host, convert config to attribute
            if (StringUtils.isNotEmpty(url) && StringUtils.isNotEmpty(SystemInfo.getLocalHost())
                && url.contains("://" + SystemInfo.getLocalHost() + "?")) {
                attributes.add(convertConfigToAttribute(overridePath, childData, false));
            }
        }
    }
    return attributes;
}
 
Example #16
Source File: NacosRegistryProviderObserver.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
/**
 * Update providers.
 *
 * @param config the config 
 * @param instances the instances
 */
void updateProviders(ConsumerConfig config, List<Instance> instances) {
    if (LOGGER.isInfoEnabled(config.getAppName())) {
        LOGGER.infoWithApp(config.getAppName(),
            "Receive update provider: serviceName={}, size={}, data={}",
            NacosRegistryHelper.buildServiceName(config, config.getProtocol()), instances.size(),
            instances);
    }
    List<ProviderInfoListener> providerInfoListeners = providerListenerMap.get(config);
    if (CommonUtils.isNotEmpty(providerInfoListeners)) {
        List<ProviderInfo> providerInfos = NacosRegistryHelper.convertInstancesToProviders(instances);
        List<ProviderInfo> matchProviders = RegistryUtils.matchProviderInfos(config, providerInfos);

        for (ProviderInfoListener providerInfoListener : providerInfoListeners) {
            providerInfoListener
                .updateAllProviders(Collections.singletonList(new ProviderGroup().addAll(matchProviders)));
        }
    }
}
 
Example #17
Source File: SofaRegistrySubscribeCallbackTest.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
/**
 * build config data
 * @param num
 * @return
 */
private ConfigData buildConfigPs(final int num) {

    ConfigData userData = new ConfigData() {
        @Override
        public String getData() {
            List<String> providerInfos = new ArrayList<String>();
            for (int i = 0; i < num; i++) {
                providerInfos.add("127.0.0.1:" + (20000 + i));
            }

            return CommonUtils.join(providerInfos, "#");
        }
    };

    return userData;
}
 
Example #18
Source File: AbstractCluster.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
/**
 * 检测服务节点的一些信息
 *
 * @param providerGroup 服务列表分组
 */
protected void checkProviderInfo(ProviderGroup providerGroup) {
    List<ProviderInfo> providerInfos = providerGroup == null ? null : providerGroup.getProviderInfos();
    if (CommonUtils.isEmpty(providerInfos)) {
        return;
    }
    Iterator<ProviderInfo> iterator = providerInfos.iterator();
    while (iterator.hasNext()) {
        ProviderInfo providerInfo = iterator.next();
        if (!StringUtils.equals(providerInfo.getProtocolType(), consumerConfig.getProtocol())) {
            if (LOGGER.isWarnEnabled(consumerConfig.getAppName())) {
                LOGGER.warnWithApp(consumerConfig.getAppName(),
                    "Unmatched protocol between consumer [{}] and provider [{}].",
                    consumerConfig.getProtocol(), providerInfo.getProtocolType());
            }
        }
    }
}
 
Example #19
Source File: AbstractCluster.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
/**
 * Select provider.
 *
 * @param targetIP the target ip
 * @return the provider
 */
protected ProviderInfo selectPinpointProvider(String targetIP, List<ProviderInfo> providerInfos) {
    ProviderInfo tp = convertToProviderInfo(targetIP);
    // 存在注册中心provider才会遍历
    if (CommonUtils.isNotEmpty(providerInfos)) {
        for (ProviderInfo providerInfo : providerInfos) {
            if (providerInfo.getHost().equals(tp.getHost())
                && StringUtils.equals(providerInfo.getProtocolType(), tp.getProtocolType())
                && providerInfo.getPort() == tp.getPort()) {
                return providerInfo;
            }
        }
    }
    // support direct target url
    return tp;
}
 
Example #20
Source File: DefaultProviderBootstrap.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
/**
 * for check fields and parameters of consumer config
 */
protected void checkParameters() {
    // 检查注入的ref是否接口实现类
    Class proxyClass = providerConfig.getProxyClass();
    String key = providerConfig.buildKey();
    T ref = providerConfig.getRef();
    if (!proxyClass.isInstance(ref)) {
        String name = ref == null ? "null" : ref.getClass().getName();
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_REFERENCE_AND_INTERFACE, name,
            providerConfig.getInterfaceId(), key));
    }
    // server 不能为空
    if (CommonUtils.isEmpty(providerConfig.getServer())) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_SERVER_EMPTY, key));
    }
    checkMethods(proxyClass);
}
 
Example #21
Source File: RpcConfigs.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * Remove value
 *
 * @param key Key
 */
@JustForTest
synchronized static void removeValue(String key) {
    Object oldValue = CFG.get(key);
    if (oldValue != null) {
        CFG.remove(key);
        List<RpcConfigListener> rpcConfigListeners = CFG_LISTENER.get(key);
        if (CommonUtils.isNotEmpty(rpcConfigListeners)) {
            for (RpcConfigListener rpcConfigListener : rpcConfigListeners) {
                rpcConfigListener.onChange(oldValue, null);
            }
        }
    }
}
 
Example #22
Source File: AllConnectConnectionHolder.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public void updateAllProviders(List<ProviderGroup> providerGroups) {
    List<ProviderInfo> mergePs = new ArrayList<ProviderInfo>();
    if (CommonUtils.isNotEmpty(providerGroups)) {
        for (ProviderGroup providerGroup : providerGroups) {
            if (!ProviderHelper.isEmpty(providerGroup)) {
                mergePs.addAll(providerGroup.getProviderInfos());
            }
        }
    }
    updateProviders(new ProviderGroup().addAll(mergePs));
}
 
Example #23
Source File: AllConnectConnectionHolder.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public void updateProviders(ProviderGroup providerGroup) {
    try {
        if (ProviderHelper.isEmpty(providerGroup)) {
            if (CommonUtils.isNotEmpty(currentProviderList())) {
                if (LOGGER.isInfoEnabled(consumerConfig.getAppName())) {
                    LOGGER.infoWithApp(consumerConfig.getAppName(),
                        "Clear all providers, may be this consumer has been add to blacklist");
                }
                closeAllClientTransports(null);
            }
        } else {
            Collection<ProviderInfo> nowall = currentProviderList();
            List<ProviderInfo> oldAllP = providerGroup.getProviderInfos();
            List<ProviderInfo> nowAllP = new ArrayList<ProviderInfo>(nowall);// 当前全部

            // 比较当前的和最新的
            ListDifference<ProviderInfo> diff = new ListDifference<ProviderInfo>(oldAllP, nowAllP);
            List<ProviderInfo> needAdd = diff.getOnlyOnLeft(); // 需要新建
            List<ProviderInfo> needDelete = diff.getOnlyOnRight(); // 需要删掉
            if (!needAdd.isEmpty()) {
                addNode(needAdd);
            }
            if (!needDelete.isEmpty()) {
                removeNode(needDelete);
            }
        }
    } catch (Exception e) {
        if (LOGGER.isErrorEnabled(consumerConfig.getAppName())) {
            LOGGER
                .errorWithApp(
                    consumerConfig.getAppName(),
                    LogCodes.getLog(LogCodes.ERROR_UPDATE_PROVIDERS, consumerConfig.getInterfaceId(), providerGroup),
                    e);
        }
    }
}
 
Example #24
Source File: AbstractInterfaceConfig.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 是否有token配置
 *
 * @return 是否配置了token boolean
 */
public boolean hasToken() {
    if (getParameter(RpcConstants.HIDDEN_KEY_TOKEN) != null) {
        return true;
    }
    if (CommonUtils.isNotEmpty(methods)) {
        for (MethodConfig methodConfig : methods.values()) {
            if (methodConfig.getParameter(RpcConstants.HIDDEN_KEY_TOKEN) != null) {
                return true;
            }
        }
    }
    return false;
}
 
Example #25
Source File: ZookeeperRegistryHelper.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * Convert child data to attribute list.
 *
 * @param configPath  the config path
 * @param currentData the current data
 * @return the attribute list
 */
static List<Map<String, String>> convertConfigToAttributes(String configPath,
                                                           List<ChildData> currentData) {
    List<Map<String, String>> attributes = new ArrayList<Map<String, String>>();
    if (CommonUtils.isEmpty(currentData)) {
        return attributes;
    }

    for (ChildData childData : currentData) {
        attributes.add(convertConfigToAttribute(configPath, childData, false));
    }
    return attributes;
}
 
Example #26
Source File: ZookeeperRegistryHelper.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * Convert url to provider list.
 *
 * @param providerPath
 * @param currentData  the current data
 * @return the list
 * @throws UnsupportedEncodingException decode exception
 */
static List<ProviderInfo> convertUrlsToProviders(String providerPath,
                                                 List<ChildData> currentData) throws UnsupportedEncodingException {
    List<ProviderInfo> providerInfos = new ArrayList<ProviderInfo>();
    if (CommonUtils.isEmpty(currentData)) {
        return providerInfos;
    }

    for (ChildData childData : currentData) {
        providerInfos.add(convertUrlToProvider(providerPath, childData));
    }
    return providerInfos;
}
 
Example #27
Source File: ProviderInfo.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * Sets static attribute.
 *
 * @param staticAttrs the static attribute
 * @return the static attribute
 */
public ProviderInfo setStaticAttrs(Map<String, String> staticAttrs) {
    this.staticAttrs.clear();
    if (CommonUtils.isNotEmpty(staticAttrs)) {
        this.staticAttrs.putAll(staticAttrs);
    }
    return this;
}
 
Example #28
Source File: ZookeeperAuthBoltServerTest.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
protected void createPathWithAuth() {
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    CuratorFrameworkFactory.Builder zkClientuilder = CuratorFrameworkFactory.builder()
        .connectString("127.0.0.1:2181")
        .sessionTimeoutMs(20000 * 3)
        .connectionTimeoutMs(20000)
        .canBeReadOnly(false)
        .retryPolicy(retryPolicy)
        .defaultData(null);

    //是否需要添加zk的认证信息
    Map authMap = new HashMap<String, String>();
    authMap.put("scheme", "digest");
    //如果存在多个认证信息,则在参数形式为为user1:passwd1,user2:passwd2
    authMap.put("addAuth", "sofazk:rpc1");

    List<AuthInfo> authInfos = buildAuthInfo(authMap);
    if (CommonUtils.isNotEmpty(authInfos)) {
        zkClientuilder = zkClientuilder.aclProvider(getDefaultAclProvider())
            .authorization(authInfos);
    }

    try {
        zkClient = zkClientuilder.build();
        zkClient.start();
        zkClient.create().withMode(CreateMode.PERSISTENT).forPath("/authtest");
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
 
Example #29
Source File: ZookeeperConfigObserver.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
private void notifyListeners(AbstractInterfaceConfig config, String configPath, ChildData data, boolean removeType) {
    List<ConfigListener> configListeners = configListenerMap.get(config);
    if (CommonUtils.isNotEmpty(configListeners)) {
        //转换子节点Data为接口级配置<配置属性名,配置属性值>,例如<timeout,200>
        Map<String, String> attribute = ZookeeperRegistryHelper.convertConfigToAttribute(configPath, data,
            removeType);
        for (ConfigListener listener : configListeners) {
            listener.configChanged(attribute);
        }
    }
}
 
Example #30
Source File: ZookeeperOverrideObserver.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
private void notifyListeners(AbstractInterfaceConfig config, String overridePath, ChildData data,
                             boolean removeType, AbstractInterfaceConfig interfaceConfig)
    throws Exception {
    List<ConfigListener> configListeners = configListenerMap.get(config);
    if (CommonUtils.isNotEmpty(configListeners)) {
        //转换子节点Data为IP级配置<配置属性名,配置属性值>,例如<timeout,200>
        Map<String, String> attribute = ZookeeperRegistryHelper.convertOverrideToAttribute(overridePath, data,
            removeType, interfaceConfig);
        for (ConfigListener listener : configListeners) {
            listener.attrUpdated(attribute);
        }
    }
}