关于springboot:SpringBoot整合DubboZookeeper

51次阅读

共计 4398 个字符,预计需要花费 11 分钟才能阅读完成。

Zookeeper 在 linux 下的装置

去官网下载 Zookeeper 压缩包,解压到服务器的任意目录下。进入 conf 文件夹,有一个 zoo_sample.cfg 的文件,关上编辑进行编辑:

# 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
# 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

## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
#开启四字命令
4lw.commands.whitelist=*

其中 dataDir 能够设置为本人制订的目录,用来寄存快照信息,同时能够增加 dataLogDir 来配置日志寄存的门路,默认与 dataDir 同一地位。最初记得开启四字命令配置,不便查看服务器状态。
最初一步,将编辑好的文件改名或复制一份叫 zoo.cfg 的文件作为配置文件。
(留神这是单机版的配置,非集群)

Zookeeper 环境变量配置

关上 /etc/profile 文件编辑,加上如下语句:

export ZOOKEEPER_HOME=/home/admin/apache-zookeeper-3.6.3-bin
export PATH=$PATH:$ZOOKEEPER_HOME/bin

保留退出。

启动 Zookeeper

进入 bin 目录,执行./zkServer.sh start 启动服务,同时也有 stop、restart 等其余命令。

应用自带客户端连贯 Zookeeper

进入 bin 目录,执行./zkCli.sh,即可连贯到服务器,应用 help 命令能够查看指令,可进行创立节点等操作。

Springboot 整合 Dubbo

创立一个 SpringBoot 我的项目作为 provider,在 pom 中退出如下依赖:

<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.8</version>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>5.2.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>5.2.0</version>
        </dependency>

目前这个版本的 Dubbo 须要 curator 的依赖。
在 application.properties 中配置:

server.port=8080
spring.application.name=dubbo-provider-demo
#蕴含 dubbo 注解心愿被 dubbo 扫描的包
dubbo.scan.base-packages=com.guomz.springbootdubbo.dubboService
#用于直连 dubbo 服务,不通过 zookeeper
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880

## 配置 zookeeper 信息
## 如果采纳直连形式不应用 zookeeper 则须要将 address 配置为 N /A,不配置 protocal
#dubbo.registry.address=N/A
dubbo.registry.protocol=zookeeper
dubbo.registry.address=zookeeper://39.105.145.179:2181

之后新创建一个 maven 工程,并编写服务接口,例如:

public interface MyService {String sayHello();

    Student getStudent();}

之后应用 maven install 命令将其打入本地 maven 仓库(也能够是本人的近程仓库),不便后续应用。
接口创立好之后在 springboot provider 中退出这个依赖,创立服务类实现这个接口并退出 Dubbo 注解:

import org.apache.dubbo.config.annotation.DubboService;
import org.guomz.entity.Student;
import org.guomz.service.MyService;

@DubboService(version = "1.0.0")
public class MyServiceImpl implements MyService {
    @Override
    public String sayHello() {System.out.println("被调用了");
        return "hello";
    }

    @Override
    public Student getStudent() {Student student = new Student();
        student.setId(1L);
        student.setName("guomz");
        System.out.println("被调用了");
        return student;
    }
}

其中 @DubboService 注解用于将以后类标识为 dubbo 服务,能够应用参数 version 与 group 来辨别不同的实现类。
再创立一个 SpringBoot 我的项目作为 consumer 消费者,依赖与 provider 统一。
application.properties 配置同样与 provider 雷同,留神区别端口与服务名。
创立 controller 援用服务:

import org.apache.dubbo.config.annotation.DubboReference;
import org.guomz.entity.Student;
import org.guomz.service.MyService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {@DubboReference(version = "1.0.0", url = "dubbo://127.0.0.1:20880")
    private MyService myService;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello(){return "hello";}

    @RequestMapping(value = "/sayhello", method = RequestMethod.GET)
    public String callMyServiceHello(){String result = myService.sayHello();
        return result;
    }

    @RequestMapping(value = "/getstudent", method = RequestMethod.GET)
    public Student getStudent(){return myService.getStudent();
    }
}

其中 @DubboReference 用来援用 dubbo 服务,如果采纳直连 provider 的形式,须要给 url 参数赋值,赋值内容为 provider 配置文件中 dubbo.protocal 中配置的协定名与端口;如果应用 zookeeper,则不须要配置这个参数。version 参数须要与 provider 中配置的统一。

正文完
 0