基于ZooKeeper的Dubbo简单抽样登记中心
生活随笔
收集整理的這篇文章主要介紹了
基于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代碼例如以下:
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.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 內容
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=虞姬}
版權聲明:本文博主原創文章,博客,未經同意不得轉載。
總結
以上是生活随笔為你收集整理的基于ZooKeeper的Dubbo简单抽样登记中心的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 09.15,一维数组,冒泡排序
- 下一篇: wampserver环境下,apache