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();
  }
Example #2
0
 @Bean
 public RegistryConfig registry() {
   RegistryConfig registryConfig = new RegistryConfig();
   registryConfig.setAddress(dubboRegistryUrl);
   registryConfig.setProtocol("zookeeper");
   return registryConfig;
 }