• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Dubbo 集成Springboot

武飞扬头像
nandao158
帮助1

今天分享Dubbo 集成Springboot 实战:

1、引入jar包:

  1.  
    <!-- 整合dubbo -->
  2.  
    <dependency>
  3.  
    <groupId>io.dubbo.springboot</groupId>
  4.  
    <artifactId>spring-boot-starter-dubbo</artifactId>
  5.  
    <version>1.0.0</version>
  6.  
    <exclusions>
  7.  
    <exclusion>
  8.  
    <artifactId>slf4j-log4j12</artifactId>
  9.  
    <groupId>org.slf4j</groupId>
  10.  
    </exclusion>
  11.  
    </exclusions>
  12.  
    </dependency>
  13.  
    <!-- zookeeper客户端 -->
  14.  
    <dependency>
  15.  
    <groupId>com.101tec</groupId>
  16.  
    <artifactId>zkclient</artifactId>
  17.  
    <version>0.7</version>
  18.  
    <exclusions>
  19.  
    <exclusion>
  20.  
    <artifactId>slf4j-log4j12</artifactId>
  21.  
    <groupId>org.slf4j</groupId>
  22.  
    </exclusion>
  23.  
    </exclusions>
  24.  
    </dependency>
  25.  
     
  26.  
    <dependency>
  27.  
    <groupId>org.apache.zookeeper</groupId>
  28.  
    <artifactId>zookeeper</artifactId>
  29.  
    <version>3.4.8</version>
  30.  
    <type>pom</type>
  31.  
    <exclusions>
  32.  
    <exclusion>
  33.  
    <artifactId>slf4j-log4j12</artifactId>
  34.  
    <groupId>org.slf4j</groupId>
  35.  
    </exclusion>
  36.  
    </exclusions>
  37.  
    </dependency>

2、配置文件处理:

  1.  
    spring:
  2.  
    dubbo:
  3.  
    application:
  4.  
    name: nandao-admin-dubbo #应用名
  5.  
    registry:
  6.  
    address: zookeeper://zk1-test.nandao.org:8181;zookeeper://zk2-test.nandao.org:8181; #zookeeper地址
  7.  
    port: 8181 #提供注册的端口
  8.  
    protocol:
  9.  
    name: dubbo
  10.  
    port: 53888 #dubbo服务暴露的端口
  11.  
    scan: com.nandao.admin #扫描的包名

3、客户端业务接口配置:

  1.  
     
  2.  
    import com.alibaba.dubbo.config.annotation.Reference;
  3.  
     
  4.  
    @RestController
  5.  
    @RequestMapping("/nandao/user")
  6.  
    @Slf4j
  7.  
    public class TestController {
  8.  
     
  9.  
    @Reference
  10.  
    IManageService iManageService;
  11.  
     
  12.  
     
  13.  
    @RequestMapping(path = "/getUserDetail")
  14.  
    @ResponseBody
  15.  
    public int test(@RequestParam("userId")long userId)
  16.  
    {
  17.  
    log.info("测试成功[{}]",userId);
  18.  
    TblManage taskManage = iManageService.selectByPrimaryKey(userId);
  19.  
    return 1;
  20.  
     
  21.  
    }
  22.  
     
  23.  
    }

4、作为服务方业务接口配置:

  1.  
    import com.alibaba.dubbo.config.annotation.Service;
  2.  
     
  3.  
    */
  4.  
    @Service
  5.  
    public class IManageServiceImpl implements IManageService {
  6.  
     
  7.  
     
  8.  
    //业务代码
  9.  
    }

5、如果分模块配置,注意服务的启动类扫描的路径,保证此服务启动类均能扫描到需要的包:

  1.  
    @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
  2.  
    @EnableTransactionManagement(proxyTargetClass = true)
  3.  
    @SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
  4.  
     
  5.  
    public class AdminRpcApplication {
  6.  
     
  7.  
    public static void main(String[] args) {
  8.  
    SpringApplication.run(AdminRpcApplication.class, args);
  9.  
    }
  10.  
     
  11.  
    }

比如:此启动类在此目录下

com.nandao.admin.rpc

 它只能扫描此目录名称下的包,切记!!!

到此,分享完毕实战集成,后期会依次分析源码,敬请期待!!!

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfjgjkk
系列文章
更多 icon
同类精品
更多 icon
继续加载