微服务解决方案2:Dubbo(待续)

第二套微服务架构解决方案 Spring Boot + Dubbo + Zookeeper

Dubbo概念

  • 官网 http://dubbo.apache.org/zh/

  • GitHub:https://github.com/apache/incubator-dubbo

  • 提供了三大核心能力:

    1. 面向接口的远程方法调用
    2. 智能容错和负载均衡
    3. 服务自动注册和发现。(需要配合服务注册中心使用,如Zookeeper,稳定性依赖于服务注册中心)

Dubbo 的组件角色

在这里插入图片描述

  1. 服务容器 Container 负责启动,加载,运行服务提供者。
  2. 服务提供者 Provider 在启动时,向注册中心注册自己提供的服务。
  3. 服务消费者 Consumer 在启动时,向注册中心订阅自己所需的服务。
  4. 注册中心 Registry 返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
  5. 服务消费者 Consumer,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
  6. 服务消费者 Consumer 和提供者 Provider,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心 Monitor

Dubbo Admin 管理控制台

通过Dubbo Admin 管理控制台 可以看有哪些服务,动态配置路由规则,动态配置,服务降级,访问控制,权重调整,负载均衡

  • 源码 https://github.com/apache/dubbo-admin
  • 官网http://dubbo.apache.org/zh/docs/v2.7/admin/ops/introduction/

管理控制台 0.1 版本,结构上采取了前后端分离的方式,前端使用 Vue 和 Vuetify 分别作为 Javascript 框架和UI框架,后端采用 Spring Boot 框架。
既可以按照标准的 Maven 方式进行打包部署,也可以采用前后端分离的部署方式,
功能上目前具备了服务查询,服务治理以及服务测试三部分内容。

Dubbo 应用有两种使用场景 , 其一为 Dubbo 服务提供方 , 另外一个是 Dubbo 服务消费方,当然也允许两者混合


代码实现

官方示例代码

  1. Dubbo 从基本到高级的各种用法,包含多个子项目
    https://github.com/apache/dubbo-samples

  2. Dubbo Spring Boot 模块工程, 包含各种 Maven 模块
    https://github.com/apache/dubbo-spring-boot-project

    1. Dubbo Spring Boot 示例工程
      https://github.com/apache/dubbo-spring-boot-project/tree/master/dubbo-spring-boot-samples

springboot xml配置使用dubbo概述

创建一个 单独的、公共的 文件/项目,用来定义 服务 与 服务中的rpc方法
语言可以用java接口,protobuf等。 这里用Java语言示例

1. 定义 rpc服务,方法

  1. 用Java接口定义rpc服务 :demoService服务 sayHello方法
    //定义rpc服务
    public interface DemoService {
    	//定义rpc方法
        String sayHello(String name);
    }
    

2. 服务提供者项目 Provider

  1. 依赖/持有 rpc服务定义文件
  2. 实现rpc服务
    public class DemoServiceImpl implements DemoService {
        @Override
        public String sayHello(String name) {
            return "hello " + name;
        }
    }
    
  3. 注册服务:编写dubbo的配置文件provider.xml(把demoService.sayHello方法提供出去)
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="......">
        <!-- 服务提供者 应用名称 -->
        <dubbo:application name="hello-world-app"  />
     
        <!-- 服务注册中心地址 -->
        <dubbo:registry address="multicast://224.5.6.7:1234" />
     
        <!-- 用dubbo协议在20880端口暴露服务 -->
        <dubbo:protocol name="dubbo" port="20880" />
      
        <!-- 和本地bean一样实现服务 -->
        <bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl" />
        
        <!-- 声明需要暴露的服务接口 -->
        <dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService" />
    
    </beans>
    
  4. 加载dubbo配置文件
    1. 通过 ClassPathXmlApplicationContext 加载xml来获取上下文Context启动
      public class Application {
          public static void main(String[] args) throws Exception {
          	// 加载dubbo配置文件
              ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-provider.xml");
              context.start();
              System.in.read();// 按任意键退出
          }
      }
      
    2. 通过 @ImportResource({ “classpath:dubbo-provider.xml” }) 加载。
      @SpringBootApplication
      @ImportResource(value = {"classpath:dubbo-provider.xml"})
      public class ProviderApplication {
          public static void main(String[] args) {
              SpringApplication.run(DemoApplication.class, args);
          }
      }
      

3. 服务消费者项目 Consumer

  1. 依赖/持有 rpc服务定义文件,但不要实现接口服务
  2. 引用服务:编写dubbo的配置文件consumer.xml(对要调用的服务进行配置)
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
        xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
     
        <!-- 服务消费者 应用名称,不要与提供方一样 -->
        <dubbo:application name="consumer-of-helloworld-app"  />
     
        <!-- 服务注册中心地址 -->
        <dubbo:registry address="multicast://224.5.6.7:1234" />
     
        <!-- 生成远程服务提供者的demoService服务代理 -->
        <dubbo:reference id="demoService" interface="org.apache.dubbo.demo.DemoService" />
    </beans>
    
  3. 使用服务 proxy:加载dubbo配置文件,使用demoService接口方法
    1. 通过 ClassPathXmlApplicationContext 加载xml来获取上下文Context启动
    	// 加载dubbo配置文件
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml");
        context.start();
        
        // 获取远程服务代理
        DemoService demoService = context.getBean("demoService", DemoService.class);
        // 调用服务提供者的demoService.sayHello方法
        hello = demoService.sayHello("world");
    
    1. 通过 @ImportResource({ “classpath:dubbo-consumer.xml” }) 加载。
    @SpringBootApplication
    @ImportResource(value = {"classpath:dubbo-consumer.xml"})
    public class ConsumerApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    
    	@Resource
        private DemoService demoService;
     
        public String demo() {
            return demoService.sayHello("word");
        }
    

本地测试

1. 使用telnet连接dubbo服务

Dubbo服务端口是定义在dubbo-provider文件中

$ telnet 127.0.0.1 20880
# 回车后键入回车进入dubbo命令模式,如下:
dubbo>

2.1 查看服务列表

dubbo> ls
com.springboot.dubbo.service.DubboTestService1
com.springboot.dubbo.service.DubboTestService2
com.springboot.dubbo.service.DubboTestService3
com.springboot.dubbo.service.DubboTestService4
com.springboot.dubbo.service.DubboTestService5
com.springboot.dubbo.service.DubboTestService6
com.springboot.dubbo.service.DubboTestService7
dubbo>

2.2 查看服务中的接口

# 方法1
dubbo> ls com.springboot.dubbo.service.DubboTestService1
sayHello
sayWord

# 方法2
dubbo> cd com.springboot.dubbo.service.DubboTestService1
dubbo> ls
sayHello
sayWord

# 查看详细
dubbo> ls -l com.springboot.dubbo.service.DubboTestService1

3. invoke调用服务接口

invoke [服务路径].[方法名](参数)

dubbo> invoke sayHello({"name": "cosmoxu"})

dubbo> invoke sayWord(111)

invoke使用dubbo版本需要升级到2.8.4,否则telnet调用会报错

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xyc1211

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值