快速启动
使用注解的方式进行配置
定义服务接口
1 2 3 4 5 6 7 8 9 10
| package com.alibaba.dubbo.examples.annotation.api;
public interface AnnotationService {
String sayHello(String name);
}
|
服务提供方
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.alibaba.dubbo.examples.annotation.impl;
import com.alibaba.dubbo.config.annotation.Service; import com.alibaba.dubbo.examples.annotation.api.AnnotationService;
@Service public class AnnotationServiceImpl implements AnnotationService {
@Override public String sayHello(String name) { System.out.println("async provider received: " + name); return "annotation: hello, " + name; }
}
|
服务提供方属性配置
1 2 3 4 5
| # dubbo-provider.properties dubbo.application.name=annotation-provider dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.protocol.name=dubbo dubbo.protocol.port=20880
|
说明: 使用dubbo注解形式一般结合属性配置,用来配置应用共享的配置项。
指定扫描路径,启动容器并暴露服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| package com.alibaba.dubbo.examples.annotation;
import com.alibaba.dubbo.config.ProviderConfig; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource;
public class AnnotationProvider {
public static void main(String[] args) throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class); context.start(); System.in.read(); }
@Configuration @EnableDubbo(scanBasePackages = "com.alibaba.dubbo.examples.annotation.impl") @PropertySource("classpath:/com/alibaba/dubbo/examples/annotation/dubbo-provider.properties") static public class ProviderConfiguration {
@Bean public ProviderConfig providerConfig() { ProviderConfig providerConfig = new ProviderConfig(); providerConfig.setTimeout(5000); return providerConfig; } }
}
|
服务消费方
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.alibaba.dubbo.examples.annotation.action;
import com.alibaba.dubbo.config.annotation.Reference; import com.alibaba.dubbo.examples.annotation.api.AnnotationService;
import org.springframework.stereotype.Component;
@Component("annotationAction") public class AnnotationAction {
@Reference private AnnotationService annotationService;
public String doSayHello(String name) { return annotationService.sayHello(name); }
}
|
服务消费方属性配置
1 2 3 4
| # dubbo-consumer.properties dubbo.application.name=annotation-consumer dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.consumer.timeout=3000
|
说明: 使用dubbo注解形式一般结合属性配置,用来配置应用共享的配置项。
扫描路径,启动容器并调用服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| package com.alibaba.dubbo.examples.annotation;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import com.alibaba.dubbo.examples.annotation.action.AnnotationAction;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource;
public class AnnotationConsumer {
public static void main(String[] args) throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class); context.start(); final AnnotationAction annotationAction = (AnnotationAction) context.getBean("annotationAction"); String hello = annotationAction.doSayHello("world"); System.out.println("result :" + hello); System.in.read(); }
@Configuration @EnableDubbo(scanBasePackages = "com.alibaba.dubbo.examples.annotation.action") @PropertySource("classpath:/com/alibaba/dubbo/examples/annotation/dubbo-consumer.properties") @ComponentScan(value = {"com.alibaba.dubbo.examples.annotation.action"}) static public class ConsumerConfiguration {
@Bean public ConsumerConfig consumerConfig() { ConsumerConfig consumerConfig = new ConsumerConfig(); consumerConfig.setTimeout(3000); return consumerConfig; }
} }
|
小结
注解实现使代码更整洁,开发效率更高,随着注解和配置化的盛行,xml的方式会渐渐地淡出舞台。但使用注解对开放者的要求更高,具体的dubbo注解如何与Spring融合,在后面的章节中会进行说明。使用注解的方式,配置对象的创建及配置对象属性设置也都是Spring完成的,注意这里Spring完成配置属性地设置是指启动加载的配置属性,如上面例子中的@PropertySource注解引入的配置文件内容,此外@Service、@Reference注解中的属性Spring会自动绑定到配置对象中,至于系统参数、dubbo.properties中的配置参数等是dubbo框架自动加载并配置的,我们可在服务暴露和服务引用中看到具体的过程。