Dubbo是一個分布式服務(wù)框架奢讨,致力于提供高性能和透明化的RPC遠程服務(wù)調(diào)用方案靠闭,以及SOA服務(wù)治理方案斗埂。Dubbo主要分為5個部分(如下圖所示):遠程運行服務(wù)容器(container)、遠程服務(wù)提供方(provider)叠洗、注冊中心(register)甘改、遠程服務(wù)消費者(consumer)、監(jiān)控中心(monitor)灭抑。
Dubbo主要有以下幾個主要功能:
1.透明化的遠程調(diào)用方法十艾,調(diào)用遠程服務(wù)就像調(diào)用本地的服務(wù)一樣。
2.服務(wù)自動注冊與發(fā)現(xiàn)腾节。
3.軟負載均衡與容錯機制忘嫉,consumer根據(jù)從注冊中心獲取的服務(wù)列表荤牍,根據(jù)軟負載均衡算法選擇一個服務(wù)提供者(provider),如果調(diào)用失敗則重新選擇一個服務(wù)提供者榄融。
Duboo運行的幾個步驟:
1.初始化服務(wù):將服務(wù)裝載到容器中参淫,然后進行服務(wù)注冊救湖。
2.暴露服務(wù):分為直接暴露服務(wù)和向注冊中心暴露服務(wù)愧杯。
3.服務(wù)應(yīng)用:既然暴露服務(wù)分為兩種,那么服務(wù)的引用也必然分為兩種鞋既,一種是直接引用服務(wù)力九,另一種是從服務(wù)中心引用服務(wù)。
SpringBoot +Dubbo+zookeeper搭建demo邑闺。
首先搭建zookeeper注冊中心跌前,從apache官網(wǎng):(https://zookeeper.apache.org/)直接下載zookeeper安裝包(解壓包),直接解壓后進入zookeeper-3.4.12\conf下陡舅,將zoo_sample.cfg文件名修改為zoo.cfg抵乓。并修改這個文件,注釋掉#dataDir=/tmp/zookeeper靶衍,并添加如下語句:dataDir=D:/program/zookeeper-3.4.12/data
dataDirLog=D:/program/zookeeper-3.4.12/log
# 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=/tmp/zookeeper
#這是我添加的 start
dataDir=D:/program/zookeeper-3.4.12/data
dataDirLog=D:/program/zookeeper-3.4.12/log
#這是我添加的 end
# 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
接著進入zookeeper-3.4.12\bin目錄下灾炭,直接點擊zkServer.cmd文件,如果不閃退出現(xiàn)如下界面那就是安裝zookeeper成功了颅眶,
搭建SpringBoot+Dubbo項目蜈出,首先搭建provider項目,項目的整體框架如下所示:
首先來看pom.xml文件涛酗,依賴了dubbo铡原、zookeeper還有Web等。
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>Spring.boot</groupId>
<artifactId>productor</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>productor</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- <dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency> -->
<!-- <dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.6.4</version>
</dependency> -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.8.4</version>
</dependency>
<!-- <dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.7.1</version>
</dependency> -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
再來看ProductorApplicatiopn啟動類的代碼商叹,加入了@ImportResource("dubbo/dubbo.xml")燕刻,去引入dubbo.xml文件,具體代碼如下所示:
package Spring.boot.productor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource("dubbo/dubbo.xml")
public class ProductorApplication {
public static void main(String[] args) {
SpringApplication.run(ProductorApplication.class, args);
}
}
dubbo.xml文件配置剖笙,代碼中有詳細注解卵洗,具體如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 添加 DUBBO SCHEMA -->
<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)用名 -->
<dubbo:application name="dubbo-provider"/>
<!-- 連接到哪個本地注冊中心 -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
<!-- 用dubbo協(xié)議在20880端口暴露服務(wù) -->
<dubbo:protocol name="dubbo" port="28080"/>
<bean id="iProviderService" class="Spring.boot.productor.server.impl.IProviderServiceImpl" />
<!-- 聲明需要暴露的服務(wù)接口 -->
<dubbo:service version="1.0.0" interface="Spring.boot.productor.server.IProviderService" ref="iProviderService"/>
</beans>
application.properties配置如下所示,只是修改了了tomcat啟動的端口枯途,
server.port = 8086
需要暴露的服務(wù)接口IProviderService忌怎,就定義了一個sayHello方法,具體代碼如下所示:
package Spring.boot.productor.server;
public interface IProviderService {
public String sayHello(String msg);
}
IProviderService的實現(xiàn)了IProviderServiceImpl代碼如下所示:
package Spring.boot.productor.server.impl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import Spring.boot.productor.server.IProviderService;
public class IProviderServiceImpl implements IProviderService {
private static final Logger LOGGER = LoggerFactory.getLogger(IProviderServiceImpl.class);
@Override
public String sayHello(String msg) {
String result = "hello" + msg;
LOGGER.info("***************" + result + "*************");
return result;
}
}
到此你的provider服務(wù)提供者已經(jīng)完成了酪夷,進入zookeeper-3.4.12\bin目錄下榴啸,點擊zkServer.cmd啟動zookeeper,接著啟動SpringBoot provider項目晚岭,進入zookeeper-3.4.12\bin目錄下鸥印,點擊zkCli.cmd,輸入ls /dubbo出現(xiàn)下圖紅色筆標志的標志,則證明你provider注冊成功库说。
既然服務(wù)提供者啟動成功那么接下來就是創(chuàng)建服務(wù)消費者了狂鞋,服務(wù)消費者項目整體框架如下所獲:
首先來看pom.xml文件配置,其實與provide提供者的pom.xml是差不多的潜的。
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>Spring.boot</groupId>
<artifactId>consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.7</version>
</dependency>
<dependency>
<groupId>Spring.boot</groupId>
<artifactId>productor</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
接下來來看看ConsumerApplication啟動類骚揍,也加入@ImportResource("dubbo/dubbo.xml")來引入dubbo.xml文件,具體代碼如下所示:
package Spring.boot.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource("dubbo/dubbo.xml")
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
接下來創(chuàng)建一個ConsumerController來驗證是否調(diào)用了provider提供的服務(wù)了啰挪,具體代碼如下所示:
package Spring.boot.consumer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.Reference;
import Spring.boot.productor.server.IProviderService;
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
@Autowired
IProviderService iProviderService;
@GetMapping("/msg")
public void testDubbo() {
String msg = "yangnima";
iProviderService.sayHello(msg);
}
}
dubbo.xml文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 添加 DUBBO SCHEMA -->
<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)用名 -->
<dubbo:application name="dubbo-consumer"/>
<!-- 連接到哪個本地注冊中心 -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
<!-- 用dubbo協(xié)議在20880端口暴露服務(wù) -->
<dubbo:protocol name="dubbo" port="28080"/>
<!-- <bean id="iProviderService" class="Spring.boot.productor.server.impl.IProviderServiceImpl" /> -->
<!-- 聲明需要暴露的服務(wù)接口 -->
<!-- <dubbo:service version="1.0.0" interface="Spring.boot.productor.server.IProviderService" ref="iProviderService"/> -->
<dubbo:reference version="1.0.0" id="iProviderService" interface="Spring.boot.productor.server.IProviderService"/>
</beans>
appliction.propertites文件配置如下所示:
server.port=8088
Debug模式啟動consumer項目即舌,在瀏覽器中http://127.0.0.1:8088/consumer/msg块请,調(diào)試項目得到如下所示的信息姐扮,證明consumer調(diào)用了provider的服務(wù)组橄,
且在服務(wù)提供項目中控制臺輸出如下信息:
***************helloyangnima*************
至此已完成SpringBoot + Dubbo + Zookeeper項目的搭建。