Java Code Examples for com.alibaba.dubbo.config.ReferenceConfig#setInterface()

The following examples show how to use com.alibaba.dubbo.config.ReferenceConfig#setInterface() . 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: ServiceFactory.java    From dubbox with Apache License 2.0 6 votes vote down vote up
protected T createClient(Class<T> cls, String targetIP, int targetPort, int connectTimeout,int clientNums){
    ReferenceConfig<T> referenceConfig = new ReferenceConfig<T>();
    referenceConfig.setInterface(cls);
    StringBuilder url = new StringBuilder();
    url.append("dubbo://");
    url.append(targetIP);
    url.append(":");
    url.append(targetPort);
    url.append("/");
    url.append(cls.getName());
    referenceConfig.setUrl(url.toString());
    // hardcode
    referenceConfig.setConnections(clientNums);
    ApplicationConfig application = new ApplicationConfig();
    application.setName("dubbo_consumer");
    referenceConfig.setApplication(application);
    referenceConfig.setTimeout(connectTimeout);
    return referenceConfig.get();
}
 
Example 2
Source File: ServiceFactory.java    From dubbox with Apache License 2.0 6 votes vote down vote up
protected T createClient(Class<T> cls, String targetIP, int targetPort, int connectTimeout,int clientNums){
    ReferenceConfig<T> referenceConfig = new ReferenceConfig<T>();
    referenceConfig.setInterface(cls);
    StringBuilder url = new StringBuilder();
    url.append("dubbo://");
    url.append(targetIP);
    url.append(":");
    url.append(targetPort);
    url.append("/");
    url.append(cls.getName());
    referenceConfig.setUrl(url.toString());
    // hardcode
    referenceConfig.setConnections(clientNums);
    ApplicationConfig application = new ApplicationConfig();
    application.setName("dubbo_consumer");
    referenceConfig.setApplication(application);
    referenceConfig.setTimeout(connectTimeout);
    return referenceConfig.get();
}
 
Example 3
Source File: ServiceFactory.java    From dubbox with Apache License 2.0 6 votes vote down vote up
protected T createClient(Class<T> cls, String targetIP, int targetPort, int connectTimeout,int clientNums, String protocol, String serialization){
    ReferenceConfig<T> referenceConfig = new ReferenceConfig<T>();
    referenceConfig.setInterface(cls);
    StringBuilder url = new StringBuilder();
    url.append(protocol);
    url.append("://");
    url.append(targetIP);
    url.append(":");
    url.append(targetPort);
    url.append("/");
    url.append(cls.getName());
    url.append("?optimizer=com.alibaba.dubbo.rpc.benchmark.SerializationOptimizerImpl");
    if (!StringUtils.isEmpty(serialization)) {
        url.append("&serialization=");
        url.append(serialization);
    }
    referenceConfig.setUrl(url.toString());
    // hardcode
    referenceConfig.setConnections(clientNums);
    ApplicationConfig application = new ApplicationConfig();
    application.setName("dubbo_consumer");
    referenceConfig.setApplication(application);
    referenceConfig.setTimeout(connectTimeout);
    return referenceConfig.get();
}
 
Example 4
Source File: ServiceFactory.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
protected T createClient(Class<T> cls, String targetIP, int targetPort, int connectTimeout,int clientNums){
    ReferenceConfig<T> referenceConfig = new ReferenceConfig<T>();
    referenceConfig.setInterface(cls);
    StringBuilder url = new StringBuilder();
    url.append("dubbo://");
    url.append(targetIP);
    url.append(":");
    url.append(targetPort);
    url.append("/");
    url.append(cls.getName());
    referenceConfig.setUrl(url.toString());
    // hardcode
    referenceConfig.setConnections(clientNums);
    ApplicationConfig application = new ApplicationConfig();
    application.setName("dubbo_consumer");
    referenceConfig.setApplication(application);
    referenceConfig.setTimeout(connectTimeout);
    return referenceConfig.get();
}
 
Example 5
Source File: Consumer.java    From dubbo-samples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

         System.out.println("\n\n\nstart to generic invoke");
         ApplicationConfig applicationConfig = new ApplicationConfig();
         ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
         applicationConfig.setName("UserProviderGer");
         reference.setApplication(applicationConfig);
         RegistryConfig registryConfig = new RegistryConfig();
         registryConfig.setAddress("zookeeper://127.0.0.1:2181");
         reference.setRegistry(registryConfig);
         reference.setGeneric(true);
         reference.setInterface("com.ikurento.user.UserProvider");
         GenericService genericService = reference.get();
         Object[] parameterArgs = new Object[]{"A003"};
         Object result = genericService.$invoke("GetUser", null , parameterArgs);
         System.out.println("res: " + result);

         System.out.println("\n\n\nstart to generic invoke1");
         User user = new User();
         user.setName("Patrick");
         user.setId("id");
         user.setAge(10);
         parameterArgs = new Object[]{user};
         Object result1 = genericService.$invoke("queryUser", new String[]{"com.ikurento.user.User"} , parameterArgs);
         System.out.println("res: " + result1);
    }
 
Example 6
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 7
Source File: ServiceFactory.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
protected T createClient(Class<T> cls, String targetIP, int targetPort, int connectTimeout,int clientNums, String protocol, String serialization){
    ReferenceConfig<T> referenceConfig = new ReferenceConfig<T>();
    referenceConfig.setInterface(cls);
    StringBuilder url = new StringBuilder();
    url.append(protocol);
    url.append("://");
    url.append(targetIP);
    url.append(":");
    url.append(targetPort);
    url.append("/");
    url.append(cls.getName());
    url.append("?optimizer=com.alibaba.dubbo.rpc.benchmark.SerializationOptimizerImpl");
    if (!StringUtils.isEmpty(serialization)) {
        url.append("&serialization=");
        url.append(serialization);
    }
    referenceConfig.setUrl(url.toString());
    // hardcode
    referenceConfig.setConnections(clientNums);
    ApplicationConfig application = new ApplicationConfig();
    application.setName("dubbo_consumer");
    referenceConfig.setApplication(application);
    referenceConfig.setTimeout(connectTimeout);
    return referenceConfig.get();
}
 
Example 8
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 9
Source File: DubboInvoker.java    From dubbo-postman with MIT License 5 votes vote down vote up
ReferenceConfig<GenericService> createReference(PostmanDubboRequest request, DubboParamValue rpcParamValue){

        ReferenceConfig<GenericService> newReference = new ReferenceConfig<GenericService>();

        //设置默认超时无限制,用于在本地调试的时候用
        newReference.setTimeout(Integer.MAX_VALUE);
        newReference.setApplication(application);
        newReference.setInterface(request.getInterfaceName());

        String group = request.getGroup();

        //default是我加的,dubbo默认是没有的
        if(group.isEmpty() || group.equals("default")){

        }else{
            newReference.setGroup(group);
        }

        if(rpcParamValue.isUseDubbo()){
            //直连
            newReference.setUrl(rpcParamValue.getDubboUrl());
            logger.info("直连dubbo地址:"+rpcParamValue.getDubboUrl());
        }else{
            //通过zk访问
            newReference.setRegistry(new RegistryConfig(rpcParamValue.getRegistry()));
        }

        newReference.setVersion(request.getVersion());
        newReference.setGeneric(true);
        //hard code
        newReference.setRetries(1);

        return newReference;
    }
 
Example 10
Source File: ConfigTest.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Test
public void testToString() {
    ReferenceConfig<DemoService> reference = new ReferenceConfig<DemoService>();
    reference.setApplication(new ApplicationConfig("consumer"));
    reference.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE));
    reference.setInterface(DemoService.class);
    reference.setUrl("dubbo://127.0.0.1:20881");
    String str = reference.toString();
    assertTrue(str.startsWith("<dubbo:reference "));
    assertTrue(str.contains(" url=\"dubbo://127.0.0.1:20881\" "));
    assertTrue(str.contains(" interface=\"com.alibaba.dubbo.config.spring.api.DemoService\" "));
    assertTrue(str.endsWith(" />"));
}
 
Example 11
Source File: ConfigTest.java    From dubbo-2.6.5 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 12
Source File: DubboReferenceInjector.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@Override
public void injectMembers(final T instance) {
    try {
        final Reference reference = field.getAnnotation(Reference.class);
        final ReferenceConfig<?> refer = new ReferenceConfig<>(reference);
        refer.setCheck(reference.check());
        refer.setInterface(field.getType());
        final Object newInstance = ReferenceConfigCache.getCache().get(refer);
        field.set(instance, newInstance);
    } catch (final IllegalAccessException e) {
        throw (Error) new IllegalAccessError(e.getMessage()).initCause(e);
    }
}
 
Example 13
Source File: XAResourceDeserializerImpl.java    From ByteJTA with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void processInitPhysicalInstanceIfNecessary(RemoteAddr remoteAddr) throws RpcException {
	RemoteCoordinatorRegistry participantRegistry = RemoteCoordinatorRegistry.getInstance();
	TransactionBeanRegistry beanRegistry = TransactionBeanRegistry.getInstance();

	RemoteCoordinator participant = participantRegistry.getPhysicalInstance(remoteAddr);
	if (participant == null) {
		ApplicationConfig applicationConfig = beanRegistry.getBean(ApplicationConfig.class);
		RegistryConfig registryConfig = beanRegistry.getBean(RegistryConfig.class);
		ProtocolConfig protocolConfig = beanRegistry.getBean(ProtocolConfig.class);

		ReferenceConfig<RemoteCoordinator> referenceConfig = new ReferenceConfig<RemoteCoordinator>();
		referenceConfig.setInterface(RemoteCoordinator.class);
		referenceConfig.setTimeout(15000);
		referenceConfig.setCluster("failfast");
		referenceConfig.setLoadbalance("bytejta");
		referenceConfig.setFilter("bytejta");
		referenceConfig.setGroup("org-bytesoft-bytejta");
		referenceConfig.setCheck(false);
		referenceConfig.setRetries(-1);
		referenceConfig.setUrl(String.format("%s:%s", remoteAddr.getServerHost(), remoteAddr.getServerPort()));
		referenceConfig.setScope(Constants.SCOPE_REMOTE);

		referenceConfig.setApplication(applicationConfig);
		if (registryConfig != null) {
			referenceConfig.setRegistry(registryConfig);
		}
		if (protocolConfig != null) {
			referenceConfig.setProtocol(protocolConfig.getName());
		} // end-if (protocolConfig != null)

		RemoteCoordinator reference = referenceConfig.get();
		if (reference == null) {
			throw new RpcException("Cannot get the application name of the remote application.");
		}

		participantRegistry.putPhysicalInstance(remoteAddr, reference);
	}
}
 
Example 14
Source File: DubboMonitorConsumerFactory.java    From EasyTransaction with Apache License 2.0 5 votes vote down vote up
private GenericService generateService(String appId, Class<?> monitorInterface) {
    
    ReferenceConfig<GenericService> referenceConfig = new ReferenceConfig<GenericService>();
    referenceConfig.setInterface(monitorInterface); // 弱类型接口名 
    referenceConfig.setVersion("1.0.0"); 
    referenceConfig.setGeneric(true); // 声明为泛化接口 
    referenceConfig.setGroup(appId + "-" + monitorInterface.getSimpleName());
    referenceConfig.setCheck(false);
    
    if(applicationConfig != null) {
        referenceConfig.setApplication(applicationConfig);
    }
    
    if(registryConfig != null) {
        referenceConfig.setRegistry(registryConfig);
    }
    
    if(protocolConfig != null) {
        referenceConfig.setProtocol(protocolConfig.getName());
    }
    
    if(moduleConfig != null) {
        referenceConfig.setModule(moduleConfig);
    }
    
    if(monitorConfig != null) {
        referenceConfig.setMonitor(monitorConfig);
    }
    
    if(consumerConfig != null) {
        referenceConfig.setConsumer(consumerConfig);
    }
    
    if(customizationer != null) {
        customizationer.customDubboReferance(appId,null,referenceConfig);
    }
    
    return referenceConfig.get();
}
 
Example 15
Source File: ConfigTest.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testToString() {
    ReferenceConfig<DemoService> reference = new ReferenceConfig<DemoService>();
    reference.setApplication(new ApplicationConfig("consumer"));
    reference.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE));
    reference.setInterface(DemoService.class);
    reference.setUrl("dubbo://127.0.0.1:20881");
    String str = reference.toString();
    assertTrue(str.startsWith("<dubbo:reference "));
    assertTrue(str.contains(" url=\"dubbo://127.0.0.1:20881\" "));
    assertTrue(str.contains(" interface=\"com.alibaba.dubbo.config.spring.api.DemoService\" "));
    assertTrue(str.endsWith(" />"));
}
 
Example 16
Source File: DubboReferenceInterceptor.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@Override
protected void inject(final MethodInvocation invocation, final Method method, final Class<?> returnType) throws Throwable {
    final Reference reference = method.getAnnotation(Reference.class);
    final ReferenceConfig<?> refer = new ReferenceConfig<>(reference);
    refer.setCheck(reference.check());
    refer.setInterface(returnType);
    final Object newInstance = ReferenceConfigCache.getCache().get(refer);
    setInstance(invocation.getThis(), method, returnType, newInstance);
}
 
Example 17
Source File: ConfigTest.java    From dubbo3 with Apache License 2.0 4 votes vote down vote up
@Test
public void testAppendFilter() throws Exception {
    ProviderConfig provider = new ProviderConfig();
    provider.setFilter("classloader,monitor");
    ServiceConfig<DemoService> service = new ServiceConfig<DemoService>();
    service.setFilter("accesslog,trace");
    service.setProvider(provider);
    service.setProtocol(new ProtocolConfig("dubbo", 20880));
    service.setApplication(new ApplicationConfig("provider"));
    service.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE));
    service.setInterface(DemoService.class);
    service.setRef(new DemoServiceImpl());
    try {
        service.export();
        List<URL> urls = service.toUrls();
        assertNotNull(urls);
        assertEquals(1, urls.size());
        assertEquals("classloader,monitor,accesslog,trace", urls.get(0).getParameter("service.filter"));
        
        ConsumerConfig consumer = new ConsumerConfig();
        consumer.setFilter("classloader,monitor");
        ReferenceConfig<DemoService> reference = new ReferenceConfig<DemoService>();
        reference.setFilter("accesslog,trace");
        reference.setConsumer(consumer);
        reference.setApplication(new ApplicationConfig("consumer"));
        reference.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE));
        reference.setInterface(DemoService.class);
        reference.setUrl("dubbo://" + NetUtils.getLocalHost() + ":20880?" + DemoService.class.getName() + "?check=false");
        try {
            reference.get();
            urls = reference.toUrls();
            assertNotNull(urls);
            assertEquals(1, urls.size());
            assertEquals("classloader,monitor,accesslog,trace", urls.get(0).getParameter("reference.filter"));
        } finally {
            reference.destroy();
        }
    } finally {
        service.unexport();
    }
}
 
Example 18
Source File: ConfigTest.java    From dubbo3 with Apache License 2.0 4 votes vote down vote up
@Test
public void testApiOverrideProperties() throws Exception {
    ApplicationConfig application = new ApplicationConfig();
    application.setName("api-override-properties");
    
    RegistryConfig registry = new RegistryConfig();
    registry.setAddress("N/A");
    
    ProtocolConfig protocol = new ProtocolConfig();
    protocol.setName("dubbo");
    protocol.setPort(13123);
    
    ServiceConfig<DemoService> service = new ServiceConfig<DemoService>();
    service.setInterface(DemoService.class);
    service.setRef(new DemoServiceImpl());
    service.setApplication(application);
    service.setRegistry(registry);
    service.setProtocol(protocol);
    service.export();
    
    try {
        URL url = service.toUrls().get(0);
        assertEquals("api-override-properties", url.getParameter("application"));
        assertEquals("world", url.getParameter("owner"));
        assertEquals(13123, url.getPort());
        
        ReferenceConfig<DemoService> reference = new ReferenceConfig<DemoService>();
        reference.setApplication(new ApplicationConfig("consumer"));
        reference.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE));
        reference.setInterface(DemoService.class);
        reference.setUrl("dubbo://127.0.0.1:13123");
        reference.get();
        try {
            url = reference.toUrls().get(0);
            assertEquals("2000", url.getParameter("timeout"));
        } finally {
            reference.destroy();
        }
    } finally {
        service.unexport();
    }
}
 
Example 19
Source File: DubboEasyTransRpcConsumerImpl.java    From EasyTransaction with Apache License 2.0 4 votes vote down vote up
private GenericService getCaller(String appId, String busCode,EasyTransRequest<?,?> request) {
	GenericService genericService = mapRef.get(getTargetKey(appId,busCode));
	if(genericService == null){
		
		List<Class<?>> typeArguments = ReflectUtil.getTypeArguments(EasyTransRequest.class, request.getClass());
		Class<?> executorCLass = typeArguments.get(1);
		RelativeInterface annotation = executorCLass.getAnnotation(RelativeInterface.class);
		Class<?> value = annotation.value();
		ReferenceConfig<GenericService> referenceConfig = new ReferenceConfig<GenericService>();
		referenceConfig.setInterface(value.getName()); // 弱类型接口名 
		referenceConfig.setVersion("1.0.0"); 
		referenceConfig.setGeneric(true); // 声明为泛化接口 
		referenceConfig.setGroup(appId + "-" + busCode);
		referenceConfig.setCheck(false);
		referenceConfig.setSticky(true);//设置粘滞连接以优化级联事务的级联提交性能
		
		if(applicationConfig != null) {
			referenceConfig.setApplication(applicationConfig);
		}
		
		if(registryConfig != null) {
			referenceConfig.setRegistry(registryConfig);
		}
		
		if(protocolConfig != null) {
			referenceConfig.setProtocol(protocolConfig.getName());
		}
		
		if(moduleConfig != null) {
			referenceConfig.setModule(moduleConfig);
		}
		
		if(monitorConfig != null) {
			referenceConfig.setMonitor(monitorConfig);
		}
		
		if(consumerConfig != null) {
			referenceConfig.setConsumer(consumerConfig);
		}
		
		if(customizationer != null) {
			customizationer.customDubboReferance(appId,busCode,referenceConfig);
		}
		
		genericService = referenceConfig.get();
		mapRef.put(getTargetKey(appId,busCode), genericService);
	}
	return genericService;
}
 
Example 20
Source File: ConfigTest.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
@Test
public void testApiOverrideProperties() throws Exception {
    ApplicationConfig application = new ApplicationConfig();
    application.setName("api-override-properties");
    
    RegistryConfig registry = new RegistryConfig();
    registry.setAddress("N/A");
    
    ProtocolConfig protocol = new ProtocolConfig();
    protocol.setName("dubbo");
    protocol.setPort(13123);
    
    ServiceConfig<DemoService> service = new ServiceConfig<DemoService>();
    service.setInterface(DemoService.class);
    service.setRef(new DemoServiceImpl());
    service.setApplication(application);
    service.setRegistry(registry);
    service.setProtocol(protocol);
    service.export();
    
    try {
        URL url = service.toUrls().get(0);
        assertEquals("api-override-properties", url.getParameter("application"));
        assertEquals("world", url.getParameter("owner"));
        assertEquals(13123, url.getPort());
        
        ReferenceConfig<DemoService> reference = new ReferenceConfig<DemoService>();
        reference.setApplication(new ApplicationConfig("consumer"));
        reference.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE));
        reference.setInterface(DemoService.class);
        reference.setUrl("dubbo://127.0.0.1:13123");
        reference.get();
        try {
            url = reference.toUrls().get(0);
            assertEquals("2000", url.getParameter("timeout"));
        } finally {
            reference.destroy();
        }
    } finally {
        service.unexport();
    }
}