springCloud之集成zookeeper

zookeeper

springCloud之集成zookeeper

准备工作

启动zookeeper

1.引入依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
plugins{
id 'org.springframework.boot' version '2.6.1'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}

ext {
set('springCloudVersion', "2021.0.0")
name = 'Eureka Server'
description = 'Eureka Server demo project'
version='0.0.1-SNAPSHOT'
sourceEncoding='UTF-8'
}

repositories {
mavenCentral()
maven { url 'https://repo.spring.io/release/' }
maven { url "https://repo.spring.io/libs-snapshot-local" }
maven { url "https://repo.spring.io/libs-milestone-local" }
maven { url "https://repo.spring.io/libs-release-local" }
maven { url "https://repo.springsource.org/plugins-release" }
}

dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}

dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-zookeeper-discovery'
implementation 'org.springframework.boot:spring-boot-starter-web'
}

test{
useJUnitPlatform()
}

2. 修改配置文件

  • 配置服务的名称和zookeeper的地址
1
2
3
4
server.port=9091

spring.application.name: micro-weather-zookeeper
spring.cloud.zookeeper.connect-string: localhost:2181

3. 修改启动类

  • 加上@EnableDiscoveryClient注解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.pyr.spring.cloud.weather;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

4.进入zookeeper客户端,查看已经注册的服务

可以看到 micro-weather-zookeeper服务已经存在,SpringCloud以[Zookeeper]为注册中心整合成功

image-20230228233716687


springCloud之集成zookeeper
http://example.com/springCloud之集成zookeeper/
作者
Panyurou
发布于
2022年7月22日
许可协议