spring boot integrated zookeeper registry

Posted by ph0ngwh0ng on Thu, 16 Apr 2020 11:54:18 +0200

ZooKeeper is a distributed service framework and a sub project of Apache Hadoop. It is mainly used to solve some data management problems often encountered in distributed applications, such as: unified naming service, state synchronization service, cluster management, distributed application configuration item management, etc

ZooKeeper is a tree structure directory service that supports change push

In ZooKeeper, there are two types of nodes:

Machine node:

The machines that make up the cluster

Data node ZNode:

Refers to the data unit in the data model

ZooKeeper stores all the data in memory. The data model is a tree (ZNode Tree). The path divided by slash (/) is a ZNode, such as / services/customer

Each ZNode will save its own data content and a series of attribute information

Znode can be divided into:

Persistent node: once the ZNode is created, unless it is actively removed, the ZNode will always be saved on ZooKeeper

Temporary node: its life cycle is bound to the client session. Once the client session fails, all temporary nodes created by the client will be removed

1.docker installation

docker pull zookeeper:3.5

2. boot

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

3. use

provider

rely on

<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>

To configure

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

Startup class

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);
    }

}

Controller

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

Add dependency

<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>

To configure

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

Startup class

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();
    }

}

Controller

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 = "error";
        }
        return result;
    }
    
}

Start provider

Start comsumer

Test GET http://127.0.0.1:8015/feignTest

output

  hello,provider

4. view

Enter zookeeper command line in docker

docker exec -it zookeeper bash

Go to the bin directory of zookeeper

cd bin

Using the zookeeper client to access the zookeeper service

./zkCli.sh

View what is contained in the current ZooKeeper

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

View all services

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

View service comsumer information

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

View details of related services according to [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}]}}

Topics: Zookeeper Spring Docker Java