前言
由于阿里dubbo团队的解散,已经很久没有更新,介于没有支持Rest服务等新特性。所以把目光投向了dubbox,dubbox是当当网基于dubbo二次开发的一个项目,集成SpringBoot需要新版本的dubox并没有提交maven仓库,接下来需要自己制作maven依赖。
制作maven依赖
1、 下载dubbox工程
使用git克隆工程,关于git的安装及使用在这里就不说了。
- git clone -b dubbox-2.8.4 --depth 1 https://github.com/dangdangdotcom/dubbox
或者去GitHub上下载
注意:一定要选择分支dubbox-2.8.4,这里是我说的巨坑。
2、cmd使用maven命令编译,需要配置maven环境变量。
cd dubbox
mvn clean install -Dmaven.test.skip=true
3、到这步制作maven依赖就完成了,到本地仓库里查看。
4、项目里引入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.8.4</version>
</dependency>
安装zookeeper
1、zookeeper简介
ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。
ZooKeeper的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效、功能稳定的系统提供给用户
而dubbo就是依赖zookeeper的一个分布式框架,当然二次开发的dubbox肯定也会依赖于zookeeper,所以我们需要先安装zookeeper
2、下载zookeeper
zookeeper官网地址http://zookeeper.apache.org/
下载地址http://apache.fayea.com/zookeeper/
3、安装zookeeper
因为我是在Windows环境配置的,所以就简单说一下windows下面的配置吧,首先解压压缩包,然后进入conf文件夹,复制一下zoo_sample.cfg创建副本,然后重命名为zoo.cfg,因为zookeeper只识别zoo.cfg,而默认是没有这个文件的,zoo_sample.cfg是默认的配置文件,但是因为文件名的原因,所以zookeeper无法识别,当然直接重命名zoo_sample.cfg也是可以的,只是看自己喜欢咯
4、启动zookeeper
Windows环境下直接运行bin目录下的zkServer.cmd就可以,如果是Linux环境,则在bin目录下运行
./zkServer.sh start
命令,就可以启动zookeeper
创建工程
1、创建如下图目录结构的工程
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>
<!-- 这里是我们子模块的设置 -->
<modules>
<module>boot-dubbo-api</module>
<module>boot-dubbo-provider</module>
<module>boot-dubbo-consumer</module>
</modules>
<!-- 在这里设置打包类型为pom,作用是为了实现多模块项目 -->
<packaging>pom</packaging>
<!-- 第一步:添加Springboot的parent -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<!-- 设置我们项目的一些版本属性 -->
<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>
<!-- 声明一些项目依赖管理,方便我们的依赖版本管理 -->
<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实现JavaBean的get、set、toString、hashCode、equals等方法的自动生成 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<!-- DubboX相关 -->
<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依赖,这也是我在做项目时遇到的坑,旧版本2.5.5不需要引入-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- DubboX相关 -->
<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相关 -->
<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添加服务提供者的接口
package com.boot.service;
public interface TestService {
String sayHello(String str);
}
4、boot-dubbo-provider服务提供者
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的注解,需要设置版本,不设置行不行请自测
@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"/>
<!-- 注册中心的ip地址 -->
<dubbo:registry address="zookeeper://192.168.0.108:2181"/>
<!-- 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类 -->
<dubbo:annotation package="com.boot.service.impl"/>
<!-- 要暴露的服务接口-->
<!--<dubbo:service interface="com.boot.service.TestService" ref="testServiceImpl" />-->
</beans>
- application.yml
server:
port: 8081
context-path: /
5、boot-dubbo-consumer服务消费者
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名字可以自定义,消费者和服务提供者Application类都需要配置此注解。
结尾
至此,spring boot集成duubox就结束了。感谢有坑,有坑才有成长。
GitHub项目地址
网友评论