Linux Centos7 环境基于Docker部署Zookeeper服务搭建实战

配置Zookeeper安装目录

在宿主机配置zookeeper安装目录:/docker/develop/zookeeper
并且在文件夹创建 data 和logs 目录:
mkdir -p /docker/develop/zookeeper/data
mkdir -p /docker/develop/zookeeper/logs

[root@centos-knight ~]# cd /docker/develop/zookeeper
[root@centos-knight zookeeper]# mkdir -p /docker/develop/zookeeper/data
[root@centos-knight zookeeper]# mkdir -p /docker/develop/zookeeper/logs

授权文件目录:chmod -R 777 /docker/develop/zookeeper/

[root@centos-knight zookeeper]# chmod -R 777 /docker/develop/zookeeper/ 
[root@centos-knight zookeeper]# 

安装和部署Zookeeper

拉取zookeeper镜像:docker pull zookeeper

[root@centos-knight zookeeper]# docker pull zookeeper
Using default tag: latest
latest: Pulling from library/zookeeper
Digest: sha256:6b6b5f7fb6a47d2b311df5af1718af5a425a679dbb844d77913fa68d1a8bf0fd
Status: Image is up to date for zookeeper:latest
docker.io/library/zookeeper:latest
[root@centos-knight zookeeper]# 

临时安装Zookeeper服务:zookeeper-tmp-server

docker run -itd -p 2181:2181 --restart always --name=zookeeper-tmp-server  --privileged=true \
-v /docker/develop/zookeeper/data:/data \
-v /docker/develop/zookeeper/datalog:/datalog \
-v /docker/develop/zookeeper/logs:/logs \
-e "ZOO_STANDALONE_ENABLED=true" \
-e "TZ=Asia/Shanghai" \
-e "ZOO_4LW_COMMANDS_WHITELIST=*" \
-e "ZOO_AUTOPURGE_SNAPRETAINCOUNT=5" \
-e "ZOO_AUTOPURGE_PURGEINTERVAL=24" \
-e "ZOO_MAX_CLIENT_CNXNS=64" \
-e "ZOO_TICK_TIME=2000" \
-e "ZOO_INIT_LIMIT=100" \
-e "ZOO_SYNC_LIMIT=5" \
zookeeper:latest

复制zookeeper-tmp-server的conf到/docker/develop/zookeeper:

[root@centos-knight ~]# docker ps
CONTAINER ID  IMAGE   COMMAND   CREATED             STATUS   PORTS   NAMES
12947bdadd84  zookeeper:latest "/docker-entrypoint.…"   36 hours ago  Up 36 hours  2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp, 8080/tcp   zookeeper-develop-server
[root@centos-knight ~]# docker cp 12947bdadd84:/conf /docker/develop/zookeeper/

编写zookeeper的zoo.cfg配置文件:

tickTime=2000
initLimit=100
syncLimit=5
dataDir=/data
dataLogDir=/datalog
clientPort=2181
quorumListenOnAllIPs=true
preAllocSize=64M
globalOutstandingLimit=100000
snapCount=50000
maxClientCnxns=64
minSessionTimeout=4000
maxSessionTimeout=40000
4lw.commands.whitelist=*
standaloneEnabled=true
admin.enableServer=true
server.1=0.0.0.0:2888:3888
tcpKeepAlive=true
ipReachableTimeout=0
cnxTimeout=3
electionAlg=3
autopurge.snapRetainCount=5
autopurge.purgeInterval=24
#忽略ACL验证
skipACL=yes
forceSync=yes
fsync.warningthresholdms=50

编写正式安装Dcoekr命令:

docker run -itd -p 2181:2181 -p 2888:2888 -p 3888:3888 --restart always --name=zookeeper-develop-server  --privileged=true \
-v /docker/develop/zookeeper/conf/zoo.cfg:/conf/zoo.cfg \
-v /docker/develop/zookeeper/data:/data \
-v /docker/develop/zookeeper/logs:/datalog \
-e "TZ=Asia/Shanghai" \
-e "JAVA_OPTS=-server -Xms512m -Xmx512m -Xmn256m -Duser.home=/opt -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m -XX:+AlwaysPreTouch -XX:-UseBiasedLocking" \
zookeeper:latest

部署Dubbo客户端

部署安装dubbo-admin:

docker run -itd --restart always --privileged=true  --name dubbo-devleop-ops -p 8083:8080  -e admin.registry.address="zookeeper://IP:2181" -e admin.config-center="zookeeper://IP:2181" -e admin.metadata-report.address="zookeeper://IP:2181" -e "JAVA_OPTS=-server -Xms512m -Xmx512m -Xmn256m -Duser.home=/opt -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m -XX:+AlwaysPreTouch -XX:-UseBiasedLocking" -e TZ="Asia/Shanghai" apache/dubbo-admin:latest

基于Springboot整合和使用Dubbo+Zookeeper

创建工程:pivotal-cloud-dependencies [统一管理工程版本依赖资源]
Linux Centos7 环境基于Docker部署Zookeeper服务搭建实战
一般都是使用Maven作为构建工具,这里便只需在pom.xml中添加如下依赖即可:
标签定义依赖版本号:

<!--spring-boot dubbo setting -->
<dubbo.version>2.7.3</dubbo.version>
<spring-boot-dubbo.version>2.7.3</spring-boot-dubbo.version>
<!--spring-boot zookeeper setting -->
<zookeeper.version>3.5.6</zookeeper.version>
<curator.version>4.2.0</curator.version>

标签添加如下内容:

 <!--spring-boot dubbo setting  begin-->
    <dependency>
     <groupId>org.apache.dubbo</groupId>
     <artifactId>dubbo</artifactId>
     <version>${dubbo.version}</version>
     <exclusions>
      <!--exclusion spring-->
       <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring</artifactId>
       </exclusion>
     <!--exclusion servlet-api-->
       <exclusion>
          <groupId>javax.servlet</groupId>
          <artifactId>servlet-api</artifactId>
       </exclusion>
    <!--exclusion log4j-->
       <exclusion>
           <groupId>log4j</groupId>
           <artifactId>log4j</artifactId>
       </exclusion>
    <!--exclusion netty-->
        <exclusion>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
      </exclusion>
     </exclusions>
  </dependency>
  <dependency>
       <groupId>org.apache.dubbo</groupId>
       <artifactId>dubbo-spring-boot-starter</artifactId>
       <version>${spring-boot-dubbo.version}</version>
       <exclusions>
          <!--exclusion dubbo-->
           <exclusion>
              <groupId>org.apache.dubbo</groupId>
               <artifactId>dubbo</artifactId>
          </exclusion>
      </exclusions>
 </dependency>
 <dependency>
     <groupId>org.apache.dubbo</groupId>
     <artifactId>dubbo-metadata-report-zookeeper</artifactId>
      <version>${dubbo.version}</version>
      <exclusions>
       <!--exclusion dubbo-common-->
         <exclusion>
           <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-common</artifactId>
         </exclusion>
       <!--exclusion dubbo-remoting-zookeeper-->
      <exclusion>
          <groupId>org.apache.dubbo</groupId>
          <artifactId>dubbo-remoting-zookeeper</artifactId>
     </exclusion>
     </exclusions>
   </dependency>
 <!--spring-boot dubbo setting  end-->
 <!--spring-boot zookeeper setting  begin-->
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>${zookeeper.version}</version>
                <exclusions>
                    <!--exclusion log4j-->
                    <exclusion>
                        <artifactId>log4j</artifactId>
                        <groupId>log4j</groupId>
                    </exclusion>
                    <!--exclusion slf4j-api-->
                    <exclusion>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-api</artifactId>
                    </exclusion>
                    <!--exclusion slf4j-log4j12-->
                    <exclusion>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>${curator.version}</version>
                <exclusions>
                    <!--exclusion curator-client-->
                    <exclusion>
                        <groupId>org.apache.curator</groupId>
                        <artifactId>curator-client</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-recipes</artifactId>
                <version>${curator.version}</version>
                <exclusions>
                    <!--exclusion curator-framework-->
                    <exclusion>
                        <groupId>org.apache.curator</groupId>
                        <artifactId>curator-framework</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-client</artifactId>
                <version>${curator.version}</version>
                <exclusions>
                    <!--exclusion curator-framework-->
                    <exclusion>
                        <groupId>org.apache.curator</groupId>
                        <artifactId>curator-framework</artifactId>
                    </exclusion>
                    <!--exclusion zookeeper-->
                    <exclusion>
                        <groupId>org.apache.zookeeper</groupId>
                        <artifactId>zookeeper</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!--spring-boot zookeeper setting  end-->

ps[注意事项]:
1.使用pivotal-cloud-dependencies的意义,可以排除多余和重复依赖,解决依赖冲突等具有指导和参考的作用
2.对于升级和改造可以灵活切换,统一管理版本号等

创建工程:pivotal-dubbo-integration

与Dubbo相关的依赖:

<!--spring-boot dubbo setting  begin-->
 <dependency>
      <groupId>org.apache.dubbo</groupId>
      <artifactId>dubbo</artifactId>
      <version>${dubbo.version}</version>
 </dependency>
 <dependency>
      <groupId>org.apache.dubbo</groupId>
      <artifactId>dubbo-spring-boot-starter</artifactId>
      <version>${spring-boot-dubbo.version}</version>
  </dependency>
 <!--spring-boot dubbo setting  end-->

与Zookeeper相关的依赖:

<!--spring-boot zookeeper setting  begin-->
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>${zookeeper.version}</version>
</dependency>
<dependency>
  <groupId>org.apache.curator</groupId>
  <artifactId>curator-framework</artifactId>
  <version>${curator.version}</version>
</dependency>
<dependency>
  <groupId>org.apache.curator</groupId>
  <artifactId>curator-recipes</artifactId>
  <version>${curator.version}</version>
</dependency>
<dependency>
  <groupId>org.apache.curator</groupId>
  <artifactId>curator-client</artifactId>
  <version>${curator.version}</version>
</dependency>
<!--spring-boot zookeeper setting  end-->

然后运行打包构建命令:mvn clean install 将工程打成一个jar,然后在工程需要使用Dubbo作为Provider和Consumer 的工程模块中依赖此内部整合工程:

<!--pivotal dubbo setting begin -->
<dependency>
 <groupId>com.mark.apple</groupId>
 <artifactId>pivotal-dubbo-integration</artifactId>
 version>${project.version}</version>
</dependency>
<!--pivotal dubbo setting end-->

ps[注意事项]:
1.使用pivotal-dubbo-integration的意义,可以自定义封装Dubbo相关特殊场景下的自定义模块[自定义Dubbo序列化等],以及Dubbo性能调优等
2.可以整合以及其他框架,譬如[Dubbo网关以及Dubbo指标监控等]

最后在Provider和Consumer的工程配置相关属性:

Provider 相关属性配置:

#####################################################
#########Springboot Dubbo Provider Setting##########
#####################################################
pivotal.zookeeper.server-address=zookeeper://IP:2181
dubbo.metadata-report.address=${pivotal.zookeeper.server-address}
dubbo.application.id=${spring.application.name}-${spring.profiles.active}
dubbo.application.name=${spring.application.name}-${spring.profiles.active}
dubbo.application.registry=${spring.application.name}-${spring.profiles.active}
dubbo.application.organization=com.mark.apple
dubbo.application.architecture=${dubbo.application.name}
dubbo.application.environment=${spring.profiles.active}
dubbo.application.logger=slf4j
dubbo.config.override=true
dubbo.config.multiple=true
#dubbo file upload length
# ProtocolConfig Bean
dubbo.protocol.id=dubbo
dubbo.protocol.name=dubbo
#dubbo.protocol.status=client
dubbo.protocol.payload=2147483647
dubbo.protocol.buffer=8192
dubbo.protocol.register=true
dubbo.protocol.server=netty
dubbo.protocol.transporter=netty
dubbo.protocol.threadpool=cached
dubbo.protocol.threads=200
dubbo.protocol.iothreads=2
dubbo.protocol.dispatcher=all
#RegistryConfig Bean
dubbo.registry.address=${pivotal.zookeeper.server-address}
# ProtocolConfig Bean
dubbo.protocol.id=dubbo
dubbo.protocol.name=dubbo
dubbo.protocol.port=28080
dubbo.protocol.status=server
dubbo.protocol.payload=2147483647
dubbo.protocol.buffer=8192
dubbo.protocol.register=true
dubbo.protocol.server=netty
dubbo.protocol.transporter=netty
dubbo.protocol.threadpool=cached
dubbo.protocol.threads=200
dubbo.protocol.dispatcher=all
dubbo.protocol.accepts=1000
#Dubbo Provider setting
dubbo.provider.accepts=0
dubbo.protocol.iothreads=2
dubbo.provider.threads=200
dubbo.provider.threadpool=cached
dubbo.provider.executes=0
dubbo.provider.validation=true
dubbo.provider.connections=0
dubbo.provider.actives=0
dubbo.provider.buffer=8192
dubbo.provider.retries=3
dubbo.provider.delay=-1
dubbo.provider.timeout=360000
#Dubbo Consumer setting
dubbo.consumer.lazy=true
dubbo.consumer.connections=0
dubbo.consumer.actives=0
dubbo.consumer.retries=3
dubbo.consumer.timeout=360000
dubbo.consumer.validation=true
dubbo.consumer.check=true
dubbo.consumer.loadbalance=consistenthash
#Base packages to scan Dubbo Components (e.g @Service , @Reference)
dubbo.scan.base-packages=com.pivotal.authority.server.impl

Consumer相关属性配置:

#####################################################
#########Springboot Dubbo Consumer Setting##########
#####################################################
pivotal.zookeeper.server-address=zookeeper://IP:2181
dubbo.metadata-report.address=${pivotal.zookeeper.server-address}
# Dubbo Config properties
dubbo.application.id=${spring.application.name}-${spring.profiles.active}
dubbo.application.name=${spring.application.name}-${spring.profiles.active}
dubbo.application.registry=${spring.application.name}-${spring.profiles.active}
dubbo.application.organization=com.mark.apple
dubbo.application.architecture=${dubbo.application.name}
dubbo.application.environment=${spring.profiles.active}
dubbo.application.logger=slf4j
dubbo.config.override=true
dubbo.config.multiple=true
# ProtocolConfig Bean
dubbo.protocol.id=dubbo
dubbo.protocol.name=dubbo
dubbo.protocol.status=client
dubbo.protocol.payload=2147483647
dubbo.protocol.buffer=8192
dubbo.protocol.register=true
dubbo.protocol.server=netty
dubbo.protocol.transporter=netty
dubbo.protocol.threadpool=cached
dubbo.protocol.threads=200
dubbo.protocol.iothreads=2
dubbo.protocol.dispatcher=all
#RegistryConfig Bean
dubbo.registry.id=${dubbo.application.name}
dubbo.registry.protocol=dubbo
dubbo.registry.check=true
dubbo.registry.address=${pivotal.zookeeper.server-address}
#Dubbo Consumer setting
dubbo.consumer.lazy=true
dubbo.consumer.connections=0
dubbo.consumer.actives=0
dubbo.consumer.retries=3
dubbo.consumer.validation=true
dubbo.consumer.check=true
dubbo.consumer.loadbalance=consistenthash

版权声明:本文为博主原创文章,遵循相关版权协议,如若转载或者分享请附上原文出处链接和链接来源。

上一篇:吴裕雄--天生自然 Zookeeper学习笔记--Zookeeper Java 客户端搭建


下一篇:ZooKeeper系列(四)—— Java 客户端 Apache Curator