当前位置:   article > 正文

Cloud整合Zookeeper代替Eureka

Cloud整合Zookeeper代替Eureka

微服务间通信重构与服务治理笔记-CSDN博客

Zookeeper是一个分布式协调工具,可以实现注册中心功能

安装Zookeeper

随便 就用最新版本吧

进入Zookeeper 包目录

cd /usr/local/develop/

解压

tar -zxvf apache-zookeeper-3.9.1-bin.tar.gz -C /usr/local/develop

进入配置文件  

cd /usr/local/develop/apache-zookeeper-3.9.1-bin/conf

复制文件

cp zoo_sample.cfg zoo.cfg

编辑文件 

vim zoo.cfg

mkdir /usr/local/develop/apache-zookeeper-3.9.1-bin/data  这个没必要  会自动创建

 cd /usr/local/develop/apache-zookeeper-3.9.1-bin/bin

启动Zookeeper

./zkServer.sh start

安装JDK

解压

tar -zxvf /usr/local/develop/jdk-8u191-linux-x64.tar.gz -C /usr/local/develop

配置JAVA_HOME

export JAVA_HOME=/usr/local/develop/jdk1.8.0_191

export PATH=$JAVA_HOME/bin:$PATH

export CLASSPATH=.:$JAVA_HOME/lib

让环境变量生效

source /etc/profile

java -version 查看jdk版本 至此JDK安装完成

which java 查看调用的是安装在哪里的java

进入Zookeeper启动目录

cd /usr/local/develop/apache-zookeeper-3.9.1-bin/bin

启动

./zkServer.sh start

停止

./zkServer.sh stop
 

配置Zookeeper为系统服务

vim /etc/systemd/system/zookeeper.service

[Unit]
Description=Apache ZooKeeper server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/develop/apache-zookeeper-3.9.1-bin/bin/zkServer.sh start
ExecStop=/usr/local/develop/apache-zookeeper-3.9.1-bin/bin/zkServer.sh stop
User=root
Group=root
Restart=on-failure
Environment="JAVA_HOME=/usr/local/develop/jdk1.8.0_191"

[Install]
WantedBy=multi-user.target

是配置生效

systemctl daemon-reload

开机自启  看自己实际需要

systemctl enable zookeeper.service

systemctl start zookeeper.service    启动
systemctl stop zookeeper.service    停止
systemctl restart zookeeper.service    重启
systemctl status zookeeper.service   查看状态

admin.serverPort=8888指定了ZooKeeper的管理服务器端口。这个管理服务器提供了一个简单的HTTP接口,用于获取ZooKeeper服务的状态和性能指标等信息。通过访问这个端口,你可以获取到ZooKeeper实例的各种管理信息,比如运行状态、连接数、节点数量等。

默认情况下,ZooKeeper的管理界面并不提供一个全面的Web界面来浏览这些信息,而是提供了一个简单的HTTP服务,通过发送HTTP请求到这个端口,可以获取到JSON格式的状态信息。

IP:8888/commands/stat

关闭Zookeeper服务

systemctl stop zookeeper.service

systemctl start zookeeper.service

Docker安装Zookeeper

docker run -d --name zookeeper --privileged=true -p 2181:2181  -v /mydata/zookeeper/data:/data -v /mydata/zookeeper/conf:/conf -v /mydata/zookeeper/logs:/datalog zookeeper:3.5.7
 

后面补

创建支付模块(生产者)

重新构建支付模块

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>SpringCloud</artifactId>
  7. <groupId>org.example</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>cloud-provider-payment8084</artifactId>
  12. <properties>
  13. <maven.compiler.source>8</maven.compiler.source>
  14. <maven.compiler.target>8</maven.compiler.target>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>org.example</groupId>
  19. <artifactId>cloud-api-commons</artifactId>
  20. <version>${project.version}</version>
  21. </dependency>
  22. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. </dependency>
  27. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-actuator</artifactId>
  31. </dependency>
  32. <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zookeeper-discovery -->
  33. <dependency>
  34. <groupId>org.springframework.cloud</groupId>
  35. <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
  36. </dependency>
  37. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
  38. <dependency>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-devtools</artifactId>
  41. <scope>runtime</scope>
  42. <optional>true</optional>
  43. </dependency>
  44. <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
  45. <dependency>
  46. <groupId>org.projectlombok</groupId>
  47. <artifactId>lombok</artifactId>
  48. <optional>true</optional>
  49. </dependency>
  50. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
  51. <dependency>
  52. <groupId>org.springframework.boot</groupId>
  53. <artifactId>spring-boot-starter-test</artifactId>
  54. <scope>test</scope>
  55. </dependency>
  56. </dependencies>
  57. </project>

application.yml

  1. server:
  2. port: 8084
  3. spring:
  4. application:
  5. name: cloud-provider-payment
  6. cloud:
  7. zookeeper:
  8. connect-string: xxx.xx.xxx.x:2181

启动类

控制器

  1. package com.exempla.pay01.controller;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import java.util.UUID;
  8. /**
  9. * @author hrui
  10. * @date 2024/3/2 6:07
  11. */
  12. @RestController
  13. @Slf4j
  14. @RequestMapping("/payment")
  15. public class PaymentController {
  16. @Value("${server.port}")
  17. private String serverPort;
  18. @GetMapping(value = "/zk")
  19. public String paymentzk(){
  20. return "springcloud with zookeeper:"+serverPort+"\t"+ UUID.randomUUID().toString();
  21. }
  22. }

启动8084 注册进Zookeeper   lombok找不到  加个版本

记得Zookeeper服务器开通安全组

有可能版本冲突  解决办法

cd /usr/local/develop/apache-zookeeper-3.9.1-bin/bin

./zkCli.sh

ls /

ls /services

ls /services/cloud-provider-payment

ls /services/cloud-provider-payment/04bbfd46-50e0-462a-bd22-b8083cc445cf

get /services/cloud-provider-payment/04bbfd46-50e0-462a-bd22-b8083cc445cf

上面这个JSON串 就是微服务注册相关的信息

quit

也可以

IP:8888/commands/stat 看下

访问接口  可以

localhost:8084/payment/zkicon-default.png?t=N7T8http://localhost:8084/payment/zk

创建订单模块(消费者)

 

pom.xml

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.atguigu.springcloud</groupId>
  4. <artifactId>cloud-api-commons</artifactId>
  5. <version>${project.version}</version>
  6. </dependency>
  7. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-web</artifactId>
  11. </dependency>
  12. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-actuator</artifactId>
  16. </dependency>
  17. <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-zookeeper-discovery -->
  18. <dependency>
  19. <groupId>org.springframework.cloud</groupId>
  20. <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
  21. </dependency>
  22. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-devtools</artifactId>
  26. <scope>runtime</scope>
  27. <optional>true</optional>
  28. </dependency>
  29. <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
  30. <dependency>
  31. <groupId>org.projectlombok</groupId>
  32. <artifactId>lombok</artifactId>
  33. <optional>true</optional>
  34. </dependency>
  35. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-test</artifactId>
  39. <scope>test</scope>
  40. </dependency>
  41. </dependencies>

启动类

application.yml

  1. server:
  2. port: 80
  3. spring:
  4. application:
  5. name: cloud-consumer-order
  6. cloud:
  7. zookeeper:
  8. connect-string: xxx.xx.xx.x:2181

配置RestTemplate和负载均衡

控制器

启动服务

localhost/consumer/payment/zk

关于@EnableDiscoveryClient 注解  早期版本确实生产者和消费者启动类要加  后来就不需要了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/195258
推荐阅读
相关标签
  

闽ICP备14008679号