dubbo源码 start

2020-07-09  本文已影响0人  Ace_b90f

几个重要的类

dubbo遵循"微内核+插件"的设计模式,大部分插件都使用了spi的方式。

大多数会使用xml的方式使用dubbo,这时候是通过DubboNamespaceHandler来对DubboBootstrapApplicationListener进行bean的注册。如果不使用xml,而是通过JavaConfig的方式,可以手动注册ServiceClassPostProcessor作为bean,如下所示

provider
@Configuration
public class CustomDubboConfig {

    @Bean
    public ServiceClassPostProcessor serviceClassPostProcessor(){
        return new ServiceClassPostProcessor("com.dubbo.provider.service.impl");
    }

    @Bean
    public ApplicationConfig applicationConfig(){
        ApplicationConfig config = new ApplicationConfig("without-xml-provide");// 要扫描的service包
        return config;
    }

    @Bean
    public RegistryConfig registryConfig(){
        return new RegistryConfig("zookeeper://127.0.0.1:2181");
    }
}

Service通过注解DubboService进行暴露。

@DubboService
public class DemoServiceImpl implements DemoService {
    public String sayHello(String s) {
        return "from provider:" + s;
    }
}
consumer

配置类

@Configuration
public class CustomDubboConfig {

    @Bean
    public ServiceClassPostProcessor serviceClassPostProcessor(){
        return new ServiceClassPostProcessor();
    }

    @Bean
    public ApplicationConfig applicationConfig(){
        ApplicationConfig config = new ApplicationConfig("without-xml-consumer");
        return config;
    }

    @Bean
    public RegistryConfig registryConfig(){
        return new RegistryConfig("zookeeper://127.0.0.1:2181");
    }

    @Bean
    public ConsumerService consumerService(){
        return new ConsumerService((DemoService) referenceConfigs().get(DemoService.class).get());
    }

    @Bean
    public Map<Class,ReferenceConfig> referenceConfigs(){
        ReferenceConfig<DemoService> config = new ReferenceConfig<>();
        config.setInterface(DemoService.class);
        Map<Class,ReferenceConfig> map = new HashMap<>();
        map.put(DemoService.class, config);
        return map;
    }
}

消费者端的service类

public class ConsumerService {
    DemoService demoService;
    public ConsumerService(DemoService service){
        demoService = service;
    }

    public String hello(){
        String result = demoService.sayHello("ace");
        return result;
    }
}

测试成功

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(CustomDubboConfig.class);
        context.start();
        ConsumerService service = context.getBean(ConsumerService.class);
        System.out.println(service.hello());
        System.in.read();

也可以使用DubboReference为方法或者属性添加注解,如下所示

public class ConsumerService {
    @DubboReference(interfaceClass = DemoService.class)
    DemoService demoService;

    public String hello(){
        String result = demoService.sayHello("ace");
        return result;
    }
}

这时候需要在配置类文件开启Dubbo配置

@Configuration
@EnableDubboConfig
public class CustomDubboConfig {
  ...

需要注意的是,当前使用的是apache.dubbo,而不是alibaba.dubbo,需要高版本的spring,还要添加org.apache.curator的依赖。curator是一个zookeeper client实现。

参考

上一篇下一篇

猜你喜欢

热点阅读