Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成。
以上介绍来源于百度百科,具体dubbo相关可以自行查找资料,本文只是介绍SpringBoot简单整合dubbo。
1.安装zookeeper
1.1 去官网下载,本文以3.4.12 版本为例子http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.12/
1.2 下载之后解压ZooKeeper
1.3 进入解压文件夹conf目录
1.4 将zoo_sample.cfg修改名称为zoo.cfg
1.5 修改内容为如下,注意,本人是将ZooKeeper解压到了e盘,具体
dataDir和dataDirLog属性可以根据自己情况修改。
# 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=E:\\zookeeper\\data
dataDirLog=E:\\zookeeper\\log
# 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
1.6 启动zookeeper,进入bin目录,双击zkServer.cmd
2 新建项目springboot_dubbo_server,项目中加入dubbo依赖,完整pom如下:
4.0.0
com.dalaoyang
springboot_dubbo_server
0.0.1-SNAPSHOT
jar
springboot_dubbo_server
springboot_dubbo_server
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
io.dubbo.springboot
spring-boot-starter-dubbo
1.0.0
org.springframework.boot
spring-boot-maven-plugin
配置文件如下:
##端口号
server.port=8880
## Dubbo 服务提供者配置
spring.dubbo.application.name=dubbo_server
spring.dubbo.registry.address=zookeeper://39.108.123.128:2181
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=20880
spring.dubbo.scan=com.dalaoyang.dubbo
定义一个Service Interface:HelloService.java
package com.dalaoyang.dubbo;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.dubbo
* @email [email protected]
* @date 2018/6/14
*/
public interface HelloService {
String SayHello(String name);
}
接口的实现类:HelloServiceImpl.java
package com.dalaoyang.dubbo.imp;
import com.alibaba.dubbo.config.annotation.Service;
import com.dalaoyang.dubbo.HelloService;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.dubbo.imp
* @email [email protected]
* @date 2018/6/14
*/
@Service(version = "1.0.0")
public class HelloServiceImpl implements HelloService {
@Override
public String SayHello(String name) {
return "Hello , "+name;
}
}
到这里dubbo服务提供者已经创建完成。
3.新建项目springboot_dubbo_client,pom与提供者一致,代码如下:
4.0.0
com.dalaoyang
springboot_dubbo_client
0.0.1-SNAPSHOT
jar
springboot_dubbo_client
springboot_dubbo_client
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
io.dubbo.springboot
spring-boot-starter-dubbo
1.0.0
org.springframework.boot
spring-boot-maven-plugin
配置文件如下:
## 端口号
server.port=8881
## Dubbo 服务消费者配置
spring.dubbo.application.name=dubbo_client
spring.dubbo.registry.address=zookeeper://39.108.123.128:2181
spring.dubbo.scan=com.dalaoyang.controller
HelloService接口如下:
package com.dalaoyang.dubbo;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.dubbo
* @email [email protected]
* @date 2018/6/14
*/
public interface HelloService {
String SayHello(String name);
}
创建一个controller进行测试,注意版本号要与提供者的版本号一致,dubbo扫描包要扫描到我们要使用的类上,代码如下:
package com.dalaoyang.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.dalaoyang.dubbo.HelloService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author dalaoyang
* @Description
* @project springboot_learn
* @package com.dalaoyang.controller
* @email [email protected]
* @date 2018/6/14
*/
@RestController
public class HelloController {
@Reference(version = "1.0.0")
HelloService helloService;
@GetMapping("sayHello")
public String sayHello(String name){
return helloService.SayHello(name);
}
}
到这里dubbo服务调用者也创建完成。
分别启动服务提供者项目和服务调用者项目,在浏览器访问http://localhost:8881/sayHello?name=dalaoyang,如图,证明调用成功。
更多springboot-dubbo配置可以参考https://github.com/JeffLi1993/springboot-learning-example/blob/master/springboot-dubbo-server/DubboProperties.md
源码下载 :大老杨码云