Search in sources :

Example 1 with Registry

use of com.alipay.sofa.rpc.registry.Registry in project sofa-rpc by sofastack.

the class DefaultProviderBootstrap method register.

/**
 * 注册服务
 */
protected void register() {
    if (providerConfig.isRegister()) {
        List<RegistryConfig> registryConfigs = providerConfig.getRegistry();
        if (registryConfigs != null) {
            for (RegistryConfig registryConfig : registryConfigs) {
                Registry registry = RegistryFactory.getRegistry(registryConfig);
                registry.init();
                registry.start();
                try {
                    registry.register(providerConfig);
                } catch (SofaRpcRuntimeException e) {
                    throw e;
                } catch (Throwable e) {
                    String appName = providerConfig.getAppName();
                    if (LOGGER.isWarnEnabled(appName)) {
                        LOGGER.errorWithApp(appName, LogCodes.getLog(LogCodes.ERROR_REGISTER_TO_REGISTRY, registryConfig.getId()), e);
                    }
                }
            }
        }
    }
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) Registry(com.alipay.sofa.rpc.registry.Registry)

Example 2 with Registry

use of com.alipay.sofa.rpc.registry.Registry in project sofa-boot by alipay.

the class RpcBindingAdapter method outBinding.

/**
 * out binding, out binding means provide service
 *
 * @param contract           binding contract
 * @param binding            binding object
 * @param target             binding target
 * @param sofaRuntimeContext sofa runtime context
 * @return binding result
 */
@Override
public Object outBinding(Object contract, RpcBinding binding, Object target, SofaRuntimeContext sofaRuntimeContext) {
    ApplicationContext applicationContext = sofaRuntimeContext.getSofaRuntimeManager().getRootApplicationContext();
    ProviderConfigContainer providerConfigContainer = applicationContext.getBean(ProviderConfigContainer.class);
    ProcessorContainer processorContainer = applicationContext.getBean(ProcessorContainer.class);
    String uniqueName = providerConfigContainer.createUniqueName((Contract) contract, binding);
    ProviderConfig providerConfig = providerConfigContainer.getProviderConfig(uniqueName);
    processorContainer.processorProvider(providerConfig);
    if (providerConfig == null) {
        throw new ServiceRuntimeException(LogCodes.getLog(LogCodes.INFO_SERVICE_METADATA_IS_NULL, uniqueName));
    }
    try {
        providerConfig.export();
    } catch (Exception e) {
        throw new ServiceRuntimeException(LogCodes.getLog(LogCodes.ERROR_PROXY_PUBLISH_FAIL), e);
    }
    if (providerConfigContainer.isAllowPublish()) {
        providerConfig.setRegister(true);
        List<RegistryConfig> registrys = providerConfig.getRegistry();
        for (RegistryConfig registryConfig : registrys) {
            Registry registry = RegistryFactory.getRegistry(registryConfig);
            registry.init();
            registry.start();
            registry.register(providerConfig);
        }
    }
    return Boolean.TRUE;
}
Also used : ProcessorContainer(com.alipay.sofa.rpc.boot.runtime.adapter.processor.ProcessorContainer) RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) ApplicationContext(org.springframework.context.ApplicationContext) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ProviderConfigContainer(com.alipay.sofa.rpc.boot.container.ProviderConfigContainer) Registry(com.alipay.sofa.rpc.registry.Registry) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException) ServiceRuntimeException(com.alipay.sofa.runtime.api.ServiceRuntimeException)

Example 3 with Registry

use of com.alipay.sofa.rpc.registry.Registry in project sofa-boot by alipay.

the class ProviderConfigContainer method publishAllProviderConfig.

/**
 * 发布所有 ProviderConfig 元数据信息到注册中心
 */
public void publishAllProviderConfig() {
    for (ProviderConfig providerConfig : getAllProviderConfig()) {
        ServerConfig serverConfig = (ServerConfig) providerConfig.getServer().get(0);
        if (!serverConfig.getProtocol().equalsIgnoreCase(SofaBootRpcConfigConstants.RPC_PROTOCOL_DUBBO)) {
            providerConfig.setRegister(true);
            List<RegistryConfig> registrys = providerConfig.getRegistry();
            for (RegistryConfig registryConfig : registrys) {
                Registry registry = RegistryFactory.getRegistry(registryConfig);
                registry.init();
                registry.start();
                registry.register(providerConfig);
                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info("service published.  interfaceId[" + providerConfig.getInterfaceId() + "]; protocol[" + serverConfig.getProtocol() + "]");
                }
            }
        }
    }
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) Registry(com.alipay.sofa.rpc.registry.Registry)

Example 4 with Registry

use of com.alipay.sofa.rpc.registry.Registry in project sofa-rpc by sofastack.

the class DefaultConsumerBootstrap method subscribeFromRegistries.

/**
 * Subscribe provider list from all registries, the providers will be merged.
 *
 * @return Provider group list
 */
protected List<ProviderGroup> subscribeFromRegistries() {
    List<ProviderGroup> result = new ArrayList<ProviderGroup>();
    List<RegistryConfig> registryConfigs = consumerConfig.getRegistry();
    if (CommonUtils.isEmpty(registryConfigs)) {
        return result;
    }
    // 是否等待结果
    int addressWaitTime = consumerConfig.getAddressWait();
    int maxAddressWaitTime = SofaConfigs.getIntegerValue(consumerConfig.getAppName(), SofaOptions.CONFIG_MAX_ADDRESS_WAIT_TIME, SofaOptions.MAX_ADDRESS_WAIT_TIME);
    addressWaitTime = addressWaitTime < 0 ? maxAddressWaitTime : Math.min(addressWaitTime, maxAddressWaitTime);
    ProviderInfoListener listener = consumerConfig.getProviderInfoListener();
    respondRegistries = addressWaitTime == 0 ? null : new CountDownLatch(registryConfigs.size());
    // 从注册中心订阅 {groupName: ProviderGroup}
    Map<String, ProviderGroup> tmpProviderInfoList = new HashMap<String, ProviderGroup>();
    for (RegistryConfig registryConfig : registryConfigs) {
        Registry registry = RegistryFactory.getRegistry(registryConfig);
        registry.init();
        registry.start();
        try {
            List<ProviderGroup> current;
            try {
                if (respondRegistries != null) {
                    consumerConfig.setProviderInfoListener(new WrapperClusterProviderInfoListener(listener, respondRegistries));
                }
                current = registry.subscribe(consumerConfig);
            } finally {
                if (respondRegistries != null) {
                    consumerConfig.setProviderInfoListener(listener);
                }
            }
            if (current == null) {
                // 未同步返回结果
                continue;
            } else {
                if (respondRegistries != null) {
                    respondRegistries.countDown();
                }
            }
            for (ProviderGroup group : current) {
                // 当前注册中心的
                String groupName = group.getName();
                if (!group.isEmpty()) {
                    ProviderGroup oldGroup = tmpProviderInfoList.get(groupName);
                    if (oldGroup != null) {
                        oldGroup.addAll(group.getProviderInfos());
                    } else {
                        tmpProviderInfoList.put(groupName, group);
                    }
                }
            }
        } catch (SofaRpcRuntimeException e) {
            throw e;
        } catch (Throwable e) {
            String appName = consumerConfig.getAppName();
            if (LOGGER.isWarnEnabled(appName)) {
                LOGGER.warnWithApp(appName, LogCodes.getLog(LogCodes.ERROR_SUBSCRIBE_FROM_REGISTRY, registryConfig.getId()), e);
            }
        }
    }
    if (respondRegistries != null) {
        try {
            respondRegistries.await(addressWaitTime, TimeUnit.MILLISECONDS);
        } catch (Exception ignore) {
        // NOPMD
        }
    }
    return new ArrayList<ProviderGroup>(tmpProviderInfoList.values());
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) ProviderInfoListener(com.alipay.sofa.rpc.listener.ProviderInfoListener) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) Registry(com.alipay.sofa.rpc.registry.Registry) CountDownLatch(java.util.concurrent.CountDownLatch) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup)

Example 5 with Registry

use of com.alipay.sofa.rpc.registry.Registry in project sofa-rpc by sofastack.

the class RpcRuntimeContext method destroy.

/**
 * 销毁方法
 *
 * @param active 是否主动销毁
 */
private static void destroy(boolean active) {
    // TODO 检查是否有其它需要释放的资源
    RpcRunningState.setShuttingDown(true);
    for (Destroyable.DestroyHook destroyHook : DESTROY_HOOKS) {
        destroyHook.preDestroy();
    }
    List<ProviderConfig> providerConfigs = new ArrayList<ProviderConfig>();
    for (ProviderBootstrap bootstrap : EXPORTED_PROVIDER_CONFIGS) {
        providerConfigs.add(bootstrap.getProviderConfig());
    }
    // 先反注册服务端
    List<Registry> registries = RegistryFactory.getRegistries();
    if (CommonUtils.isNotEmpty(registries) && CommonUtils.isNotEmpty(providerConfigs)) {
        for (Registry registry : registries) {
            registry.batchUnRegister(providerConfigs);
        }
    }
    // 关闭启动的端口
    ServerFactory.destroyAll();
    // 关闭发布的服务
    for (ProviderBootstrap bootstrap : EXPORTED_PROVIDER_CONFIGS) {
        bootstrap.unExport();
    }
    // 关闭调用的服务
    for (ConsumerBootstrap bootstrap : REFERRED_CONSUMER_CONFIGS) {
        ConsumerConfig config = bootstrap.getConsumerConfig();
        if (!CommonUtils.isFalse(config.getParameter(RpcConstants.HIDDEN_KEY_DESTROY))) {
            // 除非不让主动unrefer
            bootstrap.unRefer();
        }
    }
    // 关闭注册中心
    RegistryFactory.destroyAll();
    // 关闭客户端的一些公共资源
    ClientTransportFactory.closeAll();
    // 卸载模块
    if (!RpcRunningState.isUnitTestMode()) {
        ModuleFactory.uninstallModules();
    }
    // 卸载钩子
    for (Destroyable.DestroyHook destroyHook : DESTROY_HOOKS) {
        destroyHook.postDestroy();
    }
    // 清理缓存
    RpcCacheManager.clearAll();
    RpcRunningState.setShuttingDown(false);
    if (LOGGER.isWarnEnabled()) {
        LOGGER.warn("SOFA RPC Framework has been release all resources {}...", active ? "actively " : "");
    }
}
Also used : ConsumerBootstrap(com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap) Destroyable(com.alipay.sofa.rpc.base.Destroyable) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Registry(com.alipay.sofa.rpc.registry.Registry) ProviderBootstrap(com.alipay.sofa.rpc.bootstrap.ProviderBootstrap)

Aggregations

Registry (com.alipay.sofa.rpc.registry.Registry)8 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)7 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)5 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)3 ProviderConfigContainer (com.alipay.sofa.rpc.boot.container.ProviderConfigContainer)2 ProcessorContainer (com.alipay.sofa.rpc.boot.runtime.adapter.processor.ProcessorContainer)2 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)2 ServiceRuntimeException (com.alipay.sofa.runtime.api.ServiceRuntimeException)2 ArrayList (java.util.ArrayList)2 ApplicationContext (org.springframework.context.ApplicationContext)2 Destroyable (com.alipay.sofa.rpc.base.Destroyable)1 ConsumerBootstrap (com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap)1 ProviderBootstrap (com.alipay.sofa.rpc.bootstrap.ProviderBootstrap)1 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)1 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)1 ProviderInfoListener (com.alipay.sofa.rpc.listener.ProviderInfoListener)1 BaseZkTest (com.alipay.sofa.rpc.registry.base.BaseZkTest)1 HelloService (com.alipay.sofa.rpc.test.HelloService)1 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)1 BoltClientTransport (com.alipay.sofa.rpc.transport.bolt.BoltClientTransport)1