Java Code Examples for com.alipay.sofa.rpc.config.ProviderConfig#unExport()

The following examples show how to use com.alipay.sofa.rpc.config.ProviderConfig#unExport() . 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: RpcBindingAdapter.java    From sofa-rpc-boot-projects with Apache License 2.0 5 votes vote down vote up
/**
 * pre unout binding
 *
 * @param contract           binding contract
 * @param binding            binding object
 * @param target             binding target
 * @param sofaRuntimeContext sofa runtime context
 */
@Override
public void preUnoutBinding(Object contract, RpcBinding binding, Object target,
                            SofaRuntimeContext sofaRuntimeContext) {
    ProviderConfig providerConfig = SpringBridge.getProviderConfigHelper().getProviderConfig((Contract) contract,
        binding,
        target);
    try {
        providerConfig.unExport();
    } catch (Exception e) {
        throw new ServiceRuntimeException(
            LogCodes.getLog(LogCodes.ERROR_PROXY_PRE_UNPUBLISH_FAIL), e);
    }
}
 
Example 2
Source File: ProviderConfigContainer.java    From sofa-rpc-boot-projects with Apache License 2.0 5 votes vote down vote up
/**
 * unExport所有的 ProviderConfig
 */
public void unExportAllProviderConfig() {
    for (ProviderConfig providerConfig : getAllProviderConfig()) {
        providerConfig.unExport();
    }

}
 
Example 3
Source File: FailoverClusterTest.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Test
public void testRpcDirectInvokeFromContext() {

    ServerConfig serverConfig = new ServerConfig()
        .setProtocol("bolt")
        .setHost("0.0.0.0")
        .setPort(13900);

    ProviderConfig<HelloService> provider = new ProviderConfig();
    provider.setInterfaceId(HelloService.class.getName())
        .setRef(new HelloServiceImpl("x-demo-invoke"))
        .setApplication(new ApplicationConfig().setAppName("x-test-server"))
        .setProxy("javassist")
        .setSerialization("hessian2")
        .setServer(serverConfig)
        .setTimeout(3000);

    provider.export();

    ConsumerConfig<HelloService> consumer = new ConsumerConfig();
    consumer.setInterfaceId(HelloService.class.getName())
        .setApplication(new ApplicationConfig().setAppName("x-test-client"))
        .setProxy("javassist");

    HelloService proxy = consumer.refer();

    for (int i = 0; i < 3; i++) {
        RpcInvokeContext.getContext().setTargetURL("127.0.0.1:13900");
        Assert.assertEquals("x-demo-invoke", proxy.sayHello("x-demo-invoke", 1));
    }

    provider.unExport();
    consumer.unRefer();
}
 
Example 4
Source File: FailoverClusterTest.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Test
public void testRpcDirectInvokeFromContextWithAvailableProviders() {

    ServerConfig serverConfig = new ServerConfig()
        .setProtocol("bolt")
        .setHost("0.0.0.0")
        .setPort(13900);

    ProviderConfig<HelloService> provider = new ProviderConfig();
    provider.setInterfaceId(HelloService.class.getName())
        .setRef(new HelloServiceImpl("x-demo-invoke"))
        .setApplication(new ApplicationConfig().setAppName("x-test-server"))
        .setProxy("javassist")
        .setSerialization("hessian2")
        .setServer(serverConfig)
        .setTimeout(3000);

    provider.export();

    ConsumerConfig<HelloService> consumer = new ConsumerConfig();
    consumer.setInterfaceId(HelloService.class.getName())
        .setApplication(new ApplicationConfig().setAppName("x-test-client"))
        // 模拟有可用服务
        .setDirectUrl("bolt://127.0.0.1:65534")
        .setProxy("javassist");

    HelloService proxy = consumer.refer();

    for (int i = 0; i < 3; i++) {
        RpcInvokeContext.getContext().setTargetURL("127.0.0.1:13900");
        Assert.assertEquals("x-demo-invoke", proxy.sayHello("x-demo-invoke", 1));
    }

    provider.unExport();
    consumer.unRefer();
}
 
Example 5
Source File: FailoverClusterTest.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Test(expected = SofaRouteException.class)
public void testRpcDirectInvokeFromContextNotAllowed() {

    boolean prev = RpcConfigs.getBooleanValue(RpcOptions.RPC_CREATE_CONN_WHEN_ABSENT);

    // disable create connection from context
    RpcConfigs.putValue(RpcOptions.RPC_CREATE_CONN_WHEN_ABSENT, false);

    try {
        ServerConfig serverConfig = new ServerConfig()
            .setProtocol("bolt")
            .setHost("0.0.0.0")
            .setPort(13900);

        ProviderConfig<HelloService> provider = new ProviderConfig();
        provider.setInterfaceId(HelloService.class.getName())
            .setRef(new HelloServiceImpl("x-demo-invoke"))
            .setApplication(new ApplicationConfig().setAppName("x-test-server"))
            .setProxy("javassist")
            .setSerialization("hessian2")
            .setServer(serverConfig)
            .setTimeout(3000);

        provider.export();

        ConsumerConfig<HelloService> consumer = new ConsumerConfig();
        consumer.setInterfaceId(HelloService.class.getName())
            .setApplication(new ApplicationConfig().setAppName("x-test-client"))
            .setProxy("javassist");

        HelloService proxy = consumer.refer();

        RpcInvokeContext.getContext().setTargetURL("127.0.0.1:13900");
        proxy.sayHello("x-demo-invoke", 1);

        provider.unExport();
        consumer.unRefer();
    } finally {
        RpcConfigs.putValue(RpcOptions.RPC_CREATE_CONN_WHEN_ABSENT, prev);
    }
}
 
Example 6
Source File: ReconnectTest.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Test
public void testReconnect() throws Exception {
    ServerConfig serverConfig1 = new ServerConfig()
        .setStopTimeout(0)
        .setPort(22221)
        .setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT)
        .setQueues(100).setCoreThreads(5).setMaxThreads(5);
    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>()
        .setInterfaceId(HelloService.class.getName())
        .setRef(new HelloServiceImpl())
        .setServer(serverConfig1)
        .setRepeatedExportLimit(-1)
        .setRegister(false);
    providerConfig.export();

    final ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>()
        .setInterfaceId(HelloService.class.getName())
        .setDirectUrl("bolt://127.0.0.1:22221")
        .setConnectionHolder("all")
        .setRegister(false)
        .setLazy(true)
        .setReconnectPeriod(2000)
        .setTimeout(3000);
    HelloService helloService = consumerConfig.refer();
    Assert.assertNotNull(helloService.sayHello("xxx", 11));

    // Mock server down, and RPC will throw exception(no available provider)
    providerConfig.unExport();
    ServerFactory.destroyAll();

    BoltClientTransport clientTransport = (BoltClientTransport) consumerConfig.getConsumerBootstrap().getCluster()
        .getConnectionHolder()
        .getAvailableClientTransport(ProviderHelper.toProviderInfo("bolt://127.0.0.1:22221"));

    clientTransport.disconnect();

    TestUtils.delayGet(new Callable<Boolean>() {
        @Override
        public Boolean call() throws Exception {
            return consumerConfig.getConsumerBootstrap().getCluster().getConnectionHolder().isAvailableEmpty();
        }
    }, true, 100, 30);

    try {
        helloService.sayHello("xxx", 11);
        Assert.fail();
    } catch (Exception e) {
        Assert.assertTrue(e.getMessage().contains(LogCodes.ERROR_TARGET_URL_INVALID));
    }

    // Mock server restart
    serverConfig1 = new ServerConfig()
        .setStopTimeout(0)
        .setPort(22221)
        .setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT)
        .setQueues(100).setCoreThreads(5).setMaxThreads(5);
    providerConfig.setServer(Arrays.asList(serverConfig1)).export();
    // The consumer will reconnect to provider automatically
    TestUtils.delayGet(new Callable<Boolean>() {
        @Override
        public Boolean call() throws Exception {
            return consumerConfig.getConsumerBootstrap().getCluster().getConnectionHolder().isAvailableEmpty();
        }
    }, false, 100, 30);
    // RPC return success
    Assert.assertNotNull(helloService.sayHello("xxx", 11));
}
 
Example 7
Source File: BoltProviderBootstrapTest.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Test
public void testProviderClassLoader() throws Throwable {
    // 发布一个服务
    ServerConfig serverConfig = new ServerConfig()
        .setStopTimeout(0)
        .setPort(22223)
        .setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT)
        .setQueues(100).setCoreThreads(5).setMaxThreads(5);
    ProviderConfig<HelloService> providerConfig0 = new ProviderConfig<HelloService>()
        .setId("p-0")
        .setUniqueId("p-0")
        .setInterfaceId(HelloService.class.getName())
        .setRef(new HelloServiceImpl())
        .setServer(serverConfig)
        .setRegister(false);
    providerConfig0.export();

    // incompatible with JDK 9+
    URLClassLoader currentClassLoader = (URLClassLoader) this.getClass().getClassLoader();
    TempClassLoader tempClassLoader = new TempClassLoader(currentClassLoader.getURLs(), null);
    Class helloService = tempClassLoader.loadClass(HelloService.class.getCanonicalName());
    Class helloServiceImpl = tempClassLoader.loadClass(HelloServiceImpl.class.getCanonicalName());

    ProviderConfig<Object> providerConfig1 = new ProviderConfig<Object>()
        .setId("p-1")
        .setUniqueId("p-1")
        .setInterfaceId(HelloService.class.getName())
        .setProxyClass(helloService)
        .setRef(helloServiceImpl.getConstructor(int.class).newInstance(2000))
        .setServer(serverConfig)
        .setRegister(false);
    providerConfig1.export();

    // refer a service
    ConsumerConfig<HelloService> consumerConfig0 = new ConsumerConfig<HelloService>()
        .setInterfaceId(HelloService.class.getName())
        .setUniqueId("p-0")
        .setProxy("jdk")
        .setDirectUrl("bolt://127.0.0.1:22223")
        .setTimeout(500);
    HelloService proxy = consumerConfig0.refer();
    proxy.sayHello("11", 11);

    ClassLoader cl0 = ReflectCache.getServiceClassLoader(ConfigUniqueNameGenerator.getUniqueName(providerConfig0));
    ClassLoader cl1 = ReflectCache.getServiceClassLoader(ConfigUniqueNameGenerator.getUniqueName(providerConfig1));

    Assert.assertEquals(currentClassLoader, cl0);
    Assert.assertEquals(tempClassLoader, cl1);

    providerConfig0.unExport();
    providerConfig1.unExport();
    cl0 = ReflectCache.getServiceClassLoader(ConfigUniqueNameGenerator.getUniqueName(providerConfig0));
    cl1 = ReflectCache.getServiceClassLoader(ConfigUniqueNameGenerator.getUniqueName(providerConfig1));
    Assert.assertEquals(ClassLoaderUtils.getCurrentClassLoader(), cl0);
    Assert.assertEquals(ClassLoaderUtils.getCurrentClassLoader(), cl1);

    Method methodCache = ReflectCache.getMethodCache(ConfigUniqueNameGenerator.getUniqueName(providerConfig0),
        "sayHello");
    Assert.assertNull(methodCache);

    methodCache = ReflectCache.getOverloadMethodCache(ConfigUniqueNameGenerator.getUniqueName(providerConfig0),
        "sayHello", new String[] { "java.lang.Stringint" });
    Assert.assertNull(methodCache);

    String[] sig = ReflectCache.getMethodSigsCache(ConfigUniqueNameGenerator.getUniqueName(providerConfig0),
        "sayHello");
    Assert.assertNull(sig);
}
 
Example 8
Source File: ZookeeperOverrideObserverTest.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Test
public void testAll() throws Exception {

    try {
        RegistryConfig registryConfig = new RegistryConfig().setProtocol(RpcConstants.REGISTRY_PROTOCOL_ZK)
            .setAddress("127.0.0.1:2181");
        ZookeeperRegistry registry = (ZookeeperRegistry) RegistryFactory
            .getRegistry(registryConfig);
        registry.start();

        ServerConfig serverConfig = new ServerConfig().setPort(22222)
            .setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT);
        ProviderConfig<OverrideService> providerConfig = new ProviderConfig<OverrideService>()
            .setInterfaceId(OverrideService.class.getName()).setRef(new OverrideServiceImpl(22222))
            .setServer(serverConfig).setRegistry(registryConfig)
            .setParameter(ProviderInfoAttrs.ATTR_WARMUP_TIME, "2000")
            .setParameter(ProviderInfoAttrs.ATTR_WARMUP_WEIGHT, "100").setWeight(0);

        ServerConfig serverConfig2 = new ServerConfig().setPort(22111)
            .setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT);
        ProviderConfig<OverrideService> providerConfig2 = new ProviderConfig<OverrideService>()
            .setInterfaceId(OverrideService.class.getName()).setRef(new OverrideServiceImpl(22111))
            .setServer(serverConfig2).setRegistry(registryConfig).setRepeatedExportLimit(-1)
            .setWeight(0);

        providerConfig.export();
        providerConfig2.export();

        ConsumerConfig<OverrideService> consumerConfig = new ConsumerConfig<OverrideService>()
            .setInterfaceId(OverrideService.class.getName()).setRegistry(registryConfig)
            .setTimeout(3333).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT);
        OverrideService overrideService = consumerConfig.refer();

        AddressHolder addressHolder = consumerConfig.getConsumerBootstrap().getCluster()
            .getAddressHolder();
        Assert.assertTrue(addressHolder.getAllProviderSize() == 2);

        providerConfig2.unExport();

        Assert.assertTrue(delayGetSize(addressHolder, 1, 100) == 1);

        List<String> path = registry.getZkClient().getChildren()
            .forPath("/sofa-rpc/" + OverrideService.class.getCanonicalName() + "/providers");
        String url = URLDecoder.decode(path.get(0), "UTF-8");
        ProviderInfo providerInfo = ProviderHelper.toProviderInfo(url);

        // 模拟下发一个override
        String override1 = providerInfo.getProtocolType() + "://" + providerInfo.getHost() + ":"
            + providerInfo.getPort() + "?timeout=2345";
        String overridePath1 = "/sofa-rpc/" + OverrideService.class.getCanonicalName() + "/overrides/"
            + URLEncoder.encode(override1, "UTF-8");
        registry.getZkClient().create().creatingParentContainersIfNeeded()
            .withMode(CreateMode.PERSISTENT).forPath(overridePath1);
        Assert.assertTrue(delayGetTimeout(consumerConfig, 2345, 100) == 2345);

        // 删除目前没有影响
        registry.getZkClient().delete().forPath(overridePath1);
        Thread.sleep(500);
        Assert.assertTrue(delayGetTimeout(consumerConfig, 2345, 100) == 2345);

        // 恢复到3333
        String override2 = providerInfo.getProtocolType() + "://" + providerInfo.getHost() + ":"
            + providerInfo.getPort() + "?timeout=3333";
        String overridePath2 = "/sofa-rpc/" + OverrideService.class.getCanonicalName() + "/overrides/"
            + URLEncoder.encode(override2, "UTF-8");
        registry.getZkClient().create().creatingParentContainersIfNeeded()
            .withMode(CreateMode.PERSISTENT).forPath(overridePath2);
        Assert.assertTrue(delayGetTimeout(consumerConfig, 3333, 100) == 3333);

        // 清除持久化的 path
        registry.getZkClient().delete().forPath(overridePath2);
    } catch (Throwable e) {
        LOGGER.error("ZookeeperOverrideObserver test case failed", e);
        Assert.assertTrue(e.getMessage(), false);
    }
}