com.alibaba.dubbo.config.ServiceConfig Java Examples

The following examples show how to use com.alibaba.dubbo.config.ServiceConfig. 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: ConfigTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testProviderNestedService() {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/provider-nested-service.xml");
    ctx.start();
    try {
        ServiceConfig<DemoService> serviceConfig = (ServiceConfig<DemoService>) ctx.getBean("serviceConfig");
        assertNotNull(serviceConfig.getProvider());
        assertEquals(2000, serviceConfig.getProvider().getTimeout().intValue());
        
        ServiceConfig<DemoService> serviceConfig2 = (ServiceConfig<DemoService>) ctx.getBean("serviceConfig2");
        assertNotNull(serviceConfig2.getProvider());
        assertEquals(1000, serviceConfig2.getProvider().getTimeout().intValue());
    } finally {
        ctx.stop();
        ctx.close();
    }
}
 
Example #2
Source File: ConfigTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void testSystemPropertyOverrideReferenceConfig() throws Exception {
    System.setProperty("dubbo.reference.retries", "5");

    try {
        ServiceConfig<DemoService> service = new ServiceConfig<DemoService>();
        service.setInterface(DemoService.class);
        service.setRef(new DemoServiceImpl());
        service.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE));
        ProtocolConfig protocolConfig = new ProtocolConfig("injvm");
        service.setProtocol(protocolConfig);
        service.export();

        ReferenceConfig<DemoService> reference = new ReferenceConfig<DemoService>();
        reference.setInterface(DemoService.class);
        reference.setInjvm(true);
        reference.setRetries(2);
        reference.get();
        assertEquals(Integer.valueOf(5), reference.getRetries());
    } finally {
        System.setProperty("dubbo.reference.retries", "");
    }
}
 
Example #3
Source File: ConfigTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenericServiceConfig() throws Exception {
    ServiceConfig<GenericService> service = new ServiceConfig<GenericService>();
    service.setApplication(new ApplicationConfig("test"));
    service.setRegistry(new RegistryConfig("mock://localhost"));
    service.setInterface(DemoService.class.getName());
    service.setGeneric(Constants.GENERIC_SERIALIZATION_BEAN);
    service.setRef(new GenericService(){

        public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException {
            return null;
        }
    });
    try {
        service.export();
        Collection<Registry> collection = MockRegistryFactory.getCachedRegistry();
        MockRegistry registry = (MockRegistry)collection.iterator().next();
        URL url = registry.getRegistered().get(0);
        Assert.assertEquals(Constants.GENERIC_SERIALIZATION_BEAN, url.getParameter(Constants.GENERIC_KEY));
    } finally {
        MockRegistryFactory.cleanCachedRegistry();
        service.unexport();
    }
}
 
Example #4
Source File: ConfigTest.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenericServiceConfig() throws Exception {
    ServiceConfig<GenericService> service = new ServiceConfig<GenericService>();
    service.setApplication(new ApplicationConfig("test"));
    service.setRegistry(new RegistryConfig("mock://localhost"));
    service.setInterface(DemoService.class.getName());
    service.setGeneric(Constants.GENERIC_SERIALIZATION_BEAN);
    service.setRef(new GenericService() {

        public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException {
            return null;
        }
    });
    try {
        service.export();
        Collection<Registry> collection = MockRegistryFactory.getCachedRegistry();
        MockRegistry registry = (MockRegistry) collection.iterator().next();
        URL url = registry.getRegistered().get(0);
        Assert.assertEquals(Constants.GENERIC_SERIALIZATION_BEAN, url.getParameter(Constants.GENERIC_KEY));
    } finally {
        MockRegistryFactory.cleanCachedRegistry();
        service.unexport();
    }
}
 
Example #5
Source File: ConfigTest.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testProviderNestedService() {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/provider-nested-service.xml");
    ctx.start();
    try {
        ServiceConfig<DemoService> serviceConfig = (ServiceConfig<DemoService>) ctx.getBean("serviceConfig");
        assertNotNull(serviceConfig.getProvider());
        assertEquals(2000, serviceConfig.getProvider().getTimeout().intValue());
        
        ServiceConfig<DemoService> serviceConfig2 = (ServiceConfig<DemoService>) ctx.getBean("serviceConfig2");
        assertNotNull(serviceConfig2.getProvider());
        assertEquals(1000, serviceConfig2.getProvider().getTimeout().intValue());
    } finally {
        ctx.stop();
        ctx.close();
    }
}
 
Example #6
Source File: ConfigTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenericServiceConfig() throws Exception {
    ServiceConfig<GenericService> service = new ServiceConfig<GenericService>();
    service.setApplication(new ApplicationConfig("test"));
    service.setRegistry(new RegistryConfig("mock://localhost"));
    service.setInterface(DemoService.class.getName());
    service.setGeneric(Constants.GENERIC_SERIALIZATION_BEAN);
    service.setRef(new GenericService(){

        public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException {
            return null;
        }
    });
    try {
        service.export();
        Collection<Registry> collection = MockRegistryFactory.getCachedRegistry();
        MockRegistry registry = (MockRegistry)collection.iterator().next();
        URL url = registry.getRegistered().get(0);
        Assert.assertEquals(Constants.GENERIC_SERIALIZATION_BEAN, url.getParameter(Constants.GENERIC_KEY));
    } finally {
        MockRegistryFactory.cleanCachedRegistry();
        service.unexport();
    }
}
 
Example #7
Source File: DubboServiceConfig.java    From dubbo-mock with Apache License 2.0 6 votes vote down vote up
public ServiceConfig<GenericService> fillDubboService(MockService mockService, com.tony.test.mock.po.RegistryConfig registryConfig,
        com.tony.test.mock.po.ProtocolConfig protocolConfig, MockGenericService tmpMockservice) {
    ServiceConfig<GenericService> service = new ServiceConfig<GenericService>();
    service.setInterface(mockService.getServiceInterface());
    service.setRef(tmpMockservice); // 指向一个通用服务实现 
    RegistryConfig registry = createRegistry(registryConfig.getRegistryAddress(), registryConfig.getRegistryTimeout());
    service.setRegistry(registry);
    service.setProtocols(Lists.newArrayList(new ProtocolConfig(protocolConfig.getProtocolName(), protocolConfig.getProtocolPort())));
    if (!StringUtils.isBlank(mockService.getGroupName())) {
        service.setGroup(mockService.getGroupName());
    }
    service.setTimeout(mockService.getTimeout());
    service.setRetries(mockService.getRetries());
    service.setApplication(new ApplicationConfig(mockService.getApplicationName()));
    return service;
}
 
Example #8
Source File: TestServer.java    From brave with Apache License 2.0 6 votes vote down vote up
TestServer(Propagation.Factory propagationFactory) {
  extractor = propagationFactory.get().extractor(Map::get);
  linkLocalIp = Platform.get().linkLocalIp();
  if (linkLocalIp != null) {
    // avoid dubbo's logic which might pick docker ip
    System.setProperty(Constants.DUBBO_IP_TO_BIND, linkLocalIp);
    System.setProperty(Constants.DUBBO_IP_TO_REGISTRY, linkLocalIp);
  }
  service = new ServiceConfig<>();
  service.setApplication(new ApplicationConfig("bean-provider"));
  service.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE));
  service.setProtocol(new ProtocolConfig("dubbo", PickUnusedPort.get()));
  service.setInterface(GreeterService.class);
  service.setRef((method, parameterTypes, args) -> {
    requestQueue.add(extractor.extract(RpcContext.getContext().getAttachments()));
    return args[0];
  });
}
 
Example #9
Source File: ConfigTest.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testSystemPropertyOverrideReferenceConfig() throws Exception {
    System.setProperty("dubbo.reference.retries", "5");

    try {
        ServiceConfig<DemoService> service = new ServiceConfig<DemoService>();
        service.setInterface(DemoService.class);
        service.setRef(new DemoServiceImpl());
        service.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE));
        ProtocolConfig protocolConfig = new ProtocolConfig("injvm");
        service.setProtocol(protocolConfig);
        service.export();

        ReferenceConfig<DemoService> reference = new ReferenceConfig<DemoService>();
        reference.setInterface(DemoService.class);
        reference.setInjvm(true);
        reference.setRetries(2);
        reference.get();
        assertEquals(Integer.valueOf(5), reference.getRetries());
    } finally {
        System.setProperty("dubbo.reference.retries", "");
    }
}
 
Example #10
Source File: ConfigTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testProviderNestedService() {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/provider-nested-service.xml");
    ctx.start();
    try {
        ServiceConfig<DemoService> serviceConfig = (ServiceConfig<DemoService>) ctx.getBean("serviceConfig");
        assertNotNull(serviceConfig.getProvider());
        assertEquals(2000, serviceConfig.getProvider().getTimeout().intValue());
        
        ServiceConfig<DemoService> serviceConfig2 = (ServiceConfig<DemoService>) ctx.getBean("serviceConfig2");
        assertNotNull(serviceConfig2.getProvider());
        assertEquals(1000, serviceConfig2.getProvider().getTimeout().intValue());
    } finally {
        ctx.stop();
        ctx.close();
    }
}
 
Example #11
Source File: ConfigTest.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenericServiceConfig() throws Exception {
    ServiceConfig<GenericService> service = new ServiceConfig<GenericService>();
    service.setApplication(new ApplicationConfig("test"));
    service.setRegistry(new RegistryConfig("mock://localhost"));
    service.setInterface(DemoService.class.getName());
    service.setGeneric(Constants.GENERIC_SERIALIZATION_BEAN);
    service.setRef(new GenericService(){

        public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException {
            return null;
        }
    });
    try {
        service.export();
        Collection<Registry> collection = MockRegistryFactory.getCachedRegistry();
        MockRegistry registry = (MockRegistry)collection.iterator().next();
        URL url = registry.getRegistered().get(0);
        Assert.assertEquals(Constants.GENERIC_SERIALIZATION_BEAN, url.getParameter(Constants.GENERIC_KEY));
    } finally {
        MockRegistryFactory.cleanCachedRegistry();
        service.unexport();
    }
}
 
Example #12
Source File: DubboAutoConfiguration.java    From spring-boot-starter-dubbo with Apache License 2.0 5 votes vote down vote up
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
	DubboProperties dubboProperties = this.getPropertiesConfigurationBean(DubboProperties.targetName, DubboProperties.class);
	ApplicationConfig application = dubboProperties.getApplication();
	MonitorConfig monitor = dubboProperties.getMonitor();
	ModuleConfig module = dubboProperties.getModule();
	ProviderConfig provider = dubboProperties.getProvider();
	ConsumerConfig consumer = dubboProperties.getConsumer();
	List<ProtocolConfig> protocols = this.getProtocols(dubboProperties);
	List<RegistryConfig> registryConfigs = this.getRegistrys(dubboProperties);
	List<ReferenceConfig<?>> references = dubboProperties.getReferences();
	List<ServiceConfig<?>> services = dubboProperties.getServices();
	this.registerRegistry(registryConfigs, beanFactory);

	String basePackage = dubboProperties.getBasePackage();

	if (provider != null) {
		provider.setProtocols(this.getProtocol(protocols, "spring.dubbo.provider.protocol"));
		provider.setRegistries(this.getRegistry(registryConfigs, "spring.dubbo.provider.registry"));
	}

	if (consumer != null) {
		consumer.setRegistries(this.getRegistry(registryConfigs, "spring.dubbo.consumer.registry"));
	}

	this.registerThis(basePackage, beanFactory);
	this.registerApplication(application, beanFactory);
	this.registerProtocols(protocols, beanFactory);
	this.registerMonitor(monitor, beanFactory);
	this.registerModule(module, beanFactory);
	this.registerProvider(provider, beanFactory);
	this.registerConsumer(consumer, beanFactory);
	this.registerReferences(references, beanFactory);
	this.registerServices(services, beanFactory);
	super.postProcessBeanFactory(beanFactory);
	super.postProcessAnnotationPackageService();
}
 
Example #13
Source File: ConfigTest.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@Test
public void testPath() throws Exception {
    ServiceConfig<DemoService> service = new ServiceConfig<DemoService>();
    service.setPath("a/b$c");
    try {
        service.setPath("a?b");
        fail();
    } catch (IllegalStateException e) {
        assertTrue(e.getMessage().contains(""));
    }
}
 
Example #14
Source File: ConfigTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenericServiceConfigThroughSpring() throws Exception {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/generic-export.xml");
    try {
        ctx.start();
        ServiceConfig serviceConfig = (ServiceConfig) ctx.getBean("dubboDemoService");
        URL url = (URL)serviceConfig.getExportedUrls().get(0);
        Assert.assertEquals(Constants.GENERIC_SERIALIZATION_BEAN, url.getParameter(Constants.GENERIC_KEY));
    } finally {
        ctx.destroy();
    }
}
 
Example #15
Source File: ConfigTest.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@Test
public void testReferGenericExport() throws Exception {
    ApplicationConfig ac = new ApplicationConfig("test-refer-generic-export");
    RegistryConfig rc = new RegistryConfig();
    rc.setAddress(RegistryConfig.NO_AVAILABLE);

    ServiceConfig<GenericService> sc = new ServiceConfig<GenericService>();
    sc.setApplication(ac);
    sc.setRegistry(rc);
    sc.setInterface(DemoService.class.getName());
    sc.setRef(new GenericService() {

        public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException {
            return null;
        }
    });

    ReferenceConfig<DemoService> ref = new ReferenceConfig<DemoService>();
    ref.setApplication(ac);
    ref.setRegistry(rc);
    ref.setInterface(DemoService.class.getName());

    try {
        sc.export();
        ref.get();
        Assert.fail();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        sc.unexport();
        ref.destroy();
    }
}
 
Example #16
Source File: ConfigTest.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
@Test
public void testProtocolRandomPort() throws Exception {
    ServiceConfig<DemoService> demoService = null;
    ServiceConfig<HelloService> helloService = null;

    ApplicationConfig application = new ApplicationConfig();
    application.setName("test-protocol-random-port");

    RegistryConfig registry = new RegistryConfig();
    registry.setAddress("N/A");

    ProtocolConfig protocol = new ProtocolConfig();
    protocol.setName("dubbo");
    protocol.setPort(-1);

    demoService = new ServiceConfig<DemoService>();
    demoService.setInterface(DemoService.class);
    demoService.setRef(new DemoServiceImpl());
    demoService.setApplication(application);
    demoService.setRegistry(registry);
    demoService.setProtocol(protocol);

    helloService = new ServiceConfig<HelloService>();
    helloService.setInterface(HelloService.class);
    helloService.setRef(new HelloServiceImpl());
    helloService.setApplication(application);
    helloService.setRegistry(registry);
    helloService.setProtocol(protocol);

    try {
        demoService.export();
        helloService.export();

        Assert.assertEquals(demoService.getExportedUrls().get(0).getPort(),
                            helloService.getExportedUrls().get(0).getPort());
    } finally {
        unexportService(demoService);
        unexportService(helloService);
    }
}
 
Example #17
Source File: ConfigTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testDubboProtocolPortOverride() throws Exception {
    String dubboPort = System.getProperty("dubbo.protocol.dubbo.port");
    int port = 55555;
    System.setProperty("dubbo.protocol.dubbo.port", String.valueOf(port));
    ServiceConfig<DemoService> service = null;
    try {
        ApplicationConfig application = new ApplicationConfig();
        application.setName("dubbo-protocol-port-override");

        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("N/A");

        ProtocolConfig protocol = new ProtocolConfig();

        service = new ServiceConfig<DemoService>();
        service.setInterface(DemoService.class);
        service.setRef(new DemoServiceImpl());
        service.setApplication(application);
        service.setRegistry(registry);
        service.setProtocol(protocol);
        service.export();

        Assert.assertEquals(port, service.getExportedUrls().get(0).getPort());
    } finally {
        if (StringUtils.isNotEmpty(dubboPort)) {
            System.setProperty("dubbo.protocol.dubbo.port", dubboPort);
        }
        if (service != null) {
            service.unexport();
        }
    }
}
 
Example #18
Source File: ConfigTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testReferGenericExport() throws Exception {
    ApplicationConfig ac = new ApplicationConfig("test-refer-generic-export");
    RegistryConfig rc = new RegistryConfig();
    rc.setAddress(RegistryConfig.NO_AVAILABLE);

    ServiceConfig<GenericService> sc = new ServiceConfig<GenericService>();
    sc.setApplication(ac);
    sc.setRegistry(rc);
    sc.setInterface(DemoService.class.getName());
    sc.setRef(new GenericService() {

        public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException {
            return null;
        }
    });

    ReferenceConfig<DemoService> ref = new ReferenceConfig<DemoService>();
    ref.setApplication(ac);
    ref.setRegistry(rc);
    ref.setInterface(DemoService.class.getName());

    try {
        sc.export();
        ref.get();
        Assert.fail();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        sc.unexport();
        ref.destroy();
    }
}
 
Example #19
Source File: ConfigTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testProtocolRandomPort() throws Exception {
    ServiceConfig<DemoService> demoService = null;
    ServiceConfig<HelloService> helloService = null;

    ApplicationConfig application = new ApplicationConfig();
    application.setName("test-protocol-random-port");

    RegistryConfig registry = new RegistryConfig();
    registry.setAddress("N/A");

    ProtocolConfig protocol = new ProtocolConfig();
    protocol.setName("dubbo");
    protocol.setPort(-1);

    demoService = new ServiceConfig<DemoService>();
    demoService.setInterface(DemoService.class);
    demoService.setRef(new DemoServiceImpl());
    demoService.setApplication(application);
    demoService.setRegistry(registry);
    demoService.setProtocol(protocol);

    helloService = new ServiceConfig<HelloService>();
    helloService.setInterface(HelloService.class);
    helloService.setRef(new HelloServiceImpl());
    helloService.setApplication(application);
    helloService.setRegistry(registry);
    helloService.setProtocol(protocol);

    try {
        demoService.export();
        helloService.export();

        Assert.assertEquals(demoService.getExportedUrls().get(0).getPort(),
                            helloService.getExportedUrls().get(0).getPort());
    } finally {
        unexportService(demoService);
        unexportService(helloService);
    }
}
 
Example #20
Source File: ConfigTest.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testProtocolRandomPort() throws Exception {
    ServiceConfig<DemoService> demoService = null;
    ServiceConfig<HelloService> helloService = null;

    ApplicationConfig application = new ApplicationConfig();
    application.setName("test-protocol-random-port");

    RegistryConfig registry = new RegistryConfig();
    registry.setAddress("N/A");

    ProtocolConfig protocol = new ProtocolConfig();
    protocol.setName("dubbo");
    protocol.setPort(-1);

    demoService = new ServiceConfig<DemoService>();
    demoService.setInterface(DemoService.class);
    demoService.setRef(new DemoServiceImpl());
    demoService.setApplication(application);
    demoService.setRegistry(registry);
    demoService.setProtocol(protocol);

    helloService = new ServiceConfig<HelloService>();
    helloService.setInterface(HelloService.class);
    helloService.setRef(new HelloServiceImpl());
    helloService.setApplication(application);
    helloService.setRegistry(registry);
    helloService.setProtocol(protocol);

    try {
        demoService.export();
        helloService.export();

        Assert.assertEquals(demoService.getExportedUrls().get(0).getPort(),
                            helloService.getExportedUrls().get(0).getPort());
    } finally {
        unexportService(demoService);
        unexportService(helloService);
    }
}
 
Example #21
Source File: ConfigTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testReferGenericExport() throws Exception {
    ApplicationConfig ac = new ApplicationConfig("test-refer-generic-export");
    RegistryConfig rc = new RegistryConfig();
    rc.setAddress(RegistryConfig.NO_AVAILABLE);

    ServiceConfig<GenericService> sc = new ServiceConfig<GenericService>();
    sc.setApplication(ac);
    sc.setRegistry(rc);
    sc.setInterface(DemoService.class.getName());
    sc.setRef(new GenericService() {

        public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException {
            return null;
        }
    });

    ReferenceConfig<DemoService> ref = new ReferenceConfig<DemoService>();
    ref.setApplication(ac);
    ref.setRegistry(rc);
    ref.setInterface(DemoService.class.getName());

    try {
        sc.export();
        ref.get();
        Assert.fail();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        sc.unexport();
        ref.destroy();
    }
}
 
Example #22
Source File: UrlTestBase.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@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");
}
 
Example #23
Source File: ConfigTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testSystemPropertyOverrideApiDefault() throws Exception {
    System.setProperty("dubbo.application.name", "sysover");
    System.setProperty("dubbo.application.owner", "sysowner");
    System.setProperty("dubbo.registry.address", "N/A");
    System.setProperty("dubbo.protocol.name", "dubbo");
    System.setProperty("dubbo.protocol.port", "20834");
    try {
        ServiceConfig<DemoService> serviceConfig = new ServiceConfig<DemoService>();
        serviceConfig.setInterface(DemoService.class);
        serviceConfig.setRef(new DemoServiceImpl());
        serviceConfig.export();
        try {
            assertEquals("sysover", serviceConfig.getApplication().getName());
            assertEquals("sysowner", serviceConfig.getApplication().getOwner());
            assertEquals("N/A", serviceConfig.getRegistry().getAddress());
            assertEquals("dubbo", serviceConfig.getProtocol().getName());
            assertEquals(20834, serviceConfig.getProtocol().getPort().intValue());
        } finally {
            serviceConfig.unexport();
        }
    } finally {
        System.setProperty("dubbo.application.name", "");
        System.setProperty("dubbo.application.owner", "");
        System.setProperty("dubbo.registry.address", "");
        System.setProperty("dubbo.protocol.name", "");
        System.setProperty("dubbo.protocol.port", "");
    }
}
 
Example #24
Source File: ConfigTest.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testSystemPropertyOverrideXmlDefault() throws Exception {
    System.setProperty("dubbo.application.name", "sysover");
    System.setProperty("dubbo.application.owner", "sysowner");
    System.setProperty("dubbo.registry.address", "N/A");
    System.setProperty("dubbo.protocol.name", "dubbo");
    System.setProperty("dubbo.protocol.port", "20819");
    ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/system-properties-override-default.xml");
    providerContext.start();
    try {
        ServiceConfig<DemoService> service = (ServiceConfig<DemoService>) providerContext.getBean("demoServiceConfig");
        assertEquals("sysover", service.getApplication().getName());
        assertEquals("sysowner", service.getApplication().getOwner());
        assertEquals("N/A", service.getRegistry().getAddress());
        assertEquals("dubbo", service.getProtocol().getName());
        assertEquals(20819, service.getProtocol().getPort().intValue());
    } finally {
        System.setProperty("dubbo.application.name", "");
        System.setProperty("dubbo.application.owner", "");
        System.setProperty("dubbo.registry.address", "");
        System.setProperty("dubbo.protocol.name", "");
        System.setProperty("dubbo.protocol.port", "");
        providerContext.stop();
        providerContext.close();
    }
}
 
Example #25
Source File: ConfigTest.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testSystemPropertyOverrideXml() throws Exception {
    System.setProperty("dubbo.application.name", "sysover");
    System.setProperty("dubbo.application.owner", "sysowner");
    System.setProperty("dubbo.registry.address", "N/A");
    System.setProperty("dubbo.protocol.name", "dubbo");
    System.setProperty("dubbo.protocol.port", "20819");
    System.setProperty("dubbo.service.register", "false");
    ClassPathXmlApplicationContext providerContext = new ClassPathXmlApplicationContext(ConfigTest.class.getPackage().getName().replace('.', '/') + "/system-properties-override.xml");
    providerContext.start();
    try {
        ServiceConfig<DemoService> service = (ServiceConfig<DemoService>) providerContext.getBean("demoServiceConfig");
        URL url = service.toUrls().get(0);
        assertEquals("sysover", url.getParameter("application"));
        assertEquals("sysowner", url.getParameter("owner"));
        assertEquals("dubbo", url.getProtocol());
        assertEquals(20819, url.getPort());
        String register = url.getParameter("register");
        assertTrue(register != null && !"".equals(register));
        assertEquals(false, Boolean.valueOf(register));
    } finally {
        System.setProperty("dubbo.application.name", "");
        System.setProperty("dubbo.application.owner", "");
        System.setProperty("dubbo.registry.address", "");
        System.setProperty("dubbo.protocol.name", "");
        System.setProperty("dubbo.protocol.port", "");
        System.setProperty("dubbo.service.register", "");
        providerContext.stop();
        providerContext.close();
    }
}
 
Example #26
Source File: ConfigTest.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
@Test
public void testPath() throws Exception {
    ServiceConfig<DemoService> service = new ServiceConfig<DemoService>();
    service.setPath("a/b$c");
    try {
        service.setPath("a?b");
        fail();
    } catch (IllegalStateException e) {
        assertTrue(e.getMessage().contains(""));
    }
}
 
Example #27
Source File: ConfigTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testPath() throws Exception {
    ServiceConfig<DemoService> service = new ServiceConfig<DemoService>();
    service.setPath("a/b$c");
    try {
        service.setPath("a?b");
        fail();
    } catch (IllegalStateException e) {
        assertTrue(e.getMessage().contains(""));
    }
}
 
Example #28
Source File: DubboMockServer.java    From dubbo-mock with Apache License 2.0 5 votes vote down vote up
private void unexportService(int serviceId) {
	if (dubboServices.containsKey(serviceId)) {
		ServiceConfig<GenericService> tempService = dubboServices.get(serviceId);
		if (tempService.isExported()) {
			tempService.unexport();
		}
	}
}
 
Example #29
Source File: DubboMockServer.java    From dubbo-mock with Apache License 2.0 5 votes vote down vote up
/**
 * @see com.tony.test.protocol.MockServer#startService(int)
 */
@Override
public synchronized String startService(int serviceId) {
	// 重新加载服务配置
	MockService mockService = selectMockService(serviceId);
	List<ServiceMethedRule> methodRules = selectMethodRule(serviceId);
	RegistryConfig registryConfig = selectRegistryConfig(mockService.getRegistryId());
	ProtocolConfig protocolConfig = selectProtocolConfig(mockService.getProtocolId());

	Assert.notNull(mockService, "服务id:" + serviceId + "不存在");
	Assert.notNull(methodRules, "ServiceMethedRule:" + serviceId + "不存在");
	Assert.notNull(registryConfig, "registryConfig:" + serviceId + "不存在");
	Assert.notNull(protocolConfig, "protocolConfig:" + serviceId + "不存在");
	assertConnect(registryConfig);
	
	
	unexportService(serviceId); // 卸载服务
	clearLocalCache(serviceId); // 清除本地缓存

	// 构建服务通用mock对象
	MockGenericService tmpMockservice = new MockGenericService(mockService, methodRules);
	ServiceConfig<GenericService> service = dubboServiceConfig.fillDubboService(mockService, registryConfig,
			protocolConfig, tmpMockservice);

	service.export(); // 暴露及注册服务

	updateLocalCache(serviceId, tmpMockservice, service);// 更新本地缓存

	return getServiceStatus(serviceId);
}
 
Example #30
Source File: DubboServiceModule.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
protected void bindDubboService() {
    final List<ServiceConfig<Object>> serviceConfigs = Lists.newArrayList();
    final Injector injector = Globals.get(Injector.class);
    ClassScanner.filter(Service.class).forEach(cls -> {
        if (cls.isInterface()) {
            LOGGER.warn("Ignore interface API of {}", cls.getName());
            return;
        }

        final Class<?>[] itfs = cls.getInterfaces();
        if (ArrayUtils.isEmpty(itfs)) {
            LOGGER.warn("Ignore no interface implement API of {}", cls.getName());
            return;
        }

        final Service service = cls.getAnnotation(Service.class);
        final Object instance = injector.getInstance(cls);
        for (Class<?> itf : itfs) {
            final ServiceConfig<Object> serviceConfig = new ServiceConfig<>(service);
            serviceConfig.setInterface(itf);
            serviceConfig.setRef(instance);
            serviceConfigs.add(serviceConfig);
        }
    });

    serviceConfigs.forEach(conf -> conf.export());
}