com.alipay.sofa.rpc.config.AbstractInterfaceConfig Java Examples

The following examples show how to use com.alipay.sofa.rpc.config.AbstractInterfaceConfig. 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: ZookeeperOverrideObserver.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
/**
 * 接口配置删除子节点Data
 *
 * @param config         接口配置
 * @param overridePath   覆盖Path
 * @param data           子节点Data
 * @param registerConfig 注册配置
 * @throws Exception 转换配置异常
 */
public void removeConfig(AbstractInterfaceConfig config, String overridePath, ChildData data,
                         AbstractInterfaceConfig registerConfig)
    throws Exception {
    if (data == null) {
        if (LOGGER.isInfoEnabled(config.getAppName())) {
            LOGGER.infoWithApp(config.getAppName(), "Receive data is null");
        }
    } else if (registerConfig == null) {
        if (LOGGER.isInfoEnabled(config.getAppName())) {
            LOGGER.infoWithApp(config.getAppName(), "Register config is null");
        }
    } else {
        if (LOGGER.isInfoEnabled(config.getAppName())) {
            LOGGER.infoWithApp(config.getAppName(), "Receive data: path=[" + data.getPath() + "]" + ", data=[" +
                StringSerializer.decode(data.getData()) + "]" + ", stat=[" + data.getStat() + "]");
        }
        notifyListeners(config, overridePath, data, true, registerConfig);
    }
}
 
Example #2
Source File: HystrixFilter.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public boolean needToLoad(FilterInvoker invoker) {
    AbstractInterfaceConfig config = invoker.getConfig();
    // 只支持 consumer 侧
    if (!isConsumerSide(config)) {
        if (LOGGER.isWarnEnabled(config.getAppName())) {
            LOGGER.warnWithApp(config.getAppName(), "HystrixFilter is not allowed on provider, interfaceId: {}",
                config.getInterfaceId());
        }
        return false;
    }
    if (!isHystrixEnabled(config)) {
        return false;
    }
    if (!isHystrixOnClasspath()) {
        if (LOGGER.isWarnEnabled(config.getAppName())) {
            LOGGER
                .warnWithApp(config.getAppName(),
                    "HystrixFilter is disabled because 'com.netflix.hystrix:hystrix-core' does not exist on the classpath");
        }
        return false;
    }
    return true;
}
 
Example #3
Source File: BeanIdMatchFilterTest.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Test
public void testBeanIdMatch() {
    TestCustomizeFilter testCustomizeFilter = new TestCustomizeFilter();
    testCustomizeFilter.setIdRule("AAA,!BBB");
    Assert.assertEquals("AAA,!BBB", testCustomizeFilter.getIdRule());

    AbstractInterfaceConfig configA = new ProviderConfig();
    configA.setInterfaceId(Serializer.class.getName());
    configA.setId("AAA");
    FilterInvoker filterInvokerA = new FilterInvoker(null, null, configA);

    AbstractInterfaceConfig configB = new ProviderConfig();
    configB.setInterfaceId(Serializer.class.getName());
    configB.setId("BBB");
    FilterInvoker filterInvokerB = new FilterInvoker(null, null, configB);

    AbstractInterfaceConfig configC = new ProviderConfig();
    configC.setInterfaceId(Serializer.class.getName());
    configC.setId("CCC");
    FilterInvoker filterInvokerC = new FilterInvoker(null, null, configC);

    Assert.assertEquals(true, testCustomizeFilter.needToLoad(filterInvokerA));
    Assert.assertEquals(false, testCustomizeFilter.needToLoad(filterInvokerB));
    Assert.assertEquals(true, testCustomizeFilter.needToLoad(filterInvokerC));

}
 
Example #4
Source File: BeanIdMatchFilter.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public boolean needToLoad(FilterInvoker invoker) {
    AbstractInterfaceConfig config = invoker.config;
    String invokerId = config.getId();
    if (!formatComplete) {
        synchronized (formatLock) {
            if (!formatComplete) {
                formatId(idRule);
                formatComplete = true;
            }
        }
    }

    return isMatch(invokerId);

}
 
Example #5
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 #6
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 #7
Source File: ZookeeperRegistry.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 订阅IP级配置(服务发布暂时不支持动态配置,暂时支持订阅ConsumerConfig参数设置)
 *
 * @param config   consumer config
 * @param listener config listener
 */
protected void subscribeOverride(final ConsumerConfig config, ConfigListener listener) {
    try {
        if (overrideObserver == null) { // 初始化
            overrideObserver = new ZookeeperOverrideObserver();
        }
        overrideObserver.addConfigListener(config, listener);
        final String overridePath = buildOverridePath(rootPath, config);
        final AbstractInterfaceConfig registerConfig = getRegisterConfig(config);
        // 监听配置节点下 子节点增加、子节点删除、子节点Data修改事件
        PathChildrenCache pathChildrenCache = new PathChildrenCache(zkClient, overridePath, true);
        pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
            @Override
            public void childEvent(CuratorFramework client1, PathChildrenCacheEvent event) throws Exception {
                if (LOGGER.isDebugEnabled(config.getAppName())) {
                    LOGGER.debug("Receive zookeeper event: " + "type=[" + event.getType() + "]");
                }
                switch (event.getType()) {
                    case CHILD_ADDED: //新增IP级配置
                        overrideObserver.addConfig(config, overridePath, event.getData());
                        break;
                    case CHILD_REMOVED: //删除IP级配置
                        overrideObserver.removeConfig(config, overridePath, event.getData(), registerConfig);
                        break;
                    case CHILD_UPDATED:// 更新IP级配置
                        overrideObserver.updateConfig(config, overridePath, event.getData());
                        break;
                    default:
                        break;
                }
            }
        });
        pathChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
        INTERFACE_OVERRIDE_CACHE.put(overridePath, pathChildrenCache);
        overrideObserver.updateConfigAll(config, overridePath, pathChildrenCache.getCurrentData());
    } catch (Exception e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_SUB_PROVIDER_OVERRIDE, EXT_NAME), e);
    }
}
 
Example #8
Source File: ZookeeperOverrideObserver.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 接口配置修改子节点Data
 *
 * @param config       接口配置
 * @param overridePath 覆盖Path
 * @param data         子节点Data
 * @throws Exception 转换配置异常
 */
public void updateConfig(AbstractInterfaceConfig config, String overridePath, ChildData data) throws Exception {
    if (data == null) {
        if (LOGGER.isInfoEnabled(config.getAppName())) {
            LOGGER.infoWithApp(config.getAppName(), "Receive update data is null");
        }
    } else {
        if (LOGGER.isInfoEnabled(config.getAppName())) {
            LOGGER.infoWithApp(config.getAppName(), "Receive update data: path=[" + data.getPath() + "]" +
                ", data=[" + StringSerializer.decode(data.getData()) + "]" + ", stat=[" + data.getStat() + "]");
        }
        notifyListeners(config, overridePath, data, false, null);
    }
}
 
Example #9
Source File: ZookeeperOverrideObserver.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 接口配置修改子节点Data列表
 *
 * @param config       接口配置
 * @param overridePath 覆盖Path
 * @param currentData  子节点Data列表
 * @throws UnsupportedEncodingException 转换配置异常
 */
public void updateConfigAll(AbstractInterfaceConfig config, String overridePath, List<ChildData> currentData)
    throws UnsupportedEncodingException {
    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.convertOverrideToAttributes(
                config, overridePath, currentData);
            for (ConfigListener listener : configListeners) {
                for (Map<String, String> attribute : attributes) {
                    listener.attrUpdated(attribute);
                }
            }
        }
    }
}
 
Example #10
Source File: ZookeeperOverrideObserver.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 接口配置新增子节点Data
 *
 * @param config       接口配置
 * @param overridePath 覆盖Path
 * @param data         子节点Data
 * @throws Exception 转换配置异常
 */
public void addConfig(AbstractInterfaceConfig config, String overridePath, ChildData data) throws Exception {
    if (data == null) {
        if (LOGGER.isInfoEnabled(config.getAppName())) {
            LOGGER.infoWithApp(config.getAppName(), "Receive data is null");
        }
    } else {
        if (LOGGER.isInfoEnabled(config.getAppName())) {
            LOGGER.infoWithApp(config.getAppName(), "Receive add data: path=[" + data.getPath() + "]" + ", data=[" +
                StringSerializer.decode(data.getData()) + "]" + ", stat=[" + data.getStat() + "]");
        }
        notifyListeners(config, overridePath, data, false, null);
    }
}
 
Example #11
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);
        }
    }
}
 
Example #12
Source File: ZookeeperRegistryHelper.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * Convert child data to attribute.
 *
 * @param overridePath    the override path
 * @param childData       the child data
 * @param removeType      is remove type
 * @param interfaceConfig register provider/consumer config
 * @return the attribute
 * @throws Exception decode exception
 */
static Map<String, String> convertOverrideToAttribute(String overridePath, ChildData childData,
                                                      boolean removeType,
                                                      AbstractInterfaceConfig interfaceConfig) throws Exception {
    String url = URLDecoder.decode(childData.getPath().substring(overridePath.length() + 1),
        "UTF-8");
    Map<String, String> attribute = new ConcurrentHashMap<String, String>();
    for (String keyPairs : url.substring(url.indexOf('?') + 1).split("&")) {
        String[] overrideAttrs = keyPairs.split("=");
        // TODO 这个列表待确认,不少字段是不支持的
        List<String> configKeys = Arrays.asList(RpcConstants.CONFIG_KEY_TIMEOUT,
            RpcConstants.CONFIG_KEY_SERIALIZATION, RpcConstants.CONFIG_KEY_LOADBALANCER);
        if (configKeys.contains(overrideAttrs[0])) {
            if (removeType) {
                Class clazz = null;
                if (interfaceConfig instanceof ProviderConfig) {
                    // TODO 服务端也生效?
                    clazz = ProviderConfig.class;
                } else if (interfaceConfig instanceof ConsumerConfig) {
                    clazz = ConsumerConfig.class;
                }
                if (clazz != null) {
                    Method getMethod = ReflectUtils.getPropertyGetterMethod(clazz,
                        overrideAttrs[0]);
                    Class propertyClazz = getMethod.getReturnType();
                    //If event type is CHILD_REMOVED, attribute should return to register value
                    attribute.put(overrideAttrs[0], StringUtils.toString(BeanUtils
                        .getProperty(interfaceConfig, overrideAttrs[0], propertyClazz)));
                }
            } else {
                attribute.put(overrideAttrs[0], overrideAttrs[1]);
            }
        }
    }
    return attribute;
}
 
Example #13
Source File: ZookeeperRegistry.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 订阅接口级配置
 *
 * @param config   provider/consumer config
 * @param listener config listener
 */
protected void subscribeConfig(final AbstractInterfaceConfig config, ConfigListener listener) {
    try {
        if (configObserver == null) { // 初始化
            configObserver = new ZookeeperConfigObserver();
        }
        configObserver.addConfigListener(config, listener);
        final String configPath = buildConfigPath(rootPath, config);
        // 监听配置节点下 子节点增加、子节点删除、子节点Data修改事件
        PathChildrenCache pathChildrenCache = new PathChildrenCache(zkClient, configPath, true);
        pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
            @Override
            public void childEvent(CuratorFramework client1, PathChildrenCacheEvent event) throws Exception {
                if (LOGGER.isDebugEnabled(config.getAppName())) {
                    LOGGER.debug("Receive zookeeper event: " + "type=[" + event.getType() + "]");
                }
                switch (event.getType()) {
                    case CHILD_ADDED: //新增接口级配置
                        configObserver.addConfig(config, configPath, event.getData());
                        break;
                    case CHILD_REMOVED: //删除接口级配置
                        configObserver.removeConfig(config, configPath, event.getData());
                        break;
                    case CHILD_UPDATED:// 更新接口级配置
                        configObserver.updateConfig(config, configPath, event.getData());
                        break;
                    default:
                        break;
                }
            }
        });
        pathChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
        INTERFACE_CONFIG_CACHE.put(configPath, pathChildrenCache);
        configObserver.updateConfigAll(config, configPath, pathChildrenCache.getCurrentData());
    } catch (Exception e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_SUB_PROVIDER_CONFIG, EXT_NAME), e);
    }
}
 
Example #14
Source File: Curator4ZookeeperRegistry.java    From spring-cloud-sofastack-samples with Apache License 2.0 5 votes vote down vote up
/**
 * 订阅IP级配置(服务发布暂时不支持动态配置,暂时支持订阅ConsumerConfig参数设置)
 *
 * @param config   consumer config
 * @param listener config listener
 */
protected void subscribeOverride(final ConsumerConfig config, ConfigListener listener) {
    try {
        if (overrideObserver == null) {
            overrideObserver = new ZookeeperOverrideObserver();
        }
        overrideObserver.addConfigListener(config, listener);
        final String overridePath = buildOverridePath(rootPath, config);
        final AbstractInterfaceConfig registerConfig = getRegisterConfig(config);
        // 监听配置节点下 子节点增加、子节点删除、子节点Data修改事件
        PathChildrenCache pathChildrenCache = new PathChildrenCache(zkClient, overridePath,
            true);
        pathChildrenCache.getListenable().addListener((client1, event) -> {
            if (LOGGER.isDebugEnabled(config.getAppName())) {
                LOGGER.debug("Receive zookeeper event: " + "type=[" + event.getType() + "]");
            }
            switch (event.getType()) {
                case CHILD_ADDED:
                    overrideObserver.addConfig(config, overridePath, event.getData());
                    break;
                case CHILD_REMOVED:
                    overrideObserver.removeConfig(config, overridePath, event.getData(),
                        registerConfig);
                    break;
                case CHILD_UPDATED:
                    overrideObserver.updateConfig(config, overridePath, event.getData());
                    break;
                default:
                    break;
            }
        });
        pathChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
        INTERFACE_OVERRIDE_CACHE.put(overridePath, pathChildrenCache);
        overrideObserver.updateConfigAll(config, overridePath,
            pathChildrenCache.getCurrentData());
    } catch (Throwable t) {
        throw new SofaRpcRuntimeException(
            "Failed to subscribe provider config from zookeeperRegistry!", t);
    }
}
 
Example #15
Source File: ZookeeperRegistry.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 获取注册配置
 *
 * @param config consumer config
 * @return
 */
private AbstractInterfaceConfig getRegisterConfig(ConsumerConfig config) {
    String url = ZookeeperRegistryHelper.convertConsumerToUrl(config);
    String addr = url.substring(0, url.indexOf("?"));
    for (Map.Entry<ConsumerConfig, String> consumerUrl : consumerUrls.entrySet()) {
        if (consumerUrl.getValue().contains(addr)) {
            return consumerUrl.getKey();
        }
    }
    return null;
}
 
Example #16
Source File: BeanIdMatchFilterTest.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Test
public void testIsMatch() {
    TestCustomizeFilter testCustomizeFilter = new TestCustomizeFilter();
    Assert.assertTrue(testCustomizeFilter.isMatch(""));

    testCustomizeFilter = new TestCustomizeFilter();
    testCustomizeFilter.setIdRule("AAA,BBB");

    AbstractInterfaceConfig configA = new ProviderConfig();
    configA.setInterfaceId(Serializer.class.getName());
    configA.setId("AAA");
    FilterInvoker filterInvokerA = new FilterInvoker(null, null, configA);
    Assert.assertEquals(true, testCustomizeFilter.needToLoad(filterInvokerA));
}
 
Example #17
Source File: HystrixFilter.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
private boolean isHystrixEnabled(AbstractInterfaceConfig config) {
    String consumerHystrixEnabled = config.getParameter(HystrixConstants.SOFA_HYSTRIX_ENABLED);
    if (StringUtils.isNotBlank(consumerHystrixEnabled)) {
        return Boolean.valueOf(consumerHystrixEnabled);
    }
    return RpcConfigs.getOrDefaultValue(HystrixConstants.SOFA_HYSTRIX_ENABLED, false);
}
 
Example #18
Source File: RegistryUtils.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
public static String buildUniqueName(AbstractInterfaceConfig config, String protocol) {
    if (RpcConstants.PROTOCOL_TYPE_BOLT.equals(protocol) || RpcConstants.PROTOCOL_TYPE_TR.equals(protocol)) {
        return ConfigUniqueNameGenerator.getUniqueName(config) + "@DEFAULT";
    } else {
        return ConfigUniqueNameGenerator.getUniqueName(config) + "@" + protocol;
    }
}
 
Example #19
Source File: FilterInvoker.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 如果无需下一层过滤器
 *
 * @param config 过滤器所在的接口配置
 */
protected FilterInvoker(AbstractInterfaceConfig config) {
    this.config = config;
    if (config != null) {
        this.configContext = config.getConfigValueCache(false);
    }
}
 
Example #20
Source File: FilterInvoker.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 构造函数
 *
 * @param nextFilter 下一层过滤器
 * @param invoker    下一层调用器
 * @param config     过滤器所在的接口配置
 */
public FilterInvoker(Filter nextFilter, FilterInvoker invoker, AbstractInterfaceConfig config) {
    this.nextFilter = nextFilter;
    this.invoker = invoker;
    this.config = config;
    if (config != null) {
        this.configContext = config.getConfigValueCache(false);
    }
}
 
Example #21
Source File: SofaRegistryHelper.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 构建服务列表的DataId, 格式为interface:version[:uniqueId]@protocol
 *
 * @param config   配置
 * @param protocol 协议
 * @return 返回值
 */
public static String buildListDataId(AbstractInterfaceConfig config, String protocol) {
    if (PROTOCOL_TYPE_BOLT.equals(protocol) || PROTOCOL_TYPE_TR.equals(protocol)) {
        return ConfigUniqueNameGenerator.getUniqueName(config) + "@DEFAULT";
    } else {
        return ConfigUniqueNameGenerator.getUniqueName(config) + "@" + protocol;
    }
}
 
Example #22
Source File: MulticastRegistryHelper.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 服务注册中心的Key
 *
 * @param config   配置
 * @param protocol 协议
 * @return 返回值
 */
public static String buildListDataId(AbstractInterfaceConfig config, String protocol) {
    if (RpcConstants.PROTOCOL_TYPE_BOLT.equals(protocol)
        || RpcConstants.PROTOCOL_TYPE_TR.equals(protocol)) {
        return ConfigUniqueNameGenerator.getUniqueName(config) + "@DEFAULT";
    } else {
        return ConfigUniqueNameGenerator.getUniqueName(config) + "@" + protocol;
    }

}
 
Example #23
Source File: ConsulUtils.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
public static String buildServiceName(AbstractInterfaceConfig config) {
    String consulServiceName = config.getParameter(ConsulConstants.CONSUL_SERVICE_NAME_KEY);
    if (consulServiceName != null) {
        return consulServiceName;
    }
    return config.getInterfaceId();
}
 
Example #24
Source File: LocalRegistryHelper.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 服务注册中心的Key
 *
 * @param config   配置
 * @param protocol 协议
 * @return 返回值
 */
static String buildListDataId(AbstractInterfaceConfig config, String protocol) {
    if (RpcConstants.PROTOCOL_TYPE_BOLT.equals(protocol)
        || RpcConstants.PROTOCOL_TYPE_TR.equals(protocol)) {
        return ConfigUniqueNameGenerator.getUniqueName(config) + "@DEFAULT";
    } else {
        return ConfigUniqueNameGenerator.getUniqueName(config) + "@" + protocol;
    }
}
 
Example #25
Source File: SofaRegistryHelper.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 构建服务列表的DataId, 格式为interface:version[:uniqueId]@protocol
 *
 * @param config   配置
 * @param protocol 协议
 * @return 返回值
 */
public static String buildListDataId(AbstractInterfaceConfig config, String protocol) {
    if (RpcConstants.PROTOCOL_TYPE_BOLT.equals(protocol) || RpcConstants.PROTOCOL_TYPE_TR.equals(protocol)) {
        return ConfigUniqueNameGenerator.getUniqueName(config) + "@DEFAULT";
    } else {
        return ConfigUniqueNameGenerator.getUniqueName(config) + "@" + protocol;
    }
}
 
Example #26
Source File: AbstractSofaRpcFilter.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public boolean needToLoad(FilterInvoker invoker) {
    AbstractInterfaceConfig<?, ?> config = invoker.getConfig();

    String enabled = config.getParameter(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED);
    if (StringUtils.isNotBlank(enabled)) {
        return Boolean.parseBoolean(enabled);
    }

    return RpcConfigs.getOrDefaultValue(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, true);
}
 
Example #27
Source File: Curator4ZookeeperRegistry.java    From spring-cloud-sofastack-samples with Apache License 2.0 5 votes vote down vote up
/**
 * 获取注册配置
 *
 * @param config consumer config
 * @return
 */
private AbstractInterfaceConfig getRegisterConfig(ConsumerConfig config) {
    String url = ZookeeperRegistryHelper.convertConsumerToUrl(config);
    String addr = url.substring(0, url.indexOf("?"));
    for (Map.Entry<ConsumerConfig, String> consumerUrl : consumerUrls.entrySet()) {
        if (consumerUrl.getValue().contains(addr)) {
            return consumerUrl.getKey();
        }
    }
    return null;
}
 
Example #28
Source File: Curator4ZookeeperRegistry.java    From spring-cloud-sofastack-samples with Apache License 2.0 5 votes vote down vote up
/**
 * 订阅接口级配置
 *
 * @param config   provider/consumer config
 * @param listener config listener
 */
protected void subscribeConfig(final AbstractInterfaceConfig config, ConfigListener listener) {
    try {
        if (configObserver == null) {
            configObserver = new ZookeeperConfigObserver();
        }
        configObserver.addConfigListener(config, listener);
        final String configPath = buildConfigPath(rootPath, config);
        // 监听配置节点下 子节点增加、子节点删除、子节点Data修改事件
        PathChildrenCache pathChildrenCache = new PathChildrenCache(zkClient, configPath, true);
        pathChildrenCache.getListenable().addListener((client1, event) -> {
            if (LOGGER.isDebugEnabled(config.getAppName())) {
                LOGGER.debug("Receive zookeeper event: " + "type=[" + event.getType() + "]");
            }
            switch (event.getType()) {
                case CHILD_ADDED:
                    configObserver.addConfig(config, configPath, event.getData());
                    break;
                case CHILD_REMOVED:
                    configObserver.removeConfig(config, configPath, event.getData());
                    break;
                case CHILD_UPDATED:
                    configObserver.updateConfig(config, configPath, event.getData());
                    break;
                default:
                    break;
            }
        });
        pathChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
        INTERFACE_CONFIG_CACHE.put(configPath, pathChildrenCache);
        configObserver.updateConfigAll(config, configPath, pathChildrenCache.getCurrentData());
    } catch (Throwable t) {
        throw new SofaRpcRuntimeException(
            "Failed to subscribe provider config from zookeeperRegistry!", t);
    }
}
 
Example #29
Source File: ZookeeperConfigObserver.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 接口配置修改子节点Data
 *
 * @param config     接口配置
 * @param configPath 配置Path
 * @param data       子节点Data
 */
public void updateConfig(AbstractInterfaceConfig config, String configPath, ChildData data) {
    if (data == null) {
        if (LOGGER.isInfoEnabled(config.getAppName())) {
            LOGGER.infoWithApp(config.getAppName(), "Receive update data is null");
        }
    } else {
        if (LOGGER.isInfoEnabled(config.getAppName())) {
            LOGGER.infoWithApp(config.getAppName(), "Receive update data: path=[" + data.getPath() + "]"
                + ", data=[" + StringSerializer.decode(data.getData()) + "]"
                + ", stat=[" + data.getStat() + "]");
        }
        notifyListeners(config, configPath, data, false);
    }
}
 
Example #30
Source File: ZookeeperConfigObserver.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 接口配置删除子节点Data
 *
 * @param config     接口配置
 * @param configPath 配置Path
 * @param data       子节点Data
 */
public void removeConfig(AbstractInterfaceConfig config, String configPath, ChildData data) {
    if (data == null) {
        if (LOGGER.isInfoEnabled(config.getAppName())) {
            LOGGER.infoWithApp(config.getAppName(), "Receive remove data is null");
        }
    } else {
        if (LOGGER.isInfoEnabled(config.getAppName())) {
            LOGGER.infoWithApp(config.getAppName(), "Receive remove data: path=[" + data.getPath() + "]"
                + ", data=[" + StringSerializer.decode(data.getData()) + "]"
                + ", stat=[" + data.getStat() + "]");
        }
        notifyListeners(config, configPath, data, true);
    }
}