Search in sources :

Example 1 with ProviderBootstrap

use of com.alipay.sofa.rpc.bootstrap.ProviderBootstrap 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)

Example 2 with ProviderBootstrap

use of com.alipay.sofa.rpc.bootstrap.ProviderBootstrap in project sofa-rpc by sofastack.

the class GenerateService method generate.

public String generate(String protocol) {
    if (protocol == null) {
        protocol = defaultProtocol;
    }
    Swagger swagger = new Swagger();
    swagger.setInfo(getInfo());
    swagger.setBasePath(basePath);
    Map<Class<?>, Object> interfaceMapRef = new HashMap<>();
    List<ProviderBootstrap> providerBootstraps = RpcRuntimeContext.getProviderConfigs();
    for (ProviderBootstrap providerBootstrap : providerBootstraps) {
        ProviderConfig providerConfig = providerBootstrap.getProviderConfig();
        List<ServerConfig> server = providerConfig.getServer();
        for (ServerConfig serverConfig : server) {
            if (serverConfig.getProtocol().equals(protocol)) {
                interfaceMapRef.put(providerConfig.getProxyClass(), providerConfig.getRef());
                break;
            }
        }
    }
    Reader.read(swagger, interfaceMapRef, "");
    String result = null;
    try {
        result = Json.mapper().writeValueAsString(swagger);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    return result;
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) HashMap(java.util.HashMap) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) Swagger(io.swagger.models.Swagger) ProviderBootstrap(com.alipay.sofa.rpc.bootstrap.ProviderBootstrap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

ProviderBootstrap (com.alipay.sofa.rpc.bootstrap.ProviderBootstrap)2 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)2 Destroyable (com.alipay.sofa.rpc.base.Destroyable)1 ConsumerBootstrap (com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap)1 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)1 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)1 Registry (com.alipay.sofa.rpc.registry.Registry)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Swagger (io.swagger.models.Swagger)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1