講解一下當(dāng)前流行的分布式開(kāi)發(fā)所用到分布式服務(wù)框架dubbo和zookeeper掷伙。
至于這兩個(gè)框架到底是做什么的检柬。自行百度吧婿斥,懶得寫(xiě)了
先來(lái)一張,企業(yè)級(jí)大型網(wǎng)站的架構(gòu)圖
其中紅色部分就是我們這次要講解的巴粪,隨著網(wǎng)站的規(guī)模不斷擴(kuò)大通今,業(yè)務(wù)邏輯不斷復(fù)雜,負(fù)載逐漸增加验毡,企業(yè)級(jí)的架構(gòu)將各個(gè)業(yè)務(wù)模塊拆分成多個(gè)應(yīng)用衡创,部署到不同的服務(wù)器,然后使用RPC或者其他的遠(yuǎn)程調(diào)用方式實(shí)現(xiàn)各個(gè)服務(wù)器的遠(yuǎn)程調(diào)用晶通,以及用dubbo或者nigx實(shí)現(xiàn)負(fù)載均衡璃氢。
下面我們用dubbo+zookeeper實(shí)現(xiàn),一個(gè)簡(jiǎn)單地分布式的java應(yīng)用集群狮辽,在集群間實(shí)現(xiàn)遠(yuǎn)程調(diào)用一也。
看圖
這次一共有三個(gè)項(xiàng)目
DubboPro:生產(chǎn)者(發(fā)布遠(yuǎn)程服務(wù),供別人調(diào)用)
DubboCus:消費(fèi)者(就是調(diào)用別人發(fā)布的遠(yuǎn)程服務(wù))
DubboInterface:接口項(xiàng)目
#######PS:生產(chǎn)者和消費(fèi)正可以同時(shí)存在于一個(gè)java應(yīng)用喉脖,這里便于學(xué)習(xí)椰苟,我們將他們分開(kāi)
DubboInterface 接口項(xiàng)目
里面定義的是 我們要提供的服務(wù) 所依賴(lài)的接口,因?yàn)閐ubbo所提供的遠(yuǎn)程調(diào)用時(shí)基于代理實(shí)現(xiàn)的树叽,所以提供服務(wù)的應(yīng)用必須要繼承接口才能實(shí)現(xiàn)舆蝴。還有一個(gè)是Person實(shí)體類(lèi),這是我們一會(huì)要用到的,這里要注意洁仗,遠(yuǎn)程調(diào)用要傳輸java的對(duì)象(實(shí)體)层皱,必須是可序列換的。下面赠潦,請(qǐng)看代碼叫胖!
package com.zhongcheng.dubbo.interf;
import com.zhongcheng.dubbo.model.Person;
/**
* Hello world!
*
*/
public interface GetPerson {
String speak(Person person);
}
package com.zhongcheng.dubbo.interf;
import com.zhongcheng.dubbo.model.Person;
public interface GrowAge {
Person growPersonAge(Person person);
}
package com.zhongcheng.dubbo.model;
import java.io.Serializable;
public class Person implements Serializable {
/**
*
*/
private static final long serialVersionUID = -7408906574534643319L;
private String age;
private String name;
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<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.zhongcheng</groupId>
<artifactId>DubboInterface</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>DubboInterface</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
</dependencies>
</project>
到這,這個(gè)工程就結(jié)束了她奥,使用 mvn install打包到本地瓮增,我們一會(huì)在其他兩個(gè)項(xiàng)目中會(huì)用到
mvn install
DubboPro:生產(chǎn)者
生產(chǎn)者在dubbo負(fù)責(zé)生,提供遠(yuǎn)程調(diào)用的服務(wù)哩俭,
先看下項(xiàng)目結(jié)構(gòu)绷跑,其中有一個(gè)接口的實(shí)現(xiàn)類(lèi),和一個(gè)spring的配置文件(dubbo是依賴(lài)spring的)携茂,
package com.zhongcheng.dubbo.pro;
import com.zhongcheng.dubbo.interf.GetPerson;
import com.zhongcheng.dubbo.model.Person;
public class GetPersonImpl implements GetPerson {
public String speak(Person person) {
String s="Hello Dubbo!,My name is "+person.getName();
return s;
}
}
<?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">
<!-- 提供方應(yīng)用信息你踩,用于計(jì)算依賴(lài)關(guān)系 诅岩,一個(gè)應(yīng)用只有一個(gè) -->
<dubbo:application name="dubbo-hello-pro" />
<dubbo:provider registry="zk1" protocol="mydubbo" />
<!-- 使用multicast廣播注冊(cè)中心暴露服務(wù)地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<!-- 使用zookeeper注冊(cè)中心暴露服務(wù)地址 -->
<dubbo:registry id="zk1" address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo協(xié)議在20880端口暴露服務(wù) -->
<dubbo:protocol id="mydubbo" name="dubbo" port="20880" />
<!-- 聲明需要暴露的服務(wù)接口 -->
<dubbo:service interface="com.zhongcheng.dubbo.interf.GetPerson" ref="getPerson" />
<!-- 和本地bean一樣實(shí)現(xiàn)服務(wù) -->
<bean id="getPerson" class="com.zhongcheng.dubbo.pro.GetPersonImpl" />
</beans>
<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.zhongcheng</groupId>
<artifactId>DubboPro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>DubboPro</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>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.zhongcheng</groupId>
<artifactId>DubboInterface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.4.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
</dependencies>
</project>
這樣完成了讳苦,主要的工作都在配置文件中,接口實(shí)現(xiàn)類(lèi)只負(fù)責(zé)業(yè)務(wù)的邏輯吩谦,這時(shí)鸳谜,我們就可以啟動(dòng)了,當(dāng)然這時(shí)一個(gè)javaApplcation程序式廷,需要主函數(shù)咐扭,我們需要配置一下,看圖
執(zhí)行應(yīng)用滑废,這時(shí)我們就可以去zookeeper看下蝗肪,我們的在dubbo中注冊(cè)的服務(wù)是否已經(jīng)被存放到節(jié)點(diǎn)中
在zcClient中使用ls和get命令
ls /
圖中可以看到,我們發(fā)布的接口已經(jīng)到zookeeper的/dubbo節(jié)點(diǎn)中了蠕趁,下面我們就可以使用消費(fèi)者調(diào)用了
DubboCus:消費(fèi)者
消費(fèi)者我服調(diào)用遠(yuǎn)程服務(wù)薛闪,先看下項(xiàng)目的結(jié)構(gòu)
這里只有一個(gè)測(cè)試類(lèi)和spring的配置文件,直接看代碼
package com.zhongcheng.dubbo.pro;
import com.zhongcheng.dubbo.interf.GetPerson;
import com.zhongcheng.dubbo.model.Person;
public class GetPersonImpl implements GetPerson {
public String speak(Person person) {
String s="Hello Dubbo!,My name is "+person.getName();
return s;
}
}
<?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">
<!-- 提供方應(yīng)用信息俺陋,用于計(jì)算依賴(lài)關(guān)系 豁延,一個(gè)應(yīng)用只有一個(gè) -->
<dubbo:application name="dubbo-hello-pro" />
<dubbo:provider registry="zk1" protocol="mydubbo" />
<!-- 使用multicast廣播注冊(cè)中心暴露服務(wù)地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<!-- 使用zookeeper注冊(cè)中心暴露服務(wù)地址 -->
<dubbo:registry id="zk1" address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo協(xié)議在20880端口暴露服務(wù) -->
<dubbo:protocol id="mydubbo" name="dubbo" port="20880" />
<!-- 聲明需要暴露的服務(wù)接口 -->
<dubbo:service interface="com.zhongcheng.dubbo.interf.GetPerson" ref="getPerson" />
<!-- 和本地bean一樣實(shí)現(xiàn)服務(wù) -->
<bean id="getPerson" class="com.zhongcheng.dubbo.pro.GetPersonImpl" />
</beans>
<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.zhongcheng</groupId>
<artifactId>DubboPro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>DubboPro</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>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.zhongcheng</groupId>
<artifactId>DubboInterface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.4.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
</dependencies>
</project>
成功了!@白础S沼健!
寫(xiě)的太粗略了缴挖,就當(dāng)學(xué)習(xí)筆記吧袋狞,