Spring Cloud (4): Zookeeper service registration and discovery

1. Zookeeper service registration and discovery

1.1 Install zookeeper using docker

Please check this blog for docker installation tutorial : docker installation tutorial

  • Download mirror
docker pull zookeeper:3.4.9

Insert picture description here

  • Start the container and add the mapping:
docker run --privileged=true -d --name zookeeper --publish 2181:2181  -d zookeeper:3.4.9
  • Check if it is activated
    Insert picture description here

1.2 SpringCloud integrates Zookeeper instead of Eureka

Zookeeper is a distributed coordination tool that can realize the registry function.
Turn off the Linux server firewall and start the zookeeper server.
Zookeeper server replaces the Eureka server, and zk serves as the service registry.

service provider

  • Create a new cloud-provider-payment8004 module
  • Change pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-demo</artifactId>
        <groupId>com.lele.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-provider-payment8004</artifactId>

    <dependencies>
        <!-- SpringBoot 整合 zookeeper 客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
        <!-- 引入自定义的api通用包,可以使用Payment支付Entity -->
        <dependency>
            <groupId>com.lele.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-actuator</artifactId>-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>org.mybatis.spring.boot</groupId>-->
<!--            <artifactId>mybatis-spring-boot-starter</artifactId>-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>com.alibaba</groupId>-->
<!--            <artifactId>druid-spring-boot-starter</artifactId>-->
<!--            <version>1.1.10</version>-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>mysql</groupId>-->
<!--            <artifactId>mysql-connector-java</artifactId>-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-jdbc</artifactId>-->
<!--        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

</project>
  • 建 yml
#8004表示注册到zookeeper服务器的支付服务提供者端口号
server:
  port: 8004

#服务别名----注册zookeeper到注册中心名称
spring:
  application:
    name: cloud-provider-payment
  cloud:
    zookeeper:
      connect-string: localhost:2181  #zookeeper所在的主机ip地址
  • Main start class
package com.lele.springcloud;

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

/**
 * @author: lele
 * @date: 2021/3/9 22:24
 * @description:
 */
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8004 {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(PaymentMain8004.class, args);
    }
}
  • Controller
package com.lele.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @author: lele
 * @date: 2021/3/10 7:00
 * @description:
 */
@RestController
@Slf4j
public class PaymentController {
    
    

    @Value("${server.port")
    private String serverPort;

    @RequestMapping(value = "/payment/zk")
    public String paymentzk() {
    
    
        return "spring cloud with zookeeper: " +serverPort+"\t"+ UUID.randomUUID().toString();
    }
}

  • Start 8004 to register into zookeeper
    Insert picture description here

  • Solve the problem of zookeeper version jar package conflict
    Insert picture description here

  • Modify pom

<!-- SpringBoot 整合 zookeeper 客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            <!-- 先排除自带的 zookeeper3.5.3 -->
            <exclusions>
                <exclusion>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 添加zookeeper3.4.9版本 -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.9</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

Insert picture description here

  • 访问:http://localhost:8003/payment/zk
    Insert picture description here
  • Verification Test:
docker exec -it 容器id /bin/bash
cd bin/
zkCli.sh

ls /
ls /services

Insert picture description here
Insert picture description here
Here the name of node is the name of springapplication configured by yml

  • Verification test 2:
ls /services/cloud-provider-payment

Insert picture description here
Insert picture description here

get /services/cloud-provider-payment/a855f6d9-cb61-478e-919d-660d62e9f951

Insert picture description here
Thinking: Is the
service node a temporary node or a persistent node:

It is a temporary node.

Service consumer

  • New cloud-consumerzk-order80 module
  • Change pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-demo</artifactId>
        <groupId>com.lele.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consumerzk-order80</artifactId>

    <dependencies>
        <!-- SpringBoot 整合 zookeeper 客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            <!-- 先排除自带的 zookeeper3.5.3 -->
            <exclusions>
                <exclusion>
                    <groupId>org.apache.zookeeper</groupId>
                    <artifactId>zookeeper</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 添加zookeeper3.4.9版本 -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.9</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- 引入自定义的api通用包,可以使用Payment支付Entity -->
        <dependency>
            <groupId>com.lele.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>
  • marl
server:
  port: 80

spring:
  application:
    name: cloud-consumer-order
  cloud:
  #注册到zookeeper地址
    zookeeper:
      connect-string: localhost:2181  #zookeeper所在的主机ip地址
  • Main start class
package com.lele.springcloud;

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

/**
 * @author: lele
 * @date: 2021/3/10 22:16
 * @description:
 */
@SpringBootApplication
@EnableDiscoveryClient
public class OrderZKMain80 {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(OrderZKMain80.class, args);
    }
}
  • Configure RestTemplate:
package com.lele.springcloud.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @author: lele
 * @date: 2021/3/10 22:21
 * @description:
 */
@Configuration
public class ApplicationContextConfig {
    
    
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
    
    
        return new RestTemplate();
    }
}
  • controller
package com.lele.springcloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author: lele
 * @date: 2021/3/10 22:26
 * @description:
 */
@RestController
@Slf4j
public class OrderZKController {
    
    

    public static final String INVOKE_URL = "http://cloud-provider-payment";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping(value = "/consumer/payment/zk")
    public String paymentInfo() {
    
    
        String result = restTemplate.getForObject(INVOKE_URL+"/payment/zk", String.class);
        return result;
    }
}

Start 80 to register into zookeeper
Insert picture description here

  • 访问:
    http://localhost/consumer/payment/zk
    Insert picture description here

Guess you like

Origin blog.csdn.net/houwanle/article/details/114602137