提示:本文主要对Zookeeper服务注册与发现进行总结
前言
提示:这里可以添加本文要记录的大概内容:
本文主要对Zookeeper服务注册与发现进行总结
提示:以下是本篇文章正文内容
一、Eureka停止更新了你怎么办
https://github.com/Netflix/eureka/wiki
二、Linux下安装zookeeper
1、简介
从字义上解释就是“动物管理员”。ZooKeeper是一种集中式服务,用于维护配置信息,命名,提供分布式同步和提供组服务。
2、Zookeeper应用场景
- 集群统一配置管理
- 集群统一命名服务
- 集群统一管理
- 负载均衡
3、Zookeeper安装教程
3.1 获取安装包
- 1.使用wget命令获取
wget https://dlcdn.apache.org/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz
- 2.从官网下载
官网地址:zookeeper官网:zookeeper
当前稳定版本是3.7.1,但是安装存在问题,所以使用的还是-3.6.1-bin版本
4、 zookeeper的安装
(1)、连接服务器,输入用户名和密码后登录
(2)、创建新的文件夹
cd /usr/local/
mkdir zookeeper
cd zookeeper
(3)、进入创建的文件,上传下载下来的zookeeper
压缩包,
将 ZooKeeper
安装包解压到 /usr/local/zookeeper
中即可
cd zookeeper
tar -zxvf apache-zookeeper-3.6.3.tar.gz -C ./
解压完之后, /usr/local/zookeerper
⽬录中会出现⼀个 apache-zookeeper-3.6.1-bin
的⽬录
(4)、创建DATA目录
这⾥直接在 /usr/local/zookeeper/apache-zookeeper-3.6.1-bin
⽬录中创建⼀个 data
⽬录
等下该 data
⽬录地址要配到 ZooKeeper
的配置⽂件中:
(5)、创建配置⽂件并修改
进⼊到 zookeeper 的 conf ⽬录,复制 zoo_sample.cfg 得到 zoo.cfg :
cd conf/
cp zoo_sample.cfg zoo.cfg
修改配置⽂件 zoo.cfg
,将其中的dataDir
修改为上⾯刚创建的data
⽬录,其他选项可以按需配置
1、vim zoo.cfg
2、输入i进入编辑模式
3、修改配置⽂件 `zoo.cfg` ,将其中的` dataDir `修改为上⾯刚创建的` data `⽬录,其他选项可以按需配置
dataDir=/usr/local/zookeeper/apache-zookeeper-3.6.1-bin/data
4、esc退出编辑模式,键入:wq保存配置
这里截图懒得截了,把路径改成3.6.1-bin下的即可,其他版本没安装好
(6)、启动ZOOKEEPE
./bin/zkServer.sh start
启动后可以通过如下命令来检查启动后的状态:
查看zookeeper状态:
./bin/zkServer.sh status
这时出现一个问题,提示:It is probably not running
初步怀疑是防火墙原因:
1、如果是iptables防火墙,
查看防火墙状态service iptables status
停止防火墙service iptables stop
2、如果是firewall防火墙,
查看防火墙状态systemctl status firewalld
停止防火墙service firewalld stop
此时再去查看zookeeper状态:
还是失败的,百度了一下,说不能用 apache-zookeeper-{version}.tar.gz ,
而是应该用apache-zookeeper-{version}-bin.tar.gz(文件名包含bin)
上面所有的步骤,重新再走一遍,但是还是存在问题,
直接换版本为3.6.1-bin版本,再去查看状态
./bin/zkServer.sh status
可以发现:zookeeper默认端口为2181
(7)、配置环境变量
编辑配置⽂件:
vim /etc/profile
尾部加⼊ ZooKeeper
的 bin
路径配置即可
export ZOOKEEPER_HOME=/usr/local/zookeeper/apache-zookeeper-3.6.1-bin
export PATH=$PATH:$ZOOKEEPER_HOME/bin
最后执⾏ source /etc/profile
使环境变量⽣效即可。
source /etc/profile
(8)、设置开机自启
⾸先进⼊ /etc/rc.d/init.d
⽬录,创建⼀个名为 zookeeper
的⽂件,并赋予执⾏权限
[root@localhost ~]# cd /etc/rc.d/init.d/
[root@localhost init.d]# touch zookeeper
[root@localhost init.d]# chmod +x zookeeper
接下来编辑 zookeeper
⽂件,并在其中加⼊如下内容:
vim zookeeper
#!/bin/bash
#chkconfig:- 20 90
#description:zookeeper
#processname:zookeeper
ZOOKEEPER_HOME=/usr/local/zookeeper/apache-zookeeper-3.6.1-bin
export JAVA_HOME=/usr/local/java/jdk1.8.0_161 # 此处根据你的实际情况更换对
应
case $1 in
start) su root $ZOOKEEPER_HOME/bin/zkServer.sh start;;
stop) su root $ZOOKEEPER_HOME/bin/zkServer.sh stop;;
status) su root $ZOOKEEPER_HOME/bin/zkServer.sh status;;
restart) su root $ZOOKEEPER_HOME/bin/zkServer.sh restart;;
*) echo "require start|stop|status|restart" ;;
esac
先来看启动及状态,接着看关闭及状态
最后加⼊开机启动即可:
chkconfig --add zookeeper
chkconfig zookeeper on
并查看是否添加成功:
chkconfig --list
三、注册中心zookeeper
1 、概述
关闭防火墙:
systemctl stop firewalld
查看防火墙状态
systemctl status firewalld
查看ip:
我用的是腾讯云服务器,保证你当前ping 阿里云服务器能够ping通即可
2、创建服务提供者
2.1、新建module cloud-provider-payment8004
2.2、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>cloud2022</artifactId>
<groupId>com.atguidu.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-payment8004</artifactId>
<dependencies>
<!--SpringBoot整合web组件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入自己定义的api通用包,可以使用Payment支付Entity-->
<dependency>
<groupId>com.atguidu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--springBoot整合zookeeper客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</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>
</dependency>
</dependencies>
</project>
2.3、yml
# 8004表示注册到zookeeper服务器的支付服务提供者端口号
server:
port: 8004
# 服务名--注册到zookeeper的服务名称
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 10.0.16.8:2181 #2181是zookeeper配置文件中配置的端口号
zookeeper配置文件信息:
2.4、主启动类
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient //该注解用于向使用zookeeper或者consul作为注册中心时注册服务
public class PaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8004.class, args);
}
}
2.5、编写controller
package com.atguigu.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/zk")
public String paymentzk() {
return "springcloud with zookeeper: " + serverPort + "\t" + UUID.randomUUID().toString();
}
}
2.6、启动8004注册进zookeeper
1.启动zookeeper
2.启动主启动类
有可能会因为jar包与zookeeper版本问题导致启动报错。
报错:
要是存在该问题,则需要将依赖自带的zookeeper jar包排除掉,重新引入与安装的zookeeper版本一致的zookeeper jar包
<!--SpringBoot整合Zookeeper客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<exclusions>
<!--先排除自带的zookeeper3.5.3-->
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加zookeeper3.4.6版本 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<!--和服务器版本保持一致 -->
<version>3.6.1</version>
</dependency>
clean install
再启动主启动类
上述问题我没遇到过,遇到的是另一个问题。
解决:
我搜了大多说防火墙·没有关但是我的确定是关的
有点我疏忽了因为我用的腾讯云的服务器 ,我刚装了zookeeper 所以能设置,端口
也就是 腾讯云端口吗,没开 所以一直报此类的错,打开端口解决
注意:
application.yml里,我连接的是腾讯云服务器,服务器地址应该是你购买的,而不是ifconfig拿到的ip地址!!!
出现如下提示,说明微服务的提供者 支付模块8004成功注册到了zookeeper
2.7、 cloud-provide-payment模块注册zookeeper验证
http://localhost:8004/payment/zk
ls /
ls /payment
ls /payment/cloud-provider-payment
使用json转换
{
"name": "cloud-provider-payment",
"id": "d00c5bff-c1a1-46a3-b242-476bf0beade6",
"address": "DESKTOP-KDCOMM3",
"port": 8004,
"sslPort": null,
"payload": {
"@class": "org.springframework.cloud.zookeeper.discovery.ZookeeperInstance",
"id": "application-1",
"name": "cloud-provider-payment",
"metadata": {}
},
"registrationTimeUTC": 1657156995419,
"serviceType": "DYNAMIC",
"uriSpec": {
"parts": [{
"value": "scheme",
"variable": true
}, {
"value": "://",
"variable": false
}, {
"value": "address",
"variable": true
}, {
"value": ":",
"variable": false
}, {
"value": "port",
"variable": true
}]
}
}
2.8 、服务节点是临时节点还是持久节点
每个微服务作为zNode节点放到了zookeeper中
思考:服务节点是临时节点还是持久节点?
结论:节点是临时性的。服务注册到zookeeper中,当服务离线又重新注册到zookeeper中,服务的id是不一样的。
四、服务消费者创建与注册到zookeeper
1、新建cloud-consumerzk-order80
1、pom.xml
<?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>cloud2022</artifactId>
<groupId>com.atguidu.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumerzk-order80</artifactId>
<dependencies>
<!--SpringBoot整合web组件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--SpringBoot整合Zookeeper客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<exclusions>
<!--先排除自带的zookeeper3.5.3-->
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加zookeeper3.6.1版本 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<!-- 版本和服务器上的保持一致-->
<version>3.6.1</version>
</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>
</dependency>
</dependencies>
</project>
2、application.yml
# 80表示注册到zookeeper服务器的支付服务提供者端口号
server:
port: 80
# 服务名--注册到zookeeper的服务名称
spring:
application:
name: cloud-consumer-order
cloud:
zookeeper:
connect-string: 124.222.116.113:2181 #2181是zookeeper配置文件中配置的端口号
3、主启动类
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient //该注解用于向使用zookeeper或者consul作为注册中心时注册服务
public class OrderZKMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderZKMain80.class, args);
}
}
4、业务类(配置Bean、Controller)
config/ApplicationContextConfig.java:
package com.atguigu.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 ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
controller/OrderZKController.java:
package com.atguigu.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 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;
}
}
5、启动80,重启zookeeper,查看结果:
说明orderZK80和provider8004都注册到了zookeeper上
http://localhost/consumer/payment/zk