Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成。
以上介绍来源于百度百科,具体dubbo相关可以自行查找资料,本文只是介绍SpringBoot简单整合dubbo。
1.安装zookeeper1.1 去官网下载,本文以3.4.12 版本为例子http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.12/1.2 下载之后解压ZooKeeper1.3 进入解压文件夹conf目录1.4 将zoo_sample.cfg修改名称为zoo.cfg1.5 修改内容为如下,注意,本人是将ZooKeeper解压到了e盘,具体dataDir和dataDirLog属性可以根据自己情况修改。
# The number of milliseconds of each ticktickTime=2000# The number of ticks that the initial # synchronization phase can takeinitLimit=10# The number of ticks that can pass between # sending a request and getting an acknowledgementsyncLimit=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 connectclientPort=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=11.6 启动zookeeper,进入bin目录,双击zkServer.cmd2 新建项目springboot_dubbo_server,项目中加入dubbo依赖,完整pom如下:
4.0.0 com.dalaoyang springboot_dubbo_server0.0.1-SNAPSHOT jar springboot_dubbo_server springboot_dubbo_server org.springframework.boot spring-boot-starter-parent1.5.9.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starterorg.springframework.boot spring-boot-starter-testtest io.dubbo.springboot spring-boot-starter-dubbo1.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:2181spring.dubbo.protocol.name=dubbo spring.dubbo.protocol.port=20880spring.dubbo.scan=com.dalaoyang.dubbo
定义一个Service Interface:HelloService.java
package com.dalaoyang.dubbo;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;@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_client0.0.1-SNAPSHOT jar springboot_dubbo_client springboot_dubbo_client org.springframework.boot spring-boot-starter-parent1.5.9.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starterorg.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-testtest io.dubbo.springboot spring-boot-starter-dubbo1.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:2181spring.dubbo.scan=com.dalaoyang.controller
HelloService接口如下:
package com.dalaoyang.dubbo;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;@RestControllerpublic 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,如图,证明调用成功。
image
更多springboot-dubbo配置可以参考https://github.com/JeffLi1993/springboot-learning-example/blob/master/springboot-dubbo-server/DubboProperties.md
源码下载 :大老杨码云
作者:dalaoyang
链接:https://www.jianshu.com/p/f36b1592e8c1



