前言
由于阿里dubbo團(tuán)隊(duì)的解散山上,已經(jīng)很久沒有更新,介于沒有支持Rest服務(wù)等新特性。所以把目光投向了dubbox骗爆,dubbox是當(dāng)當(dāng)網(wǎng)基于dubbo二次開發(fā)的一個(gè)項(xiàng)目,集成SpringBoot需要新版本的dubox并沒有提交maven倉庫蔽介,接下來需要自己制作maven依賴摘投。
制作maven依賴
1煮寡、 下載dubbox工程
使用git克隆工程,關(guān)于git的安裝及使用在這里就不說了犀呼。
- git clone -b dubbox-2.8.4 --depth 1 https://github.com/dangdangdotcom/dubbox
或者去GitHub上下載
注意:一定要選擇分支dubbox-2.8.4幸撕,這里是我說的巨坑。
2外臂、cmd使用maven命令編譯坐儿,需要配置maven環(huán)境變量。
cd dubbox
mvn clean install -Dmaven.test.skip=true
3宋光、到這步制作maven依賴就完成了貌矿,到本地倉庫里查看。
4罪佳、項(xiàng)目里引入依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.8.4</version>
</dependency>
安裝zookeeper
1逛漫、zookeeper簡(jiǎn)介
ZooKeeper是一個(gè)分布式的,開放源碼的分布式應(yīng)用程序協(xié)調(diào)服務(wù)菇民,是Google的Chubby一個(gè)開源的實(shí)現(xiàn)尽楔,是Hadoop和Hbase的重要組件。它是一個(gè)為分布式應(yīng)用提供一致性服務(wù)的軟件第练,提供的功能包括:配置維護(hù)阔馋、域名服務(wù)、分布式同步娇掏、組服務(wù)等呕寝。
ZooKeeper的目標(biāo)就是封裝好復(fù)雜易出錯(cuò)的關(guān)鍵服務(wù),將簡(jiǎn)單易用的接口和性能高效婴梧、功能穩(wěn)定的系統(tǒng)提供給用戶
而dubbo就是依賴zookeeper的一個(gè)分布式框架下梢,當(dāng)然二次開發(fā)的dubbox肯定也會(huì)依賴于zookeeper,所以我們需要先安裝zookeeper
2塞蹭、下載zookeeper
zookeeper官網(wǎng)地址http://zookeeper.apache.org/
下載地址http://apache.fayea.com/zookeeper/
3孽江、安裝zookeeper
因?yàn)槲沂窃赪indows環(huán)境配置的,所以就簡(jiǎn)單說一下windows下面的配置吧番电,首先解壓壓縮包岗屏,然后進(jìn)入conf文件夾,復(fù)制一下zoo_sample.cfg創(chuàng)建副本漱办,然后重命名為zoo.cfg,因?yàn)閦ookeeper只識(shí)別zoo.cfg这刷,而默認(rèn)是沒有這個(gè)文件的,zoo_sample.cfg是默認(rèn)的配置文件娩井,但是因?yàn)槲募脑蛳疚荩詚ookeeper無法識(shí)別,當(dāng)然直接重命名zoo_sample.cfg也是可以的洞辣,只是看自己喜歡咯
4咐刨、啟動(dòng)zookeeper
Windows環(huán)境下直接運(yùn)行bin目錄下的zkServer.cmd就可以昙衅,如果是Linux環(huán)境,則在bin目錄下運(yùn)行
./zkServer.sh start
命令定鸟,就可以啟動(dòng)zookeeper
創(chuàng)建工程
1绒尊、創(chuàng)建如下圖目錄結(jié)構(gòu)的工程
2、修改pom.xml
- boot-dubbo 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>
<groupId>com.boot</groupId>
<artifactId>boot-dubbo</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 這里是我們子模塊的設(shè)置 -->
<modules>
<module>boot-dubbo-api</module>
<module>boot-dubbo-provider</module>
<module>boot-dubbo-consumer</module>
</modules>
<!-- 在這里設(shè)置打包類型為pom仔粥,作用是為了實(shí)現(xiàn)多模塊項(xiàng)目 -->
<packaging>pom</packaging>
<!-- 第一步:添加Springboot的parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<!-- 設(shè)置我們項(xiàng)目的一些版本屬性 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot.version>1.5.2.RELEASE</spring-boot.version>
<java.version>1.8</java.version>
<lombok.version>1.16.18</lombok.version>
<dubbo.version>2.8.4</dubbo.version>
<zkclient.version>0.6</zkclient.version>
</properties>
<!-- 聲明一些項(xiàng)目依賴管理,方便我們的依賴版本管理 -->
<dependencyManagement>
<dependencies>
<!-- Springboot依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- Springboot-web依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- 使用lombok實(shí)現(xiàn)JavaBean的get蟹但、set躯泰、toString、hashCode华糖、equals等方法的自動(dòng)生成 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<!-- DubboX相關(guān) -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- zookeeper 第三方操作工具類 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
- boot-dubbo-api 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">
<parent>
<artifactId>boot-dubbo</artifactId>
<groupId>com.boot</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>boot-dubbo-api</artifactId>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
- boot-dubbo-provider 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">
<parent>
<artifactId>boot-dubbo</artifactId>
<groupId>com.boot</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>boot-dubbo-provider</artifactId>
<dependencies>
<dependency>
<groupId>com.boot</groupId>
<artifactId>boot-dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--dubbo2.8.4必須引入spring-boot-starter-web依賴麦向,這也是我在做項(xiàng)目時(shí)遇到的坑,舊版本2.5.5不需要引入-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- DubboX相關(guān) -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!-- zookeeper 第三方操作工具類 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- boot-dubbo-consumer 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">
<parent>
<artifactId>boot-dubbo</artifactId>
<groupId>com.boot</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>boot-dubbo-consumer</artifactId>
<dependencies>
<dependency>
<groupId>com.boot</groupId>
<artifactId>boot-dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--dubbo2.8.4必須引入spring-boot-starter-web依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- DubboX相關(guān) -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<!-- zookeeper 第三方操作工具類 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3客叉、boot-dubbo-api添加服務(wù)提供者的接口
package com.boot.service;
public interface TestService {
String sayHello(String str);
}
4诵竭、boot-dubbo-provider服務(wù)提供者
package com.boot.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.boot.service.TestService;
import java.text.SimpleDateFormat;
import java.util.Date;
//這里Service是dubbo的注解,需要設(shè)置版本,不設(shè)置行不行請(qǐng)自測(cè)
@Service(version = "1.0.0")
public class TestServiceImpl implements TestService {
@Override
public String sayHello(String str) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return dateFormat.format(new Date()) + ": " + str;
}
}
- dubbox.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="provider"/>
<!-- 注冊(cè)中心的ip地址 -->
<dubbo:registry address="zookeeper://192.168.0.108:2181"/>
<!-- 掃描注解包路徑兼搏,多個(gè)包用逗號(hào)分隔卵慰,不填pacakge表示掃描當(dāng)前ApplicationContext中所有的類 -->
<dubbo:annotation package="com.boot.service.impl"/>
<!-- 要暴露的服務(wù)接口-->
<!--<dubbo:service interface="com.boot.service.TestService" ref="testServiceImpl" />-->
</beans>
- application.yml
server:
port: 8081
context-path: /
5、boot-dubbo-consumer服務(wù)消費(fèi)者
package com.boot.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.boot.service.TestService;
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;
@RestController
@RequestMapping("/")
public class TestController {
@Reference(version = "1.0.0")
private TestService testService;
@GetMapping("hello")
public String hello() {
return testService.sayHello("Hello springboot and dubbo!");
}
}
- dubbox.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="consumer"/>
<dubbo:registry address="zookeeper://192.168.0.108:2181"/>
<dubbo:annotation package="com.boot.controller"/>
<!--<dubbo:reference id="testServiceImpl" interface="com.boot.service.TestService"/>-->
</beans>
- application.yml
server:
port: 8082
context-path: /
6佛呻、引用dubbo配置xml
在Spring boot的Application類添加注解
@ImportResource({"classpath:dubbo.xml"})
xml名字可以自定義裳朋,消費(fèi)者和服務(wù)提供者Application類都需要配置此注解。
結(jié)尾
至此吓著,spring boot集成duubox就結(jié)束了鲤嫡。感謝有坑,有坑才有成長(zhǎng)绑莺。
GitHub項(xiàng)目地址