DUBBO--基础篇(一)--简介(示意Demo)

本文涉及的产品
云原生网关 MSE Higress,422元/月
注册配置 MSE Nacos/ZooKeeper,118元/月
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: DUBBO--基础篇(一)--简介(示意Demo)

DUBBO--基础篇(一)--简介(示意Demo)

一、Dubbo由来的渐进史:

(1)ORM史:开发过程中,占用的资源越来越多,而且随着流量的增加越来越难以维护。

(2)MVC史:垂直应用架构解决了单一应用架构所面临的扩容问题,流量能够分散到各个子系统当中,且系统的体积可控,一定程度上降低了开发人员之间协同以及维护的成本,提升了开发效率。

缺点:但是在垂直架构中相同逻辑代码需要不断的复制,不能复用。 也只能在一台服务器中,所以无法抵挡高并发。

(3)RPC(分布式应用架构)史:当垂直应用越来越多,应用之间交互不可避免,将核心业务抽取出来,作为独立的服务,逐渐形成稳定的服务中心 。但是RPC容易出现的问题就是系统之间(IP+PORT)相互调用,错综复杂。不好维护。

(4)SOA(流动计算架构)史:面向服务的架构体系(SOA),也因此衍生出了一系列相应的技术,如对服务提供、服务调用、连接处理、通信协议、序列化方式、服务发现、服务路由、日志输出等行为进行封装的服务框架。

二、Dubbo定义

Dubbo是 阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 Spring框架无缝集成。

Dubbo提供了三大核心功能:(1)面向接口的远程方法调用。(2)智能容错和负载均衡。(3)服务自动注册和发现

三、工作原理图

Provider: 暴露服务方称之为“服务提供者”。

Consume: 可以调用远程服务方 称之为“服务消费者”。

Registry: 服务注册于发现的中心目录称之为 “服务注册中心”。

Monitor: 统计服务的调用次数和调用时间的日志服务称之为“服务监控中心”。

调用流程:

(1)服务容器负责启动,加载,运行服务提供者。

(2).服务提供者在启动时,向注册中心注册自己提供的服务。

(3).服务消费者在启动时,向注册中心订阅自己所需的服务。。

(4)注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。

(5)服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

(6)服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

通过将服务统一管理起来,可以有效地优化内部应用对服务发布/使用的流程和管理。服务注册中心可以通过特定协议来完成服务对外的统一。

Dubbo提供的注册中心有如下几种类型可供选择

  • Multicast注册中心
  • Zookeeper注册中心
  • Redis注册中心
  • Simple注册中心

在这里我们使用的服务注册中心为zookeeper

四、Dubbo准备工作

(1)Linux下 安装JDK。

网上一箩筐...自行百度

(2)Linux下安装zookeeper(在这儿启动一台就行...)

分布式系统详解--框架(Zookeeper-简介和集群搭建)

即:开启注册中心

五、建立项目--服务方(student-server)

5.1 新建maven项目(simple就可以)--war工程

5.2 pox文件添加相应的Jar包。

<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.0</modelVersion>
  <groupId>org.dubbo</groupId>
  <artifactId>students-server</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <!-- 统一版本号 -->
  <properties>
    <spring.version>4.3.17.RELEASE</spring.version>
  </properties>
  
  
  <dependencies>
  <!-- spring的各种依赖 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>
    
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>${spring.version}</version>
    </dependency>
    
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jms</artifactId>
      <version>${spring.version}</version>
    </dependency>
    
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>
    
    
    <dependency>
      <groupId>commons-dbcp</groupId>
      <artifactId>commons-dbcp</artifactId>
      <version>1.4</version>
    </dependency>
    
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1.1</version>
    </dependency>
    
    <!-- dubbo 组件 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.5.10</version>
    </dependency>
    
    <!-- zookeeper组件 -->
    <dependency>
      <groupId>org.apache.zookeeper</groupId>
      <artifactId>zookeeper</artifactId>
      <version>3.4.12</version>
    </dependency>
    
    <!-- zookeeper客户端 -->
    <dependency>
      <groupId>com.github.sgroschupf</groupId>
      <artifactId>zkclient</artifactId>
      <version>0.1</version>
    </dependency>
    
    <!-- java 帮助类 -->
    <dependency>
      <groupId>org.javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.21.0-GA</version>
    </dependency>
    
    
     <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.6</version>
    </dependency> 
  </dependencies>
  
  <build>
    <plugins>
    <!-- tomcat的组件可以帮助我们内置一个tomcat,可以直接运行。 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <encoding>UTF8</encoding>
        </configuration>
      </plugin>
      <plugin>
      <groupId>org.apache.tomcat.maven</groupId>  
      <artifactId>tomcat7-maven-plugin</artifactId> 
      <configuration>
        <port>8881</port>
        <path>/</path>
      </configuration>  
      </plugin>
        </plugins>
  </build>
</project>

5.3 写一个简单借接口和实现类

package org.students.server;
 
public interface StudentServer {
  
  public String server(String name);
 
}
package org.students.server.impl;
 
import org.students.server.StudentServer;
 
import com.alibaba.dubbo.config.annotation.Service;
 
@Service  //该@Service为alibba 提供的注解(需要放在dubbo中被扫描,也需要放在spring中被扫描)
public class StudentServerImpl implements StudentServer{
 
  public String server(String name) {
    // TODO Auto-generated method stub
    return "server: "+name;
  }
 
}

5.4 在webaspp下建立WEB-INF文件夹,新家web.xml。

5.5 编辑web.xml。(web.xml和application头文件需要根据自己的情况而选择相应的配置)

将applicationContext.xml放置web.xml中进行管理加载。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 
<!-- 集成spring 标志一下spring文件位置 -->
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
</context-param>
 
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
 
</web-app>

5.6 创建applicaltionContext.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://code.alibabatech.com/schema/dubbo"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 
           <!-- dubbo相关 -->
 
<!-- 配置dubbo的应用名称 -->
<dubbo:application name = "students-service" />
<!-- 配置注册中心地址(尚未配置) -->
<dubbo:registry protocol = "zookeeper" address = "zookeeper://192.168.1.110:2181" />
<!-- 配置dubbo的扫描包  将service放置在其中,为了dubbo在之后的RPC(远程调用时使用)              -->
<dubbo:annotation package = "org.students.server.impl.StudentServerImpl" />
 
<context:component-scan base-package = "org.students.server.impl.StudentServerImpl">
</context:component-scan>
 
</beans>

在这里值得注意的是,在service的实现类上加上@service注解的时候,在applicationContext中进行配置的时候,需要在Dubbo中进行配置,在spring中也需要配置。

六、建立项目--API

6.1 项目搭建(普通Maven项目,simple)

6.2 建立接口(在com.dubbo包中建立DemoService类)

package com.dubbo;
 
public interface DemoService {
  
  public String sayHello(String name);
  
}

七、项目搭建--服务端(server)

7.1 建立工程(普通Maven项目)

7.2 pom文件

在pom文件中,需要将API的pom的各种版本信息,作为一个dependency放在server中,这样就可以调用其API。

<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.0</modelVersion>
  <groupId>com.dubbo</groupId>
  <artifactId>dubbo-server</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
<dependencies>
  <dependency>
    <groupId>com.dubbo</groupId>
      <artifactId>dubbo-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </dependency>
  <dependency>
      <groupId>com.101tec</groupId>
      <artifactId>zkclient</artifactId>
      <version>0.10</version>
  </dependency>
 
     <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.6.0</version>
    </dependency>
</dependencies>
 
</project>

7.3 写一下DemoService实现类

package com.dubbo;
 
public class DemoServerImpl implements DemoService{
 
  public String sayHello(String name) {
    // TODO Auto-generated method stub
    return "Hello--"+name;
  }
}

7.3 server.xml

配置文件中的dubbo做了一件事,就是将DemoService借口以及其实现类放在了dubbo的Service中,在zookeeper中的服务中心进行了注册。

在server.xml中需要配置好关于dubbo的记个点:

(1)供应方的名称,即dubbo:application 的name属性。

(2)供应方将信息注册到zookeeper中是的地址。即dubbo:registery 中的address 属性。

(3)dubbo协议的暴露端口号(默认为20880--不配置也可以)

(4)将API中的借口DemoService实现类写好后,配置在spring中。值得注意的是,需要将<bean>需要放入的地方是<dubbo:service>中。

<?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"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  <!--定义了提供方应用信息,用于计算依赖关系;在 dubbo-admin 或 dubbo-monitor 会显示这个名字,方便辨识 -->
  <dubbo:application name="demotest-server" />
  <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper -->
  <dubbo:registry address="zookeeper://192.168.0.222:2181" />
  <!-- 用dubbo协议在20880端口暴露服务 -->
   <dubbo:protocol name="dubbo" port="20880" />
  <dubbo:service interface="com.dubbo.DemoService" ref="demoServer" protocol="dubbo" />
  <bean id="demoServer" class="com.dubbo.DemoServerImpl"> </bean>
</beans>

7.4 写一个启动类。Server.

ClassPathXmlApplicationCpntext进行读取配置文件,并进行启动。

System.in.read()。只要不在控制台上输入。该服务将一直处于启动状态。

package com.dubbo;
 
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Server {
  public static void main(String[] args) throws Exception{
    ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("server.xml");
    app.start();
    System.out.println("----server start-----");
    System.in.read();
  }
}

八. 项目搭建(客户端)Client

8.1 项目搭建(简单Maven项目)

8.2 pom文件

将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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.dubbo</groupId>
  <artifactId>dubbo-client</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
    <dependency>
      <groupId>com.dubbo</groupId>
      <artifactId>dubbo-api</artifactId>
     <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.6.0</version>
  </dependency>
 
  <dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.10</version>
  </dependency>
 
  </dependencies>
  
</project>

8.3 client.xml

需要配置的跟上面不尽相同。

除了应用名称和注册地址一样之外。dubbo:reference 如同 Spring中的 @Autowired注解。将DemoService依赖注入进来。而不同的是,reference是可以进行不同项目之间的调用,他调用自己的刚才注册到Dubbo中的Service类。

<?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-admin 或 dubbo-monitor 会显示这个名字,方便辨识 -->
    <dubbo:application name="demotest-client" />
    <!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper -->
    <dubbo:registry address="zookeeper://192.168.0.222:2181" />
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />  
    <dubbo:reference id="demoServe" interface="com.dubbo.DemoService"></dubbo:reference>  
</beans>

8.4 Client 类

如同上面一样,可以读取xml文件之后进行开启。

因为在xml文件中我们进行了dubbo:reference配置。所以我们可以通过getBean的方式获取到DemoService接口。

package com.dubbo;
 
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Client {
  
  public static void main(String[] args) throws InterruptedException {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Client.xml");
    context.start();
    while(true) {
      Thread.sleep(1000);
      DemoService demoService = (DemoService)context.getBean(DemoService.class);
      String sayHello = demoService.sayHello("zxxx");
      System.out.println(sayHello);
    }
  }
}

如此我们就算配置完成。

先启动server,后启动Client。

九。效果

9.1 启动server

(1)server--控制台

(2)zookeeper中

9.2 启动Client

(1)控制台-1秒钟打印一个字符串

(2)在server的控制台中随机打印

双方服务停止。

完美~~

相关实践学习
基于MSE实现微服务的全链路灰度
通过本场景的实验操作,您将了解并实现在线业务的微服务全链路灰度能力。
目录
相关文章
|
4月前
|
监控 Dubbo 前端开发
快速入门分布式系统与Dubbo+zookeeper Demo
快速入门分布式系统与Dubbo+zookeeper Demo
469 0
|
2月前
|
监控 Dubbo 网络协议
Dubbo背景和简介
Dubbo背景和简介
35 10
|
开发框架 负载均衡 Dubbo
带你读《Apache Dubbo微服务开发从入门到精通》—— 一、 Dubbo简介
带你读《Apache Dubbo微服务开发从入门到精通》—— 一、 Dubbo简介
879 3
带你读《Apache Dubbo微服务开发从入门到精通》—— 一、 Dubbo简介
|
开发框架 Dubbo Java
带你读《Apache Dubbo微服务开发从入门到精通》——二、 核心架构(2)
带你读《Apache Dubbo微服务开发从入门到精通》——二、 核心架构(2)
426 3
|
XML 缓存 负载均衡
dubbo详解及demo实例
dubbo详解及demo实例
301 0
|
Kubernetes Dubbo 数据可视化
带你读《Apache Dubbo微服务开发从入门到精通》——二、 核心架构(3)
带你读《Apache Dubbo微服务开发从入门到精通》——二、 核心架构(3)
298 3
|
存储 Dubbo 网络协议
带你读《Apache Dubbo微服务开发从入门到精通》——三、 Dubbo核心特点(2)
带你读《Apache Dubbo微服务开发从入门到精通》——三、 Dubbo核心特点(2)
203 1
|
监控 Dubbo Cloud Native
带你读《Apache Dubbo微服务开发从入门到精通》——三、 Dubbo核心特点(4)
带你读《Apache Dubbo微服务开发从入门到精通》——三、 Dubbo核心特点(4)
211 2
|
编解码 开发框架 Dubbo
带你读《Apache Dubbo微服务开发从入门到精通》——二、 核心架构(1)
带你读《Apache Dubbo微服务开发从入门到精通》——二、 核心架构(1)
502 3
|
运维 Dubbo 数据可视化
带你读《Apache Dubbo微服务开发从入门到精通》——三、 Dubbo核心特点(1)
带你读《Apache Dubbo微服务开发从入门到精通》——三、 Dubbo核心特点(1)
257 1