最近刚学习dubbo,总结一下入门知识点:
一、系统之间的调用方式:
1、前台直接访问:使用ajax或jsonp(访问对外网公开的服务,只对内网公开则不行)。
2、HTTPCLIENT访问:基于http协议的短连接,每次访问时建立连接,完成后关闭。对于高并发、访问量大的情况则性能很低。
3、RPC访问:基于RPC协议的远程调用,是一种长连接,访问完成后连接不会关闭,性能高。
二、dubbo:
1、什么是dubbo?
dubbo是一种分布式的服务框架,提供高性能和透明化的RPC远程服务调用方案。
2、dubbo能做什么?
dubbo最大的特点是按照分层的方式来架构,使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合)。比如有个查询功能访问量很大,而且和其他功能耦合度较高,这个时候我们想单独对该功能做优化或扩展就不太容易,这个时候我们使用dubbo把可以把该功能独立成一个单独的系统应用。
3、什么时候用dubbo?
业务量大,系统间应用复杂,对性能要求高的时候可以使用dubbo,简单的系统间调用其实可以用httpclient(个人理解,如有不对请指正)。
三:dubbo架构:
dubbo架构分以上四个模块,访问步骤如下
1. 服务容器Container负责启动,加载,运行服务提供者Provider。
2. Provider在启动时,向注册中心Registry注册自己提供的服务。
3. 服务消费者Consunmer在启动时,向注册中心订阅自己所需的服务。
4. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
5. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
6. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心Monitor。
四:dubbo下载:
dubbo已经没有更新了,因为已经完全够用了,建议下载2.5.3版本。官网没有提供下载,而是将源码依托于github,核心包依托于maven中央仓库。
github地址:https://github.com/alibaba/dubbo
maven仓库地址:http://repo1.maven.org/maven2/com/alibaba/dubbo/
五:dubbo 的构建:
常规使用dubbo需要四个模块:核心框架、管理控制台、简易监控中心,简易注册中心。其中只有核心模块能在maven仓库中下载到,所以我们需要下载源码自己构建。
1、github中下载源码。
2、下载编译依赖hessian-lite.zip和opensesame.zip。
3、解压
4、导入eclipse
先导入依赖,再导入dubbo源码。
5、将两个依赖install到本地仓库。
6、选中dubbo源码中的dubbo-dubbo-2.5.3,右键maven build构建
7、构建完成:
可以在dubbo源码里不同项目的target中找到以下模块:
核心框架——dubbo项目中(当然这个可以从maven仓库直接下载)
管理控制台——dubbo-admin项目中
简易监控中心——dubbo-monitor-simple项目中
简易注册中心——dubbo-registry-simple项目中(我们下面的实例用的注册中心是zookeeper)
六:入门实例:
1、实现功能:存在2个系统,A系统和B系统,A系统调用B系统的接口获取数据,用于查询用户列表。
2、注册中心选用zookeeper。(zookeeper一般以奇数个集群出现,这里演示用1个),安装zookeeper:
下载后直接解压,然后创建一个目录zooData存放数据
将zookeeper配置文件zoo_sample.cfg改名为:zoo.cfg
修改配置文件中dataDir的路径为zooData的路径。
进入bin目录中启动zkServer.cmd。(zookeeper启动后,相当于dubbo的注册中心已启动)
3、搭建B系统:dubbo-b(maven项目),打包方式为war
导入依赖:
<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>cn.itcast.dubbo</groupId>
<artifactId>dubbo-b</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- dubbo采用spring配置方式,所以需要导入spring容器依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<!-- 排除传递spring依赖 -->
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8081</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
注意上面的排除依赖传递:因为dubbo已经没有更新,所以我们要排除dubbo自己依赖的低版本的spring,选用我们自己的。
创建log4j.xml
log4j.rootLogger=DEBUG,A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n
创建User对象
package cn.itcast.dubbo.pojo;
// 使用dubbo要求传输的对象必须实现序列化接口
public class User implements java.io.Serializable {
private static final long serialVersionUID = -2668999087589887337L;
private Long id;
private String username;
private String password;
private Integer age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
创建UserService接口(provider用来暴露到注册中心的接口)
package cn.itcast.dubbo.service;
import java.util.List;
import cn.itcast.dubbo.pojo.User;
public interface UserService {
/**
* 查询所有的用户数据
*
* @return
*/
public List<User> queryAll();
}
创建UserServiceImpl实现类
package cn.itcast.dubbo.service.impl;
import java.util.ArrayList;
import java.util.List;
import cn.itcast.dubbo.pojo.User;
import cn.itcast.dubbo.service.UserService;
public class UserServiceImpl implements UserService {
/**
* 实现查询,这里做模拟实现,不做具体的数据库查询
*/
public List<User> queryAll() {
List<User> list = new ArrayList<User>();
for (int i = 0; i < 10; i++) {
User user = new User();
user.setAge(10 + i);
user.setId(Long.valueOf(i + 1));
user.setPassword("123456");
user.setUsername("username_" + i);
list.add(user);
}
return list;
}
}
编写dubbo配置文件dubbo-server.xml:
具体配置:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dubbo-b-server" />
<!-- 这里使用的注册中心是zookeeper -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"
client="zkclient" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 将该接口暴露到dubbo中 -->
<dubbo:service interface="cn.itcast.dubbo.service.UserService"
ref="userServiceImpl" />
<!-- 将具体的实现类加入到Spring容器中 -->
<bean id="userServiceImpl" class="cn.itcast.dubbo.service.impl.UserServiceImpl" />
</beans>
注意:如果配置时没有dubbo标签的提示,需要在eclipse中导入dubbo.xsd文件
pom导入zookeeper依赖
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>dubbo-b</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dubbo/dubbo-*.xml</param-value>
</context-param>
<!--Spring的ApplicationContext 载入 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
到这里,配置完成,我们可以启动tomcat了,查看tomcat启动日志:
2016-06-03 09:38:35,307 [localhost-startStop-1] [com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry]-[INFO] [DUBBO] Register: dubbo://192.168.56.1:20880/cn.itcast.dubbo.service.UserService?anyhost=true&application=dubbo-b-server&dubbo=2.5.3&
interface=cn.itcast.dubbo.service.UserService&methods=queryAll&pid=5436&side=provider×tamp=1464917914608, dubbo version: 2.5.3, current host: 127.0.0.1
可以看到,已经将UserService服务注册到zookeeper注册中心,协议采用的是dubbo。
4、搭建A系统——打包方式用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>cn.itcast.dubbo</groupId>
<artifactId>dubbo-a</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- dubbo采用spring配置方式,所以需要导入spring容器依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<!-- 排除传递spring依赖 -->
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
</dependencies>
</project>
log4j.xml(同上)
从b系统中拷贝User对象、UserService接口到a系统
编写dubbo配置文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="dubbo-a-consumer" />
<!-- 这里使用的注册中心是zookeeper -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient"/>
<!-- 从注册中心中查找的服务是UserService -->
<dubbo:reference id="userService" interface="cn.itcast.dubbo.service.UserService"/>
</beans>
在test目录下编写测试类调用该服务:
package cn.itcast.dubbo.service;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.dubbo.pojo.User;
public class UserServiceTest {
private UserService userService;
@Before
public void setUp() throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"classpath:dubbo/*.xml");
this.userService = applicationContext.getBean(UserService.class);
}
@Test
public void testQueryAll() {
List<User> users = this.userService.queryAll();
for (User user : users) {
System.out.println(user);
}
}
}
查看控制台结果:
到这里,我们可以看到,A、B两个独立的系统,B提供服务并注册到注册中心,
A通过注册中心订阅后访问到B中的服务,并没有直接去访问B系统。
现在,我们已经完成了提供者和消费者的部分(下图红圈中的部分),还剩下监控中心monitor,如下:
当然,上面的实例中,A和B中的User,UserService都是一样的,我们可以单独建立一个系统dubbo-api,
然后抽取到dubbo-api中,最后在A和B中分别依赖api系统,就可以共用User和UserService
七:监控:
原理:服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
监控中心有了这些数据后,就可以统计出并发量,访问量,是否该增加服务器等。
1、搭建监控服务
上面我们已经构建了dubbo,并且细说了每个模块的路径,找到简易监控中心,解压,
然后修改conf下的dubbo.properties文件:
使用zookeeper那一行的配置,注意加上参数?client=zkclient
dubbo.jetty.port是修改端口的
经过这一步,就把监控注册到注册中心去了。
监控启用后,在dubbo-b中配置监控:
bin下启动监控start.bat
访问http://127.0.0.1:8088/,就可以看到监控的信息了。
八:管理
现在就剩下管理模块了,其实管理模块,就是dubbo提供的一套在线管理整个服务的功能,找到我们构建后的管理模块,它是一个war包,我们现在一个单独的tomcat,将解压包放到到tomcat的webapps下的ROOT中(先把ROOT清空),
修改WEN-INF下的dubbo.properties文件:
启动tomcat,然后就可以访问了。可以使用root或是guest用户登录
管理界面如下:里面就可以看到整套系统服务的情况
以上的实例、图片等是我学习相关资料时截取的,加上自己的总结,本来想看看dubbo的书籍,发现dubbo没有出书。所以要深入的话,还得多找找其他资料。
本文介绍Dubbo微服务框架的基本概念、应用场景及搭建步骤。通过A、B系统间的调用示例,展示如何利用Zookeeper作为注册中心实现服务提供与消费。
270

被折叠的 条评论
为什么被折叠?



