dubbo 环境搭建

一、Dubbo概要

1、Dubbo介绍
  • Dubbo是由阿里巴巴开放的高性能,基于java的RPC框架,以及SOA服务治理方案。
2、Dubbo架构介绍
  • 架构图

  • 架构详解

    • 节点角色说明

      • Provider: 暴露服务的服务提供方。
      • Consumer: 调用远程服务的服务消费方。
      • Registry: 服务注册与发现的注册中心。
      • Monitor:统计服务的调用次调和调用时间的监控中心。
      • Container: 服务运行容器。
    • 调用关系说明

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

二、快速入门

1、准备条件
JDK:jdk6以上
Maven:版本3或更高版本
2、Maven依赖
  • 可能需要使用最新版本来构建您的dubbo应用程序。

    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.5.5</version>
    </dependency>
    • 3、定义服务提供者的接口
      package com.alibaba.dubbo.demo;
      
      public interface DemoService {
         String sayHello(String name);
      }
    • 4、实现服务提供者
      package com.alibaba.dubbo.demo.provider;
      import com.alibaba.dubbo.demo.DemoService;
      
      public class DemoServiceImpl implements DemoService {
        public String sayHello(String name) {
            return "Hello " + name;
        }
      }
5、配置服务提供
  • dubbo服务提供程序如何配置spring框架
<?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://code.alibabatech.com/schema/dubbo"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
      <dubbo:application name="demo-provider"/>
      <dubbo:registry address="multicast://224.5.6.7:1234"/>
      <dubbo:protocol name="dubbo" port="20880"/>
      <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>
      <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>
  </beans>
6、启动服务提供者
import org.springframework.context.support.ClassPathXmlApplicationContext;

  public class Provider {
      public static void main(String[] args) throws Exception {
          ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                  new String[] {"META-INF/spring/dubbo-demo-provider.xml"});
          context.start();
          System.in.read(); // press any key to exit
      }
  }
7、配置消费者
  • dubbo服务消费者程序如何配置spring框架
<?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://code.alibabatech.com/schema/dubbo"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
      <dubbo:application name="demo-consumer"/>
      <dubbo:registry address="multicast://224.5.6.7:1234"/>
      <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService"/>
  </beans>
8、运行消费者
 import com.alibaba.dubbo.demo.DemoService;
  import org.springframework.context.support.ClassPathXmlApplicationContext;

  public class Consumer {
      public static void main(String[] args) throws Exception {
          ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                  new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
          context.start();
          DemoService demoService = (DemoService) context.getBean("demoService"); // obtain proxy object for remote invocation
          String hello = demoService.sayHello("world"); // execute remote invocation
          System.out.println(hello); // show the result
      }
  } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Dubbo是一个分布式服务框架,可以实现服务的注册与发现、远程通信、负载均衡等功能。为了学习和使用Dubbo,我们可以搭建一个Dubbo demo来进行实践。 首先,我们需要准备好开发环境。我们可以使用Maven构建项目,代码使用Java编写。可以选择任意一个Java开发工具,比如Eclipse或者IntelliJ IDEA。 接下来,我们需要下载Dubbo的安装包。可以从Dubbo的官方网站上进行下载,选择适合自己的版本。然后解压到本地目录。 然后,我们创建一个Maven项目,并在pom.xml文件中添加Dubbo的依赖。可以在Dubbo的安装包中找到pom.xml的路径,将其复制到我们的项目中。 在项目中创建一个服务提供者和一个服务消费者的模块。服务提供者是一个提供服务的服务端,服务消费者是一个调用服务的客户端。 在服务提供者模块中,我们需要定义一个接口,并实现该接口。接口定义了服务的功能,实现类为具体的业务逻辑。 在服务消费者模块中,我们需要配置Dubbo的相关信息,比如注册中心的地址以及要消费的服务提供者的接口。然后我们可以通过调用该接口的方法来使用远程服务。 最后,我们分别启动服务提供者和服务消费者模块。可以通过命令行或者在开发工具中直接运行。 通过以上的步骤,我们就成功搭建了一个Dubbo demo。我们可以在服务提供者和服务消费者之间进行远程调用,并观察调用结果。这个demo可以帮助我们理解和学习Dubbo的基本使用方法和原理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值