Java Code Examples for org.apache.dubbo.config.ReferenceConfig#setRegistry()

The following examples show how to use org.apache.dubbo.config.ReferenceConfig#setRegistry() . 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: DubboConsumer.java    From alibabacloud-microservice-demo with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    ReferenceConfig<GreetingsService> reference = new ReferenceConfig<>();
    reference.setApplication(new ApplicationConfig("dubbo-consumer"));
    reference.setRegistry(new RegistryConfig("nacos://127.0.0.1:8848"));
    reference.setInterface(GreetingsService.class);
    GreetingsService service = reference.get();
    String message = service.sayHi("dubbo");
    System.out.println(message);
}
 
Example 2
Source File: ApplicationConfigCache.java    From soul with Apache License 2.0 5 votes vote down vote up
/**
 * Build reference config.
 *
 * @param metaData the meta data
 * @return the reference config
 */
public ReferenceConfig<GenericService> build(final MetaData metaData) {
    ReferenceConfig<GenericService> reference = new ReferenceConfig<>();
    reference.setGeneric(true);
    reference.setApplication(applicationConfig);
    reference.setRegistry(registryConfig);
    reference.setInterface(metaData.getServiceName());
    reference.setProtocol("dubbo");
    String rpcExt = metaData.getRpcExt();
    DubboParamExtInfo dubboParamExtInfo = GsonUtils.getInstance().fromJson(rpcExt, DubboParamExtInfo.class);
    if (Objects.nonNull(dubboParamExtInfo)) {
        if (StringUtils.isNoneBlank(dubboParamExtInfo.getVersion())) {
            reference.setVersion(dubboParamExtInfo.getVersion());
        }
        if (StringUtils.isNoneBlank(dubboParamExtInfo.getGroup())) {
            reference.setGroup(dubboParamExtInfo.getGroup());
        }
        if (StringUtils.isNoneBlank(dubboParamExtInfo.getLoadbalance())) {
            final String loadBalance = dubboParamExtInfo.getLoadbalance();
            reference.setLoadbalance(buildLoadBalanceName(loadBalance));
        }
        Optional.ofNullable(dubboParamExtInfo.getTimeout()).ifPresent(reference::setTimeout);
        Optional.ofNullable(dubboParamExtInfo.getRetries()).ifPresent(reference::setRetries);
    }
    Object obj = reference.get();
    if (obj != null) {
        log.info("init apache dubbo reference success there meteData is :{}", metaData.toString());
        cache.put(metaData.getServiceName(), reference);
    }
    return reference;
}
 
Example 3
Source File: GenericServiceTest.java    From jmeter-plugins-for-apache-dubbo with Apache License 2.0 5 votes vote down vote up
@Test
public void testZk() {
    for(int i=0;i<5;i++) {
        ApplicationConfig application = new ApplicationConfig();
        application.setName("api-generic-consumer");
        ReferenceConfig<GenericService> reference = new ReferenceConfig<>();
        reference.setVersion("1.0.0");
        RegistryConfig registry = new RegistryConfig();
        registry.setProtocol(Constants.REGISTRY_ZOOKEEPER);
        registry.setAddress("192.168.0.44:2181,192.168.0.44:2182,192.168.0.44:2183");
        registry.setTimeout(10000);
        reference.setRegistry(registry);
        ConfigCenterConfig cc = new ConfigCenterConfig();
        cc.setAddress("192.168.0.58:2181,192.168.0.59:2181,192.168.0.60:2181");
        cc.setProtocol(Constants.REGISTRY_ZOOKEEPER);
        cc.setTimeout(Long.valueOf("10000"));
        cc.setGroup("");
        cc.setNamespace("");
        reference.setConfigCenter(cc);
        reference.setTimeout(2000);
        reference.setGeneric(true);
        reference.setApplication(application);
        reference.setInterface("com.jiuyescm.account.api.IUserService");
        GenericService genericService = reference.get();
        Object obj = genericService.$invoke("getUserById", new String[]{Long.class.getName()}, new Long[]{1L});
        String json = JsonUtils.toJson(obj);
        System.out.println(json);
    }
}
 
Example 4
Source File: Application.java    From dubbo-samples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    ReferenceConfig<GreetingsService> reference = new ReferenceConfig<>();
    reference.setApplication(new ApplicationConfig("first-dubbo-consumer"));
    reference.setRegistry(new RegistryConfig("zookeeper://" + zookeeperHost + ":2181"));
    reference.setInterface(GreetingsService.class);
    GreetingsService service = reference.get();
    String message = service.sayHi("dubbo");
    System.out.println(message);
}
 
Example 5
Source File: GreetingServiceIT.java    From dubbo-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
    ReferenceConfig<GreetingsService> reference = new ReferenceConfig<>();
    reference.setApplication(new ApplicationConfig("first-dubbo-consumer"));
    reference.setRegistry(new RegistryConfig("zookeeper://" + zookeeperHost + ":2181"));
    reference.setInterface(GreetingsService.class);
    GreetingsService service = reference.get();
    String message = service.sayHi("dubbo");
    TestCase.assertEquals(message, "hi, dubbo");
}
 
Example 6
Source File: MonitorServiceIT.java    From dubbo-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
    for (int i = 0; i < 10; i++) {
        demoService.sayHello("world");
        Thread.sleep(50);
    }

    ReferenceConfig<MonitorService> reference = new ReferenceConfig<>();
    reference.setApplication(new ApplicationConfig("demo-consumer"));
    reference.setRegistry(new RegistryConfig("zookeeper://" + zookeeperHost + ":2181"));
    reference.setInterface(MonitorService.class);
    reference.setFilter("-monitor");
    MonitorService service = reference.get();
    List<URL> stats = service.lookup(null);

    boolean countProvider = false;
    boolean countConsumer = false;
    for (URL stat : stats) {
        Assert.assertEquals("count", stat.getProtocol());
        Assert.assertEquals("org.apache.dubbo.samples.monitor.api.DemoService/sayHello", stat.getPath());
        if (stat.getParameter("application").equals("demo-provider")) {
            countProvider = true;
        }
        if (stat.getParameter("application").equals("demo-consumer")) {
            countConsumer = true;
        }
        System.out.println(stat);
    }
    Assert.assertTrue(countConsumer && countProvider);
}
 
Example 7
Source File: DubboProxyService.java    From bird-java with MIT License 5 votes vote down vote up
private ReferenceConfig<GenericService> buildReferenceConfig(final DubboHandle dubboHandle) {
    ReferenceConfig<GenericService> reference = REFERENCE_CONFIG_MAP.get(dubboHandle);
    if (Objects.isNull(reference)) {
        reference = new ReferenceConfig<>();
        reference.setInterface(dubboHandle.getInterfaceName());
        reference.setGeneric(true);

        reference.setRegistry(cacheRegistry(dubboHandle));
        reference.setConsumer(getConsumer(dubboHandle));

        if (StringUtils.isNoneBlank(dubboHandle.getVersion())) {
            reference.setVersion(dubboHandle.getVersion());
        }
        if (StringUtils.isNoneBlank(dubboHandle.getProtocol())) {
            reference.setProtocol(dubboHandle.getProtocol());
        }
        if (StringUtils.isNoneBlank(dubboHandle.getGroup())) {
            reference.setGroup(dubboHandle.getGroup());
        }
        if (StringUtils.isNoneBlank(dubboHandle.getLoadBalance())) {
            reference.setLoadbalance(dubboHandle.getLoadBalance());
        }

        Optional.ofNullable(dubboHandle.getTimeout()).ifPresent(reference::setTimeout);
        Optional.ofNullable(dubboHandle.getRetries()).ifPresent(reference::setRetries);

        REFERENCE_CONFIG_MAP.put(dubboHandle, reference);
    }

    return reference;
}
 
Example 8
Source File: Consumer.java    From rpcx-benchmark with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    ReferenceConfig<DemoService> reference = new ReferenceConfig<>();
    reference.setApplication(new ApplicationConfig("dubbo-demo-api-consumer"));
    String zk = "zookeeper://127.0.0.1:2181";
    if (args.length > 2) {
        zk = args[2];
    }
    reference.setRegistry(new RegistryConfig(zk));
    reference.setInterface(DemoService.class);
    reference.setTimeout(10000);
    DemoService service = reference.get();

    DemoAction demoAction = new DemoAction();
    demoAction.setDemoService(service);

    if (args.length > 0) {
        demoAction.threads = Integer.parseInt(args[0]);
    }
    if (args.length > 1) {
        demoAction.count = Integer.parseInt(args[1]);
    }

    demoAction.warmup();

    demoAction.start();

}
 
Example 9
Source File: DubboRepeater.java    From jvm-sandbox-repeater with Apache License 2.0 4 votes vote down vote up
@Override
protected Object executeRepeat(RepeatContext context) throws Exception {
    Invocation invocation = context.getRecordModel().getEntranceInvocation();
    if (!(invocation instanceof DubboInvocation)) {
        throw new RepeatException("type miss match, required DubboInvocation but found " + invocation.getClass().getSimpleName());
    }
    DubboInvocation dubboInvocation = (DubboInvocation) invocation;
    ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
    ApplicationConfig applicationConfig = new ApplicationConfig();
    applicationConfig.setName("jvm-sandbox-repeater");
    // require address to initialize registry config
    RegistryConfig registryConfig = new RegistryConfig();
    String address = context.getMeta().getExtension().get("dubbo.address");
    // using special address
    if (StringUtils.isNotEmpty(address)) {
        registryConfig.setAddress(address);
    } else {
        registryConfig.setAddress(dubboInvocation.getAddress());
    }
    String group = context.getMeta().getExtension().get("dubbo.group");
    // using special group
    if (StringUtils.isNotEmpty(group)) {
        registryConfig.setGroup(group);
    } else {
        registryConfig.setGroup(dubboInvocation.getGroup());
    }
    reference.setApplication(ConfigManager.getInstance().getApplication().orElse(applicationConfig));
    reference.setRegistry(registryConfig);

    // set protocol / interface / version / timeout
    reference.setProtocol(dubboInvocation.getProtocol());
    reference.setInterface(dubboInvocation.getInterfaceName());
    if (StringUtils.isNotEmpty(dubboInvocation.getVersion())) {
        reference.setVersion(dubboInvocation.getVersion());
    }
    // timeout
    reference.setTimeout(context.getMeta().getTimeout());
    // use generic invoke
    reference.setGeneric(true);
    // fix issue #45
    ClassLoader swap = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(GenericService.class.getClassLoader());
        GenericService genericService = reference.get();
        return genericService.$invoke(dubboInvocation.getMethodName(), dubboInvocation.getParameterTypes(), invocation.getRequest());
    } finally {
        Thread.currentThread().setContextClassLoader(swap);
    }
}