Dubbo 基础教程
原文地址:Dubbo 基础教程
博客地址:http://www.extlight.com
一、前言
当服务越来越多时,容量的评估,小服务资源的浪费等问题逐渐显现,此时需要增加一个调度中心基于访问压力实时管理集群容量,提供集群利用率。其中,用于提高机器利用率的资源调度和治理中心是关键。
二、Dubbo 简介
2.1 概念
Dubbo 是阿里巴巴开源项目的一个分布式服务框架。其致力于提供高性能和透明化的 RPC 远程调用方案,以及 SOA 服务治理方案。
2.2 原理
调用关系说明:
1) 服务容器启动、加载和运行服务提供者;
2) 服务提供者在启动时,向注册中心注册自己提供的服务;
3) 服务消费者在启动时,向注册中心订阅自己所需的服务;
4) 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更给消费者;
5) 服务消费者从地址列表中,基于软负载均衡算法选一台服务提供者进行调用,如果调用失败再选另一台;
6) 服务消费者和服务提供者在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
节点角色说明
节点 | 角色说明 |
---|---|
Container | 服务运行容器 |
Provider | 暴露服务的服务提供者 |
Consumer | 调用远程服务的服务消费者 |
Registry | 服务注册与发现的注册中心 |
Monitor | 统计服务的调用此处和调用时间的监控中心 |
三、快速入门
Dubbo 采用全 Spring 配置方式,透明化接入应用,对应用没有任何 API 入侵,只需用 Spring 加载 Dubbo 配置即可。
3.1 安装注册中心
官方推荐使用 Zookeeper 作为注册中心,因此本次测试使用 Zookeeper,将其放置在 ip 为 192.168.2.14 的虚拟机上。
# 解压和转移目录
tar -zxvf zookeeper-3.4.8.tar.gz -C /usr/
cd /usr
mv zookeeper-3.4.8 zookeeper
# 设置配置文件
cd /usr/zookeeper/conf
cp zoo_sample.cfg zoo.cfg
# 启动 zookeeper
/usr/zookeeper/bin/zkServer.sh start
# 查看 zookeeper 运行状态,如果出现 Mode: standalone 说明运行成功
/usr/zookeeper/bin/zkServer.sh status
3.2 服务提供者
创建一个 Maven 项目(名为 dubbo-service 的 web 项目)。
pom.xml 配置:
<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.light</groupId>
<artifactId>dubbo-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.10.RELEASE</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.9</version>
</dependency>
</dependencies>
</project>
web.xml 配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>dubbo-service</display-name>
<!-- spring容器 start -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-dubbo.xml</param-value>
</context-param>
<!-- spring容器 end -->
</web-app>
接口:
public interface HelloService {
String sayHello(String name);
}
实现类:
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "Hello," + name;
}
}
applicationContext-dubbo.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"
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="hello-demo"/>
<dubbo:registry address="zookeeper://192.168.2.14:2181"/>
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:service interface="com.light.dubbo.service.HelloService" ref="helloService"/>
<bean id="helloService" class="com.light.dubbo.service.impl.HelloServiceImpl"/>
</beans>
3.3 服务消费者
创建一个 Maven 项目(名为 dubbo-consumer 的 web 项目)。
pom.xml 配置:
<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.light</groupId>
<artifactId>dubbo-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</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.9</version>
</dependency>
</dependencies>
</project>
web.xml 配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>dubbo-consumer</display-name>
<!-- spring容器 start -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-dubbo.xml</param-value>
</context-param>
<!-- spring容器 end -->
<!-- springmvc容器 start -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- springmvc容器 end -->
</web-app>
将 dubbo-service 项目中的 HelloService 接口复制到该项目(dubbo-consumer)中。
控制层:
@Controller
public class HelloController {
@Autowired
private HelloService helloService;
@RequestMapping("hello")
@ResponseBody
public String hello(String name) {
return this.helloService.sayHello(name);
}
}
applicationContext-dubbo.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"
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="hello-demo"/>
<dubbo:registry address="zookeeper://192.168.2.14:2181"/>
<dubbo:protocol name="dubbo" port="20880"/>
<dubbo:reference interface="com.light.dubbo.service.HelloService"/>
</beans>
springmvc.xml 配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 只扫描含有@Controller注解的类 -->
<context:component-scan base-package="com.light.dubbo.controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 加载解析 @rquestMapping等注解的解析器 -->
<mvc:annotation-driven/>
</beans>
先启动服务提供者的项目(8080),再启动服务消费者的项目(8081)。打开浏览器访问http://localhost:8081/hello?name=jack,结果如下图:
四、监控
4.1 获取源码
git clone --branch dubbo-2.6.0 https://github.com/alibaba/dubbo.git
下载完成后使用 IDE 工具引入其子项目 dubbo-sample\dubbo-monitor-sample 进行编译和打包。打包后会在项目的 target 目录下生成 dubbo-monitor-simple-2.6.0-assembly.tar.gz 压缩文件。
4.2 修改配置
- 解压 dubbo-monitor-simple-2.6.0-assembly.tar.gz 压缩包,修改 dubbo-monitor-simple-2.6.0\conf\dubbo.properties:
dubbo.registry.address=zookeeper://192.168.2.14:2181
- 在服务提供者的配置文件中添加:
<!-- 注册中心自动查找监控服务 -->
<dubbo:monitor protocol="registry"/>
最后启动 dubbo-monitor-simple-2.6.0\bin\start.bat。打开浏览器访问http://localhost:8080/,效果图如下:
五、管理控制台
Dubbo 提供了一套在线管理服务的管理控制台,该管理控制台为阿里巴巴内部裁减版本,开源部分主要包含:路由规则、动态配置、服务降级、访问控制、权重调整和负载均衡。
5.1 获取运行项目
在第四节下载的 duboo 源码中,通过 IDE 工具引入其子项目 dubbo-admin 进行编译和打包。
打包后会在项目的 target 目录下生成 dubbo-admin-2.6.0.war 压缩文件。
5.2 修改配置
将 dubbo-admin-2.6.0.war 里边的文件和文件夹复制粘贴到 tomcat 的 ROOT 目录中并修改 webapps\ROOT\WEB-INF\dubbo.properties 文件内容:
dubbo.registry.address=zookeeper://192.168.2.14:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
其中,配置中设置 2 个用户:root 和 guest。
最后启动 tomcat 容器,打开浏览器访问http://localhost:8080/,页面要求输入账号和密码,登录后效果图如下:
六、参考资料
Dubbo 基础教程的更多相关文章
- Spring Cloud Alibaba基础教程-Nacos(一)
2019快结束,也有很久没写博客了,今天我们来谈谈Nacos,如果对您有帮助,麻烦左上角点个关注 ,谢谢 ! 嘻嘻 今天先写第一篇 文章目录 为什么要使用Nacos Eureka 闭源 Nacos的优 ...
- matlab基础教程——根据Andrew Ng的machine learning整理
matlab基础教程--根据Andrew Ng的machine learning整理 基本运算 算数运算 逻辑运算 格式化输出 小数位全局修改 向量和矩阵运算 矩阵操作 申明一个矩阵或向量 快速建立一 ...
- <<Bootstrap基础教程>> 新书出手,有心栽花花不开,无心插柳柳成荫
并非闲的蛋疼,做技术也经常喜欢蛋疼,纠结于各种技术,各种需求变更,还有一个很苦恼的就是UI总是那么不尽人意.前不久自己开源了自己做了多年的仓储项目(开源地址:https://github.com/he ...
- Memcache教程 Memcache零基础教程
Memcache是什么 Memcache是danga.com的一个项目,来分担数据库的压力. 它可以应对任意多个连接,使用非阻塞的网络IO.由于它的工作机制是在内存中开辟一块空间,然后建立一个Hash ...
- Selenium IDE 基础教程
Selenium IDE 基础教程 1.下载安装 a 在火狐浏览其中搜索附件组件,查找 Selenium IDE b 下载安装,然后重启firefox 2.界面讲解 在菜单- ...
- html快速入门(基础教程+资源推荐)
1.html究竟是什么? 从字面上理解,html是超文本标记语言hyper text mark-up language的首字母缩写,指的是一种通用web页面描述语言,是用来描述我们打开浏览器就能看到的 ...
- 转发-UI基础教程 – 原生App切图的那些事儿
UI基础教程 – 原生App切图的那些事儿 转发:http://www.shejidaren.com/app-ui-cut-and-slice.html 移动APP切图是UI设计必须学会的一项技能,切 ...
- 【Unity3D基础教程】给初学者看的Unity教程(四):通过制作Flappy Bird了解Native 2D中的RigidBody2D和Collider2D
作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 引子 在第一篇文章[Unity3D基础教程] ...
- oracle基础教程(8)oracle修改字符集
oracle基础教程(8)oracle修改字符集 1.用dba连接数据库 -->sqlplus / as sysdba 2.查看字符集 -->SELECT parameter, value ...
随机推荐
- object类之toString方法
object是所有类的基类 如果没有使用extends关键字指明其基类,则默认基类为object类 public class Person{ ........ } 等价于: public class ...
- python2.7无法安装python-ldap、django-auth-ldap
1.安装报错信息: error: Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27 或者fatal ...
- 20145307陈俊达《网络对抗》Exp2 后门原理与实践
20145307陈俊达<网络对抗>Exp2 后门原理与实践 基础问题回答 例举你能想到的一个后门进入到你系统中的可能方式? 非正规网站下载的软件 脚本 或者游戏中附加的第三方插件 例举你知 ...
- 2017-2018-1 信息安全技术 实验二 20155201——Windows口令破解
2017-2018-1 信息安全技术 实验二 20155201--Windows口令破解 一.实验原理 口令破解方法 口令破解主要有两种方法:字典破解和暴力破解. 字典破解是指通过破解者对管理员的了解 ...
- Xshell5 访问虚拟机Ubuntu16.04
1.Ubuntu安装telnet 安装openbsd-inetd sudo apt-get install openbsd-inetd 安装telnetd sudo apt-get install t ...
- luogu P1141 01迷宫
https://www.luogu.org/problem/show?pid=1141 还不太会用 BFS 然后就跟着感觉走了一波 经历了很多错误 刚开始的读入 然后BFS的过程 最后T三个点 看到别 ...
- 10_MySQL DQL_子查询(嵌套的select)
#子查询/* 含义:出现在其他语句中的select语句,称为子查询(内查询) 内部嵌套其他select语句的查询,称为主查询(外查询) 特点: 1.子查询都会放在小括号内 2.单行 ...
- js脚本控制图片水平与垂直居中
使用方法: 1.定义ResizeImg(obj)方法 function ResizeImg(obj) { var boxHeight = $(".box").height(); v ...
- python 堆栈
class Node: #堆栈链结节点的声明 def __init__(self): self.data= #堆栈数据的声明 self.next=None #堆栈中用来指向下一个节点 top=None ...
- JAVA技术分享:消失的线程
很多小伙伴都问过我一个问题,就是任务线程跑着跑着消失了,而且没有任何异常日志.我都是条件反射式的回复,是不是用了线程池的submit提交任务.而且很大几率对方给予肯定答复. 解决方案,很多人都听过不少 ...