码迷,mamicode.com
首页 > 编程语言 > 详细

dubbo-整合springboot、基于注解的简单实例

时间:2019-07-19 21:06:06      阅读:145      评论:0      收藏:0      [点我收藏+]

标签: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>
View Code

其中:

<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;
    }
View Code

四、启动,注意,这个实例用的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();
    }
}
View Code

消费端:

重点是获取远程对象

一、如何获取远程对象

   在远程接口上使用注解:@Reference,com.alibaba.dubbo.config.annotation.Reference包下

  注意:@ComponentScan要用在配置类上(有@Configuration),其作用是导入spring bean,而@DubboComponentScan用来在provider端导出远程对象

gitHub地址:git@github.com:dengrongrong/dubbo-spring-boot.git

 

dubbo-整合springboot、基于注解的简单实例

标签:scope   cti   click   base   ali   packages   open   自动扫描   stat   

原文地址:https://www.cnblogs.com/dengrong/p/11215343.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!