org.apache.dubbo.config.ServiceConfig Java Examples

The following examples show how to use org.apache.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: TestServer.java    From brave with Apache License 2.0 6 votes vote down vote up
TestServer(Propagation.Factory propagationFactory, ApplicationConfig application) {
  extractor = propagationFactory.get().extractor(Map::get);
  linkLocalIp = Platform.get().linkLocalIp();
  if (linkLocalIp != null) {
    // avoid dubbo's logic which might pick docker ip
    System.setProperty(CommonConstants.DUBBO_IP_TO_BIND, linkLocalIp);
    System.setProperty(Constants.DUBBO_IP_TO_REGISTRY, linkLocalIp);
  }
  service = new ServiceConfig<>();
  service.setApplication(application);
  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 #2
Source File: JbootDubborpc.java    From jboot with Apache License 2.0 6 votes vote down vote up
@Override
public <T> boolean serviceExport(Class<T> interfaceClass, Object object, JbootrpcServiceConfig config) {
    ServiceConfig<T> service = DubboUtil.toServiceConfig(config);
    service.setInterface(interfaceClass);
    service.setRef((T) object);

    String provider = rpcConfig.getProvider(interfaceClass.getName());
    if (provider != null) {
        service.setProvider(DubboUtil.getProvider(provider));
    }

    if (service.getGroup() == null) {
        service.setGroup(rpcConfig.getGroup(interfaceClass.getName()));
    }

    if (service.getVersion() == null) {
        service.setVersion(rpcConfig.getVersion(interfaceClass.getName()));
    }

    service.export();
    return true;
}
 
Example #3
Source File: DubboMetadataServiceExporter.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
/**
 * export {@link DubboMetadataService} as Dubbo service.
 * @return the exported {@link URL URLs}
 */
public List<URL> export() {

	if (serviceConfig == null || !serviceConfig.isExported()) {

		serviceConfig = new ServiceConfig<>();

		serviceConfig.setInterface(DubboMetadataService.class);
		// Use DubboMetadataService.VERSION as the Dubbo Service version
		serviceConfig.setVersion(DubboMetadataService.VERSION);
		// Use current Spring application name as the Dubbo Service group
		serviceConfig.setGroup(currentApplicationName);
		serviceConfig.setRef(dubboMetadataService.getIfAvailable());
		serviceConfig.setApplication(applicationConfig);
		serviceConfig.setProtocol(protocolConfigSupplier.get());

		serviceConfig.export();

		if (logger.isInfoEnabled()) {
			logger.info("The Dubbo service[{}] has been exported.",
					serviceConfig.toString());
		}
	}

	return serviceConfig.getExportedUrls();
}
 
Example #4
Source File: GenericImplProvider.java    From dubbo-samples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    new EmbeddedZooKeeper(2181, false).start();

    ApplicationConfig applicationConfig = new ApplicationConfig();
    applicationConfig.setName("generic-impl-provider");
    RegistryConfig registryConfig = new RegistryConfig();
    registryConfig.setAddress(zookeeperAddress);

    GenericService helloService = new GenericImplOfHelloService();
    ServiceConfig<GenericService> service = new ServiceConfig<>();
    service.setApplication(applicationConfig);
    service.setRegistry(registryConfig);
    service.setInterface("org.apache.dubbo.samples.generic.call.api.HelloService");
    service.setRef(helloService);
    service.setGeneric("true");
    service.export();

    System.out.println("dubbo service started");
    new CountDownLatch(1).await();
}
 
Example #5
Source File: Provider2.java    From dubbo-samples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    ServiceConfig<GreetingsService> greetingsService = new ServiceConfig<>();
    greetingsService.setApplication(application);
    greetingsService.setConfigCenter(configCenter);
    greetingsService.setRegistry(registry);
    greetingsService.setInterface(GreetingsService.class);
    greetingsService.setRef(new GreetingsServiceImpl());
    greetingsService.export();

    ServiceConfig<DemoService> demoService = new ServiceConfig<>();
    demoService.setApplication(application);
    demoService.setConfigCenter(configCenter);
    demoService.setInterface(DemoService.class);
    demoService.setRegistry(registry);
    demoService.setRef(new DemoServiceImpl());
    demoService.export();

    System.out.println("dubbo service started");
    new CountDownLatch(1).await();
}
 
Example #6
Source File: DubboConfigsMetadata.java    From dubbo-spring-boot-project with Apache License 2.0 6 votes vote down vote up
public Map<String, Map<String, Map<String, Object>>> configs() {

        Map<String, Map<String, Map<String, Object>>> configsMap = new LinkedHashMap<>();

        addDubboConfigBeans(ApplicationConfig.class, configsMap);
        addDubboConfigBeans(ConsumerConfig.class, configsMap);
        addDubboConfigBeans(MethodConfig.class, configsMap);
        addDubboConfigBeans(ModuleConfig.class, configsMap);
        addDubboConfigBeans(MonitorConfig.class, configsMap);
        addDubboConfigBeans(ProtocolConfig.class, configsMap);
        addDubboConfigBeans(ProviderConfig.class, configsMap);
        addDubboConfigBeans(ReferenceConfig.class, configsMap);
        addDubboConfigBeans(RegistryConfig.class, configsMap);
        addDubboConfigBeans(ServiceConfig.class, configsMap);

        return configsMap;

    }
 
Example #7
Source File: Application.java    From dubbo-samples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    ServiceConfig<UserService> service = new ServiceConfig<>();
    service.setInterface(UserService.class);
    service.setRef(new UserServiceImpl());

    ProtocolConfig protocolConfig = new ProtocolConfig("rest");
    protocolConfig.setPort(8090);

    DubboBootstrap bootstrap = DubboBootstrap.getInstance();
    bootstrap.application(new ApplicationConfig("dubbo-provider-for-sc"))
            .registry(new RegistryConfig("consul://127.0.0.1:8500?registry-type=service"))
            .protocol(protocolConfig)
            .service(service)
            .start()
            .await();
}
 
Example #8
Source File: DubboProvider.java    From alibabacloud-microservice-demo with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    ServiceConfig<GreetingsService> service = new ServiceConfig<>();
    service.setApplication(new ApplicationConfig("dubbo-provider"));
    service.setRegistry(new RegistryConfig("nacos://127.0.0.1:8848"));
    service.setInterface(GreetingsService.class);
    service.setRef(new GreetingsServiceImpl());
    service.export();

    System.out.println("dubbo service started");
    new CountDownLatch(1).await();
}
 
Example #9
Source File: Application.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Bean(destroyMethod = "unexport")
public ServiceConfig<GreetService> service() {
    ServiceConfig<GreetService> serviceConfig = new ServiceConfig<>();
    serviceConfig.setApplication(applicationConfig);
    serviceConfig.setRegistry(registryConfig);
    serviceConfig.setProtocol(protocolConfig);
    serviceConfig.setInterface(GreetService.class);
    serviceConfig.setRef(new GreetServiceImpl());
    serviceConfig.setTimeout(5000);
    serviceConfig.export();
    return serviceConfig;
}
 
Example #10
Source File: Provider.java    From rpcx-benchmark with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    ServiceConfig<DemoServiceImpl> service = new ServiceConfig<>();
    service.setApplication(new ApplicationConfig("dubbo-demo-api-provider"));
    String zk = "zookeeper://127.0.0.1:2181";
    if (args.length > 0) {
        zk = args[0];
    }
    service.setRegistry(new RegistryConfig(zk));
    service.setInterface(DemoService.class);
    service.setRef(new DemoServiceImpl());
    service.export();

    Thread.sleep(24 * 3600 * 1000);

}
 
Example #11
Source File: Provider1.java    From dubbo-samples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    ServiceConfig<GreetingsService> service = new ServiceConfig<>();
    service.setApplication(applicationConfig);
    service.setConfigCenter(configCenter);
    service.setInterface(GreetingsService.class);
    service.setRef(new GreetingsServiceImpl());
    service.export();
    System.out.println("dubbo service started");
    System.in.read();
}
 
Example #12
Source File: MonitorServiceIT.java    From dubbo-samples with Apache License 2.0 5 votes vote down vote up
@Before
    public void setUp() throws Exception {
        ServiceConfig<MonitorService> service = new ServiceConfig<>();
        // FIXME: has to set application name to "demo-consumer"
        service.setApplication(new ApplicationConfig("demo-consumer"));
        service.setRegistry(new RegistryConfig("zookeeper://" + zookeeperHost + ":2181"));
//        MonitorConfig monitorConfig = new MonitorConfig();
//        monitorConfig.setProtocol("registry");
//        monitorConfig.setInterval("100");
//        service.setMonitor(monitorConfig);
        service.setInterface(MonitorService.class);
        service.setFilter("-monitor");
        service.setRef(new MonitorServiceImpl());
        service.export();
    }
 
Example #13
Source File: Application.java    From dubbo-samples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    ServiceConfig<GreetingsService> service = new ServiceConfig<>();
    service.setApplication(new ApplicationConfig("first-dubbo-provider"));
    service.setRegistry(new RegistryConfig("zookeeper://" + zookeeperHost + ":2181"));
    service.setInterface(GreetingsService.class);
    service.setRef(new GreetingsServiceImpl());
    service.export();

    System.out.println("dubbo service started");
    new CountDownLatch(1).await();
}
 
Example #14
Source File: ServiceBeanIdConflictProcessor.java    From dubbo-spring-boot-project with Apache License 2.0 5 votes vote down vote up
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    if (conflictedBeanNames.contains(beanName) && bean instanceof ServiceConfig) {
        ServiceConfig serviceConfig = (ServiceConfig) bean;
        if (isConflictedServiceConfig(serviceConfig)) {
            // Set id as the bean name
            serviceConfig.setId(beanName);
        }

    }
    return bean;
}
 
Example #15
Source File: ServiceBeanIdConflictProcessor.java    From dubbo-spring-boot-project with Apache License 2.0 5 votes vote down vote up
@Override
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
    // Get raw bean type
    Class<?> rawBeanType = getUserClass(beanType);
    if (isAssignable(ServiceConfig.class, rawBeanType)) { // ServiceConfig type or sub-type
        String interfaceName = (String) beanDefinition.getPropertyValues().get("interface");
        String mappedBeanName = interfaceNamesToBeanNames.putIfAbsent(interfaceName, beanName);
        // If mapped bean name exists and does not equal current bean name
        if (mappedBeanName != null && !mappedBeanName.equals(beanName)) {
            // conflictedBeanNames will record current bean name.
            conflictedBeanNames.add(beanName);
        }
    }
}
 
Example #16
Source File: Dubbo27ITest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
private static ServiceConfig<GreeterService> getService() {
  final ServiceConfig<GreeterService> service = new ServiceConfig<>();
  service.setApplication(new ApplicationConfig("test"));
  service.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE));
  service.setProtocol(new ProtocolConfig("dubbo", TestUtil.nextFreePort()));
  service.setInterface(GreeterService.class);
  service.setRef(new GreeterServiceImpl());
  return service;
}
 
Example #17
Source File: Dubbo27ITest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws Exception {
  final CountDownLatch latch = TestUtil.initExpectedSpanLatch(2);
  final String linkLocalIp = "127.0.0.1";
  final ServiceConfig<GreeterService> service = getService();
  service.export();
  final ReferenceConfig<GreeterService> client = getClient(linkLocalIp, service.getProtocol().getPort());
  client.get().sayHello("jorge");
  TestUtil.checkSpan(latch, new ComponentSpanCount("java-dubbo", 2));
  client.destroy();
  service.unexport();
}
 
Example #18
Source File: MockServer.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
MockServer() {
  linkLocalIp = NetUtils.getLocalAddress().getHostAddress();
  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("test"));
  service.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE));
  service.setProtocol(new ProtocolConfig("dubbo", TestUtil.nextFreePort()));
  service.setInterface(GreeterService.class);
  service.setRef(new GreeterServiceImpl());
}
 
Example #19
Source File: ChronusAutoConfiguration.java    From chronus with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnBean({ApplicationConfig.class, RegistryConfig.class})
public ChronusSdkProcessor chronusSdkFacade() {
    ChronusSdkProcessor chronusClientFacade = new AbstractSdkService(){};
    ServiceConfig<ChronusSdkProcessor> serviceConfig = new ServiceConfig<>();
    serviceConfig.setApplication(applicationConfig);
    serviceConfig.setRegistry(registryConfig);
    serviceConfig.setInterface(ChronusSdkProcessor.class);
    serviceConfig.setRef(chronusClientFacade);
    serviceConfig.setPath("/" + applicationConfig.getName() + "/" + ChronusSdkProcessor.class.getName());
    //serviceConfig.setGroup(applicationConfig.getName());
    serviceConfig.export();
    return chronusClientFacade;
}
 
Example #20
Source File: SslBasicProvider.java    From dubbo-samples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    new EmbeddedZooKeeper(2181, false).start();
    // wait for embedded zookeeper start completely.
    Thread.sleep(1000);

    SslConfig sslConfig = new SslConfig();
    if (args.length > 0) {
        if (args.length < 2 || args.length > 3) {
            System.out.println(
                    "USAGE: BasicProvider certChainFilePath privateKeyFilePath " +
                            "[trustCertCollectionFilePath]\n Specify 'trustCertCollectionFilePath' only if you want " +
                            "need Mutual TLS.");
            System.exit(0);
        }


        sslConfig.setServerKeyCertChainPath(args[0]);
        sslConfig.setServerPrivateKeyPath(args[1]);
        if (args.length == 3) {
            sslConfig.setServerTrustCertCollectionPath(args[2]);
        }
    }

    ProtocolConfig protocolConfig = new ProtocolConfig("dubbo");
    protocolConfig.setSslEnabled(true);

    DubboBootstrap bootstrap = DubboBootstrap.getInstance();
    bootstrap.application(new ApplicationConfig("ssl-provider"))
             .registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))
             .protocol(protocolConfig)
             .ssl(sslConfig);

    ServiceConfig<DemoService> service = new ServiceConfig<>();
    service.setInterface(DemoService.class);
    service.setRef(new DemoServiceImpl());

    bootstrap.service(service);
    bootstrap.start();

    System.out.println("dubbo service started");
    new CountDownLatch(1).await();
}
 
Example #21
Source File: ServiceBeanIdConflictProcessor.java    From dubbo-spring-boot-project with Apache License 2.0 4 votes vote down vote up
private boolean isConflictedServiceConfig(ServiceConfig serviceConfig) {
    return Objects.equals(serviceConfig.getId(), serviceConfig.getInterface());
}
 
Example #22
Source File: SslBasicProvider.java    From dubbo-samples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    new EmbeddedZooKeeper(2181, false).start();
    // wait for embedded zookeeper start completely.
    Thread.sleep(1000);

    SslConfig sslConfig = new SslConfig();
    if (args.length > 0) {
        if (args.length < 2 || args.length > 3) {
            System.out.println(
                    "USAGE: BasicProvider certChainFilePath privateKeyFilePath " +
                            "[trustCertCollectionFilePath]\n Specify 'trustCertCollectionFilePath' only if you want " +
                            "need Mutual TLS.");
            System.exit(0);
        }


        sslConfig.setServerKeyCertChainPath(args[0]);
        sslConfig.setServerPrivateKeyPath(args[1]);
        if (args.length == 3) {
            sslConfig.setServerTrustCertCollectionPath(args[2]);
        }
    }

    ProtocolConfig protocolConfig = new ProtocolConfig("grpc");
    protocolConfig.setSslEnabled(true);

    DubboBootstrap bootstrap = DubboBootstrap.getInstance();
    bootstrap.application(new ApplicationConfig("ssl-provider"))
             .registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))
             .protocol(protocolConfig)
             .ssl(sslConfig);

    ServiceConfig<IGreeter> service = new ServiceConfig<>();
    service.setInterface(IGreeter.class);
    service.setRef(new GrpcGreeterImpl());

    bootstrap.service(service);
    bootstrap.start();

    System.out.println("dubbo service started");
    new CountDownLatch(1).await();
}
 
Example #23
Source File: ApacheDubboInstrumentationTest.java    From apm-agent-java with Apache License 2.0 4 votes vote down vote up
@Override
protected DubboTestApi buildDubboTestApi() {
    ApplicationConfig providerAppConfig = new ApplicationConfig();
    providerAppConfig.setName("dubbo-demo");

    ProtocolConfig protocolConfig = new ProtocolConfig();
    protocolConfig.setName("dubbo");
    protocolConfig.setPort(getPort());
    protocolConfig.setThreads(10);

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

    serviceConfig = new ServiceConfig<>();
    serviceConfig.setApplication(providerAppConfig);
    serviceConfig.setProtocol(protocolConfig);
    serviceConfig.setInterface(DubboTestApi.class);
    serviceConfig.setRef(new DubboTestApiImpl());
    serviceConfig.setRegistry(registryConfig);
    serviceConfig.export();

    referenceConfig = new ReferenceConfig<>();
    referenceConfig.setApplication(providerAppConfig);
    referenceConfig.setInterface(DubboTestApi.class);
    referenceConfig.setUrl("dubbo://localhost:" + getPort());
    referenceConfig.setTimeout(3000);

    List<MethodConfig> methodConfigList = new LinkedList<>();
    referenceConfig.setMethods(methodConfigList);

    MethodConfig asyncConfig = new MethodConfig();
    asyncConfig.setName("async");
    asyncConfig.setAsync(true);
    methodConfigList.add(asyncConfig);

    MethodConfig asyncNoReturnConfig = new MethodConfig();
    asyncNoReturnConfig.setName("asyncNoReturn");
    asyncNoReturnConfig.setAsync(true);
    asyncNoReturnConfig.setReturn(false);
    methodConfigList.add(asyncNoReturnConfig);

    return referenceConfig.get();
}