标签:scope cti click base ali packages open 自动扫描 stat
一、导包:
<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
标签:scope cti click base ali packages open 自动扫描 stat
原文地址:https://www.cnblogs.com/dengrong/p/11215343.html