一、導包:

<dependencies> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.7</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
其中:
<dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency>
是spring和dubbo的整合包,不導入這個包,配置registryConfig.setClient("curator")
@Bean public RegistryConfig registryConfig(){ RegistryConfig registryConfig = new RegistryConfig(); registryConfig.setAddress("zookeeper://127.0.0.1:2181"); registryConfig.setClient("curator"); return registryConfig; }
將出現錯誤:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/curator/framework/CuratorFrameworkFactory
二、遠程對象:使用@Service注解(是com.alibaba.dubbo.config.annotation.Service),遠程接口不用任何注解,
三、如何導出遠程對象
在配置類上使用注解@DubboComponentScan(basePackages = "com.dr.service"),自動掃描包下的遠程對象(使用@Service注解),client目前只知道不用
curator就出錯

@Configuration @DubboComponentScan(basePackages = "com.dr.service") public class DubboConfig { @Bean public ApplicationConfig applicationConfig(){ ApplicationConfig applicationConfig = new ApplicationConfig(); applicationConfig.setName("annotate-dubbo"); return applicationConfig; } @Bean public RegistryConfig registryConfig(){ RegistryConfig registryConfig = new RegistryConfig(); registryConfig.setAddress("zookeeper://127.0.0.1:2181"); registryConfig.setClient("curator"); return registryConfig; }
四、啟動,注意,這個實例用的zookeeper作為注冊中心,所以必須先啟動zookeeper服務

@SpringBootApplication @DubboComponentScan(basePackages="com.dr.service") public class ProviderApp { public static void main(String[] args)throws IOException { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DubboConfig.class); ctx.start(); System.out.println("Provider start..."); System.in.read(); } }
消費端:
重點是獲取遠程對象
一、如何獲取遠程對象
在遠程接口上使用注解:@Reference,com.alibaba.dubbo.config.annotation.Reference包下
注意:@ComponentScan要用在配置類上(有@Configuration),其作用是導入spring bean,而@DubboComponentScan用來在provider端導出遠程對象
gitHub地址:git@github.com:dengrongrong/dubbo-spring-boot.git