spring boot集成zookeeper注册中心

ZooKeeper是一个分布式服务框架,是Apache Hadoop 的一个子项目,它主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务、状态同步服务、集群管理、分布式应用配置项的管理等

ZooKeeper是一个树形结构的目录服务,支持变更推送

在ZooKeeper中,节点分为两类:

机器节点:

  指构成集群的机器

数据节点ZNode:

  指数据模型中的数据单元  

  ZooKeeper将所有数据存储在内存中,数据模型是一棵树(ZNode Tree),由斜杠(/)进行分割的路径,就是一个ZNode,例如/services/customer

  每个ZNode上都会保存自己的数据内容,同时还会保存一系列属性信息

  Znode可分为:

    持久节点:指一旦这个ZNode被创建了,除非主动进行ZNode的移除操作,否则这个ZNode将一直保存在ZooKeeper上

    临时节点:它的生命周期和客户端会话绑定,一旦客户端会话失效,那么这个客户端创建的所有临时节点都会被移除

1.docker安装

docker pull zookeeper:3.5

spring boot集成zookeeper注册中心

2.启动

docker run --name zookeeper -p 2181:2181 -d zookeeper:3.5

3.使用

provider

依赖

<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

配置

server.port=8010
spring.application.name=service-provider
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
spring.cloud.zookeeper.connect-string=192.168.99.100:2181

启动类

package com.xyz.provider;

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

@EnableDiscoveryClient
@SpringBootApplication
public class ProviderApplication {

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

}

控制器

package com.xyz.provider.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class demoController {
    @RequestMapping("/hello")
    public String Hello() {
        return "hello,provider";
    }

}

comsumer

添加依赖

<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

配置

server.port=8015
spring.application.name=service-comsumer
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
feign.client.config.default.connect-timeout=500
feign.client.config.default.read-timeout=500
spring.cloud.zookeeper.connect-string=192.168.99.100:2181

启动类

package com.xyz.comsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableFeignClients
@SpringBootApplication
public class ComsumerApplication {

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

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

控制器

package com.xyz.comsumer.controller;

import com.xyz.comsumer.feign.RemoteHelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FeignController {
    @Autowired
    RemoteHelloService remoteHelloService;

    @RequestMapping("feignTest")
    public String feignTest() {
        String result = remoteHelloService.hello();
        if (result == null) {
            result = "出错";
        }
        return result;
    }
    
}

启动provider

启动comsumer

测试 GET http://127.0.0.1:8015/feignTest

输出

hello,provider

4.查看

在docker中进入zookeeper命令行

docker exec -it zookeeper bash

进入zookeeper的bin目录

cd bin

使用zookeeper client访问zookeeper服务

./zkCli.sh

查看当前 ZooKeeper 中所包含的内容

[zk: localhost:2181(CONNECTED) 1] ls /
[services, zookeeper]

查看所有services

[zk: localhost:2181(CONNECTED) 2] ls /services
[service-comsumer, service-provider]

查看service-comsumer的信息

[zk: localhost:2181(CONNECTED) 13] ls /services/service-comsumer
[efac86a0-1d0c-47e8-8f29-998661f50e04]

根据[id]查看相关服务的详细信息

[zk: localhost:2181(CONNECTED) 23] ls /services/service-comsumer
[f8c462b5-a6e6-4861-b20d-e48cdcdda207]
[zk: localhost:2181(CONNECTED) 24] get /services/service-comsumer/f8c462b5-a6e6-4861-b20d-e48cdcdda207
{"name":"service-comsumer","id":"f8c462b5-a6e6-4861-b20d-e48cdcdda207","address":"hkgi-PC","port":8015,"sslPort":null,"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance","id":"service-comsumer-1","name":"service-comsumer","metadata":{}},"registrationTimeUTC":1586917597776,"serviceType"
:"DYNAMIC","uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}}

相关推荐