Search in sources :

Example 81 with ConsumerConfig

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

the class FilterChainTest method buildProviderChain.

@Test
public void buildProviderChain() {
    ProviderConfig providerConfig = new ProviderConfig();
    providerConfig.setFilter(Arrays.asList("testChainFilter0", "-testChainFilter8"));
    providerConfig.setInterfaceId(Serializer.class.getName());
    ConsumerConfig consumerConfig = new ConsumerConfig();
    ArrayList<Filter> list = new ArrayList<Filter>();
    consumerConfig.setFilter(Arrays.asList("testChainFilter0", "-testChainFilter8"));
    list.add(new TestChainFilter1());
    list.add(new TestChainFilter2());
    list.add(new TestChainFilter3());
    list.add(new TestChainFilter4());
    list.add(new ExcludeFilter("-testChainFilter5"));
    consumerConfig.setFilterRef(list);
    consumerConfig.setInterfaceId(Serializer.class.getName());
    // mock provider chain (0,6,7)
    FilterChain providerChain = FilterChain.buildProviderChain(providerConfig, new TestProviderFilterInvoker(providerConfig));
    // mock consumer chain(0,7,2,4)
    FilterChain consumerChain = FilterChain.buildConsumerChain(consumerConfig, new TestConsumerFilterInvoker(consumerConfig, providerChain));
    Assert.assertNotNull(consumerChain.getChain());
    SofaRequest request = new SofaRequest();
    request.setMethodArgs(new String[] { "xxx" });
    request.setInvokeType("sync");
    String result = (String) consumerChain.invoke(request).getAppResponse();
    Assert.assertEquals("xxx_q0_q7_q2_q4_q0_q6_q7_s7_s6_s0_s4_s2_s7_s0", result);
    request = new SofaRequest();
    request.setMethodArgs(new String[] { "xxx" });
    request.setInvokeType("callback");
    SofaResponse response = consumerChain.invoke(request);
    consumerChain.onAsyncResponse(consumerConfig, request, response, null);
    result = (String) response.getAppResponse();
    Assert.assertEquals("xxx_q0_q7_q2_q4_q0_q6_q7_a4_a2_a7_a0", result);
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ArrayList(java.util.ArrayList) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Serializer(com.alipay.sofa.rpc.codec.Serializer) Test(org.junit.Test)

Example 82 with ConsumerConfig

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

the class ClusterFactory method getCluster.

/**
 * 构造Cluster对象
 *
 * @param consumerBootstrap 客户端配置
 * @return Cluster对象
 */
public static Cluster getCluster(ConsumerBootstrap consumerBootstrap) {
    String cluster = null;
    try {
        ConsumerConfig consumerConfig = consumerBootstrap.getConsumerConfig();
        cluster = consumerConfig.getCluster();
        ExtensionClass<Cluster> ext = ExtensionLoaderFactory.getExtensionLoader(Cluster.class).getExtensionClass(cluster);
        if (ext == null) {
            throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_CLUSTER, cluster));
        }
        return ext.getExtInstance(new Class[] { ConsumerBootstrap.class }, new Object[] { consumerBootstrap });
    } catch (SofaRpcRuntimeException e) {
        throw e;
    } catch (Throwable e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_CLUSTER, cluster), e);
    }
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

Example 83 with ConsumerConfig

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

the class Bootstraps method from.

/**
 * 引用一个服务
 *
 * @param consumerConfig 服务消费者配置
 * @param <T>            接口类型
 * @return 引用启动类
 */
public static <T> ConsumerBootstrap<T> from(ConsumerConfig<T> consumerConfig) {
    String bootstrap = consumerConfig.getBootstrap();
    ConsumerBootstrap consumerBootstrap;
    if (StringUtils.isNotEmpty(bootstrap)) {
        consumerBootstrap = ExtensionLoaderFactory.getExtensionLoader(ConsumerBootstrap.class).getExtension(bootstrap, new Class[] { ConsumerConfig.class }, new Object[] { consumerConfig });
    } else {
        // default is same with protocol
        bootstrap = consumerConfig.getProtocol();
        ExtensionLoader extensionLoader = ExtensionLoaderFactory.getExtensionLoader(ConsumerBootstrap.class);
        ExtensionClass<ConsumerBootstrap> extensionClass = extensionLoader.getExtensionClass(bootstrap);
        if (extensionClass == null) {
            // if not exist, use default consumer bootstrap
            bootstrap = RpcConfigs.getStringValue(RpcOptions.DEFAULT_CONSUMER_BOOTSTRAP);
            consumerConfig.setBootstrap(bootstrap);
            consumerBootstrap = ExtensionLoaderFactory.getExtensionLoader(ConsumerBootstrap.class).getExtension(bootstrap, new Class[] { ConsumerConfig.class }, new Object[] { consumerConfig });
        } else {
            consumerConfig.setBootstrap(bootstrap);
            consumerBootstrap = extensionClass.getExtInstance(new Class[] { ConsumerConfig.class }, new Object[] { consumerConfig });
        }
    }
    return (ConsumerBootstrap<T>) consumerBootstrap;
}
Also used : ExtensionClass(com.alipay.sofa.rpc.ext.ExtensionClass) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ExtensionLoader(com.alipay.sofa.rpc.ext.ExtensionLoader)

Example 84 with ConsumerConfig

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

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

the class BoltClientMultipleMain method main.

public static void main(String[] args) throws InterruptedException {
    ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:22000").setTimeout(3000).setRegister(false);
    final HelloService helloService = consumerConfig.refer();
    ConsumerConfig<EchoService> consumerConfig2 = new ConsumerConfig<EchoService>().setInterfaceId(EchoService.class.getName()).setDirectUrl("bolt://127.0.0.1:22000").setTimeout(3000).setRegister(false);
    final EchoService echoService = consumerConfig2.refer();
    LOGGER.error("started at pid {}", RpcRuntimeContext.PID);
    final int threads = 50;
    final AtomicLong cnt = new AtomicLong(0);
    final ThreadPoolExecutor service1 = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(), // 无队列
    new NamedThreadFactory("client-"));
    for (int i = 0; i < threads; i++) {
        service1.execute(new Runnable() {

            @Override
            public void run() {
                int n = 0;
                while (true) {
                    try {
                        echoService.echoStr("1234567890");
                        // 1k
                        // service.echoStr("10241234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
                        // 5k
                        // service.echoStr("12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
                        cnt.incrementAndGet();
                    } catch (Exception e) {
                        LOGGER.error("", e);
                    }
                }
            }
        });
    }
    Thread thread = new Thread(new Runnable() {

        private long last = 0;

        @Override
        public void run() {
            while (true) {
                long count = cnt.get();
                long tps = count - last;
                LOGGER.error("last 1s invoke: {}, queue: {}", tps, service1.getQueue().size());
                last = count;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            }
        }
    }, "Print-tps-THREAD");
    thread.start();
}
Also used : EchoService(com.alipay.sofa.rpc.test.EchoService) NamedThreadFactory(com.alipay.sofa.rpc.common.struct.NamedThreadFactory) HelloService(com.alipay.sofa.rpc.test.HelloService) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Aggregations

ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)139 Test (org.junit.Test)86 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)68 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)61 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)44 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)40 HelloService (com.alipay.sofa.rpc.test.HelloService)38 CountDownLatch (java.util.concurrent.CountDownLatch)27 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)21 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)21 ArrayList (java.util.ArrayList)19 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)18 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)14 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)12 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)12 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)10 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 ConsumerBootstrap (com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap)8 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)8