Dubbo + Zookeeper 简单配置

Dubbo + Zookeeper

Zookeeper 下载及配置

  1. 下载到本机/usr/local目录
wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz
  1. 解压文件 tar zxvf zookeeper-3.4.6.tar.gz,进入conf目录,复制zoo_sample.cfg为zoo.cfg
  2. 进入 bin目录运行zkServer.sh start 起动zookeeper
  3. 运行zkCli.sh --server 217.0.0.1:2181
zkCli.sh --server 127.0.0.1:2181
// 显示如下
[zk: localhost:2181(CONNECTED) 0] // 运行 ls / 显示两个节点
[dubbo, zookeeper]

Dubbo

Java接口


// 服务接口
package cn.byref.dubbo.service; public interface SayBaba {
String jaoBaba();
} // 服务实现
public class SayBabaImpl implements SayBaba {
@Override
public String jaoBaba() {
System.out.println("ServerSide: 爸爸!");
return "爸爸!";
}
}

Spring配置

  1. provider
<?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:context="http://www.springframework.org/schema/context"
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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <bean id="sayBabaService" class="cn.byref.dubbo.service.impl.SayBabaImpl"></bean> <!--10.66.100.26-->
<dubbo:registry protocol="zookeeper" address="localhost:2181"/> <dubbo:protocol accesslog="true" name="dubbo" port="20880"/> <dubbo:application name="son"/> <dubbo:service interface="cn.byref.dubbo.service.SayBaba" ref="sayBabaService"/> </beans>
  1. consumer
<?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:context="http://www.springframework.org/schema/context"
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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:registry protocol="zookeeper" address="localhost:2181" /> <dubbo:protocol accesslog="true" name="dubbo" port="20880" /> <dubbo:application name="son"/> <dubbo:reference id="sayBabaService" interface="cn.byref.dubbo.service.SayBaba"/> </beans>

Provider 与 Consumer 类

// Provider
package cn.byref.dubbo; import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by GJ on 2017/7/31.
*/
public class Provider {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"classpath*:spring/provider/applicationContext.xml");
context.start();
while(true);
}
} // Consumer
package cn.byref.dubbo; import cn.byref.dubbo.service.SayBaba;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by GJ on 2017/7/31.
*/
public class Consumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"classpath*:spring/consumer/applicationContext.xml");
SayBaba sayBabaService = (SayBaba) context.getBean("sayBabaService");
System.out.println("getMessage: " + sayBabaService.jaoBaba());
}
}

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>cn.byref.dubbo</groupId>
<artifactId>dubbodemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <name>dubbodemo</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency> <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>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
</dependencies>
</project>

运行java代码

  1. 运行Provider的main方法,会在zookeeper里创建dubbo节点
  2. 运行Consumer的main方法,调用接口
上一篇:STL"源码"剖析


下一篇:通读《STL源码剖析》之后的一点读书笔记