use of org.apache.dubbo.config.ApplicationConfig in project dubbo by alibaba.
the class Application method startWithExport.
private static void startWithExport() throws InterruptedException {
ServiceConfig<DemoServiceImpl> service = new ServiceConfig<>();
service.setInterface(DemoService.class);
service.setRef(new DemoServiceImpl());
service.setApplication(new ApplicationConfig("dubbo-demo-api-provider"));
service.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
service.export();
System.out.println("dubbo service started");
new CountDownLatch(1).await();
}
use of org.apache.dubbo.config.ApplicationConfig in project dubbo by alibaba.
the class Application method runWithRefer.
private static void runWithRefer() {
ReferenceConfig<DemoService> reference = new ReferenceConfig<>();
reference.setApplication(new ApplicationConfig("dubbo-demo-api-consumer"));
reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
reference.setInterface(DemoService.class);
DemoService service = reference.get();
String message = service.sayHello("dubbo");
System.out.println(message);
}
use of org.apache.dubbo.config.ApplicationConfig in project dubbo by alibaba.
the class Application method runWithBootstrap.
private static void runWithBootstrap() {
ReferenceConfig<DemoService> reference = new ReferenceConfig<>();
reference.setInterface(DemoService.class);
reference.setGeneric("true");
DubboBootstrap bootstrap = DubboBootstrap.getInstance();
bootstrap.application(new ApplicationConfig("dubbo-demo-api-consumer")).registry(new RegistryConfig("zookeeper://127.0.0.1:2181")).reference(reference).start();
DemoService demoService = ReferenceConfigCache.getCache().get(reference);
String message = demoService.sayHello("dubbo");
System.out.println(message);
// generic invoke
GenericService genericService = (GenericService) demoService;
Object genericInvokeResult = genericService.$invoke("sayHello", new String[] { String.class.getName() }, new Object[] { "dubbo generic invoke" });
System.out.println(genericInvokeResult);
}
use of org.apache.dubbo.config.ApplicationConfig in project dubbo by alibaba.
the class ConfigValidationUtils method loadRegistries.
public static List<URL> loadRegistries(AbstractInterfaceConfig interfaceConfig, boolean provider) {
// check && override if necessary
List<URL> registryList = new ArrayList<URL>();
ApplicationConfig application = interfaceConfig.getApplication();
List<RegistryConfig> registries = interfaceConfig.getRegistries();
if (CollectionUtils.isNotEmpty(registries)) {
for (RegistryConfig config : registries) {
String address = config.getAddress();
if (StringUtils.isEmpty(address)) {
address = ANYHOST_VALUE;
}
if (!RegistryConfig.NO_AVAILABLE.equalsIgnoreCase(address)) {
Map<String, String> map = new HashMap<String, String>();
AbstractConfig.appendParameters(map, application);
AbstractConfig.appendParameters(map, config);
map.put(PATH_KEY, RegistryService.class.getName());
AbstractInterfaceConfig.appendRuntimeParameters(map);
if (!map.containsKey(PROTOCOL_KEY)) {
map.put(PROTOCOL_KEY, DUBBO_PROTOCOL);
}
List<URL> urls = UrlUtils.parseURLs(address, map);
if (urls == null) {
throw new IllegalStateException(String.format("url should not be null,address is %s", address));
}
for (URL url : urls) {
url = URLBuilder.from(url).addParameter(REGISTRY_KEY, url.getProtocol()).setProtocol(extractRegistryType(url)).build();
if ((provider && url.getParameter(REGISTER_KEY, true)) || (!provider && url.getParameter(SUBSCRIBE_KEY, true))) {
registryList.add(url);
}
}
}
}
}
return genCompatibleRegistries(registryList, provider);
}
use of org.apache.dubbo.config.ApplicationConfig in project dubbo by alibaba.
the class GenericServiceTest method testGenericServiceException.
@Test
public void testGenericServiceException() {
ServiceConfig<GenericService> service = new ServiceConfig<GenericService>();
service.setInterface(DemoService.class.getName());
service.setRef(new GenericService() {
public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException {
if ("sayName".equals(method)) {
return "Generic " + args[0];
}
if ("throwDemoException".equals(method)) {
throw new GenericException(DemoException.class.getName(), "Generic");
}
if ("getUsers".equals(method)) {
return args[0];
}
return null;
}
});
ReferenceConfig<DemoService> reference = new ReferenceConfig<DemoService>();
reference.setInterface(DemoService.class);
reference.setUrl("dubbo://127.0.0.1:29581?generic=true&timeout=3000");
DubboBootstrap bootstrap = DubboBootstrap.getInstance().application(new ApplicationConfig("generic-test")).registry(new RegistryConfig("N/A")).protocol(new ProtocolConfig("dubbo", 29581)).service(service).reference(reference);
bootstrap.start();
try {
DemoService demoService = ReferenceConfigCache.getCache().get(reference);
// say name
Assertions.assertEquals("Generic Haha", demoService.sayName("Haha"));
// get users
List<User> users = new ArrayList<User>();
users.add(new User("Aaa"));
users = demoService.getUsers(users);
Assertions.assertEquals("Aaa", users.get(0).getName());
// throw demo exception
try {
demoService.throwDemoException();
Assertions.fail();
} catch (DemoException e) {
Assertions.assertEquals("Generic", e.getMessage());
}
} finally {
bootstrap.stop();
}
}
Aggregations