二、分布式服务框架-Dubbo

 

1、简介:Dubbo是阿里巴巴开源的一个高性能分布式服务框架,使得应用可以通过高性能的RPC实现服务的输出和输入功能,可以和Spring实现无缝集成。

2、原理:在分布式需求下,通常服务提供方被称为:Service Provider,服务需求方被称为:Service Consumer,另外还会使用一个注册中心,官方推荐使用Zookeeper。

Registry:注册中心;

Provider:服务提供方;

Consumer:服务消费方;

Monitor:监控中心

1.服务提供方需要在注册中心注册自己提供的服务;2.服务消费方需要向注册中心订阅自己需要的服务;

3.注册中心会将服务提供方的地址发送给消费方;4.服务消费方获取到地址列表后,基于软负载均衡算法,挑选一台合适的服务提供方并调用服务,如果失败,很快切换到另一台服务提供方;

5.服务提供方和服务消费方会将调用次数、调用时间等信息定时发送给监控中心,由监控中心统计。

这一系列过程就好比我们找房屋中介租房子,首先房东如果有需要出租的房子,则会先到房屋中介登记房屋信息;当有人需要租房子时,会到房屋中介去了解房源信息,并把自己的需求告诉中介,比如房子面积、需要几居室的、月租多少钱等。中介会根据需求查询已登记的房源,并把合适房源告诉给租房者,可能是一套房源,也可能是多套房源,租房者会在其中挑选一套最合适的房源,并和房东面议价格、签订合同等,达成协议则拎包入住,中介会把这个过程记录下来备案。

其中,Service-Provider就好比房东,Service-Consumer就好比租房者,Registry、Monitor就好比中介,在分布式服务中,ZooKeeper扮演的就是中介的角色:

3、安装部署,首先安装ZooKeeper,直译为动物园管理员,是Apache Hadoop下的子项目。

首先安装Zookeeper:

通过WinSCP上传到指定的linux服务器的/usr/local/src目录下,解压安装包:

cd到安装目录:cd /urs/local/src

解压缩:tar -zxvf zookeeper-3.4.6.tar.gz

解压缩会有一个同名文件夹:

cd进入zookeeper-3.4.6文件夹:

 cd zookeerer-3.4.6,找到conf文件夹:

在conf下有三个文件,找到zoo_sample.cfg:

将配置文件复制一份:

cp zoo_sample.cfg zoo.cfg

重新cd 到bin目录下,启动Zookeeper:./zkServer.sh start

显示已启动。

然后配置Dubbo:

新建Maven工程,在工程下新建四个子工程,分别是:

父工程:parent-demo

子工程:shopping-common    通用类,jar包

    shopping-interface    接口类,jar包

    shopping-service-product 商品服务,war包

    shopping-web-console   后台,war包

在parent-demo中设置统一控制版本、统一依赖管理,且不强制依赖,在shopping-common中设置依赖管理。

依赖关系是shopping-service-product、shopping-web-console都依赖shopping-common,shopping-service-product依赖shopping-interface,shopping-web-console依赖shopping-service-product。

接下来就是配置服务提供方和服务消费方了,引入必要的依赖包后,在shopping-interface下新建接口类:DubboTestService.java

在shopping-service-product下新建接口实现类:DubboTestServiceImpl.java

使用@Service注解将id设置为dubboTestService。

在resources下新建spring配置文件和dubbo配置文件。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task" 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/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 
        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://www.springframework.org/schema/task
           http://www.springframework.org/schema/task/spring-task-4.0.xsd
        http://code.alibabatech.com/schema/dubbo        
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <context:component-scan base-package="com.xx" />
    <import resource="config/*.xml"/>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
    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/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 
        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://www.springframework.org/schema/task
           http://www.springframework.org/schema/task/spring-task-4.0.xsd
        http://code.alibabatech.com/schema/dubbo        
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
        
    <!-- 配置dubbo提供方名称 -->
    <dubbo:application name="shopping-service-product" />
    <!-- 配置注册中心地址 -->
    <dubbo:registry address="172.16.30.51:2181" protocol="zookeeper" />
    <!-- 定义dubbo协议端口 -->
    <dubbo:protocol port="20880" name="dubbo" />
    <!-- 提供方暴露实现类 -->
    <dubbo:service interface="com.xx.shopping.service.DubboTestService" ref="dubboTestService" />
</beans>

这里ref就是前面@Service注解里定义的id:dubboTestService,两者必须一样,interface就是接口所在包名。

接着编写服务消费方:shopping-web-console

配置springmvc和Controller,并在WEB-INF下新建一个index.jsp页面:

 

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task" 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/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 
        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://www.springframework.org/schema/task
           http://www.springframework.org/schema/task/spring-task-4.0.xsd
        http://code.alibabatech.com/schema/dubbo        
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <context:component-scan base-package="com.xx"></context:component-scan>
    <mvc:annotation-driven></mvc:annotation-driven>
    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/console/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <!-- <import resource="dubbo-consumer.xml"/>  -->
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task" 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/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 
        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://www.springframework.org/schema/task
           http://www.springframework.org/schema/task/spring-task-4.0.xsd
        http://code.alibabatech.com/schema/dubbo        
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
    <!-- 配置dubbo消费方 -->
    <dubbo:application name="shopping-web-console"/>
    <!-- 配置注册中心地址 -->
    <dubbo:registry address="172.16.30.51:2181" protocol="zookeeper" check="false"/>
    <!-- 消费方调用实现类 -->
    <dubbo:reference interface="com.xx.shopping.service.DubboTestService" id="dubboTestService"/>
    
    
</beans>

 index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    Hello,World!
    <input id="inputOne" name="inputOne" type="text"/> 
</body>
<script>
</script>
</html>

最后测试一下dubbo是否成功,启动zookeeper,将shopping-service-product和shopping-web-console部署到tomcat容器,先启动服务提供方shopping-service-product,再启动服务消费方shopping-web-console,在浏览器输入地址:

http://localhost:8081/shopping/test/index.do

可以看到如下页面:

说明dubbo测试成功!

posted @ 2019-04-09 18:43  ClubHouse  阅读(199)  评论(0编辑  收藏  举报