首页 > 代码库 > zookeeper使用

zookeeper使用

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/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>net.datafans</groupId>    <artifactId>exercise.dubbo</artifactId>    <version>1.0.0</version>    <packaging>jar</packaging>    <name>exercise.dubbo</name>    <url>http://maven.apache.org</url>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <spring.version>3.1.1.RELEASE</spring.version>    </properties>    <repositories>        <repository>            <id>com.springsource.repository.bundles.release</id>            <name>EBR Spring Release Repository</name>            <url>http://repository.springsource.com/maven/bundles/release</url>        </repository>        <repository>            <id>com.springsource.repository.bundles.external</id>            <name>EBR External Release Repository</name>            <url>http://repository.springsource.com/maven/bundles/external</url>        </repository>    </repositories>    <dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.7</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>log4j</groupId>            <artifactId>log4j</artifactId>            <version>1.2.17</version>        </dependency>        <!-- dobbo begin -->        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>dubbo</artifactId>            <version>2.5.3</version>            <exclusions>                <exclusion>                    <artifactId>spring</artifactId>                    <groupId>org.springframework</groupId>                </exclusion>            </exclusions>        </dependency>        <dependency>            <groupId>org.apache.zookeeper</groupId>            <artifactId>zookeeper</artifactId>            <version>3.4.5</version>        </dependency>        <dependency>            <groupId>com.github.sgroschupf</groupId>            <artifactId>zkclient</artifactId>            <version>0.1</version>        </dependency>        <!-- dobbo end -->        <!-- spring begin -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-aop</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-asm</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-beans</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-expression</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-jdbc</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-orm</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-tx</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-web</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>net.sourceforge.cglib</groupId>            <artifactId>com.springsource.net.sf.cglib</artifactId>            <version>2.1.3</version>        </dependency>        <!-- spring end -->    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-source-plugin</artifactId>                <executions>                    <execution>                        <id>attach-sources</id>                        <goals>                            <goal>jar</goal>                        </goals>                    </execution>                </executions>            </plugin>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-javadoc-plugin</artifactId>                <executions>                    <execution>                        <id>attach-javadocs</id>                        <goals>                            <goal>jar</goal>                        </goals>                    </execution>                </executions>            </plugin>        </plugins>    </build></project>
View Code

在自己主机上面安装zookeeper,然后就可以写代码了,我们计划是将接口底层跟上层相互分离;

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.xsd        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd        ">     <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->    <dubbo:application name="consumer-of-helloworld-app"  />         <dubbo:registry        address="ip:port"        protocol="zookeeper" />     <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->    <dubbo:reference id="demoService" interface="net.datafans.exercise.dubbo.DemoService" /> </beans>

 

provider.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"    xmlns="http://www.springframework.org/schema/beans"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd        " >    <!-- 提供方应用信息,用于计算依赖关系 -->    <dubbo:application name="zookeeper-registry" />    <!-- 当前应用信息配置 -->    <!-- 暴露服务协议配置 --><!--     <dubbo:registry        address="ip:port"        protocol="zookeeper" /> -->               <dubbo:registry        address="120.132.37.251:2181"        protocol="zookeeper" />     <dubbo:protocol port="9090" />    <!-- 声明需要暴露的服务接口 -->    <!-- 和本地bean一样实现服务 -->    <bean        id="demoService"        class="net.datafans.exercise.dubbo.DemoServiceImpl" />    <dubbo:service        interface="net.datafans.exercise.dubbo.DemoService"        ref="demoService" >    </dubbo:service></beans>

log4j.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd"><log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">    <!-- Appenders -->    <appender name="CONSOLE.ERR" class="org.apache.log4j.ConsoleAppender">        <param name="target" value=http://www.mamicode.com/"System.err" />        <layout class="org.apache.log4j.PatternLayout">            <param name="ConversionPattern"                value="%d  %l %n%p: %m%n" />        </layout>        <filter class="org.apache.log4j.varia.LevelRangeFilter">            <param name="LevelMin" value=http://www.mamicode.com/"warn" />            <param name="LevelMax" value=http://www.mamicode.com/"fatal" />            <param name="AcceptOnMatch" value=http://www.mamicode.com/"false" />        </filter>    </appender>    <appender name="CONSOLE.OUT" class="org.apache.log4j.ConsoleAppender">        <param name="target" value=http://www.mamicode.com/"System.out" />        <layout class="org.apache.log4j.PatternLayout">            <param name="ConversionPattern"                value="%d  %l %n%p: %m%n" />        </layout>        <filter class="org.apache.log4j.varia.LevelRangeFilter">            <param name="LevelMin" value=http://www.mamicode.com/"debug" />            <param name="LevelMax" value=http://www.mamicode.com/"info" />            <param name="AcceptOnMatch" value=http://www.mamicode.com/"false" />        </filter>    </appender>        <logger name="com.errout">        <level value=http://www.mamicode.com/"info" />    </logger>    <!-- Root Logger -->    <root>        <priority value=http://www.mamicode.com/"warn" />        <appender-ref ref="CONSOLE.ERR" />        <appender-ref ref="CONSOLE.OUT" />    </root></log4j:configuration>

DemoService

package net.datafans.exercise.dubbo;public interface DemoService {         String sayHello(String name);}
View Code

DemoServiceImpl

package net.datafans.exercise.dubbo;public class DemoServiceImpl implements DemoService {     public String sayHello(String name) {        System.out.println(name);        return "Hello " + name;    }}
View Code

Provider

package net.datafans.exercise.dubbo;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Provider {     public static void main(String[] args) throws Exception {        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"provider.xml"});        context.start();        System.in.read(); //    } }
View Code

Consumer

package net.datafans.exercise.dubbo;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Consumer {    public static void main(String[] args) throws Exception {        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "consumer.xml" });        context.start();        for (int i = 0; i < 20000; i++) {            long start = System.currentTimeMillis();            DemoService demoService = (DemoService) context.getBean("demoService"); //            System.out.println(demoService.sayHello("world"));            long end = System.currentTimeMillis();            System.out.println(end - start);            System.in.read();        }        // long end = System.currentTimeMillis();        // System.out.println("TPS: " + 20000 / ((end - start) / 1000.0));        // System.out.println(end-start);    }}
View Code

 

zookeeper使用