Apache Dubbo 源码搭建与解读(三)—— 基于注解和API配置

Apache Dubbo 源码搭建与解读(一)

Apache Dubbo 源码搭建与解读(二)

Apache Dubbo 源码搭建与解读(三)

Apache Dubbo 源码搭建与解读(四)

Demo 2:基于注解配置

  • dubbo-demo-annotation 模块是基于 Spring 注解配置的示例,无非就是将 XML 的那些配置信息转移到了注解上

先来看 dubbo-demo-annotation-provider 这个示例模块:

public class Application { 
    public static void main(String[] args) throws Exception { 
	    // 使用AnnotationConfigApplicationContext初始化Spring容器, 
        // 从ProviderConfiguration这个类的注解上拿相关配置信息 
        AnnotationConfigApplicationContext context =  
              new AnnotationConfigApplicationContext( 
                  ProviderConfiguration.class); 
        context.start(); 
        System.in.read(); 
    } 
    @Configuration // 配置类 
    // @EnableDubbo注解指定包下的Bean都会被扫描,并做Dubbo服务暴露出去 
    @EnableDubbo(scanBasePackages = "org.apache.dubbo.demo.provider")      
    // @PropertySource注解指定了其他配置信息 
    @PropertySource("classpath:/spring/dubbo-provider.properties")      
    static class ProviderConfiguration { 
        @Bean 
        public RegistryConfig registryConfig() { 
            RegistryConfig registryConfig = new RegistryConfig(); 
            registryConfig.setAddress("zookeeper://127.0.0.1:2181"); 
            return registryConfig; 
        } 
    } 
} 
  • 这里,同样会有一个 DemoServiceImpl 实现了 DemoService 接口,并且在org.apache.dubbo.demo.provider 目录下,能被扫描到,暴露成 Dubbo 服务。

  • 接着再来看 dubbo-demo-annotation-consumer 模块,其中 Application 中也是通过 AnnotationConfigApplicationContext 初始化 Spring 容器,也会扫描指定目录下的 Bean,会扫到 DemoServiceComponent 这个 Bean,其中就通过 @Reference 注解注入 Dubbo 服务相关的 Bean:

@Component("demoServiceComponent") 
public class DemoServiceComponent implements DemoService { 
    @Reference // 注入Dubbo服务 
    private DemoService demoService; 
    @Override 
    public String sayHello(String name) { 
        return demoService.sayHello(name); 
    } 
	  // 其他方法 
} 

Demo 3:基于 API 配置

  • 在有的场景中,不能依赖于 Spring 框架,只能使用 API 来构建 Dubbo Provider 和 Consumer,比较典型的一种场景就是在写 SDK 的时候

dubbo-demo-api-provider 模块,其中 Application.main() 方法是入口:

// 创建一个ServiceConfig的实例,泛型参数是业务接口实现类, 
// 即DemoServiceImpl 
ServiceConfig<DemoServiceImpl> service = new ServiceConfig<>(); 
// 指定业务接口 
service.setInterface(DemoService.class); 
// 指定业务接口的实现,由该对象来处理Consumer的请求 
service.setRef(new DemoServiceImpl()); 
// 获取DubboBootstrap实例,这是个单例的对象 
DubboBootstrap bootstrap = DubboBootstrap.getInstance(); 
//生成一个 ApplicationConfig 的实例、指定ZK地址以及ServiceConfig实例 
bootstrap.application(new ApplicationConfig("dubbo-demo-api-provider")) 
        .registry(new RegistryConfig("zookeeper://127.0.0.1:2181")) 
        .service(service) 
        .start() 
        .await(); 

这里,同样会有一个 DemoServiceImpl 实现了 DemoService 接口,并且在 org.apache.dubbo.demo.provider 目录下,能被扫描到,暴露成 Dubbo 服务。

再来看 dubbo-demo-api-consumer 模块,其中 Application 中包含一个普通的 main() 方法入口:

 // 创建ReferenceConfig,其中指定了引用的接口DemoService 
 ReferenceConfig<DemoService> reference = new ReferenceConfig<>(); 
 reference.setInterface(DemoService.class); 
 reference.setGeneric("true"); 
  
 // 创建DubboBootstrap,指定ApplicationConfig以及RegistryConfig 
 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 demoService = ReferenceConfigCache.getCache() 
    .get(reference); 
 String message = demoService.sayHello("dubbo"); 
 System.out.println(message); 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值