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 ...
随机推荐
- Javaworkers团队第五周项目总结
本周项目进展 本周是我们项目开发的最后一周,所以接着上周未解决的问题,我们进行了认真的查阅与测试. 下面是我们本周解决的问题概述. 蛇爬行的速度控制 答:这是上周的遗留问题,经过一周的查阅与学习,我们 ...
- Linux下查找大文件,大目录的方法
查找大文件 //列举出当前目录所有大于800M的文件 find . -type f -size +800M 1 2 第一个方法只用到了一个命令find,它能够帮我们做一些文件查找的操作.它常用的参数有 ...
- 从0开始学习 GITHUB 系列之「GIT 速成」【转】
本文转载自:http://stormzhang.com/github/2016/05/30/learn-github-from-zero3/ 版权声明:本文为 stormzhang 原创文章,可以随意 ...
- [SpringBoot] - 配置文件的多种形式及JSR303数据校验
Springboot配置文件: application.yml application.properties(自带) yml的格式写起来稍微舒服一点 在application.properties ...
- 【转载】make: Nothing to be done for `all'. 解决方法
转自:make: Nothing to be done for `all'.解决方法 make: Nothing to be done for `all' 解决方法 1.这句提示是说明你已经编译好了, ...
- msgsrvmgr.cpp:5:37: fatal error: kdl_conversions/kdl_msg.h: No such file or directory #include <kdl_conversions/kdl_msg.h>
/home/xxx/ros_workspace/src/bp_protocol_bridge/protospot/src/msgsrvmgr.cpp::: fatal error: kdl_conve ...
- Android之获取输入用户名与密码发送短信
打算在过两三天的时间我就要准备出发去浙江了,所以把之前的资料来个总结. 这都是在课堂上做过的作业. 好了,废话少说,直接上代码. 步骤: 1.设置单击事件2.获取输入的QQ号码与密码3.判断输入获取的 ...
- RoR unobtrusive scripting adapter--UJS(一些Javascript的语法糖)
Learn how the new Rails UJS library works and compares with the old version of jquery_ujs that it re ...
- windows下的IO模型之事件选择(WSAEventSelect)模型
异步选择模型类似的是,它也允许应用程序在一个或多个套接字上,接收以事件为基础的网络事件通知.对于异步选择模型采用的网络事件来说,它们均可原封不动地移植到事件选择模型.事件选择模型和异步选择模型最主要的 ...
- 同一sql表,在页面展示多级菜单
2.