一、服务中间件Dubbo
1、服务中间件,相当于webservice;
2、Dubbo为Java语言开发,只服务于Java项目之间的通信;
3、使用dubbo需在zookeeper开启的状态下,因为需要连接注册中心zookeeper;
4、Javaweb maven项目中使用dubbo只需要在pom.xml引用dubbo和zookeeper的jar包即可使用:
<properties>
<dubbo.version>2.5.6</dubbo.version>
<zookeeper.version>3.4.7</zookeeper.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- dubbo相关 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
5、由spring容器启动
spring.xml中服务提供者的dubbo和zookeeper配置:
<?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">
<!-- 配置包扫描器,扫描所有带@Service注解的类 -->
<context:component-scan base-package="com.taotao.content.service"/>
<!-- 发布dubbo服务 -->
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="taotao-content" />
<!-- 注册中心的地址 -->
<dubbo:registry protocol="zookeeper" address="192.168.244.118:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20881"/>
<!-- 声明需要暴露的服务接口 -->
<!-- 解决degug时,dubbo链接超时的问题 -->
<dubbo:service interface="com.taotao.content.service.ContentCategoryService" ref="contentCategoryServiceImpl" timeout="300000"/>
<dubbo:service interface="com.taotao.content.service.ContentService" ref="contentServiceImpl" timeout="300000"/>
</beans>
spring.xml中消费者的dubbo和zookeeper配置:
<?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">
<!--加载上传文件服务器URL -->
<context:property-placeholder location="classpath:resource/resource.properties"/>
<!-- 配置注解驱动 -->
<mvc:annotation-driven />
<!-- 视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置包扫描器,扫描@Controller注解的类 -->
<context:component-scan base-package="com.taotao.controller"/>
<!-- 静态资源映射 -->
<mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
<mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
<!-- 配置文件上传解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设定默认编码 -->
<property name="defaultEncoding" value="UTF-8"></property>
<!-- 设定文件上传的最大值5MB,5*1024*1024 -->
<property name="maxUploadSize" value="5242880"></property>
</bean>
<!-- 引用dubbo服务 -->
<dubbo:application name="taotao-manager-web"/>
<dubbo:registry protocol="zookeeper" address="192.168.244.118:2181"/>
<dubbo:reference interface="com.taotao.service.ItemService" id="itemService" />
<dubbo:reference interface="com.taotao.service.ItemCatService" id="itemCatService" />
<dubbo:reference interface="com.taotao.content.service.ContentCategoryService" id="contentCategoryService" />
<dubbo:reference interface="com.taotao.content.service.ContentService" id="contentService" />
</beans>
6、Dubbo的架构:
*服务提供者(Provider,Spring容器(Container)来发布):提供服务,并在注册中心注册;服务端一启动,会自动连注册中心,把自己的ip和端口号进行登记;
*注册中心(Registry):即Zookeeper,相当于中介,保存服务提供者的ip和端口号,给消费者提供所需服务的ip和端口号
*消费者(Consumer):要调用哪个服务,就去注册中心查询,从注册中心得到服务提供者的ip和端口号,然后直接和服务提供者建立联系;
*监控中心(Monitor):服务调用和监控中心无关,监控服务的调用信息。如服务被调用次数,和被谁调用等。
二、 注册中心Zookeeper
1、Java开发,所以安装时需要安装jdk;
2、直译为:动物园管理员;
3、是Apache Hadoop的子项目,集群管理工具, 也可以作为注册中心来对服务登记和查询,不转发请求,压力较小;
4、zookeeper安装步骤: