Spring cloud入门-6:服务注册中心-Zookeeper单机环境构建

Spring cloud入门-6:服务注册中心-Zookeeper单机环境构建,第1张

Spring cloud入门-6:服务注册中心-Zookeeper单机环境构建

Spring cloud入门-6:服务注册中心-Zookeeper单机环境构建

1、安装zookeeper

1.1 下载zookeeper1.2 安装zookeeper 2、构建服务提供者模块

2.1 建module2.2 改pom2.3 写yml2.4 主启动2.5 业务类2.6 启动8004服务注册到zookeeper2.7 测试2.8 查看节点信息2.9 临时节点or持久节点 3、构建服务消费者

3.1 建module3.2 改pom3.3 写yml3.4 主启动3.5 业务类3.6 测试

1、安装zookeeper

  前面几篇已经完成了基于eureka服务注册与发现,本篇开始完成基于zookeeper的服务注册。demo的架构与前面一致,只是将服务注册中心换成了zookeeper。

1.1 下载zookeeper

下载地址:https://downloads.apache.org/zookeeper/
我下载的是3.5.9。

1.2 安装zookeeper

首先是创建zookeeper的文件夹:

mkdir /home/koping/Downloads/soft/zookeeper

然后进入下载文件夹(/home/koping/Downloads)解压文件:

tar -zxvf apache-zookeeper-3.5.9-bin.tar.gz

解压完成后,将解压后的文件夹放入zookeeper文件夹中:

mv apache-zookeeper-3.5.9-bin /home/koping/Downloads/soft/zookeeper/

然后创建zookeeper的data和log文件夹:

mkdir /home/koping/Downloads/soft/zookeeper/apache-zookeeper-3.5.9-bin/data
mkdir /home/koping/Downloads/soft/zookeeper/apache-zookeeper-3.5.9-bin/log

进入conf文件夹修改创建配置文件:zoo.cfg

cd soft/zookeeper/apache-zookeeper-3.5.9-bin/conf/
cp zoo_sample.cfg zoo.cfg 

修改配置文件zoo.cfg

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
#dataDir=/tmp/zookeeper
dataDir=/home/koping/Downloads/soft/zookeeper/apache-zookeeper-3.5.9-bin/data
dataLogDir=/home/koping/Downloads/soft/zookeeper/apache-zookeeper-3.5.9-bin/log
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#

tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。dataDir: Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。dataLogDir: Zookeeper 保存日志文件的目录。clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口(默认2181),Zookeeper 会监听这个端口,接受客户端的访问请求。

修改号配置文件后,进入bin目录

cd ../bin/

启动zookeeper:./zkServer.sh start

koping@koping-HP:~/Downloads/soft/zookeeper/apache-zookeeper-3.5.9-bin/bin$ ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /home/koping/Downloads/soft/zookeeper/apache-zookeeper-3.5.9-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

显示STARTED后,zookeeper就安装成功了。

然后连接客户端:

./zkCli.sh

连接上了之后,查看根节点,里面只有zookeeper节点;
再查看zookeeper节点,里面只有config节点、quote节点:

[zk: localhost:2181(CONNECTED) 2] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 3] ls /zookeeper 
[config, quota]

2、构建服务提供者模块

  这里不再使用之前的订单服务模块8001和8002了,现在重新构建一个模块:cloud-provider-payment-8004。
  步骤和之前的博文基本一致,可以参考之前构建模块的文章:Spring cloud入门-2:构建订单模块服务

2.1 建module

  新建cloud-provider-payment-8004模块:

2.2 改pom

可以参考之前博文的pom文件,将之前的eureka换成zookeeper即可:

        
            org.springframework.cloud
            spring-cloud-starter-zookeeper-discovery
        

pom文件如下:



    
        cloud2021
        org.example.springcloud
        1.0-SNAPSHOT
    
    4.0.0

    cloud-provider-payment-8004

    
        8
        8
    

    
        
        
            org.springframework.cloud
            spring-cloud-starter-zookeeper-discovery
        
        
        
            org.example.springcloud
            cloud-api-commons
            1.0-SNAPSHOT
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.projectlombok
            lombok
            true
            1.14.0
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

2.3 写yml

配置文件中,常规的要写:
1)服务的端口号
2)服务在注册中心里的别名
3)注册中心的IP地址和端口号。我的是linux,通过ifconfig命令就可以得到我的机器的IP地址,端口号是刚刚设置默认的2181

server.port=8004

# 服务别名--注册到zookeeper的服务名称
spring.application.name=cloud-provider-payment
# zookeeper的服务器IP和端口号
spring.cloud.zookeeper.connect-string=192.168.5.112:2181
2.4 主启动

  主启动类上还需要加上@EnableDiscoveryClient的注解,该注解用于向使用consul或者zookeeper作为注册中心时注册服务

package com.example.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);
    }
}
2.5 业务类

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

@RestController
@Slf4j
public class PaymentController {
    @Value("${server.port}")
    private String serverPort;

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

2.6 启动8004服务注册到zookeeper

  启动PaymentMain8004服务:

8004服务启动后,再进入zookeeper客户端,查看是否成功注册进zookeeper中。
可以看到现在根节点下面多了一个services节点,services节点下面现在有一个cloud-provicer-payment节点,也就是我们刚刚构建的8004服务的别名。

[zk: localhost:2181(CONNECTED) 4] ls /
[services, zookeeper]
[zk: localhost:2181(CONNECTED) 5] ls /services 
[cloud-provider-payment]

2.7 测试

  在zookeeper客户端中看到了我们的服务后,然后再测试下刚刚构建的接口,看下是否成功返回数据。
  如下图,通过调用接口:http://localhost:8004/payment/zookeeper,成功返回了结果。
  

2.8 查看节点信息

  通过zookeeper客户端,可以看到zookeeper节点下面有了刚刚注册的服务别名,接下来获取该服务的实例信息:
  通过:ls /services/cloud-provider-payment ,查看该服务下的实例名称;
  然后通过get /services/cloud-provider-payment/98612c4f-7edb-4e82-99ae-953a79c4ca72 ,查看该服务实例的详细信息。

[zk: localhost:2181(CONNECTED) 5] ls /services 
[cloud-provider-payment]
[zk: localhost:2181(CONNECTED) 6] ls /services/cloud-provider-payment 
[98612c4f-7edb-4e82-99ae-953a79c4ca72]
[zk: localhost:2181(CONNECTED) 7] get /services/cloud-provider-payment/98612c4f-7edb-4e82-99ae-953a79c4ca72 
{"name":"cloud-provider-payment",
"id":"98612c4f-7edb-4e82-99ae-953a79c4ca72",
"address":"192.168.5.112",
"port":8004,
"sslPort":null,
"payload":{"@class":"org.springframework.cloud.zookeeper.discovery.ZookeeperInstance",
       "id":"application-1",
       "name":"cloud-provider-payment",
       "metadata":{}
       },
"registrationTimeUTC":1641651635738,
"serviceType":"DYNAMIC",
"uriSpec":{"parts":[{"value":"scheme","variable":true},{"value":"://","variable":false},{"value":"address","variable":true},{"value":":","variable":false},{"value":"port","variable":true}]}}

我们可以得到服务实例的ip地址是:192.168.5.112,得到服务实例的端口是:8004。

2.9 临时节点or持久节点

  上一篇博文中可以看到,注册中心Eureka具有自我保护机制,且某一个服务节点非正常宕机后,eureka也会保留一段时间后才会移除该节点。
  那么zookeeper是否也会和eureka一样可以一直保留服务节点呢?还是当服务不发送心跳之后很快就移除?接下来,快速关闭下订单服务进行测试。
  几秒钟后,通过zookeeper客户端可以看到,服务节点下面已经没有节点了。因此可以看到zookeeper和eureka不一样,它是临时节点,即服务不可用了之后,zookeeper一段时间未收到心跳后就会移除该服务。

[zk: localhost:2181(CONNECTED) 26] ls /services 
[]
3、构建服务消费者 3.1 建module

新建服务消费者:cloud-consumer-order-zookeeper-80

3.2 改pom

服务消费者模块和服务提供者模块的依赖项基本一致。



    
        cloud2021
        org.example.springcloud
        1.0-SNAPSHOT
    
    4.0.0

    cloud-consumer-order-zookeeper-80

    
        8
        8
    

    
        
        
            org.springframework.cloud
            spring-cloud-starter-zookeeper-discovery
        
        
        
            org.example.springcloud
            cloud-api-commons
            1.0-SNAPSHOT
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.projectlombok
            lombok
            true
            1.14.0
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

3.3 写yml

配置文件可参考服务提供8004模块:

server.port=8080

# 服务别名--注册到zookeeper的服务名称
spring.application.name=cloud-consumer-order
# zookeeper的服务器IP和端口号
spring.cloud.zookeeper.connect-string=192.168.5.112:2181
3.4 主启动
package com.example.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class OrderZookeeperMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderZookeeperMain80.class, args);
    }
}
3.5 业务类

首先创建远程调用bean:restTemplate,代码与eureka中的消费订单模块一致:

接下来构建业务类,也就是调用刚刚的/payment/zookeeper接口:

package com.example.springcloud.controller;

import lombok.experimental.PackagePrivate;
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;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@Slf4j
public class OrderZookeeperController {
    public static final String INVOKE_URL = "http://cloud-consumer-order";

    @Resource
    private RestTemplate restTemplate;

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

3.6 测试

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/zaji/5702007.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存