zoukankan      html  css  js  c++  java
  • dubbo-整合springboot、基于注解的简单实例

    一、导包:

    <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

  • 相关阅读:
    适配器模式(PHP实现)
    php基础设计模式 注册树模式、工厂模式、单列模式
    mongodb数据库操作--备份 还原 导出 导入
    mongodb 非 admin 库 认证登陆失败 原因(百度好多都 是渣)db.addUser() 请走开。
    css3 标题超过长度自动省略号
    html5新增及废除属性
    HTML5 改良的 input 元素的种类
    SQLite学习笔记(十一)&&虚拟机原理
    SQLite使用(三)&&核心API使用
    SQLite使用(二)&&数据类型
  • 原文地址:https://www.cnblogs.com/dengrong/p/11215343.html
Copyright © 2011-2022 走看看