Dubbo+Zookeeper

Dubbo+Zookeeper

zookeeper

下载地址

https://zookeeper.apache.org/releases.html

基本使用

服务端

管理员运行bin/zkServer.cmd

闪退处理

编辑bin/zkServer.cmd 加上pause以暂停查看错误

setlocal
call "%~dp0zkEnv.cmd"

set ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain
set ZOO_LOG_FILE=zookeeper-%USERNAME%-server-%COMPUTERNAME%.log

echo on
call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" "-Dzookeeper.log.file=%ZOO_LOG_FILE%" "-XX:+HeapDumpOnOutOfMemoryError" "-XX:OnOutOfMemoryError=cmd /c taskkill /pid %%%%p /t /f" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %*
pause
endlocal

错误信息--配置文件问题

Invalid config, exiting abnormally
org.apache.zookeeper.server.quorum.QuorumPeerConfig$ConfigException: Error processing D:\environment\apache-zookeeper-3.6.0-bin\bin\..\conf\zoo.cfg

复制conf下的zoo_sample.cfg并更名为zoo.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

配置添加之后即可启动

客户端

首先启动服务端,再管理员运行zkCli.cmd

查看所有节点命令

ls /

创建节点命令 此处是创建一个值是123,名为“kai”的节点

create -e /kai 123

get节点

get /kai

Dubbo-admin

Dubbo-admin是一个监控管理后台,它可以查看注册了哪些服务,哪些服务被消费了

下载地址

注意下载master分支较稳定

https://github.com/apache/dubbo-admin

使用maven打包

mvn clean package -Dmaven.test.skip=true

先运行zookeeper,再运行打好的jar包,打好的包可以保存之后使用

java -jar dubbo-admin-0.0.1-SNAPSHOT.jar

在地址栏进行访问,默认账号密码root-root

http://localhost:7001/

SpringBoot整合Dubbo+Zookeeper,服务注册与发现

消费者和提供者都要导入依赖

<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter 
dubbo整合springboot的包-->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.7.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient 
zookeeper的客户端-->
<dependency>
    <groupId>com.github.sgroschupf</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>4.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-recipes -->
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>4.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.14</version>
<!--解决日志冲突-->
    <exclusions>
        <exclusion>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
    </exclusions>
</dependency>

服务注册

application.properties

server.port=8001
#服务应用名字
dubbo.application.name=provider-server
#注册中心地址
dubbo.registry.address=zookeeper://127.0.0.1:2181
#哪些服务要被注册
dubbo.scan.base-packages=com.kai.service

Service

package com.kai.service;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

//zookeeper服务注册
@Service
@Component//使用了dubbo后尽量不要使用spring的service注解,因为与dubbo的service重名
public class TicketServiceImpl implements TicketService {
    @Override
    public String getTicket() {
        return "车票生成了";
    }
}

运行zookeeper的zkServer.cmd,运行项目,即可开启服务
运行dubbo-admin-0.0.1-SNAPSHOT.jar,即可在网页查看服务状态

服务消费

application.properties

server.port=8002
#消费者需要暴露自己的名字
dubbo.application.name=consumer
#消费者去注册中心拿服务
dubbo.registry.address=zookeeper://127.0.0.1:2181

service远程引用

方式一:定义路径相同的接口名

service

package com.kai.service;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

@Service//此处是Spring的Service,放到容器中
public class UserService {
    //从注册中心拿到provider-server提供的票
    @Reference//远程引用 : pom坐标方式或者定义路径相同的接口
    TicketService ticketService;
    public void buyTicket(){
        String ticket = ticketService.getTicket();
        System.out.println("从注册中心拿到==>"+ticket);
    }
}

定义一个与服务端相同的接口

TicketService

package com.kai.service;

public interface TicketService {
    public String getTicket();
}

测试

@Test
void contextLoads() {
    userService.buyTicket();
}

运行结果
从运行结果可以看出消费端调用到了服务端的方法

从注册中心拿到==>车票生成了

步骤总结

前提:zookeeper服务已开启

提供者提供服务

1.导入依赖
2.配置注册中心的地址,以及服务发现名和要扫描的包
3.在想要被注册的服务上增加@Service注解

消费者消费

1.导入依赖
2.配置注册中心的地址,配置自己的服务名
3.@Reference从远处注入服务

引用Pom坐标

一般开发引用Pom坐标,引入坐标后需要取消启动类的@SpringBootApplication注解

分析原因:因为项目是springboot+dubbo架构,dao项目依赖于pojo项目,
在pojo项目中也有启动类@SpringBootApplication,因为在dao的测试类中启动方法时,
会加载pojo项目启动类,所以会造成上图所示错误,
注释掉pojo项目启动类上的@SpringBootApplication注解即可

<!--引入提供者的坐标-->
<dependency>
    <groupId>com.shiyan</groupId>
    <artifactId>dubbo-test1-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>