构建微服务架构(Zookeeper篇)
B站视频:https://www.bilibili.com/video/BV18E411x7eT?from=search&seid=13583080742381804215
原Blog笔记文档:https://blog.csdn.net/u011863024/article/details/114298270
本篇文章大部分图片用到了参考的blog笔记中的图片。
本人代码下载:下载
zookeeper是一个分布式协调工具,可以实现注册中心功能
1. Zookeeper的安装
1.1 Zookeeper 下载地址
下载地址:https://zookeeper.apache.org/
1.2 Zookeeper 安装
解压
1
| tar -vxf apache-zookeeper-3.7.0-bin.tar.gz
|
移动至合适的安装目录
1
| mv apache-zookeeper-3.7.0-bin /apache-zookeeper/
|
按照参考的配置文件创建配置文件
1
| cp /apache-zookeeper/conf/zoo_sample.cfg /apache-zookeeper/conf/zoo.cfg
|
配置文件参数如下图
启动zookeeper
查看启动是否成功
2. 支付微服务注册进 zookeeper
2.1 创建父工程 springcloud2021
2.2 springcloud2021 父工程 pom.xml 代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| <?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.sevattal.springcloud</groupId> <artifactId>springcloud2021</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <!--子模块继承之后,提供作用:锁定版本+ 子modlue不用写groupId 和 version--> <!--dependencyManagement 里只声明依赖,并不实现引入,因此子项目需要显示声明需要的依赖--> <!--统一管理jar包版本--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <junit.version>4.12</junit.version> <log4j.version>1.2.17</log4j.version> <lombok.version>1.16.18</lombok.version> <mysql.version>5.1.47</mysql.version> <druid.version>1.1.16</druid.version> <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version> <spring.boot.devtools.version>2.3.10.RELEASE</spring.boot.devtools.version> </properties> <!--子模块继承之后,提供作用:锁定版本+子module不用groupId和version--> <dependencyManagement> <dependencies> <!--spring boot 2.2.2--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--spring cloud Hoxton.SR1--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR1</version> <type>pom</type> <scope>import</scope> </dependency> <!--spring cloud alibaba 2.1.0.RELEASE--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.1.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.spring.boot.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <version>${spring.boot.devtools.version}</version> <scope>runtime</scope> <optional>true</optional> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <addResources>true</addResources> </configuration> </plugin> </plugins> </build> </project>
|
2.3 创建cloud-provider-payment8004 的 Maven 工程
2.4 cloud-provider-payment8004 工程 pom.xml 文件如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| <?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>springcloud2021</artifactId> <groupId>com.sevattal.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-provider-payment8004</artifactId> <dependencies> <!--引入自己定义的api通用包,--> <dependency> <groupId>com.sevattal.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> <!--先排除自带的zookeeper 依赖--> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency> <!--由于我这边后台装的zookeeper 版本是3.7.0 所以我这边配置的zookeeper为 3.7.0--> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.7.0</version> <!--排除 slf4j--> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </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> <!--mysql-connector-java--> <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>
|
注:如果需要指定的 zookeeper 的版本,请先将 zookeeper 的依赖去除掉,单独引入。
2.5 cloud-provider-payment8004 工程 application.yml 文件如下
1 2 3 4 5 6 7 8 9 10 11 12
| # 8004表示注册到zookeeper服务器的支付服务提供者端口号 server: port: 8004 # 服务别名----注册zookeeper到注册中心名称 spring: application: name: cloud-provider-payment cloud: zookeeper: connect-string: 192.168.10.30:2181 # 若是zookeeper集群环境,只需要改动connect-string如下 # connect-string: 192.168.10.30:2181, 192.168.10.31.2181....
|
2.6 cloud-provider-payment8004 工程 主启动类如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package com.sevattal.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /* * @EnableDiscoveryClient * 该注解用于向使用consul或者zookeeper作为注册中心注册服务 * */ @SpringBootApplication @EnableDiscoveryClient public class PaymentMain8004 { public static void main(String[] args) { SpringApplication.run(PaymentMain8004.class, args); } }
|
2.7 cloud-provider-payment8004 工程 Controller 类如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.sevattal.springcloud.controller; import com.sevattal.springcloud.service.PaymentService; import lombok.extern.slf4j.*; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.UUID; @RestController @Slf4j public class PaymentController { @Resource private PaymentService paymentService; @Value("${server.port}") private String serverPort; @RequestMapping(value = "/payment/zk") public String paymentzk(){ return "springcloud with zookeeper: " + serverPort + "\t" + UUID.randomUUID().toString(); } }
|
2.8 启动 8004 注册进 zookeeper(要先启动 zookeeper 的 server)
验证测试:浏览器 http://localhost:8004/payment/zk
验证测试2 :接着用 zookeeper 客户端操作
1 2 3
| ls / ls /services get /services/cloud-provider-payment/844c8da2-ba07-4a8a-9287-712748f681e6
|
json 格式化 get /services/cloud-provider-payment/844c8da2-ba07-4a8a-9287-712748f681e6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| { "name": "cloud-provider-payment", "id": "844c8da2-ba07-4a8a-9287-712748f681e6", "address": "localhost", "port": 8004, "sslPort": null, "payload": { "@class": "org.springframework.cloud.zookeeper.discovery.ZookeeperInstance", "id": "application-1", "name": "cloud-provider-payment", "metadata": {} }, "registrationTimeUTC": 1620257954594, "serviceType": "DYNAMIC", "uriSpec": { "parts": [ { "value": "scheme", "variable": true }, { "value": "://", "variable": false }, { "value": "address", "variable": true }, { "value": ":", "variable": false }, { "value": "port", "variable": true } ] } }
|
2.9 Zookeeper 临时还是持久节点
ZooKeeper 的服务节点是临时节点,没有Eureka那含情脉脉。
3 订单服务注册进 zookeeper
3.1 创建 cloud-consumerzk-order80 项目
3.2 cloud-consumerzk-order80 工程 pom.xml 文件如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| <?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>springcloud2021</artifactId> <groupId>com.sevattal.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-consumerzk-order80</artifactId> <dependencies> <!--引入自己定义的api通用包,--> <dependency> <groupId>com.sevattal.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> <!--先排除自带的zookeeper 依赖--> <exclusions> <exclusion> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </exclusion> </exclusions> </dependency> <!--由于我这边后台装的zookeeper 版本是3.7.0 所以我这边配置的zookeeper为 3.7.0--> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.7.0</version> <!--排除 slf4j--> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </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.3 cloud-consumerzk-order80 工程 application.yml 文件如下
1 2 3 4 5 6 7 8 9 10 11
| server: port: 80 # 服务别名----注册zookeeper到注册中心名称 spring: application: name: cloud-consumer-order cloud: zookeeper: connect-string: 192.168.10.30:2181 # 若是zookeeper集群环境,只需要改动connect-string如下 # connect-string: 192.168.10.30:2181, 192.168.10.31.2181....
|
3.4 cloud-consumerzk-order80 工程 启动类
1 2 3 4 5 6 7 8 9 10 11
| package com.sevattal.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); } }
|
3.5 cloud-consumerzk-order80 工程 业务类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.sevattal.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 { /* * 使用@LoadBalanced注解赋予RestTemplate负载均衡的能力 * 默认为轮询 * */ @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } }
|
3.5 cloud-consumerzk-order80 工程 Controller 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.sevattal.springcloud.controller; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; @RestController @Slf4j public class OrderController { /* * 中注册的微服务名称 * */ public static final String INVOKE_URL = "http://cloud-provider-payment"; @Resource private RestTemplate restTemplate; @GetMapping("/consumer/payment/zk") public String paymentInfo(){ String result = restTemplate.getForObject(INVOKE_URL + "/payment/zk",String.class); return result; } }
|
3.6.验证测试
运行 ZooKeeper 服务端,cloud-consumerzk-order80,cloud-provider-payment8004。
打开 ZooKeeper 客户端: