• dubbo3+zookeeper/nacos+dubbo-admin


    工程结构:
    在这里插入图片描述
    版本信息:

    jdk版本:1.8
    springboot-parent版本:2.6.6
        springboot版本:2.6.6
    dubbo-spring-boot-starter版本:3.0.7
        dubbo版本:3.0.7
    dubbo-registry-zookeeper版本:3.0.7
        curator版本:4.2.0
    dubbo-registry-nacos版本:3.0.7
        nacos-client版本:2.0.4
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    注意事项:正确的版本很重要,否则会报莫名其妙的错误!!!

    1. 本地启动zookeeper服务/nacos服务(略)
    2. 新建父工程testDubbo
      pom文件:
    
    <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
    
        <groupId>org.example.testDubbogroupId>
        <artifactId>testDubboartifactId>
        <packaging>pompackaging>
        <version>1.0-SNAPSHOTversion>
        <modules>
            <module>testProvidermodule>
            <module>testConsumermodule>
            <module>apimodule>
        modules>
    
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.6.6version>
        parent>
    
        <properties>
            <maven.compiler.source>8maven.compiler.source>
            <maven.compiler.target>8maven.compiler.target>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        properties>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.apache.dubbogroupId>
                    <artifactId>dubbo-spring-boot-starterartifactId>
                    <version>3.0.7version>
                dependency>
    
                
    
                
    
    
    
    
    
    
    
                
                <dependency>
                    <groupId>org.apache.dubbogroupId>
                    <artifactId>dubbo-registry-zookeeperartifactId>
                    <version>3.0.7version>
                dependency>
    
                <dependency>
                    <groupId>org.apache.dubbogroupId>
                    <artifactId>dubbo-registry-nacosartifactId>
                    <version>3.0.7version>
                dependency>
            dependencies>
        dependencyManagement>
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    1. 新建子工程api
      pom文件:
    
    <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>testDubboartifactId>
            <groupId>org.example.testDubbogroupId>
            <version>1.0-SNAPSHOTversion>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>apiartifactId>
    
        <properties>
            <maven.compiler.source>8maven.compiler.source>
            <maven.compiler.target>8maven.compiler.target>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        properties>
    
        <dependencies>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
            dependency>
        dependencies>
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
     定义DTO和Provider接口:
    
    • 1
    package org.example.testDubbo.api.dto;
    
    import lombok.Data;
    
    import java.io.Serializable;
    import java.util.Random;
    
    @Data
    public class UserDTO implements Serializable {
    
        private Long id;
        private String name;
        private Integer age;
    
        public static UserDTO mockUser(Long id){
            UserDTO userDTO = new UserDTO();
            userDTO.setId(id);
            userDTO.setAge(18);
            userDTO.setName("张三_"+userDTO.getId());
            return userDTO;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    package org.example.testDubbo.api.provider;
    
    import org.example.testDubbo.api.dto.UserDTO;
    
    public interface UserProvider {
    
        UserDTO getById(Long id);
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    1. 新建子工程testProvider
      pom文件:
    
    <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>testDubboartifactId>
            <groupId>org.example.testDubbogroupId>
            <version>1.0-SNAPSHOTversion>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>testProviderartifactId>
    
        <properties>
            <maven.compiler.source>8maven.compiler.source>
            <maven.compiler.target>8maven.compiler.target>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        properties>
    
        <dependencies>
            <dependency>
                <groupId>org.example.testDubbogroupId>
                <artifactId>apiartifactId>
                <version>1.0-SNAPSHOTversion>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starterartifactId>
            dependency>
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubbo-spring-boot-starterartifactId>
            dependency>
    
    
    
    
    
    
    
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubbo-registry-zookeeperartifactId>
            dependency>
    
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubbo-registry-nacosartifactId>
            dependency>
        dependencies>
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    配置文件application.yml:

    dubbo:
      application:
        name: testProvider
      registry:
      #  address: zookeeper://localhost:2181
        address: nacos://localhost:8848
        group: registry_group_testDubbo_service
      protocol:
        id: dubbo
        name: dubbo
        port: 8088
      scan:
        base-packages: org.example.testDubbo.testProvider.provider
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    实现provider接口:

    package org.example.testDubbo.testProvider.provider;
    
    import org.apache.dubbo.config.annotation.DubboService;
    import org.example.testDubbo.api.dto.UserDTO;
    import org.example.testDubbo.api.provider.UserProvider;
    
    @DubboService
    public class UserProviderImpl implements UserProvider {
    
        @Override
        public UserDTO getById(Long id) {
            System.out.println("我是服务提供者,被调用了");
            return UserDTO.mockUser(id);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    1. 新建子工程testConsumer
      pom文件:
    
    <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>testDubboartifactId>
            <groupId>org.example.testDubbogroupId>
            <version>1.0-SNAPSHOTversion>
        parent>
        <modelVersion>4.0.0modelVersion>
    
        <artifactId>testConsumerartifactId>
    
        <properties>
            <maven.compiler.source>8maven.compiler.source>
            <maven.compiler.target>8maven.compiler.target>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        properties>
    
        <dependencies>
            <dependency>
                <groupId>org.example.testDubbogroupId>
                <artifactId>apiartifactId>
                <version>1.0-SNAPSHOTversion>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starterartifactId>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
            dependency>
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubbo-spring-boot-starterartifactId>
            dependency>
    
    
    
    
    
    
    
    
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubbo-registry-zookeeperartifactId>
            dependency>
    
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubbo-registry-nacosartifactId>
            dependency>
        dependencies>
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    配置文件application.yml:

    server:
      port: 80
    
    dubbo:
      application:
        name: testConsumer
      registry:
    #    address: zookeeper://localhost:2181
        address: nacos://localhost:8848
        group: registry_group_testDubbo_service
      protocol:
        id: dubbo
        name: dubbo
        port: 8088
    #  consumer:
    #    check: false
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    测试类Controller:

    package org.example.testDubbo.testConsumer.controller;
    
    import org.apache.dubbo.config.annotation.DubboReference;
    import org.example.testDubbo.api.dto.UserDTO;
    import org.example.testDubbo.api.provider.UserProvider;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping(value = "/user")
    public class UserController {
    
        @DubboReference
        private UserProvider userProvider;
    
        @GetMapping(value = "/getById")
        public UserDTO getById(@RequestParam Long id){
            return userProvider.getById(id);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    1. 部署dubbo-admin
      8.1 下载源码:https://github.com/apache/dubbo-admin.git
      8.2 由于本测试dubbo版本是3.0.7,因此我拉取了靠近该版本的git版本:2353c814
      在这里插入图片描述
      8.3 修改子工程dubbo-admin-server的配置文件:application.properties
    ……
    admin.registry.address=zookeeper://localhost:2181
    admin.config-center=zookeeper://localhost:2181
    admin.metadata-report.address=zookeeper://localhost:2181
    
    admin.registry.group=registry_group_testDubbo_service
    admin.config-center.group=registry_group_testDubbo_service
    admin.metadata-report.group=registry_group_testDubbo_service
    ……
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    8.4 启动子工程dubbo-admin-server(默认会占用端口8080,用于ui工程访问,可修改该端口)
    8.5 启动ui工程dubbo-admin-ui,启动命令:

    npm run dev
    
    • 1

    启动成功后会打印访问地址:http://localhost:8082/
    8.6 如需修改dubbo-admin-server端口,需要同时修改2处:
    dubbo-admin-server的配置文件application.properties:

    server.port=8989
    
    • 1

    dubbo-admin-ui的配置文件vue.config.js:

    
    module.exports = {
    	……
        proxy: {
          '/': {
            target: 'http://localhost:8989/',
        ……
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    Cocos Creator 3D资源的导入与动画播放详解
    澜起科技发布业界首款DDR5第三子代寄存时钟驱动器工程样片
    DSMM是什么?一篇内容让你快速了解
    PT_中心极限定理CLT:棣莫佛-拉普拉斯定理de Moivre - Laplace CLT+林德伯格-列维(Lindeberg-Levy)定理
    python毕业设计作品基于django框架新闻信息管理系统毕设成品(1)开发概要
    2023,全网最真实的自动化测试学习路线,看不懂来打我!
    Springboot企业人力资源管理系统的开发与数据挖掘t1law计算机毕业设计-课程设计-期末作业-毕设程序代做
    nginx之configure解析以及模板简介
    无线回程mesh组网从入门到精通【伸手党福利】打破行业壁垒!
    链表面试题:链表的回文结构+链表分割+相交链表+环形链表(思路+图文+代码详解)
  • 原文地址:https://blog.csdn.net/u013813491/article/details/133700703