コード例 #1
0
  public static void dubboStartByAPI() {
    HelloService helloService = new HelloServiceImpl();
    // 当前应用配置
    ApplicationConfig application = new ApplicationConfig();
    application.setName("helloService-provider");

    // 连接注册中心配置
    RegistryConfig registry = new RegistryConfig();
    registry.setAddress("127.0.0.1");
    registry.setPort(2181);
    registry.setProtocol("zookeeper");
    // registry.setUsername("aaa");
    // registry.setPassword("bbb");

    // 服务提供者协议配置
    ProtocolConfig protocol = new ProtocolConfig();
    protocol.setName("dubbo");
    protocol.setPort(20880);
    protocol.setThreads(10);

    // 注意:ServiceConfig为重对象,内部封装了与注册中心的连接,以及开启服务端口

    // 服务提供者暴露服务配置
    ServiceConfig<HelloService> service = new ServiceConfig<>();
    // 此实例很重,封装了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏
    service.setApplication(application);
    service.setRegistry(registry); // 多个注册中心可以用setRegistries()
    service.setProtocol(protocol); // 多个协议可以用setProtocols()
    service.setInterface(HelloService.class);
    service.setRef(helloService);
    service.setVersion("1.0.0");

    // 暴露及注册服务
    service.export();
  }
コード例 #2
0
 @Bean
 public RegistryConfig registry() {
   RegistryConfig registryConfig = new RegistryConfig();
   registryConfig.setAddress(dubboRegistryUrl);
   registryConfig.setProtocol("zookeeper");
   return registryConfig;
 }
コード例 #3
0
ファイル: ServiceBean.java プロジェクト: wushujing/dubbo
 @SuppressWarnings({"unchecked", "deprecation"})
 public void afterPropertiesSet() throws Exception {
   // 如果没有配置provider
   if (getProvider() == null) {
     // 获取IOC容器里的所有provider
     Map<String, ProviderConfig> providerConfigMap =
         applicationContext == null
             ? null
             : BeanFactoryUtils.beansOfTypeIncludingAncestors(
                 applicationContext, ProviderConfig.class, false, false);
     if (providerConfigMap != null && providerConfigMap.size() > 0) {
       Map<String, ProtocolConfig> protocolConfigMap =
           applicationContext == null
               ? null
               : BeanFactoryUtils.beansOfTypeIncludingAncestors(
                   applicationContext, ProtocolConfig.class, false, false);
       if ((protocolConfigMap == null || protocolConfigMap.size() == 0)
           && providerConfigMap.size() > 1) { // 兼容旧版本
         List<ProviderConfig> providerConfigs = new ArrayList<ProviderConfig>();
         for (ProviderConfig config : providerConfigMap.values()) {
           if (config.isDefault() != null && config.isDefault().booleanValue()) {
             providerConfigs.add(config);
           }
         }
         if (providerConfigs.size() > 0) {
           setProviders(providerConfigs);
         }
       } else {
         ProviderConfig providerConfig = null;
         for (ProviderConfig config : providerConfigMap.values()) {
           if (config.isDefault() == null || config.isDefault().booleanValue()) {
             if (providerConfig != null) {
               throw new IllegalStateException(
                   "Duplicate provider configs: " + providerConfig + " and " + config);
             }
             providerConfig = config;
           }
         }
         if (providerConfig != null) {
           setProvider(providerConfig);
         }
       }
     }
   }
   if (getApplication() == null
       && (getProvider() == null || getProvider().getApplication() == null)) {
     Map<String, ApplicationConfig> applicationConfigMap =
         applicationContext == null
             ? null
             : BeanFactoryUtils.beansOfTypeIncludingAncestors(
                 applicationContext, ApplicationConfig.class, false, false);
     if (applicationConfigMap != null && applicationConfigMap.size() > 0) {
       ApplicationConfig applicationConfig = null;
       for (ApplicationConfig config : applicationConfigMap.values()) {
         if (config.isDefault() == null || config.isDefault().booleanValue()) {
           if (applicationConfig != null) {
             throw new IllegalStateException(
                 "Duplicate application configs: " + applicationConfig + " and " + config);
           }
           applicationConfig = config;
         }
       }
       if (applicationConfig != null) {
         setApplication(applicationConfig);
       }
     }
   }
   if (getModule() == null && (getProvider() == null || getProvider().getModule() == null)) {
     Map<String, ModuleConfig> moduleConfigMap =
         applicationContext == null
             ? null
             : BeanFactoryUtils.beansOfTypeIncludingAncestors(
                 applicationContext, ModuleConfig.class, false, false);
     if (moduleConfigMap != null && moduleConfigMap.size() > 0) {
       ModuleConfig moduleConfig = null;
       for (ModuleConfig config : moduleConfigMap.values()) {
         if (config.isDefault() == null || config.isDefault().booleanValue()) {
           if (moduleConfig != null) {
             throw new IllegalStateException(
                 "Duplicate module configs: " + moduleConfig + " and " + config);
           }
           moduleConfig = config;
         }
       }
       if (moduleConfig != null) {
         setModule(moduleConfig);
       }
     }
   }
   if ((getRegistries() == null || getRegistries().size() == 0)
       && (getProvider() == null
           || getProvider().getRegistries() == null
           || getProvider().getRegistries().size() == 0)
       && (getApplication() == null
           || getApplication().getRegistries() == null
           || getApplication().getRegistries().size() == 0)) {
     Map<String, RegistryConfig> registryConfigMap =
         applicationContext == null
             ? null
             : BeanFactoryUtils.beansOfTypeIncludingAncestors(
                 applicationContext, RegistryConfig.class, false, false);
     if (registryConfigMap != null && registryConfigMap.size() > 0) {
       List<RegistryConfig> registryConfigs = new ArrayList<RegistryConfig>();
       for (RegistryConfig config : registryConfigMap.values()) {
         if (config.isDefault() == null || config.isDefault().booleanValue()) {
           registryConfigs.add(config);
         }
       }
       if (registryConfigs != null && registryConfigs.size() > 0) {
         super.setRegistries(registryConfigs);
       }
     }
   }
   if (getMonitor() == null
       && (getProvider() == null || getProvider().getMonitor() == null)
       && (getApplication() == null || getApplication().getMonitor() == null)) {
     Map<String, MonitorConfig> monitorConfigMap =
         applicationContext == null
             ? null
             : BeanFactoryUtils.beansOfTypeIncludingAncestors(
                 applicationContext, MonitorConfig.class, false, false);
     if (monitorConfigMap != null && monitorConfigMap.size() > 0) {
       MonitorConfig monitorConfig = null;
       for (MonitorConfig config : monitorConfigMap.values()) {
         if (config.isDefault() == null || config.isDefault().booleanValue()) {
           if (monitorConfig != null) {
             throw new IllegalStateException(
                 "Duplicate monitor configs: " + monitorConfig + " and " + config);
           }
           monitorConfig = config;
         }
       }
       if (monitorConfig != null) {
         setMonitor(monitorConfig);
       }
     }
   }
   if ((getProtocols() == null || getProtocols().size() == 0)
       && (getProvider() == null
           || getProvider().getProtocols() == null
           || getProvider().getProtocols().size() == 0)) {
     Map<String, ProtocolConfig> protocolConfigMap =
         applicationContext == null
             ? null
             : BeanFactoryUtils.beansOfTypeIncludingAncestors(
                 applicationContext, ProtocolConfig.class, false, false);
     if (protocolConfigMap != null && protocolConfigMap.size() > 0) {
       List<ProtocolConfig> protocolConfigs = new ArrayList<ProtocolConfig>();
       for (ProtocolConfig config : protocolConfigMap.values()) {
         if (config.isDefault() == null || config.isDefault().booleanValue()) {
           protocolConfigs.add(config);
         }
       }
       if (protocolConfigs != null && protocolConfigs.size() > 0) {
         super.setProtocols(protocolConfigs);
       }
     }
   }
   if (getPath() == null || getPath().length() == 0) {
     if (beanName != null
         && beanName.length() > 0
         && getInterface() != null
         && getInterface().length() > 0
         && beanName.startsWith(getInterface())) {
       setPath(beanName);
     }
   }
   if (!isDelay()) {
     export();
   }
 }