直接切入正題,springboot2.x 集成alibaba-dubbo真的非常方便。因?yàn)閐ubbo需要服務(wù)注冊(cè)中心,這里我們使用推薦的zookeeper。
整個(gè)項(xiàng)目搭建的第一步就是搭建zookeeper??梢匀ookeeper官網(wǎng)下載最新的穩(wěn)定版 官網(wǎng)地址:http://mirrors.shu.edu.cn/apache/zookeeper/
下載完成后解壓,進(jìn)入conf文件夾修改zoo_sample.cfg 為zoo.cfg并修改為如下內(nèi)容(主要是各種路徑的修改)
# 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=/home/jason/Tools/zookeeper-3.4.12/zookeeperdir/zookeeper-data
dataLogDir=/home/jason/Tools/zookeeper-3.4.12/zookeeperdir/logs
# 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
然后進(jìn)入bin文件夾啟動(dòng)zookeeper服務(wù)。(根據(jù)os系統(tǒng)選擇啟動(dòng)文件linux->.sh, windows->.cmd)
zkServer.sh start
或者
zkServer.cmd
jason@jason-lenovo:~/Tools/zookeeper-3.4.12/bin$ zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /home/jason/Tools/zookeeper-3.4.12/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
至此zookeeper已經(jīng)啟動(dòng)成功,下面開(kāi)始springboot+dubbo搭建
先搭建一個(gè)空的springboot項(xiàng)目,然后新建兩個(gè)module,一個(gè)服務(wù)提供者provider,一個(gè)服務(wù)消費(fèi)者consumer。
我們先來(lái)看看provider的pom文件,主要是引入了dubbo-spring-boot-starter,這個(gè)真的挺方便的。
<?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>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.***</groupId>
<artifactId>dubbo-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-service</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.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在application.properties文件添加項(xiàng)目配置,主要是zookeeper的配置
spring.application.name= springboot-dubbo
server.port=9090
demo.service.version = 1.0.0
dubbo.scan.base-packages=com.***.dubboservice.serviceImpl
dubbo.application.id = springboot-dubbo
dubbo.application.name=springboot-dubbo
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 20880
dubbo.registry.id = my-registry1
dubbo.registry.address=zookeeper://localhost:2181
新建一個(gè)Service接口 HelloService
public interface HelloService {
String sayHello(String name);
}
新建接口實(shí)現(xiàn)類(lèi)HelloServiceImpl,注意這里的@Service注解為dubbo注解而不是spring注解。
此@Service注解會(huì)將服務(wù)注冊(cè)至zookeeper。
import com.alibaba.dubbo.config.annotation.Service;
import com.***.dubboservice.HelloService;
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name){
return "Hello "+name;
}
}
最后注意springboot啟動(dòng)類(lèi)上需要添加@EnableDubbo注解,至此provider全部完成。
@SpringBootApplication
@EnableDubbo
public class DubboServiceApplication {
public static void main(String[] args) {
SpringApplication.run(DubboServiceApplication.class, args);
}
}
接下來(lái)需要實(shí)現(xiàn)consumer
同樣是新建一個(gè)module,pom文件與provider一致。需要引入dubbo-spring-boot-starter

修改properties文件,同樣注意別忘記服務(wù)注冊(cè)中心zookeeper的配置
spring.application.name = dubbo-consumer-demo
server.port=8080
demo.service.version = 1.0.0
dubbo.application.id = dubbo-consumer-demo
dubbo.application.name = dubbo-consumer-demo
dubbo.protocol.id = dubbo
dubbo.protocol.name = dubbo
dubbo.protocol.port = 12345
dubbo.registry.address=zookeeper://localhost:2181
現(xiàn)在開(kāi)發(fā)一個(gè)簡(jiǎn)單的Controller, 注意@Reference注解,該注解也是Dubbo注解用來(lái)注入HelloService.
如果注入的地方報(bào)錯(cuò),先檢查一下dependency有沒(méi)有問(wèn)題。
@RestController
public class sayHelloController {
@Reference
HelloService helloService;
@GetMapping("/hello")
public ResponseEntity hello(){
String a = helloService.sayHello("jason");
return ResponseEntity.ok(a);
}
}
最后就是啟動(dòng)類(lèi)了,同樣需要@EnableDubbo注解
@EnableDubbo
@SpringBootApplication(scanBasePackages = {"com.***.dubboweb.controller"})
public class DubboWebApplication {
public static void main(String[] args) {
SpringApplication.run(DubboWebApplication.class, args);
}
}
至此provider和consumer全部完成,分別啟動(dòng)provider和consumer。
從瀏覽器訪(fǎng)問(wèn)接口:http://localhost:8080/hello得到如下結(jié)果,至此搭建結(jié)束,mark以下。
