SpringBoot整合Dubbo&ZooKeeper

Posted 笔筒上的熊先生

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot整合Dubbo&ZooKeeper相关的知识,希望对你有一定的参考价值。

前言

关于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:[email protected]] - 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的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot整合dubbo

SpringBoot整合Dubbo+ZooKeeper——实现发布并访问远程服务

SpringBoot整合Dubbo&ZooKeeper

Springboot 整合 Dubbo/ZooKeeper

dubbo入门学习-----dubbo整合springboot

springboot整合dubbo