服务中间件dubbo的使用
什么是dubbo?dubbo是一个分布式服务框架(RPC:Remote Procedure Call远程过程调用),也就是一个基于soa(面向服务的架构)。简单来说就是一个管理工具,用来管理和调度服务层。它有5个节点:provider:服务提供者(服务层) consummer:调用远程服务的消费者(表现层) ...
什么是dubbo?
dubbo是一个分布式服务框架(RPC:Remote Procedure Call远程过程调用),也就是一个基于soa(面向服务的架构)。简单来说就是一个管理工具,用来管理和调度服务层。
它有5个节点:provider:服务提供者(服务层)
consummer:调用远程服务的消费者(表现层)
registry:服务注册中心与调用中心(一般是zookeeper)
monitor:监控中心。作用:统计服务的调用次数和调用时间(另一个dubbo监控的war工程)
如何使用?
1.在服务器上安装zookeeper(注册中心)
(1)Zookeeper下载:链接:https://pan.baidu.com/s/11DI5levKexA-aOBvmu3efA 密码:i4zq
(2)安装完成,进入主目录--》创建data目录
(3)进入conf目录下,修改zoo_sampl.cfg名-----》zoo.cfg
(4)打开zoo.cfg,修改它内容: dataDir=目录\data (也就是刚才创建的data目录的路径)
(5)bin目录下启动zkServer.cmd / zkServer.sh。(window/linux)linux需要关闭防火墙service iptables stop
2.为service服务层(被调用)、web表现层(调用)添加jar包(两个工程都需要添加)
<!-- dubbo相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
</dependency>
3.在service层的spring-service.xml中添加dubbo约束,并配置dubbo用来发布服务
<?xml version="1.0" encoding="UTF-8"?>
<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:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<context:component-scan base-package="cn.e3mall.service"></context:component-scan>
<!-- 使用dubbo发布服务 -->
<!-- 提供方应用信息,用于计算依赖关系.WIFI情况下ip地址每天都会改变,否则spring启动不了 -->
<!-- 多个ip可用 , 隔开-->
<dubbo:application name="e3-manager" />
<dubbo:registry protocol="zookeeper"
address="本机ip:2181" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="cn.e3mall.service.ItemService" ref="itemServiceImpl" timeout="600000"/>
</beans>
4.web表现层的springmvc.xml添加dubbo约束,并且引用服务(服务在注册中心取)
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:component-scan base-package="cn.e3mall.controller" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 引用dubbo服务 -->
<dubbo:application name="e3-manager-web"/>
<dubbo:registry protocol="zookeeper" address="192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183"/>
<dubbo:reference interface="cn.e3mall.service.ItemService" id="itemService" />
</beans>
5.监控中心Monitor安装
(1)去下载dubbo-admin.war包(需要与jdk版本对应)。或者自己到官网下载导入eclipse打包。这提供jdk1.8版本
链接:https://pan.baidu.com/s/1nGlU1mnhk3BPJq8yHl8NAA 密码:lhvl
(2)把war包复制到tomcat的webapps下。修改war包名字为dubbo-admin.war
注意:dubbo-admin与zookeeper最好放在同一服务器上(这样不用怎么修改配置文件)。如果不在同一服务器,需要修改已经部署成功的dubbo-admin下的 /WEB-INF/dubbo.properties 文件。指定zookeeper服务器的ip,其他不变

(3)启动Tomcat,会自动部署该工程
(4)访问localhost:8080/dubbo-admin (能访问主页则成功)。登录账户:root 密码:root
(5)启动zookeeper,启动自己的service、web工程,访问调用自己的服务,再去查看监控中心,里面会有调用服务情况/时间
更多推荐
所有评论(0)