Dubbo+Nacos 简单小案例

其他文章

Nacos介绍和安装:https://blog.csdn.net/xyjcfucdi128/article/details/108355876

1.项目结构

  • dubbo-base : 存放公共接口;
  • dubbo-consumer : 调用远程服务;
  • dubbo-provider : 提供远程服务;
    Dubbo+Nacos 简单小案例_第1张图片

2.项目依赖

2.1 dubbo-base 的 pom 依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wangmx.base</groupId>
    <artifactId>dubbo-base</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-base</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.2 dubbo-consumer 的 pom 依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>dubbo-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-consumer</name>
    <packaging>jar</packaging>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--服务接口-->
        <dependency>
            <groupId>com.wangmx.base</groupId>
            <artifactId>dubbo-base</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <!-- Dubbo依赖 -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!-- Nacos依赖 -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-nacos</artifactId>
            <version>2.7.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>1.0.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.3 dubbo-provider 的 pom 依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>dubbo-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-provider</name>
    <packaging>jar</packaging>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


        <!--服务接口-->
        <dependency>
            <groupId>com.wangmx.base</groupId>
            <artifactId>dubbo-base</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <!-- Dubbo依赖 -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!-- Nacos依赖 -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-nacos</artifactId>
            <version>2.7.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>1.0.0</version>
        </dependency>



    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

3.项目 application.properties 配置

3.1 dubbo-base 的 application.properties 配置

#空

3.2 dubbo-consumer 的 application.properties 配置

server:
  port: 8862
dubbo:
  # 配置服务信息
  application:
    name: dubbo-consumer
    # 禁用QOS同一台机器可能会有端口冲突现象
    qos-enable: false
    qos-accept-foreign-ip: false
  # 配置注册中心
  registry:
    address: nacos://127.0.0.1:8848
  # 设置超时时间
  consumer:
    timeout: 4000
spring:
  main:
    # 解决Bean重复定义问题
    allow-bean-definition-overriding: true

3.3 dubbo-provider 的 application.properties 配置

server:
  port: 8861
dubbo:
  # 配置服务信息
  application:
    name: dubbo-provider
    # 禁用QOS同一台机器可能会有端口冲突现象
    qos-enable: false
    qos-accept-foreign-ip: false
  # 配置注册中心
  registry:
    address: nacos://127.0.0.1:8848
  # 设置协议-协议由提供方指定消费方被动接受
  protocol:
    name: dubbo
    port: 20880
spring:
  main:
    # 解决Bean重复定义问题
    allow-bean-definition-overriding: true

4.项目 启动类

4.1 dubbo-base 的启动类

@SpringBootApplication
public class DubboBaseApplication {
     

    public static void main(String[] args) {
     
        SpringApplication.run(DubboBaseApplication.class, args);
    }

}

4.2 dubbo-consumer 的启动类

/**
 * 消费者
 */
@EnableDubbo //开启Dubbo的注解支持
@SpringBootApplication
public class DubboConsumerApplication {
     

    public static void main(String[] args) {
     
        SpringApplication.run(DubboConsumerApplication.class, args);
    }

}

4.3 dubbo-provider 的启动类

/**
 * 生产者
 */
@EnableDubbo //开启Dubbo的注解支持
@SpringBootApplication
public class DubboProviderApplication {
     

    public static void main(String[] args) {
     
        SpringApplication.run(DubboProviderApplication.class, args);
    }

}

5.项目 测试代码

5.1 dubbo-base 公共方法 和 实体

  • 目录结构
    Dubbo+Nacos 简单小案例_第2张图片

  • entity

    /**
     * RPC接口DTO
     * 注意这里要实现序列化接口
     * @Author wangmx
     */
    @Data
    public class ProviderTestEntity implements Serializable {
           
        // ID
        private int id;
        // 名字
        private String name;
        // 序号
        private Integer number;
    }
    
    
  • service

    /**
     * RPC接口
     * @Author wangmx
     */
    public interface IProviderService {
           
        List<ProviderTestEntity> queryList();
    }
    
    
  • vo

    /**
     * 返回数据
     *
     * @author wangmx
     */
    public class R extends HashMap<String, Object> {
           
    	private static final long serialVersionUID = 1L;
    	
    	public R() {
           
    		put("code", 200);
    		put("msg", "success");
    	}
    	
    	public static R error() {
           
    		return error(500, "未知异常,请联系管理员");
    	}
    	
    	public static R error(String msg) {
           
    		return error(500, msg);
    	}
    	
    	public static R error(int code, String msg) {
           
    		R r = new R();
    		r.put("code", code);
    		r.put("msg", msg);
    		return r;
    	}
    
    	public static R errorMassgae(int code, String msg) {
           
    		R r = new R();
    		r.put("code", code);
    		r.put("data", new ArrayList<String>());
    		r.put("msg", msg);
    		return r;
    	}
    
    	public static R ok(String msg) {
           
    		R r = new R();
    		r.put("msg", msg);
    		return r;
    	}
    	
    	public static R ok(Map<String, Object> map) {
           
    		R r = new R();
    		r.putAll(map);
    		return r;
    	}
    	
    	public static R ok() {
           
    		return new R();
    	}
    
    	@Override
    	public R put(String key, Object value) {
           
    		super.put(key, value);
    		return this;
    	}
    }
    

5.2 dubbo-consumer 的 controller

  • 目录结构
    Dubbo+Nacos 简单小案例_第3张图片

  • controller

    /**
     * 消费测试接口
     * @Author wangmx
     */
    @RestController
    @RequestMapping("/consumer")
    public class ConsumerController {
           
        // Dubbo远程调用注解
        @Reference
        private IProviderService providerService;
    
        @RequestMapping(value = "/list",method = RequestMethod.GET)
        public R getList(){
           
            // 远程调用
            List<ProviderTestEntity> list = providerService.queryList();
            return R.ok().put("data",list);
        }
    }
    

5.3 dubbo-provider 的 serviceImpl

  • 目录结构
    Dubbo+Nacos 简单小案例_第4张图片

  • impl

    /**
     * 生产者Dubbo接口实现
     * @Author wangmx
     */
    @Service
    public class ProviderServiceImpl implements IProviderService {
           
        @Override
        public List<ProviderTestEntity> queryList() {
           
            // 初始化数据
            ProviderTestEntity testEntity1 = new ProviderTestEntity();
            testEntity1.setId(1);
            testEntity1.setName("张三");
            testEntity1.setNumber(100);
    
            ProviderTestEntity testEntity2 = new ProviderTestEntity();
            testEntity2.setId(2);
            testEntity2.setName("李四");
            testEntity2.setNumber(101);
    
            ProviderTestEntity testEntity3 = new ProviderTestEntity();
            testEntity3.setId(2);
            testEntity3.setName("王五");
            testEntity3.setNumber(102);
    
            // 组装数据
            List<ProviderTestEntity> list = new ArrayList<>();
            list.add(testEntity1);
            list.add(testEntity2);
            list.add(testEntity3);
    
            return list;
        }
    }
    

6.启动 Nacos 并 访问

我这里 Nacos 是 windows 版本的 ,不知道 Nacos 或者 没有的 可以看我 Nacos 介绍和安装

  • 启动Nacos
    Dubbo+Nacos 简单小案例_第5张图片

  • 访问
    Dubbo+Nacos 简单小案例_第6张图片

7.启动服务 并 测试接

  • 启动服务

分别启动 dubbo-provide , dubbo-consumer 服务
Dubbo+Nacos 简单小案例_第7张图片
查看 Nacos 服务列表

Dubbo+Nacos 简单小案例_第8张图片

  • 调用测试接口
    Dubbo+Nacos 简单小案例_第9张图片

8.GitLab 下载地址

下载地址:https://github.com/2224132867/dubbo-nacos

你可能感兴趣的:(nacos,dubbo,分布式,spring,boot)