Zookeeper实现注册中心—SpringCloud(H版)微服务学习教程(14)

Java技术 潘老师 7个月前 (03-25) 469 ℃ (0) 扫码查看

我们知道Eureka官方已经停更,现在技术选型时基本不会再用Eureka,我们也可以使用Zookeeper代替Eureka来实现注册中心功能。接下来我们来来看看实现替代的过程。

一、Zookeeper介绍

ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,可以实现注册中心功能。

二、Zookeeper环境准备

我们需要准备安装好Zookeeper的虚拟机(我们这里演示使用单机版,注意要开启2181端口),具体参考:

Linux如何安装Zookeeper(单机版和集群版模式)

第1步:环境准备 1)要在Linux上安装Zookeeper,首先我们要准备好系统环境,即先安装好Vmware […]

三、实现注册中心功能

注意:在此演示我们忽略掉数据库的增删改查,因此,相关依赖和service、dao、mapper就不写了,如果你需要可以参考之前的eureka部分

第1步:实现服务提供者

1)创建Module
我们在mscloud下新建名为cloud-provider-payment8004的Module
2)修改pom
我们修改8004的pom.xml如下(参考payment8001的,去掉数据库依赖,将eureka依赖改为zookeeper依赖):

<?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>mscloud</artifactId>
        <groupId>com.panziye.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-provider-payment8004</artifactId>
    <dependencies>
        <!--   zookeeper     -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>

        <!--  引入自定义的api通用包,可以使用支付Payment等Entity      -->
        <dependency>
            <groupId>com.panziye.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.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>

3)写yml
在该模块的resources新建application.yml,参考payment8001配置(剔除数据库配置),如下:

server:
  port: 8004

spring:
  application:
    name: cloud-payment-service
  cloud:
    zookeeper:
      # zookeeper服务器地址
      connect-string: 192.168.61.128:2181

4)主启动类
com.panziye.springcloud包下新建PaymentMain8004主启动类:

package com.panziye.springcloud;

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

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

5)业务类
这里我们业务类只创建controller演示即可,在com.panziye.springcloud.controller包中新建PaymentController

package com.panziye.springcloud.controller;

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

@RestController
@Slf4j
public class PaymentController {
    @Value("${server.port}")
    private String serverPort;
    @GetMapping(value = "/payment/zk")
    public String zk(){
        return "payment zookeeper server port:"+serverPort+",随机数:"+ Math.random();
    }
}

6)测试
1)我们先启动Linux上的Zookeeper
2)我们再启动payment8004,如果发现如下报错,说明没有开启2181端口,去开启即可。
Zookeeper实现注册中心—SpringCloud(H版)微服务学习教程(14)
3)浏览器访问http://localhost:8004/payment/zk
Zookeeper实现注册中心—SpringCloud(H版)微服务学习教程(14)
4)查看zookeeper
a)执行bin目录中如下指令进入zk客户端:

zkCli.sh

b)执行如下指令查看节点,发现成功注册:

ls /
ls /services
/services/cloud-payment-service

发现除了zookeeper自身节点还多了个services节点,里面有cloud-payment-service,再里面有一个序列号,如下:
Zookeeper实现注册中心—SpringCloud(H版)微服务学习教程(14)
c)查看序列号

get /services/cloud-payment-service/929917f7-c1be-4c9c-abee-7f77ef8d77ce

Zookeeper实现注册中心—SpringCloud(H版)微服务学习教程(14)
JSON串格式化如下:

{
    "name": "cloud-payment-service",
    "id": "929917f7-c1be-4c9c-abee-7f77ef8d77ce",
    "address": "XTZJ-2021DICDMM",
    "port": 8004,
    "sslPort": null,
    "payload": {
        "@class": "org.springframework.cloud.zookeeper.discovery.ZookeeperInstance",
        "id": "application-1",
        "name": "cloud-payment-service",
        "metadata": {
            "instance_status": "UP"
        }
    },
    "registrationTimeUTC": 1616640042560,
    "serviceType": "DYNAMIC",
    "uriSpec": {
        "parts": [{
            "value": "scheme",
            "variable": true
        }, {
            "value": "://",
            "variable": false
        }, {
            "value": "address",
            "variable": true
        }, {
            "value": ":",
            "variable": false
        }, {
            "value": "port",
            "variable": true
        }]
    }
}
注意:zookeeper中的节点是临时节点,不是持久节点,因为如果你关闭8004服务,zookeeper会很快移除该节点信息

第2步:实现服务消费者

参考cloud-consumer-order80

1)建Module
mscloud下新建cloud-consumer-zk-order80模块
2)改pom
参考cloud-consumer-order80的将eureka依赖改为zookeeper依赖

<?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>mscloud</artifactId>
        <groupId>com.panziye.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consumer-zk-order80</artifactId>

    <dependencies>
        <!--   zookeeper    -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>

        <!--  引入自定义的api通用包,可以使用支付Payment等Entity      -->
        <dependency>
            <groupId>com.panziye.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.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>

3)写yml

server:
  port: 80

spring:
  application:
    name: cloud-order-zk-service
  cloud:
    zookeeper:
      # zookeeper服务器地址
      connect-string: 192.168.61.128:2181

4)主启动类
com.panziye.springcloud包中新建主启动类OrderZkMain80

package com.panziye.springcloud;

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

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

5)业务类
a)在com.panziye.springcloud包中新建cofnig包,在config中新建ApplicationContextCofig如下:

package com.panziye.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;

@Configuration
public class ApplicationContextCofig {
    /**
     * 配置RestTemplate
     * @return
     */
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

b)在com.panziye.springcloud包中新建controller包,在controller中新建OrderController

package com.panziye.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;

@RestController
@Slf4j
public class OrderController {
    // 注册中心中服务地址-zookeeper中service名称
    public static final String PAYMENT_URL = "http://cloud-payment-service";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/zk")
    public String zk(){
        return restTemplate.getForObject(PAYMENT_URL+"/payment/zk",String.class);
    }

}

6)测试
a)启动payment8004和zk-order-80,查看zookeeper发现都注册进来了:
Zookeeper实现注册中心—SpringCloud(H版)微服务学习教程(14)
b)访问http://localhost/consumer/payment/zk
Zookeeper实现注册中心—SpringCloud(H版)微服务学习教程(14)


版权声明:本站所有文章,如无特殊说明,均为本站原创。全部下载资源版权归原作者所有。任何个人或组织,若未征得本站同意,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。若需转载请注明文章来源。
本文链接:Zookeeper实现注册中心—SpringCloud(H版)微服务学习教程(14)
喜欢 (0)
请潘老师喝杯Coffee吧!】
分享 (0)
用户头像
发表我的评论
取消评论
表情 贴图 签到 代码

Hi,您需要填写昵称和邮箱!

  • 昵称【必填】
  • 邮箱【必填】
  • 网址【可选】

您也可以 微信登录 来发表评论!