Search in sources :

Example 1 with RegistryConfig

use of com.alibaba.dubbo.config.RegistryConfig in project dubbo by alibaba.

the class AnnotationBean method postProcessAfterInitialization.

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (!isMatchPackage(bean)) {
        return bean;
    }
    Service service = bean.getClass().getAnnotation(Service.class);
    if (service != null) {
        ServiceBean<Object> serviceConfig = new ServiceBean<Object>(service);
        if (void.class.equals(service.interfaceClass()) && "".equals(service.interfaceName())) {
            if (bean.getClass().getInterfaces().length > 0) {
                serviceConfig.setInterface(bean.getClass().getInterfaces()[0]);
            } else {
                throw new IllegalStateException("Failed to export remote service class " + bean.getClass().getName() + ", cause: The @Service undefined interfaceClass or interfaceName, and the service class unimplemented any interfaces.");
            }
        }
        if (applicationContext != null) {
            serviceConfig.setApplicationContext(applicationContext);
            if (service.registry() != null && service.registry().length > 0) {
                List<RegistryConfig> registryConfigs = new ArrayList<RegistryConfig>();
                for (String registryId : service.registry()) {
                    if (registryId != null && registryId.length() > 0) {
                        registryConfigs.add((RegistryConfig) applicationContext.getBean(registryId, RegistryConfig.class));
                    }
                }
                serviceConfig.setRegistries(registryConfigs);
            }
            if (service.provider() != null && service.provider().length() > 0) {
                serviceConfig.setProvider((ProviderConfig) applicationContext.getBean(service.provider(), ProviderConfig.class));
            }
            if (service.monitor() != null && service.monitor().length() > 0) {
                serviceConfig.setMonitor((MonitorConfig) applicationContext.getBean(service.monitor(), MonitorConfig.class));
            }
            if (service.application() != null && service.application().length() > 0) {
                serviceConfig.setApplication((ApplicationConfig) applicationContext.getBean(service.application(), ApplicationConfig.class));
            }
            if (service.module() != null && service.module().length() > 0) {
                serviceConfig.setModule((ModuleConfig) applicationContext.getBean(service.module(), ModuleConfig.class));
            }
            if (service.provider() != null && service.provider().length() > 0) {
                serviceConfig.setProvider((ProviderConfig) applicationContext.getBean(service.provider(), ProviderConfig.class));
            } else {
            }
            if (service.protocol() != null && service.protocol().length > 0) {
                List<ProtocolConfig> protocolConfigs = new ArrayList<ProtocolConfig>();
                for (String protocolId : service.registry()) {
                    if (protocolId != null && protocolId.length() > 0) {
                        protocolConfigs.add((ProtocolConfig) applicationContext.getBean(protocolId, ProtocolConfig.class));
                    }
                }
                serviceConfig.setProtocols(protocolConfigs);
            }
            try {
                serviceConfig.afterPropertiesSet();
            } catch (RuntimeException e) {
                throw (RuntimeException) e;
            } catch (Exception e) {
                throw new IllegalStateException(e.getMessage(), e);
            }
        }
        serviceConfig.setRef(bean);
        serviceConfigs.add(serviceConfig);
        serviceConfig.export();
    }
    return bean;
}
Also used : RegistryConfig(com.alibaba.dubbo.config.RegistryConfig) ArrayList(java.util.ArrayList) Service(com.alibaba.dubbo.config.annotation.Service) BeansException(org.springframework.beans.BeansException) ProtocolConfig(com.alibaba.dubbo.config.ProtocolConfig)

Example 2 with RegistryConfig

use of com.alibaba.dubbo.config.RegistryConfig in project dubbo by alibaba.

the class InvokerSideConfigUrlTest method initRefConf.

// ======================================================
//   private helper
// ====================================================== 
private void initRefConf() {
    appConfForConsumer = new ApplicationConfig();
    appConfForReference = new ApplicationConfig();
    regConfForConsumer = new RegistryConfig();
    regConfForReference = new RegistryConfig();
    methodConfForReference = new MethodConfig();
    refConf = new ReferenceConfig<DemoService>();
    consumerConf = new ConsumerConfig();
    methodConfForReference.setName("sayName");
    regConfForReference.setAddress("127.0.0.1:9090");
    regConfForReference.setProtocol("mockregistry");
    appConfForReference.setName("ConfigTests");
    refConf.setInterface("com.alibaba.dubbo.config.api.DemoService");
    refConf.setApplication(appConfForReference);
    consumerConf.setApplication(appConfForConsumer);
    refConf.setRegistry(regConfForReference);
    consumerConf.setRegistry(regConfForConsumer);
    refConf.setConsumer(consumerConf);
    refConf.setMethods(Arrays.asList(new MethodConfig[] { methodConfForReference }));
    refConf.setScope(Constants.SCOPE_REMOTE);
}
Also used : RegistryConfig(com.alibaba.dubbo.config.RegistryConfig) MethodConfig(com.alibaba.dubbo.config.MethodConfig) ApplicationConfig(com.alibaba.dubbo.config.ApplicationConfig) DemoService(com.alibaba.dubbo.config.api.DemoService) ConsumerConfig(com.alibaba.dubbo.config.ConsumerConfig)

Example 3 with RegistryConfig

use of com.alibaba.dubbo.config.RegistryConfig in project dubbo by alibaba.

the class UrlTestBase method initServConf.

@SuppressWarnings("deprecation")
protected void initServConf() {
    appConfForProvider = new ApplicationConfig();
    appConfForService = new ApplicationConfig();
    regConfForProvider = new RegistryConfig();
    regConfForService = new RegistryConfig();
    provConf = new ProviderConfig();
    protoConfForProvider = new ProtocolConfig();
    protoConfForService = new ProtocolConfig();
    methodConfForService = new MethodConfig();
    servConf = new ServiceConfig<DemoService>();
    provConf.setApplication(appConfForProvider);
    servConf.setApplication(appConfForService);
    provConf.setRegistry(regConfForProvider);
    servConf.setRegistry(regConfForService);
    provConf.setProtocols(Arrays.asList(new ProtocolConfig[] { protoConfForProvider }));
    servConf.setProtocols(Arrays.asList(new ProtocolConfig[] { protoConfForService }));
    servConf.setMethods(Arrays.asList(new MethodConfig[] { methodConfForService }));
    servConf.setProvider(provConf);
    servConf.setRef(demoService);
    servConf.setInterfaceClass(DemoService.class);
    methodConfForService.setName("sayName");
    regConfForService.setAddress("127.0.0.1:9090");
    regConfForService.setProtocol("mockregistry");
    appConfForService.setName("ConfigTests");
}
Also used : RegistryConfig(com.alibaba.dubbo.config.RegistryConfig) MethodConfig(com.alibaba.dubbo.config.MethodConfig) ApplicationConfig(com.alibaba.dubbo.config.ApplicationConfig) ProviderConfig(com.alibaba.dubbo.config.ProviderConfig) DemoService(com.alibaba.dubbo.config.api.DemoService) ProtocolConfig(com.alibaba.dubbo.config.ProtocolConfig)

Example 4 with RegistryConfig

use of com.alibaba.dubbo.config.RegistryConfig in project dubbo by alibaba.

the class ValidationTest method testProviderValidation.

@Test
public void testProviderValidation() {
    ServiceConfig<ValidationService> service = new ServiceConfig<ValidationService>();
    service.setApplication(new ApplicationConfig("validation-provider"));
    service.setRegistry(new RegistryConfig("N/A"));
    service.setProtocol(new ProtocolConfig("dubbo", 29582));
    service.setInterface(ValidationService.class.getName());
    service.setRef(new ValidationServiceImpl());
    service.setValidation(String.valueOf(true));
    service.export();
    try {
        ReferenceConfig<ValidationService> reference = new ReferenceConfig<ValidationService>();
        reference.setApplication(new ApplicationConfig("validation-consumer"));
        reference.setInterface(ValidationService.class);
        reference.setUrl("dubbo://127.0.0.1:29582");
        ValidationService validationService = reference.get();
        try {
            // Save OK
            ValidationParameter parameter = new ValidationParameter();
            parameter.setName("liangfei");
            parameter.setEmail("liangfei@liang.fei");
            parameter.setAge(50);
            parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
            parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
            validationService.save(parameter);
            // Save Error
            try {
                parameter = new ValidationParameter();
                validationService.save(parameter);
                Assert.fail();
            } catch (RpcException e) {
                Assert.assertTrue(e.getMessage().contains("ConstraintViolation"));
            }
            // Delete OK
            validationService.delete(2, "abc");
            // Delete Error
            try {
                validationService.delete(0, "abc");
                Assert.fail();
            } catch (RpcException e) {
                Assert.assertTrue(e.getMessage().contains("ConstraintViolation"));
            }
            try {
                validationService.delete(2, null);
                Assert.fail();
            } catch (RpcException e) {
                Assert.assertTrue(e.getMessage().contains("ConstraintViolation"));
            }
            try {
                validationService.delete(0, null);
                Assert.fail();
            } catch (RpcException e) {
                Assert.assertTrue(e.getMessage().contains("ConstraintViolation"));
            }
        } finally {
            reference.destroy();
        }
    } finally {
        service.unexport();
    }
}
Also used : RegistryConfig(com.alibaba.dubbo.config.RegistryConfig) ServiceConfig(com.alibaba.dubbo.config.ServiceConfig) ApplicationConfig(com.alibaba.dubbo.config.ApplicationConfig) ReferenceConfig(com.alibaba.dubbo.config.ReferenceConfig) RpcException(com.alibaba.dubbo.rpc.RpcException) ProtocolConfig(com.alibaba.dubbo.config.ProtocolConfig) Date(java.util.Date) Test(org.junit.Test)

Example 5 with RegistryConfig

use of com.alibaba.dubbo.config.RegistryConfig in project dubbo by alibaba.

the class ValidationTest method testValidation.

@Test
public void testValidation() {
    ServiceConfig<ValidationService> service = new ServiceConfig<ValidationService>();
    service.setApplication(new ApplicationConfig("validation-provider"));
    service.setRegistry(new RegistryConfig("N/A"));
    service.setProtocol(new ProtocolConfig("dubbo", 29582));
    service.setInterface(ValidationService.class.getName());
    service.setRef(new ValidationServiceImpl());
    service.setValidation(String.valueOf(true));
    service.export();
    try {
        ReferenceConfig<ValidationService> reference = new ReferenceConfig<ValidationService>();
        reference.setApplication(new ApplicationConfig("validation-consumer"));
        reference.setInterface(ValidationService.class);
        reference.setUrl("dubbo://127.0.0.1:29582?scope=remote&validation=true");
        ValidationService validationService = reference.get();
        try {
            // Save OK
            ValidationParameter parameter = new ValidationParameter();
            parameter.setName("liangfei");
            parameter.setEmail("liangfei@liang.fei");
            parameter.setAge(50);
            parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
            parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
            validationService.save(parameter);
            try {
                parameter = new ValidationParameter();
                parameter.setName("l");
                parameter.setEmail("liangfei@liang.fei");
                parameter.setAge(50);
                parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
                parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
                validationService.save(parameter);
                Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
            }
            // Save Error
            try {
                parameter = new ValidationParameter();
                validationService.save(parameter);
                Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
            }
            // Delete OK
            validationService.delete(2, "abc");
            // Delete Error
            try {
                validationService.delete(2, "a");
                Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
                Assert.assertEquals(1, violations.size());
            }
            // Delete Error
            try {
                validationService.delete(0, "abc");
                Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
                Assert.assertEquals(1, violations.size());
            }
            try {
                validationService.delete(2, null);
                Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
                Assert.assertEquals(1, violations.size());
            }
            try {
                validationService.delete(0, null);
                Assert.fail();
            } catch (RpcException e) {
                ConstraintViolationException ve = (ConstraintViolationException) e.getCause();
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
                Assert.assertEquals(2, violations.size());
            }
        } finally {
            reference.destroy();
        }
    } finally {
        service.unexport();
    }
}
Also used : RegistryConfig(com.alibaba.dubbo.config.RegistryConfig) Set(java.util.Set) Date(java.util.Date) ServiceConfig(com.alibaba.dubbo.config.ServiceConfig) ApplicationConfig(com.alibaba.dubbo.config.ApplicationConfig) ReferenceConfig(com.alibaba.dubbo.config.ReferenceConfig) RpcException(com.alibaba.dubbo.rpc.RpcException) ConstraintViolationException(javax.validation.ConstraintViolationException) ProtocolConfig(com.alibaba.dubbo.config.ProtocolConfig) Test(org.junit.Test)

Aggregations

RegistryConfig (com.alibaba.dubbo.config.RegistryConfig)20 ApplicationConfig (com.alibaba.dubbo.config.ApplicationConfig)17 ProtocolConfig (com.alibaba.dubbo.config.ProtocolConfig)15 Test (org.junit.Test)15 ServiceConfig (com.alibaba.dubbo.config.ServiceConfig)11 DemoService (com.alibaba.dubbo.config.spring.api.DemoService)11 ReferenceConfig (com.alibaba.dubbo.config.ReferenceConfig)10 DemoServiceImpl (com.alibaba.dubbo.config.spring.impl.DemoServiceImpl)7 URL (com.alibaba.dubbo.common.URL)5 JUnitMatchers.containsString (org.junit.matchers.JUnitMatchers.containsString)5 RpcException (com.alibaba.dubbo.rpc.RpcException)4 ConsumerConfig (com.alibaba.dubbo.config.ConsumerConfig)3 GenericService (com.alibaba.dubbo.rpc.service.GenericService)3 Date (java.util.Date)3 MethodConfig (com.alibaba.dubbo.config.MethodConfig)2 ProviderConfig (com.alibaba.dubbo.config.ProviderConfig)2 DemoService (com.alibaba.dubbo.config.api.DemoService)2 GenericException (com.alibaba.dubbo.rpc.service.GenericException)2 BeansException (org.springframework.beans.BeansException)2 Service (com.alibaba.dubbo.config.annotation.Service)1