Springboot2.X+Alibaba-Dubbo+Zookeeper入門(mén)搭建

直接切入正題,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

在開(kāi)發(fā)之前需要做的事情是將provider引入至consumer,itellij中設(shè)置引入module dependency或者將provider打jar包的方式引入
Screenshot from 2019-02-20 18-53-27.png

修改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以下。

Screenshot from 2019-02-20 19-04-13.png

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容