Dubbo的應用

導語:Dubbo是阿里巴巴的一個分佈式服務的開源框架,致力於提供高性能和透明化的RPC遠程服務調用方案,是阿里巴巴SOA服務化治理方案的核心框架,天天爲2,000+個服務提供3,000,000,000+次訪問量支持,並被普遍應用於阿里巴巴集團的各成員站點。html

  參考網站:http://dubbo.io/
1.dubbo-admin管理平臺搭建(對消費者和生產者進行管理) zookeeper---服務註冊中心
部署dubbo-admin:
軟件下載:連接: http://pan.baidu.com/s/1midNOMk 密碼: f2yy
1.安裝jdk
2.安裝tomcat
3.將dubbo-admin工程在tomcat上運行起來(要先運行zookeeper)
下載dubbo-admin包(dubbo-admin-2.5.4-SNAPSHOT.war
找到 tomcat安裝路徑下的 .\webapps\ROOT目錄,而後清空裏面的全部文件
dubbo-admin包(dubbo-admin-2.5.4-SNAPSHOT.war)解壓到此文件中
在解壓的文件中找到\WEB-INF文件夾下的dubbo.properties文件,而後進行配置,默認屬性配置以下:
  1. dubbo.registry.address=zookeeper://127.0.0.1:2181
  2. dubbo.admin.root.password=root
  3. dubbo.admin.guest.password=guest
而後啓動tomcat(zookeeper已啓動,此步tomcat要訪問zookeeper,若是zk沒有啓動,tomcat會一直等待其啓動)。在瀏覽器中輸入localhost:8080,進入監控中心的管理界面(默認管理員帳戶密碼爲:root,root)
linux命令:
#清空ROOT裏面的全部文件
yum install –y unzip
rm –rf /usr/local/tomcat/webapp/ROOT/* unzip dubbo-admin-2.5.4.war –d /usr/local/tomcat/webapp/ROOT
啓動tomcat:./usr/local/tomcat/bin/startup.sh
安裝zookeeper:(注意:zookeeper集羣超過半數以上的機器掛掉,集羣不能夠,如:2n+1臺server,只要有n+1臺就能夠使用。也就是你說的少於一半集羣就無效了)
mkdir /home/lgp :建立包用於存放 zookeeper cp –rf zookeeper-3.4.6.tar.gz /home/lgp cd /home/lgp tar -xzvf zookeeper-3.4.6.tar.gz cd zookeeper-3.4.6/conf/ mv zoo_sample.cfg zoo.cfg vi zoo.cfg :參考詳情
server.X=A:B:C # 其中X是一個數字, 表示這是第幾號server. A是該server所在的IP地址. B配置該server和集羣中的leader交換消息所使用的端口. C配置選舉leader時所使用的端口. 這裏的x是一個數字,與myid文件中的id是一致的。
單機版能夠不用配置server.X=A:B:C
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/home/xudan/zookeeper-3.4.6/data #myid文件存放的路徑
dataLogDir=/home/xudan/zookeeper-3.4.6/logs #日誌存放路徑
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
server.1=Master:2555:3555
server.2=Slave1:2556:3556
server.3=Slave2:2557:3557
 
cd .. mkdir data mkdir logs touch /data/myid echo 1 > /home/lgp/ zookeeper-3.4.6/data/myid //設置myid的值爲1
   vi /etc/hosts //設置host

 
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 root
127.0.0.1 localhost
192.168.1.63 Master
192.168.1.53 Slave1
192.168.1.54 Slave2

 
b、集羣(把單臺zookeeper的配置copy到其餘機器上,改下hosts和對應的myid,根據zoo.cfg的配置)
複製:對應的機器建立 /home/lgp yum -y install openssh-clients 修改對應 myid ,etc/hosts scp /home/lgp/zookeeper-3.4.6 192.168.1.81:/home/lgp //跨機器複製 啓動服務: service iptables stop:分別關閉對應機器的 iptables cd /home/lgp/zookeeper-3.4.6/bin ./bin/zkServer.sh start :開啓zookeeper ./bin/zkServer.sh status :狀態查詢 ./bin/zkServer.sh stop :關閉zookeeper
二、dubbo整合Zookeeper
cd /usr/local/tomcat/webapp/ROOT/WEB-INF vi dubbo.properties

dubbo.registry.address=zookeeper://192.168.1.63:2181?backup=192.168.1.53:2181,192.168.1.54:2181 //backup是配合zookeeper掛掉後的替代
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest

 
最後,先起zookeeper在啓動tomcat
4.登陸dubbo(帳號密碼都是:root)
 
2.zookeeper註冊中心的配置安裝(service在zookeeper上註冊運行)
 
3.dubbo項目整合SpringMVC(maven)
一、下載jar:
maven導入所需jar包:dubbo、zookeeper、zkclient <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> </dependency> <!-- Zookeeper 用於分佈式服務管理 --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.3</version> </dependency> <!-- Zookeeper 用於分佈式服務管理 end -->
二、eclipse配置dubbo.xsd,解決標籤不識別的問題
    百度雲盤:https://pan.baidu.com/s/1nuIKgMd
三、在.properties配置zookeeper的註冊地址
dubbo.registry.address=192.168.1.72:2181\,192.168.1.73:2182\,192.168.1.74:2183
四、service提供服務:spring-dubbo-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.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="guduo-service-user" /> <!-- 使用zookeeper註冊中心暴露服務地址 --> <dubbo:registry protocol="zookeeper" address="${dubbo.registry.address}" />   <!—本機 --> <dubbo:protocol name="dubbo" port="20823" /> <!-- 監控中心配置,protocol="registry",表示從註冊中心發現監控中心地址 --> <dubbo:monitor protocol="registry"/> <!-- 當ProtocolConfig和ServiceConfig某屬性沒有配置時,採用此缺省值 --> <dubbo:provider timeout="30000" threadpool="fixed" threads="100" accepts="1000" /> <!-- 服務接口 --> <dubbo:service retries="0" interface="com.guduo.service.system.UserReportService" ref="userReportService" /> <dubbo:service retries="0" interface="com.guduo.service.system.UserSuggestionService" ref="userSuggestionService" /> </beans>
  spring-context.xml引入spring-dubbo-provider.xml
五、consumer註冊服務:dubbo-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="guduo-web-publish" /> <!-- 使用zookeeper註冊中心暴露服務地址 --> <!-- 註冊中心地址 --> <dubbo:registry protocol="zookeeper" address="${dubbo.registry.address}" /> <!-- 用戶服務接口 --> <dubbo:reference interface="com.guduo.service.user.PmsActionService" id="pmsActionService" check="false"/> <dubbo:reference interface="com.guduo.service.user.PmsMenuService" id="pmsMenuService" check="false"/> </beans>
 
四、開發階段,啓動Dubbo服務
  建議放在"src/test"目錄下面
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * * @描述: 啓動Dubbo服務用的MainClass. * @做者: 劉廣平 * @建立時間: 2016-9-5,下午9:47:55 . * @版本: 1.0 . */ public class DubboProvider { private static final Log log = LogFactory.getLog(DubboProvider.class); public static void main(String[] args) { try { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml"); context.start(); } catch (Exception e) { log.error("== DubboProvider context start error:",e); } synchronized (DubboProvider.class) { while (true) { try { DubboProvider.class.wait(); } catch (InterruptedException e) { log.error("== synchronized error:",e); } } } } }
相關文章
相關標籤/搜索