• Dubbo+Zookeeper远程调用服务


    1、什么是Dubbo

    官网:https://dubbo.apache.org/zh/

    Dubbo 是一款微服务开发框架,它提供了 RPC通信 与 微服务治理 两大关键能力。这意味着,使用 Dubbo 开发的微服务,将具备相互之间的远程发现与通信能力, 同时利用 Dubbo 提供的丰富服务治理能力,可以实现诸如服务发现、负载均衡、流量调度等服务治理诉求。
    服务发现:
    Dubbo 基于消费端的自动服务发现能力,其基本工作原理如下图:
    在这里插入图片描述
    在本片中笔者将用到zookeeper为注册中心实现远程调用,zookeeper的安装在另一篇文章中有相关的介绍。

    2.创建父工程(DubboTest)

    创建父工程的主要目的是为了方便管理

    在这里插入图片描述

    3.创建dubbo-api (公共类接口)

    工程目录:
    在这里插入图片描述
    创建接口ApiService

    public interface ApiService {
       String test();
    }
    
    • 1
    • 2
    • 3

    4.创建生产者(dubbo-provider)

    工程目录
    在这里插入图片描述

    pom文件引入依赖

    
    
        
            org.springframework.boot
            spring-boot-starter-parent
            2.6.4
             
        
        4.0.0
    
        dubbo-provider1
    
        
    
            
            
                org.springframework.boot
                spring-boot-starter-web
                2.5.4
            
            
            
                org.apache.dubbo
                dubbo-spring-boot-starter
                2.7.1
            
            
                org.apache.dubbo
                dubbo
                2.7.1
            
            
            
                org.apache.dubbo
                dubbo-dependencies-zookeeper
                2.7.1
                pom
            
            
            
                com.porridge.www
                dubbo-api
                1.0-SNAPSHOT
            
        
    
    
    • 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

    application.yml

    server:
      port: 8001
    
    dubbo:
      application:
        name: dubbo-provider
      registry:
        address: zookeeper://127.0.0.1:2181
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    DubboProviderApplication

    //开启Dubbo远程调用
    @EnableDubbo
    @SpringBootApplication
    public class DubboProviderApplication {
        public static void main(String[] args) {
            SpringApplication.run(DubboProviderApplication.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    IndexServiceImpl

    **注意:**这里的@Service注解是Dubbo包下的注解
    在这里插入图片描述

    @Service
    public class IndexServiceImpl implements ApiService {
    
        @Override
        public String test() {
            return "This is test()";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4.创建消费者(dubbo-consumer)

    工程目录

    在这里插入图片描述

    pom

    
    
        
            org.springframework.boot
            spring-boot-starter-parent
            2.6.4
             
        
    
        4.0.0
    
        dubbo-comsumer
        
    
            
            
                org.springframework.boot
                spring-boot-starter-web
                2.5.4
            
            
            
                org.apache.dubbo
                dubbo-spring-boot-starter
                2.7.1
            
            
                org.apache.dubbo
                dubbo
                2.7.1
            
            
                org.apache.dubbo
                dubbo-dependencies-zookeeper
                2.7.1
                pom
            
            
            
                com.porridge.www
                dubbo-api
                1.0-SNAPSHOT
            
        
    
    
    
    • 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

    application.yml

    server:
      port: 8002
    
    dubbo:
      application:
        name: dubbo-comsumer
      registry:
        address: zookeeper://127.0.0.1:2181
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    DubboComsumerApplication

    @SpringBootApplication
    @EnableDubbo
    public class DubboComsumerApplication {
        public static void main(String[] args) {
            SpringApplication.run(DubboComsumerApplication.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    IndexController

    这里依赖注入使用@Reference

    @RestController
    public class IndexController {
        @Reference
        ApiService apiService;
    
        @RequestMapping("/")
        public String index(){
            return apiService.test();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    5.测试

    访问接口localhost:8002
    在这里插入图片描述

  • 相关阅读:
    端口探测技术总结
    修改密码复杂度
    TLS协议深度解析:挖掘现代网络安全防御的底层技术
    Java抽象类和接口
    科技赋能,MTW400A为农村饮水安全打通“最后一公里”
    算法入门 | 分治策略
    责任链模式
    OpenHarmony适配移植:X86、ARM、RISC-V、MIPS、LoongArch芯片架构简析
    C++数据结构题:DS 顺序表--连续操作
    useEffect(fn, []) 不等于 componentDidMount()
  • 原文地址:https://blog.csdn.net/m0_67393295/article/details/126327837