Search in sources :

Example 66 with ProviderConfig

use of com.alipay.sofa.rpc.config.ProviderConfig 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 67 with ProviderConfig

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

Example 68 with ProviderConfig

use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.

the class MeshRegistryTest method testOnlyPublish.

@Test
public void testOnlyPublish() throws InterruptedException {
    Field registedAppField = null;
    try {
        registedAppField = MeshRegistry.class.getDeclaredField("registedApp");
        registedAppField.setAccessible(true);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
    Boolean registedAppValue = null;
    // in case of effected by other case.
    try {
        registedAppValue = (Boolean) registedAppField.get(registry);
        registedAppField.set(registry, false);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    ServerConfig serverConfig = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12200);
    ProviderConfig<?> provider = new ProviderConfig();
    provider.setInterfaceId("com.alipay.xxx.TestService").setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setRegister(true).setRegistry(registryConfig).setSerialization("hessian2").setServer(serverConfig).setWeight(222).setTimeout(3000);
    registry.register(provider);
    Thread.sleep(3000);
    try {
        registedAppValue = (Boolean) registedAppField.get(registry);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
    LOGGER.info("final registedAppValue is " + registedAppValue);
    Assert.assertTrue(registedAppValue);
}
Also used : Field(java.lang.reflect.Field) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) Test(org.junit.Test)

Example 69 with ProviderConfig

use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.

the class NacosRegistryHelperTest method convertProviderToInstances.

@Test
public void convertProviderToInstances() {
    ServerConfig serverConfig = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12200);
    ProviderConfig<?> provider = new ProviderConfig();
    provider.setInterfaceId("com.alipay.xxx.TestService").setApplication(new ApplicationConfig().setAppName("test-server")).setUniqueId("nacos-test").setProxy("javassist").setRegister(true).setSerialization("hessian2").setServer(serverConfig).setWeight(222).setTimeout(3000);
    List<Instance> instances = NacosRegistryHelper.convertProviderToInstances(provider);
    assertNotNull(instances);
    assertEquals(1, instances.size());
    Instance instance = instances.get(0);
    assertNotNull(instance);
    assertEquals(NacosRegistryHelper.DEFAULT_CLUSTER, instance.getClusterName());
    assertEquals(serverConfig.getPort(), instance.getPort());
    assertEquals(serverConfig.getProtocol(), instance.getMetadata().get(RpcConstants.CONFIG_KEY_PROTOCOL));
    assertEquals(provider.getSerialization(), instance.getMetadata().get(RpcConstants.CONFIG_KEY_SERIALIZATION));
    assertEquals(provider.getUniqueId(), instance.getMetadata().get(RpcConstants.CONFIG_KEY_UNIQUEID));
    assertEquals(provider.getWeight(), Integer.parseInt(instance.getMetadata().get(RpcConstants.CONFIG_KEY_WEIGHT)));
    assertEquals(provider.getTimeout(), Integer.parseInt(instance.getMetadata().get(RpcConstants.CONFIG_KEY_TIMEOUT)));
    assertEquals(provider.getSerialization(), instance.getMetadata().get(RpcConstants.CONFIG_KEY_SERIALIZATION));
    assertEquals(provider.getAppName(), instance.getMetadata().get(RpcConstants.CONFIG_KEY_APP_NAME));
    assertEquals("com.alipay.xxx.TestService:nacos-test:DEFAULT", instance.getServiceName());
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) Instance(com.alibaba.nacos.api.naming.pojo.Instance) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) Test(org.junit.Test)

Example 70 with ProviderConfig

use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.

the class NacosRegistry method destroy.

@Override
public void destroy() {
    for (ProviderConfig providerConfig : providerInstances.keySet()) {
        unRegister(providerConfig);
    }
    for (ConsumerConfig consumerConfig : consumerListeners.keySet()) {
        unSubscribe(consumerConfig);
    }
    namingService = null;
    providerObserver = null;
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

Aggregations

ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)121 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)88 Test (org.junit.Test)73 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)52 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)47 HelloService (com.alipay.sofa.rpc.test.HelloService)33 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)33 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)28 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)21 CountDownLatch (java.util.concurrent.CountDownLatch)19 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)17 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)10 Filter (com.alipay.sofa.rpc.filter.Filter)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 EchoService (com.alipay.sofa.rpc.test.EchoService)9 ArrayList (java.util.ArrayList)9 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)8 EchoServiceImpl (com.alipay.sofa.rpc.test.EchoServiceImpl)8 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)7 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)7