Search in sources :

Example 56 with ApplicationConfig

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

the class ConsulRegistryTest method providerConfig.

private ProviderConfig<?> providerConfig(String uniqueId, int... ports) {
    ProviderConfig<?> provider = new ProviderConfig();
    provider.setInterfaceId(INTERFACE_ID).setUniqueId(uniqueId).setApplication(new ApplicationConfig().setAppName("consul-registry-test")).setProxy("javassist").setRegister(true).setRegistry(registryConfig).setSerialization("hessian2").setWeight(222).setTimeout(3000);
    IntStream.of(ports).mapToObj(port -> new ServerConfig().setProtocol("bolt").setHost("localhost").setPort(port)).forEach(provider::setServer);
    return provider;
}
Also used : IntStream(java.util.stream.IntStream) ConsulStarterBuilder(com.pszymczyk.consul.ConsulStarterBuilder) HealthService(com.ecwid.consul.v1.health.model.HealthService) RegistryFactory(com.alipay.sofa.rpc.registry.RegistryFactory) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) After(org.junit.After) Map(java.util.Map) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) Before(org.junit.Before) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ConsulClient(com.ecwid.consul.v1.ConsulClient) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ConsulProcess(com.pszymczyk.consul.ConsulProcess) RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) Response(com.ecwid.consul.v1.Response) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ProviderInfoListener(com.alipay.sofa.rpc.listener.ProviderInfoListener) HealthServicesRequest(com.ecwid.consul.v1.health.HealthServicesRequest) Assert(org.junit.Assert) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig)

Example 57 with ApplicationConfig

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

the class ConsulRegistryTest method consumerConfig.

private ConsumerConfig<?> consumerConfig(String uniqueId) {
    ConsumerConfig<?> consumer = new ConsumerConfig();
    consumer.setInterfaceId(INTERFACE_ID).setUniqueId(uniqueId).setApplication(new ApplicationConfig().setAppName("consul-registry-test")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
    return consumer;
}
Also used : ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

Example 58 with ApplicationConfig

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

the class MeshRegistryTest method testAll.

@Test
public void testAll() throws Exception {
    int timeoutPerSub = 1000;
    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);
    ConsumerConfig<?> consumer = new ConsumerConfig();
    consumer.setInterfaceId("com.alipay.xxx.TestService").setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
    String tag0 = MeshRegistryHelper.buildMeshKey(provider, serverConfig.getProtocol());
    String tag1 = MeshRegistryHelper.buildMeshKey(consumer, consumer.getProtocol());
    Assert.assertEquals(tag1, tag0);
    // 订阅
    MeshRegistryTest.MockProviderInfoListener providerInfoListener = new MeshRegistryTest.MockProviderInfoListener();
    consumer.setProviderInfoListener(providerInfoListener);
    List<ProviderGroup> groups = registry.subscribe(consumer);
    Assert.assertNull(groups);
    Thread.sleep(3000);
    Map<String, ProviderGroup> ps = providerInfoListener.getData();
    Assert.assertTrue(ps.toString(), ps.size() == 1);
    // 反注册
    CountDownLatch latch = new CountDownLatch(1);
    providerInfoListener.setCountDownLatch(latch);
    registry.unRegister(provider);
    latch.await(timeoutPerSub, TimeUnit.MILLISECONDS);
    // mesh 并不直接感知.
    Assert.assertTrue(ps.size() == 1);
    // 一次发2个端口的再次注册
    latch = new CountDownLatch(1);
    providerInfoListener.setCountDownLatch(latch);
    provider.getServer().add(new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12201));
    registry.register(provider);
    latch.await(timeoutPerSub * 2, TimeUnit.MILLISECONDS);
    // 重复订阅
    ConsumerConfig<?> consumer2 = new ConsumerConfig();
    consumer2.setInterfaceId("com.alipay.xxx.TestService").setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
    CountDownLatch latch2 = new CountDownLatch(1);
    MeshRegistryTest.MockProviderInfoListener providerInfoListener2 = new MeshRegistryTest.MockProviderInfoListener();
    providerInfoListener2.setCountDownLatch(latch2);
    consumer2.setProviderInfoListener(providerInfoListener2);
    List<ProviderGroup> groups2 = registry.subscribe(consumer2);
    Assert.assertNull(groups);
    Thread.sleep(3000);
    Map<String, ProviderGroup> ps2 = providerInfoListener2.getData();
    Assert.assertTrue(ps2.size() == 1);
    // 取消订阅者1
    registry.unSubscribe(consumer);
    // 批量反注册,判断订阅者2的数据
    latch = new CountDownLatch(1);
    providerInfoListener2.setCountDownLatch(latch);
    List<ProviderConfig> providerConfigList = new ArrayList<ProviderConfig>();
    providerConfigList.add(provider);
    registry.batchUnRegister(providerConfigList);
    latch.await(timeoutPerSub, TimeUnit.MILLISECONDS);
    Assert.assertTrue(ps2.size() == 1);
    // 批量取消订阅
    List<ConsumerConfig> consumerConfigList = new ArrayList<ConsumerConfig>();
    consumerConfigList.add(consumer2);
    registry.batchUnSubscribe(consumerConfigList);
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) MeshEndpoint(com.alipay.sofa.rpc.registry.mesh.model.MeshEndpoint) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) Test(org.junit.Test)

Example 59 with ApplicationConfig

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

the class ConsulRegistryAclTest method consumerConfig.

private ConsumerConfig<?> consumerConfig(String uniqueId) {
    ConsumerConfig<?> consumer = new ConsumerConfig();
    consumer.setInterfaceId(INTERFACE_ID).setUniqueId(uniqueId).setApplication(new ApplicationConfig().setAppName("consul-registry-test")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
    return consumer;
}
Also used : ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

Example 60 with ApplicationConfig

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

the class ConsumerConfigHelper method getConsumerConfig.

/**
 * 获取 ConsumerConfig
 *
 * @param contract the Contract
 * @param binding  the RpcBinding
 * @return the ConsumerConfig
 */
public ConsumerConfig getConsumerConfig(Contract contract, RpcBinding binding) {
    RpcBindingParam param = binding.getRpcBindingParam();
    String id = binding.getBeanId();
    String interfaceId = contract.getInterfaceType().getName();
    String uniqueId = contract.getUniqueId();
    Integer timeout = param.getTimeout();
    Integer retries = param.getRetries();
    String type = param.getType();
    Integer addressWaitTime = param.getAddressWaitTime();
    Object callbackHandler = param.getCallbackHandler();
    String genericInterface = param.getGenericInterface();
    String loadBalancer = param.getLoadBalancer();
    Boolean lazy = param.getLazy();
    Boolean check = param.getCheck();
    String mockMode = param.getMockMode();
    String serialization = param.getSerialization();
    List<Filter> filters = param.getFilters();
    List<MethodConfig> methodConfigs = convertToMethodConfig(param.getMethodInfos());
    String targetUrl = param.getTargetUrl();
    String referenceLimit = sofaBootRpcProperties.getConsumerRepeatedReferenceLimit();
    ConsumerConfig consumerConfig = new ConsumerConfig();
    if (StringUtils.hasText(appName)) {
        consumerConfig.setApplication(new ApplicationConfig().setAppName(appName));
    }
    if (StringUtils.hasText(id)) {
        consumerConfig.setId(id);
    }
    if (StringUtils.hasText(genericInterface)) {
        consumerConfig.setGeneric(true);
        consumerConfig.setInterfaceId(genericInterface);
    } else if (StringUtils.hasText(interfaceId)) {
        consumerConfig.setInterfaceId(interfaceId);
    }
    if (StringUtils.hasText(uniqueId)) {
        consumerConfig.setUniqueId(uniqueId);
    }
    if (timeout != null) {
        consumerConfig.setTimeout(timeout);
    }
    if (retries != null) {
        consumerConfig.setRetries(retries);
    }
    if (StringUtils.hasText(type)) {
        consumerConfig.setInvokeType(type);
    }
    if (addressWaitTime != null) {
        consumerConfig.setAddressWait(addressWaitTime);
    }
    if (StringUtils.hasText(loadBalancer)) {
        consumerConfig.setLoadBalancer(loadBalancer);
    }
    if (lazy != null) {
        consumerConfig.setLazy(lazy);
    }
    if (check != null) {
        consumerConfig.setCheck(check);
    }
    if (mockMode != null) {
        consumerConfig.setMockMode(mockMode);
    }
    if (callbackHandler != null) {
        if (callbackHandler instanceof SofaResponseCallback) {
            consumerConfig.setOnReturn((SofaResponseCallback) callbackHandler);
        } else {
            throw new SofaBootRpcRuntimeException("callback handler must implement SofaResponseCallback [" + callbackHandler + "]");
        }
    }
    if (!CollectionUtils.isEmpty(filters)) {
        consumerConfig.setFilterRef(filters);
    }
    if (!CollectionUtils.isEmpty(methodConfigs)) {
        consumerConfig.setMethods(methodConfigs);
    }
    if (StringUtils.hasText(targetUrl)) {
        consumerConfig.setDirectUrl(targetUrl);
        consumerConfig.setLazy(true);
        consumerConfig.setSubscribe(false);
        consumerConfig.setRegister(false);
    }
    if (StringUtils.hasText(referenceLimit)) {
        consumerConfig.setRepeatedReferLimit(Integer.valueOf(referenceLimit));
    }
    String protocol = binding.getBindingType().getType();
    consumerConfig.setBootstrap(protocol);
    if (protocol.equals(SofaBootRpcConfigConstants.RPC_PROTOCOL_DUBBO)) {
        consumerConfig.setInJVM(false);
    }
    if (param.getRegistrys() != null && param.getRegistrys().size() > 0) {
        List<String> registrys = param.getRegistrys();
        for (String registryAlias : registrys) {
            RegistryConfig registryConfig = registryConfigContainer.getRegistryConfig(registryAlias);
            consumerConfig.setRegistry(registryConfig);
        }
    } else if (registryConfigContainer.isMeshEnabled(protocol)) {
        RegistryConfig registryConfig = registryConfigContainer.getRegistryConfig(SofaBootRpcConfigConstants.REGISTRY_PROTOCOL_MESH);
        consumerConfig.setRegistry(registryConfig);
    } else {
        RegistryConfig registryConfig = registryConfigContainer.getRegistryConfig();
        consumerConfig.setRegistry(registryConfig);
    }
    if (StringUtils.hasText(serialization)) {
        consumerConfig.setSerialization(serialization);
    }
    if (Boolean.TRUE.toString().equals(sofaBootRpcProperties.getHystrixEnable())) {
        consumerConfig.setParameter(HystrixConstants.SOFA_HYSTRIX_ENABLED, Boolean.TRUE.toString());
    }
    // after sofaBootRpcProperties#getHystrixEnable for override global config
    if (param.getParameters() != null) {
        consumerConfig.setParameters(param.getParameters());
    }
    return consumerConfig.setProtocol(protocol);
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) RpcBindingParam(com.alipay.sofa.rpc.boot.runtime.param.RpcBindingParam) MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) SofaBootRpcRuntimeException(com.alipay.sofa.rpc.boot.common.SofaBootRpcRuntimeException) Filter(com.alipay.sofa.rpc.filter.Filter) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

Aggregations

ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)113 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)77 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)64 Test (org.junit.Test)53 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)52 HelloService (com.alipay.sofa.rpc.test.HelloService)30 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)21 CountDownLatch (java.util.concurrent.CountDownLatch)20 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)15 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)14 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)14 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)13 ArrayList (java.util.ArrayList)12 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)11 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)11 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)10 EchoService (com.alipay.sofa.rpc.test.EchoService)10 EchoRequest (com.alipay.sofa.rpc.server.bolt.pb.EchoRequest)8 DemoServiceImpl (com.alipay.sofa.rpc.bootstrap.dubbo.demo.DemoServiceImpl)7 Filter (com.alipay.sofa.rpc.filter.Filter)7