dubbo+zoopkeeper入门详解

zookeeper安装教程

由于服务器有限,故在一台服务器上安装三个zookeeper.
1.创建三个文件夹
mkdir server1
mkdir server2
mkdir server3
2.分别在三个文件夹下解压zookeeper压缩包
tar -xzvf zookeeper-3.4.9.tar.gz
3.分别在三个文件夹中创建data,dataLog,logs 三个文件夹
mkdir data
mkdir dataLog
mkdir logs
4,切换到zookeeper-3.4.9配置文件默认.cfg结尾的文件 三个文件的clientPort需不一样,避免端口冲突

5.分别在三个server的data文件夹中创建myid文件,类容依次为1,2,3
vi myid

6.启动zoopkeeper
切换到bin目录下
输入命令
./zkServer.sh start 启动
./zkServer.sh status 查看状态
./zkServer.sh stop 停止
7.启动完之后运行如下命令,监听zoopkeeper状态
./zkCli.sh -server 127.0.0.1:2181
注:端口号为配置文件的clientPort

dubbo入门实例

1.创建提供服务的maven项目 dubbo-server
目录结构如下
dubbo+zoopkeeper入门详解_第1张图片

2.pom.xml引入相关jar包

<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.0modelVersion>   <groupId>com.dubbogroupId>   <artifactId>dubbo-serverartifactId>   <version>0.0.1-SNAPSHOTversion>   <packaging>jarpackaging>   <name>dubbo-servername>   <url>http://maven.apache.orgurl>   <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>   properties>
    <dependencies>  
          
        <dependency>  
            <groupId>org.springframeworkgroupId>  
            <artifactId>spring-contextartifactId>  
            <version>4.1.6.RELEASEversion>  
        dependency>  
          
          
        <dependency>  
            <groupId>com.alibabagroupId>  
            <artifactId>dubboartifactId>  
            <version>2.5.3version>  
        dependency>  
          
          
        <dependency>  
            <groupId>org.apache.zookeepergroupId>  
            <artifactId>zookeeperartifactId>  
            <version>3.3.6version>  
        dependency>  
          
          
        <dependency>  
            <groupId>commons-logginggroupId>  
            <artifactId>commons-loggingartifactId>  
            <version>1.1.1version>  
        dependency>  
        <dependency>  
            <groupId>log4jgroupId>  
            <artifactId>log4jartifactId>  
            <version>1.2.15version>  
            <exclusions>  
                <exclusion>  
                    <groupId>com.sun.jdmkgroupId>  
                    <artifactId>jmxtoolsartifactId>  
                exclusion>  
                <exclusion>  
                    <groupId>com.sun.jmxgroupId>  
                    <artifactId>jmxriartifactId>  
                exclusion>  
                <exclusion>  
                    <artifactId>jmsartifactId>  
                    <groupId>javax.jmsgroupId>  
                exclusion>  
                <exclusion>  
                    <artifactId>mailartifactId>  
                    <groupId>javax.mailgroupId>  
                exclusion>  
            exclusions>  
        dependency>  
         
          
        <dependency>  
            <groupId>org.jboss.nettygroupId>  
            <artifactId>nettyartifactId>  
            <version>3.2.0.Finalversion>  
        dependency>  
        <dependency>  
            <groupId>com.101tecgroupId>  
            <artifactId>zkclientartifactId>  
            <version>0.8version>  
        dependency>  
          
    dependencies>  project>

3.配置服务提供的xml文件

<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="dubbo-demo" />  
      
    <dubbo:registry address="zookeeper://192.168.1.15:2181" />  
    <dubbo:protocol name="dubbo" port="9888" />  

       
    <bean id="demoService" class="com.interfacesImpl.DemoServiceImpl" />  
      
    <dubbo:service interface="com.interfaces.DemoService"  
        ref="demoService" executes="10" />  
beans>  

如果xml dubbo异常,按如下步骤处理
1)下载dubbo.xsd到本地
2)添加下载的文件
dubbo+zoopkeeper入门详解_第2张图片

3)修改key,和配置文件保持一致
dubbo+zoopkeeper入门详解_第3张图片

4.添加接口,实现类,以及测试类

public interface DemoService {
    String sayHello(String name);
}
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
 System.out.println("init : " + name);  
     return "hello " + name;  
}
}
public class ServerMain {
 public static void main(String[] args) throws IOException {  
       ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "applicationProvider.xml" });  
       context.start();  

       System.out.println("输入任意按键退出 ~ ");  
       System.in.read();  
       context.close();  
   }  
}

5.创建提供服务的maven项目 dubbo-client

6,引入相关jar包

<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.0modelVersion>
  <groupId>com.dubbogroupId>
  <artifactId>dubbo-clientartifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>jarpackaging>
  <name>dubbo-clientname>
  <url>http://maven.apache.orgurl>
  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
  properties>
 <dependencies>  
          
        <dependency>  
            <groupId>org.springframeworkgroupId>  
            <artifactId>spring-contextartifactId>  
            <version>4.1.6.RELEASEversion>  
        dependency>  
          

          
        <dependency>  
            <groupId>com.alibabagroupId>  
            <artifactId>dubboartifactId>  
            <version>2.5.3version>  
        dependency>  
          

          
        <dependency>  
            <groupId>org.apache.zookeepergroupId>  
            <artifactId>zookeeperartifactId>  
            <version>3.3.6version>  
        dependency>  
          

          
        <dependency>  
            <groupId>log4jgroupId>  
            <artifactId>log4jartifactId>  
            <version>1.2.15version>  
            <exclusions>  
                <exclusion>  
                    <groupId>com.sun.jdmkgroupId>  
                    <artifactId>jmxtoolsartifactId>  
                exclusion>  
                <exclusion>  
                    <groupId>com.sun.jmxgroupId>  
                    <artifactId>jmxriartifactId>  
                exclusion>  
                <exclusion>  
                    <artifactId>jmsartifactId>  
                    <groupId>javax.jmsgroupId>  
                exclusion>  
                <exclusion>  
                    <artifactId>mailartifactId>  
                    <groupId>javax.mailgroupId>  
                exclusion>  
            exclusions>  
        dependency>  
          
          
        <dependency>  
            <groupId>com.101tecgroupId>  
            <artifactId>zkclientartifactId>  
            <version>0.8version>  
        dependency>  
        <dependency>  
            <groupId>com.dubbogroupId>  
            <artifactId>dubbo-serverartifactId>  
            <version>0.0.1-SNAPSHOTversion>  
        dependency> 
          
    dependencies>  
project>

7.配置xml文件

<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="dubbo-demo" />  
      
    <dubbo:registry address="zookeeper://192.168.1.15:2181" />  


    
    <dubbo:reference id="demoService" interface="com.interfaces.DemoService" />  

beans>  

7.创建main方法

public class ClientMain {  
  public static void main(String[] args) {  
      ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
              new String[] { "applicationConsumer.xml" });  
      context.start();  
      DemoService service = (DemoService) context.getBean("demoService");  
      System.out.println(service.sayHello("world"));  
      context.close();  
  }  
} 

8.测试结果
先启动 ServerMain方法 ,待出现如下,运行 ClientMain方法

dubbo+zoopkeeper入门详解_第4张图片

dubbo+zoopkeeper入门详解_第5张图片

dubbo+zoopkeeper入门详解_第6张图片

你可能感兴趣的:(dubbo)