1. 程式人生 > >SpringBoot整合Dubbo&ZooKeeper

SpringBoot整合Dubbo&ZooKeeper

AI protoc say err ppi develop 提供者 .get toc

前言

關於dubbo這裏就不多說了,想了解更多關於dubbo的請移步到dubbo官網。文章中涉及的代碼都在我的Github:https://github.com/nyvi/dubbo。

服務註冊中心

這裏選用zookeeper為註冊中心
1.首先你需要在ZooKeeper官網 下載一個zk的包。這裏使用的版本是zookeeper-3.4.11。
2.windows下安裝zookeeper也很簡單,首先解壓zookeeper-3.4.11.tar.gz,然後將conf文件下的zoo_sample.cfg更名為zoo.cfg,並修改裏面的配置,配置如下:

dataDir=D:\\develop\\zookeeper\\data
dataLogDir=D:\\develop\\zookeeper\\log

3.然後到bin目錄下雙擊運行zkServer.cmd,出現如下界面就代表zk啟動成功了。默認端口為2181。

其余省略...
2018-04-08 18:08:38,205 [myid:] - INFO  [main:NIOServerCnxnFactory@89] - binding to port 0.0.0.0/0.0.0.0:2181

構建項目

這裏采用模塊化構建項目

dubbo

dubbo的pom.xml如下:

<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.dubbo</groupId>
    <artifactId>dubbo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <java.version>1.8</java.version>
        <curator.version>4.0.1</curator.version>
        <zookeeper.version>3.4.11</zookeeper.version>
        <dubbo-spring-boot-starter.version>2.0.0</dubbo-spring-boot-starter.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
        
            <dependency>
                <groupId>com.dubbo</groupId>
                <artifactId>dubbo-api</artifactId>
                <version>${project.version}</version>
            </dependency>
            
            <dependency>
                <groupId>com.alibaba.spring.boot</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>${dubbo-spring-boot-starter.version}</version>
            </dependency>
            
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                    </exclusion>
                </exclusions>
                <version>${zookeeper.version}</version>
            </dependency>
            
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>${curator.version}</version>
            </dependency>
            
        </dependencies>
    </dependencyManagement>

    <modules>
        <module>dubbo-api</module>
        <module>dubbo-provider</module>
        <module>dubbo-consumer</module>
    </modules>
</project>

dubbo-api

api 主要的職責就是定義接口

public interface IHelloService {
    String sayHello(String name);
}

dubbo-provider

provider 服務提供者
dubbo-provider的pom.xml如下:

<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>com.dubbo</groupId>
        <artifactId>dubbo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>dubbo-provider</artifactId>

    <dependencies>
    
        <dependency>
            <groupId>com.dubbo</groupId>
            <artifactId>dubbo-api</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <dependency>
            <groupId>com.alibaba.spring.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
        </dependency>
    
    </dependencies>

</project>

服務提供方法:

package com.nyvi.dubbo;

import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;

@Service
@Component
public class HelloServiceImpl implements IHelloService {
    @Override
    public String sayHello(String name) {
        return "Hello " + name + " !";
    }
}

配置參數:

server.port=8081
spring.application.name=dubbo-provider-test
spring.dubbo.registry=zookeeper://127.0.0.1:2181?client=curator
spring.dubbo.protocol=dubbo

dubbo-consumer

consumer 服務消費者
dubbo-consumer 的pom.xml和 dubbo-provider的pom.xml 差不多這裏就不再重復了。
服務消費方法:

package com.nyvi.dubbo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.Reference;

@RestController
public class HelloConsumer {

    @Reference
    private IHelloService helloService;

    @GetMapping("sayHello/{name}")
    public String sayHello(@PathVariable("name") String name) {
        return helloService.sayHello(name);
    }
}

配置參數:

server.port=8082
spring.dubbo.appname=dubbo-spring-boot-starter-consumer-test
spring.dubbo.registry=zookeeper://127.0.0.1:2181?client=curator
spring.dubbo.protocol=dubbo

驗證

1.啟動生產者(dubbo-provider)
2.啟動生產者(duboo-consumer)
3.瀏覽器輸入:?http://127.0.0.1:8082/sayHello/dubbo
如果輸出: Hello dubbo ! 那麽恭喜你,成功了。

管理平臺

dubbo有一套管理平臺dubbo-admin可以自行去下載然後打包成war扔到tomcat裏面運行,初始賬戶為:root/root。 這裏就不再描述了。

SpringBoot整合Dubbo&ZooKeeper