Zookeeper注册中心

发布于 2022-04-18  6.75k 次阅读


Zookeeper的安装配置使用不赘述,主要演示在SpringCloud中使用Zookeeper作为注册中心

详细了解zk可跳转

1,开启Zookeeper并关闭防火墙1,开启Zookeeper并关闭防火墙
zkServer.sh start
systemctl stop firewall

使用jps检查zk是否开启

2,新建一个模块并配置(cloud-zookeeper-server)
① 父工程的maven依赖
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <junit.version>4.12</junit.version>
    <lombok.version>1.18.10</lombok.version>
    <mybatis.spring.boot.version>2.1.1</mybatis.spring.boot.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.2.2.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>2.1.0.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
② 导入依赖
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-actuator</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
    <!--zk依赖-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    </dependency>
</dependencies>
③ 配置application文件
server:
  port: 9092
spring:
  application:
    name: cloud-zookeeper-server
  cloud:
    zookeeper:
      connect-string: 192.168.68.133:2181 #连接外部zk
④ 主文件
@SpringBootApplication
@EnableDiscoveryClient //获取注册信息(一般使用注册中心都需要加)
public class ZookeeperMain {

    public static void main(String[] args) {
        SpringApplication.run(ZookeeperMain.class,args);
    }}

运行这个SpringBoot模块并查看zk是否存在注册信息:


路漫漫其修远兮,吾将上下而求索