[SpringCloud教程]4. Zookeeper服务注册中心集成

介绍基于Zookeeper的SpringCloud服务注册中心搭建,此种注册中心实现方式,实际使用不多,这里只作为备忘参考

关于本篇文章所属父项目前文已经介绍,这里不再赘述

  • 上篇文章地址:https://www.cnblogs.com/spzmmd/p/14627523.html
  • SpringCloud教程合集: https://www.cnblogs.com/spzmmd/tag/微服务教程/
  • 案例项目地址: https://gitee.com/spzmmd/spring-cloud-demo

安装Zookeeper服务

Vmware安装CentOS7

建议zookeeper安装到Linux系统,本地可使用虚拟机代替,虚拟机安装CentOS7可于此文查看: VMware 安装 CentOS 7,安装好虚拟机后,假设虚拟机ip为192.168.115.50(宿主机可以ping通虚拟机),且建立了用户app。

CentOS7里安装Zookeeper3.4.9(需要Java环境)

  • 下载地址
  • 下载后上传到CentOS7,解压到/home/app/soft目录下
  • 进入目录/home/app/soft/zookeeper-3.4.9/conf
  • 将zoo_sample.cfg文件复制一份并重命名为zoo.cfg
  • 进入目录/home/app/soft/zookeeper-3.4.9/bin,执行下方命令来启动zookeeper服务
# 启动服务 2181端口sh zkServer.sh start# 检查是否启动成功sh zkServer.sh status
  • 至此,宿主机应该可以在192.168.115.50:2181访问到zookeeper

集群

关于zookeeper集群,网上有很多教程,这里不赘述

服务提供者模块(ms-provider-zookeeper)

模块搭建

基于Zookeeper的服务提供者模块搭建方法与基于Eureka客户端的搭建方法类似,区别是基于Eureka客户端服务提供者需要在启动类声明@EnableEurekaClient,基于zookeeper的不需要;并且两者依赖有些不同

  • 在根项目pom.xml的modules标签下加入模块 ms-provider-zookeeper
  • 在项目根目录建立 ms-provider-zookeeper 目录,并在ms-provider-zookeeper目录下建立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>com.spz.demo</groupId>        <artifactId>spring-cloud-demo</artifactId>        <version>1.0-SNAPSHOT</version>    </parent>    <artifactId>ms-provider-zookeeper</artifactId>    <packaging>jar</packaging>    <description>服务提供者模块 - 使用zookeeper注册中心</description>    <dependencies>        <!-- Zookeeper注册中心 -->        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>            <exclusions>                <exclusion>                    <groupId>org.apache.zookeeper</groupId>                    <artifactId>zookeeper</artifactId>                </exclusion>            </exclusions>        </dependency>        <!-- Zookeeper 3.4.9 -->        <dependency>            <groupId>org.apache.zookeeper</groupId>            <artifactId>zookeeper</artifactId>            <version>3.4.9</version>            <exclusions>                <exclusion>                    <groupId>org.slf4j</groupId>                    <artifactId>slf4j-log4j12</artifactId>                </exclusion>            </exclusions>        </dependency>        <!-- sleuth zipkin 服务追踪 -->        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-zipkin</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-actuator</artifactId>        </dependency>        <dependency>            <groupId>com.spz.demo</groupId>            <artifactId>api-common</artifactId>            <version>${project.version}</version>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.projectlombok</groupId>            <artifactId>lombok</artifactId>            <optional>true</optional>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>                <executions>                    <execution>                        <goals>                            <goal>repackage</goal>                        </goals>                    </execution>                </executions>                <configuration>                    <excludes>                        <exclude>                            <groupId>org.projectlombok</groupId>                            <artifactId>lombok</artifactId>                        </exclude>                    </excludes>                </configuration>            </plugin>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <configuration>                    <source>1.8</source>                    <target>1.8</target>                </configuration>            </plugin>        </plugins>        <resources>            <resource>                <directory>src/main/resources</directory>                <filtering>true</filtering>            </resource>        </resources>    </build></project>
  • 建立src/main/java和src/main/resources目录
  • 在src/main/java目录下建立包com.spz.demo.scloud.provider.zk
  • 在zk包下建立SpringBoot启动类 ProviderZookeeperApp.java
package com.spz.demo.scloud.provider.zk;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;/** * 启动类 * @author spzmmd * @createTime 2021/04/08 */@EnableDiscoveryClient@SpringBootApplicationpublic class ProviderZookeeperApp {    public static void main(String[] args) {        SpringApplication.run(ProviderZookeeperApp.class, args);    }}
  • AppServiceImpl类和IndexController类与前文基于Eureka客户端的服务提供者相关代码相同,请查看前文,不同的是com.spz.demo.scloud.provider.zk.service.AppServiceImpl#instanceNameAndPort方法返回值里可以添加一句": (基于Zookeeper注册中心)"来便于测试
  • 在src/main/resources目录下建立application.properties文件
server.port=8001spring.application.name=MS-PROVIDER-ZOOKEEPERspring.cloud.zookeeper.connect-string=192.168.115.50:2181

集群部署

与前文基于Eureka客户端的服务提供者部署方法一致,修改idea启动配置,设置vm options,加入下列配置即可

-Dserver.port=8001

服务消费者模块(ms-consumer-zookeeper)

消费者模块搭建方法与前文类似,这里不再赘述,请参考案例项目里的ms-consumer-zookeeper模块,启动ms-provider-zookeeper多节点以及ms-consumer-zookeeper单节点,不断访问下方链接:

http://localhost:7001/discovery/zookeeper/appService/projectInfo

将交替出现8001、8002两个服务提供者端口,证明服务均已成功注册进zookeeper

{"code": 2000,"message": "MS-PROVIDER-ZOOKEEPER:8002: (基于Zookeeper注册中心)","data": null}{"code": 2000,"message": "MS-PROVIDER-ZOOKEEPER:8001: (基于Zookeeper注册中心)","data": null}


©著作权归作者所有:来自51CTO博客作者mb6066e453ca35b的原创作品,如需转载,请注明出处,否则将追究法律责任