Dubbo集成Nacos

灰太狼 2023-02-20 06:56 95阅读 0赞

一、Nacos配置安装

可查看:SpringCloud 集成 Nacos 使用小结

二、Provider工程

  • pom.xml

    <?xml version=”1.0” encoding=”UTF-8”?>


    4.0.0

    org.springframework.boot
    spring-boot-starter-parent
    2.3.0.RELEASE


    com.example
    dubbo_client
    0.0.1-SNAPSHOT
    dubbo_client
    Demo project for Spring Boot


    1.8
    2.7.7
    2.1.1.RELEASE




    org.springframework.boot
    spring-boot-starter



    org.springframework.boot
    spring-boot-starter-web




    org.apache.dubbo
    dubbo-dependencies-bom
    ${dubbo.version}
    pom
    import




    org.apache.dubbo
    dubbo-spring-boot-starter
    2.7.7



    org.apache.curator
    curator-recipes
    2.13.0




    com.alibaba.cloud
    spring-cloud-starter-alibaba-nacos-discovery
    ${nacos.version}


    com.alibaba.cloud
    spring-cloud-starter-alibaba-nacos-config
    ${nacos.version}



    org.springframework.boot
    spring-boot-starter-test
    test


    org.junit.vintage
    junit-vintage-engine




    com.example
    dubbo_facade
    1.0.0
    compile






    org.springframework.boot
    spring-boot-maven-plugin




  • bootstrap.yml

    spring:
    application:

    1. name: dubbo_server

    cloud:

    1. nacos:
    2. server-addr: 127.0.0.1:8848
    3. config:
    4. namespace: 9c6b8156-d045-463d-8fe6-4658ce78d0cc
    5. file-extension: yml

    dubbo:
    protocol:

    1. name: dubbo
    2. port: 20880

    registry:

    1. address: nacos://127.0.0.1:8848

    application:

    1. name: dubbo_server

    consumer:

    1. timeout: 3000

    scan:

    1. base-packages: com.example.dubbo.service
  • DubboServerApplication

    package com.example.dubbo;

    import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    @EnableDubbo
    @SpringBootApplication
    public class DubboServerApplication {

    1. public static void main(String[] args) {
    2. SpringApplication.run(DubboServerApplication.class, args);
    3. }

    }

  • package com.example.dubbo.service.impl;

    import com.example.dubbo.facade.service.UserService;
    import org.apache.dubbo.config.annotation.DubboService;
    import org.springframework.stereotype.Component;

    /* @author liuteng */
    @Component
    @DubboService(timeout = 500, version = “1.0.0”, interfaceClass = UserService.class)
    public class UserServiceImpl implements UserService {

    1. @Override
    2. public String getUserIdByPhone(String phone) {
    3. return "1000000101001";
    4. }

    }

三、Consumer工程

  • pom.xml

    <?xml version=”1.0” encoding=”UTF-8”?>


    4.0.0

    org.springframework.boot
    spring-boot-starter-parent
    2.3.0.RELEASE


    com.example
    dubbo_client
    0.0.1-SNAPSHOT
    dubbo_client
    Demo project for Spring Boot


    1.8
    2.7.7
    2.1.1.RELEASE




    org.springframework.boot
    spring-boot-starter



    org.springframework.boot
    spring-boot-starter-web




    org.apache.dubbo
    dubbo-dependencies-bom
    ${dubbo.version}
    pom
    import




    org.apache.dubbo
    dubbo-spring-boot-starter
    2.7.7



    org.apache.curator
    curator-recipes
    2.13.0




    com.alibaba.cloud
    spring-cloud-starter-alibaba-nacos-discovery
    ${nacos.version}


    com.alibaba.cloud
    spring-cloud-starter-alibaba-nacos-config
    ${nacos.version}



    org.springframework.boot
    spring-boot-starter-test
    test


    org.junit.vintage
    junit-vintage-engine




    com.example
    dubbo_facade
    1.0.0
    compile






    org.springframework.boot
    spring-boot-maven-plugin




  • bootstrap.yml

    server:
    port: 9090

    spring:
    application:

    1. name: dubbo_client

    cloud:

    1. nacos:
    2. server-addr: 127.0.0.1:8848
    3. config:
    4. namespace: 9c6b8156-d045-463d-8fe6-4658ce78d0cc
    5. file-extension: yml

    dubbo:
    application:

    1. name: dubbo_client

    registry:

    1. address: nacos://127.0.0.1:8848

    scan:

    1. base-packages: com.example.demo.controller
  • DubboClientApplication

    package com.example.demo;

    import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;

    @EnableDubbo
    @SpringBootApplication
    public class DubboClientApplication {

    1. public static void main(String[] args) {
    2. SpringApplication.run(DubboClientApplication.class, args);
    3. }

    }

  • UserController

    package com.example.demo.controller;

    import com.example.dubbo.facade.service.UserService;
    import org.apache.dubbo.config.annotation.DubboReference;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    /* @author liuteng */
    @RestController
    @RequestMapping(value = “user/“)
    public class UserController {

    1. @DubboReference(version = "1.0.0")
    2. private UserService userService;
    3. @GetMapping(value = "/getUserIdByPhone")
    4. public String getUserIdByPhone(String phone){
    5. return userService.getUserIdByPhone(phone);
    6. }

    }

四、Facade工程

  • UserService

    package com.example.dubbo.facade.service;

    /* @author liuteng */
    public interface UserService {

    1. String getUserIdByPhone(String phone);

    }

五、工程结构

  1. ├─dubbo_client
  2. ├─src
  3. ├─main
  4. ├─java
  5. └─com
  6. └─example
  7. └─demo
  8. └─controller
  9. └─UserController.java
  10. └─resources
  11. └─bootstrap.yml
  12. ├─dubbo_facade
  13. ├─src
  14. └─main
  15. ├─java
  16. └─com
  17. └─example
  18. └─dubbo
  19. └─facade
  20. └─service
  21. └─UserService.java
  22. └─dubbo_server
  23. ├─src
  24. └─main
  25. ├─java
  26. └─com
  27. └─example
  28. └─dubbo
  29. └─service
  30. └─impl
  31. └─UserServiceImpl.java
  32. └─resources
  33. └─bootstrap.yml

六、Nacos平台
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,95人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Dubbo整合Nacos

    Dubbo项目将Nacos作为其注册中心和配置中心 Nacos提供了四个主要功能 服务发现和服务运行状况检查(服务治理):Nacos使服务易于注册自己并通过DNS

    相关 Dubbo整合Nacos

    Dubbo项目将Nacos作为其注册中心和配置中心 Nacos提供了四个主要功能 服务发现和服务运行状况检查(服务治理):Nacos使服务易于注册自己并通过DNS