美文网首页
SpringBoot+Dubbo+Zookeeper项目搭建

SpringBoot+Dubbo+Zookeeper项目搭建

作者: 675ea0b3a47d | 来源:发表于2019-01-04 14:26 被阅读0次

Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。Dubbo主要分为5个部分(如下图所示):远程运行服务容器(container)、远程服务提供方(provider)、注册中心(register)、远程服务消费者(consumer)、监控中心(monitor)。


image.png

Dubbo主要有以下几个主要功能:
1.透明化的远程调用方法,调用远程服务就像调用本地的服务一样。
2.服务自动注册与发现。
3.软负载均衡与容错机制,consumer根据从注册中心获取的服务列表,根据软负载均衡算法选择一个服务提供者(provider),如果调用失败则重新选择一个服务提供者。

Duboo运行的几个步骤:
1.初始化服务:将服务装载到容器中,然后进行服务注册。
2.暴露服务:分为直接暴露服务和向注册中心暴露服务。
3.服务应用:既然暴露服务分为两种,那么服务的引用也必然分为两种,一种是直接引用服务,另一种是从服务中心引用服务。

SpringBoot +Dubbo+zookeeper搭建demo。
首先搭建zookeeper注册中心,从apache官网:(https://zookeeper.apache.org/)直接下载zookeeper安装包(解压包),直接解压后进入zookeeper-3.4.12\conf下,将zoo_sample.cfg文件名修改为zoo.cfg。并修改这个文件,注释掉#dataDir=/tmp/zookeeper,并添加如下语句:dataDir=D:/program/zookeeper-3.4.12/data
dataDirLog=D:/program/zookeeper-3.4.12/log

# 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=/tmp/zookeeper

#这是我添加的 start
dataDir=D:/program/zookeeper-3.4.12/data  
dataDirLog=D:/program/zookeeper-3.4.12/log  
#这是我添加的 end

# 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

接着进入zookeeper-3.4.12\bin目录下,直接点击zkServer.cmd文件,如果不闪退出现如下界面那就是安装zookeeper成功了,


image.png

搭建SpringBoot+Dubbo项目,首先搭建provider项目,项目的整体框架如下所示:

image.png

首先来看pom.xml文件,依赖了dubbo、zookeeper还有Web等。

<?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>1.5.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>Spring.boot</groupId>
    <artifactId>productor</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>productor</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>io.dubbo.springboot</groupId>
            <artifactId>spring-boot-starter-dubbo</artifactId>
            <version>1.0.0</version>
        </dependency> -->
        
      <!--  <dependency>
           <groupId>com.alibaba.boot</groupId>
           <artifactId>dubbo-spring-boot-starter</artifactId>
           <version>2.6.4</version>
       </dependency> -->

      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.8.4</version>
      </dependency>
      <!-- <dependency>
        <groupId>org.apache.curator</groupId>
        <artifactId>curator-recipes</artifactId>
        <version>2.7.1</version>
      </dependency> -->
      <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.7</version>
      </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

再来看ProductorApplicatiopn启动类的代码,加入了@ImportResource("dubbo/dubbo.xml"),去引入dubbo.xml文件,具体代码如下所示:

package Spring.boot.productor;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource("dubbo/dubbo.xml")
public class ProductorApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProductorApplication.class, args);
    }
}

dubbo.xml文件配置,代码中有详细注解,具体如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 添加 DUBBO SCHEMA -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 应用名 -->
    <dubbo:application name="dubbo-provider"/>
    <!-- 连接到哪个本地注册中心 -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="28080"/>
    <bean id="iProviderService" class="Spring.boot.productor.server.impl.IProviderServiceImpl" />
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service version="1.0.0" interface="Spring.boot.productor.server.IProviderService" ref="iProviderService"/>
</beans>

application.properties配置如下所示,只是修改了了tomcat启动的端口,

server.port = 8086

需要暴露的服务接口IProviderService,就定义了一个sayHello方法,具体代码如下所示:

package Spring.boot.productor.server;

public interface IProviderService {
    public String sayHello(String msg);
}

IProviderService的实现了IProviderServiceImpl代码如下所示:

package Spring.boot.productor.server.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import Spring.boot.productor.server.IProviderService;

public class IProviderServiceImpl implements IProviderService {
    private static final Logger LOGGER = LoggerFactory.getLogger(IProviderServiceImpl.class);
    @Override
    public String sayHello(String msg) {
        String result = "hello" + msg;
        LOGGER.info("***************" + result + "*************");
        return result;
    }
}

到此你的provider服务提供者已经完成了,进入zookeeper-3.4.12\bin目录下,点击zkServer.cmd启动zookeeper,接着启动SpringBoot provider项目,进入zookeeper-3.4.12\bin目录下,点击zkCli.cmd,输入ls /dubbo出现下图红色笔标志的标志,则证明你provider注册成功。


既然服务提供者启动成功那么接下来就是创建服务消费者了,服务消费者项目整体框架如下所获:


image.png

首先来看pom.xml文件配置,其实与provide提供者的pom.xml是差不多的。

<?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>1.5.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>Spring.boot</groupId>
    <artifactId>consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>consumer</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</groupId>
            <artifactId>dubbo</artifactId>
              <version>2.8.4</version>
      </dependency>
      <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.7</version>
      </dependency>
        <dependency>
            <groupId>Spring.boot</groupId>
            <artifactId>productor</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

接下来来看看ConsumerApplication启动类,也加入@ImportResource("dubbo/dubbo.xml")来引入dubbo.xml文件,具体代码如下所示:

package Spring.boot.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource("dubbo/dubbo.xml")
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

接下来创建一个ConsumerController来验证是否调用了provider提供的服务了,具体代码如下所示:

package Spring.boot.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.Reference;
import Spring.boot.productor.server.IProviderService;

@RestController
@RequestMapping("/consumer")
public class ConsumerController {
    @Autowired
    IProviderService iProviderService;
    @GetMapping("/msg")
    public void testDubbo() {
        String msg = "yangnima";
        iProviderService.sayHello(msg);
    }
}

dubbo.xml文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 添加 DUBBO SCHEMA -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 应用名 -->
    <dubbo:application name="dubbo-consumer"/>
    <!-- 连接到哪个本地注册中心 -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="28080"/>
   <!--  <bean id="iProviderService" class="Spring.boot.productor.server.impl.IProviderServiceImpl" /> -->
    <!-- 声明需要暴露的服务接口 -->
   <!--  <dubbo:service version="1.0.0" interface="Spring.boot.productor.server.IProviderService" ref="iProviderService"/> -->
    <dubbo:reference version="1.0.0" id="iProviderService" interface="Spring.boot.productor.server.IProviderService"/>
</beans>

appliction.propertites文件配置如下所示:

server.port=8088

Debug模式启动consumer项目,在浏览器中http://127.0.0.1:8088/consumer/msg,调试项目得到如下所示的信息,证明consumer调用了provider的服务,

image.png

且在服务提供项目中控制台输出如下信息:

***************helloyangnima*************

至此已完成SpringBoot + Dubbo + Zookeeper项目的搭建。

相关文章

网友评论

      本文标题:SpringBoot+Dubbo+Zookeeper项目搭建

      本文链接:https://www.haomeiwen.com/subject/dvvfrqtx.html