服务注册中心之zookeeper

springcloud官方对zookeeper也做了集成,只需要我们修改配置、添加注解,即可完成zookeepr版的服务中心搭建。

下面是对zookeeper版的服务中心搭建步骤:

1.在linux服务器上安装zookeeper集群

2.关闭linux防火墙

3.从服务注册中心服务器能ping通zookeeper集群中的任一个结点

以下是进入开发端环境搭建及编码阶段

4.在pom.xml中添加如下依赖:

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zookeeper-discovery -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

5.在yml中添加如下配置

server:
  port: 8001

spring:
  application:
    name: test   #服务名称
cloud:
  zookeeper:
    connect-string: zk集群地址

6.开发主启动类(例子如下):


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

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

注意,以前使用eureka做为服务注册中心的时候,需要在主启动类加上@EnabledEurekaServer注解,在使用zookeeper做为服务注册中心的时候,需要把@EnabledEurekaServer注解更换为@EnableDiscoveryClient注解来激活服务注册中心。

7.运行启动类

8.查看运行状态

  由于现在还没有服务注册进来,所以无法查看运行状态。

  也可以认为主启动类没有报错,就是状态正常。

 

posted @ 2020-06-15 14:17  dddfeng  阅读(261)  评论(0)    收藏  举报