基于ZooKeeper的Dubbo简单抽样登记中心
一:设备zookeeper
系统环境
Ubuntu 14.04.2 LTS x64
IP : 192.168.1.102
下载zookeeper-3.4.6.tar.gz到文件夹/opt。拉开拉链
mkdir /opt/zookeeper-3.4.6/data
vim /opt/zookeeper-3.4.6/conf/zoo.cfg
输入例如以下内容
tickTime=2000 dataDir=/opt/zookeeper-3.4.6/data clientPort=2181
然后。启动 sh /opt/zookeeper-3.4.6/bin/zkServer.sh start
这样,一个简单的单机zookeeper就安装好了
二:编写dubbo代码
这里有三个maven项目
dubbo-service 定义了一个接口,供其它两个项目使用
dubbo-provider提供服务
dubbo-consumer消费服务
dubbo-service代码例如以下:
package com.lala.service; import java.util.List; import java.util.Map; public interface UserService { public Map<String, Object> findById(int id); public List<Map<String, Object>> queryList(); }
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>com.lala</groupId> <artifactId>dubbo-service</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>dubbo-service</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.10</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.8</source> <target>1.8</target> <verbose>true</verbose> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.4</version> <executions> <execution> <id>attach-sources</id> <phase>verify</phase> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
完毕之后,运行mvn install 安装到本地仓库
dubbo-provider 代码例如以下:
package com.lala.service; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class UserServiceImpl implements UserService { public Map<String, Object> findById(int id) { return get(id); } public List<Map<String, Object>> queryList() { List<Map<String, Object>> list = new ArrayList<>(); for(int i=1;i<=10;i++) { list.add(get(i)); } return list; } private Map<String, Object> get(int id) { Map<String, Object> res = new HashMap<>(); res.put("id", id); res.put("name", "项羽"); res.put("type", "西楚霸王"); res.put("date", "公元前232年―公元前202年"); return res; } }
package com.lala.dubbo; import java.util.concurrent.TimeUnit; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Hello world! * */ public class StartServer { public static void main( String[] args )throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[]{"application-provider.xml"}); context.start(); TimeUnit.HOURS.sleep(1l); } }
application-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-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="my_provider" /> <!-- 使用multicast广播注冊中心暴露服务地址 --> <dubbo:registry protocol="zookeeper" address="192.168.1.102:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 详细的实现bean --> <bean id="userService" class=" com.lala.service.UserServiceImpl" /> <!-- 声明须要暴露的服务接口 --> <dubbo:service interface=" com.lala.service.UserService" ref="userService" /> </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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lala</groupId> <artifactId>dubbo-Provider</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>dubbo-provider</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>com.lala</groupId> <artifactId>dubbo-service</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.5</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.8</source> <target>1.8</target> <verbose>true</verbose> </configuration> </plugin> </plugins> </build> </project>
然后,运行StartServer.java 启动服务
dubbo-consumer 代码:
application-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-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="my_provider" /> <!-- 使用multicast广播注冊中心暴露服务地址 --> <dubbo:registry address="zookeeper://192.168.1.102:2181" /> <dubbo:reference id="userService" interface="com.lala.service.UserService" /> </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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lala</groupId> <artifactId>dubbo-Consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>dubbo-consumer</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <dependency> <groupId>com.lala</groupId> <artifactId>dubbo-service</artifactId> <version>1.0.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.8</source> <target>1.8</target> <verbose>true</verbose> </configuration> </plugin> </plugins> </build> </project>
package com.lala.client; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lala.service.UserService; public class Client { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[]{"application-consumer.xml"}); UserService us = (UserService)context.getBean("userService"); System.out.println(us.findById(1)); } }
最后。运行Client.java 输出结果为:
{date=公元前232年―公元前202年, name=项羽, id=1, type=虞姬}
版权声明:本文博主原创文章,博客,未经同意不得转载。