ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。
它是一个为分布式应用提供一致性服务的软件
提供的功能包括:配置维护、域名服务、分布式同步、组服务等。
ZooKeeper的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效、功能稳定的系统提供给用户。
ZooKeeper包含一个简单的原语集,提供Java和C的接口。
ZooKeeper代码版本中,提供了分布式独享锁、选举、队列的接口,代码在zookeeper-3.4.3\src\recipes。其中分布锁和队列有Java和C两个版本,选举只有Java版本。
zookeeper的特点:
1、负载均衡:
如果用户访问的是统一服务,则会以负载均衡的方式进行链接
2、心跳检测
Zk会定时进行心跳检测.将死亡的机器自动的移除服务的名单
3、服务发现
当后台服务再次启动时,zk不需要重启.会自动的将新的服务添加到名单中.方便客户端调用
SOA搭建流程图
zookeeper+dubbo
处理逻辑:
1、Dubbo公用api接口
2、dubbo-consumer:调用dubbo-api接口方法
3、dubbo-provider:实现dubbo-api接口,并重写接口方法,完成业务实现!
文件配置
1、dubbo-consumer
applicationContext-consumer.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-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="jt-dubbo-web"/> <dubbo:registry address="zookeeper://192.168.220.133:2181" /> <!-- check=false 表示启动时不检查是否有提供者, 只有调用时才检查 id 应该和提供者给定的接口id一致 interface 表示生产者的路径 --> <dubbo:reference check="false" id="cartService" interface="com.jt.dubbo.cart.DubboCartService" timeout="10000"/> <dubbo:reference check="false" id="orderService" interface="com.jt.dubbo.order.DubboOrderService" timeout="10000"/> </beans>
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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>jt-dubbo-web</artifactId> <packaging>war</packaging> <name>jt-dubbo-web Maven Webapp</name> <url>http://maven.apache.org</url> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8092</port> <path>/</path> </configuration> </plugin> </plugins> </build> <parent> <groupId>com.jt</groupId> <artifactId>jt-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>com.jt</groupId> <artifactId>jt-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.jt</groupId> <artifactId>jt-dubbo</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!--dubbo依赖文件 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.7</version> </dependency> <dependency> <groupId>com.github.abel533</groupId> <artifactId>mapper</artifactId> <version>2.3.2</version> </dependency> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.1.0.Final</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>3.0.7.Final</version> </dependency> </dependencies> </project>
2、dubbo-provider:
applicationContext-provider.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-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="jt-order" /> <!-- 使用multicast广播注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://192.168.220.133:2181"/> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20881" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.jt.dubbo.order.DubboOrderService" ref="orderService" /> <!-- 具体的实现bean --> <bean id="orderService" class="com.jt.dubbo.order.service.OrderServiceImpl" /> </beans>
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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>jt-dubbo-order</artifactId> <packaging>war</packaging> <name>jt-dubbo-order Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>com.jt</groupId> <artifactId>jt-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>com.jt</groupId> <artifactId>jt-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.jt</groupId> <artifactId>jt-dubbo</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!--dubboy提供者依赖文件 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.7</version> </dependency> <dependency> <groupId>com.github.abel533</groupId> <artifactId>mapper</artifactId> <version>2.3.2</version> </dependency> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.1.0.Final</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>3.0.7.Final</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8095</port> <path>/</path> </configuration> </plugin> </plugins> </build> </project>
zookeeper安装
1、安装jdk
解压:tar -xvf zookeeper-3.4.8.tar.gz
2、安装配置zookeeper
- 修改配置文件
a) 复制文件
cp zoo_sample.cfg zoo.cfg
b) 修改文件
vim zoo.cfg
启动zk
sh zkServer.sh start
sh zkServer.sh stop
sh zkServer.sh status
tomcat+zookeeper监控dubbo
说明:修改zk的IP地址.
路径apache-tomcat-7.0.57-dubbo\webapps\ROOT\WEB-INF\dubbo.properties
dubbo.registry.address=zookeeper://192.168.126.145:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
3、
所有评论(0)