微服务治理:CloudConsul和ZooKeeper,java面试数据库隔离级别实战

微服务治理:CloudConsul和ZooKeeper,java面试数据库隔离级别实战,第1张

服务治理:CloudConsul和ZooKeeper,java面试数据库隔离级别实战

spring-cloud-starter-netflix-hystrix

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

pId>org.springframework.boot

spring-boot-starter-actuator

org.springframework.boot

spring-boot-starter-web

其中,spring-cloud-starter-consul-discovery为Spring Cloud Consul的服务发现模块。由于这个工程也会进行健康检查,所以也需要引入actuator模块,而 spring-boot-starter-web模块必须引入,否则可能无法启动工程。

(2)新增配置文件application.yml,并编写以下内容:

spring:

application:

name: consul-clientcloud :

consul:

host: 127.8.8.1port: 8508

discovery:

serviceName: ${spring.application.name}healthCheckPath: /actuator/health

healthCheckInterval: 5s

instanceId: s p r i n g . a p p l i c a t i o n . n a m e : {spring.application.name}: spring.application.name:{vcap.application.instance_id;

${spring.application.instance_id: ${random.value}}}

server:

port: 8080

在上述配置中,spring.cloud.consul就是Consul 的基本配置,其中,host和 port 指定Consul注册中心的P和端口,即13.2.1节浏览器访问的地址; serviceName为客户端服务名; healthCheckPath为健康检查地址,本示例采用的是Actuator,因此指定为/actuator/health端点; healthCheckInterval为健康检查间隔时间,本示例为5秒,即每隔5秒会调用健康检查端点; instanceId 为实例服务,它是唯一的,服务发现都是通过instanceId来进行的。

(3)新建启动类(该类和前面介绍的启动类代码一致,此处略)。

(4)启动工程并访问localhost:8500,可以看到客户端被注册到Consul了,且每隔5秒会进行一次健康检查,如图13-3所示。

Spring Cloud ZooKeeper的使用

=========================

ZooKeeper是一个分布式的应用程序协调服务,是 Apache Hadoop 的一个子项目,最初主要是用于提供Hadoop和 Hbase的调度服务,能够解决Hadoop中统一命名、配置维护、同步服务等问题。

微服务流行以后,ZooKeeper开始被人们重视,大家发现通过ZooKeeper 也可以解决微服务中的服务治理问题。在比较流行的微服务框架中,Dubbo和 Spring Cloud都可以用ZooKeepr作为服务治理框架,尤其是 Dubbo,一般都是通过ZooKeeper实现服务治理的。

ZooKeeper的安装和部署

ZooKeeper的安装和部署也比较简单,首先从官网https://www-eu.apache.org/distzookeeper/stable中下载ZooKeeper的稳定版,然后解压进入ZooKeeper 根目录的conf 文件夹,可以看到一个名叫zoo_sample.cfg的文件,将其命名为zoo.cfg并返回上一级目录,最后进入 bin文件夹,双击zkServer.cmd文件,就可启动ZooKeeper。

ZooKeeper的默认启动端口为2181,我们可以通过修改zoo.cfg来修改启动端口,在zoo.cfg修改下面配置即可:

clientPort=2182

当然,除了修改默认端口外,我们还可以其他参数,表13-2列举了ZooKeeper的主要参数配置。

Spring Cloud集成ZooKeeper

Spring Cloud集成ZooKeeper的步骤如下。

(1)新建一个项目,命名为 spring-cloud-zookeeper,在其 pom.xml文件中添加如下代码:

org.springframework.boot

spring-boot-starter-parent< / artifactId>2.日.3.RELEASE

UTF-8

UTF-81.8

1.18.B

org.springframework.cloud

spring-cloud-dependenciesFinchley.RELEASE

pom

import

org.springframework.cloud

spring-cloud-starter-zookeeper-all

org.apache.zookeeperzookeeper< / artifactId>

org.apache.zookeeperzookeeper< / artifactId>

3.4.12

org.slf4j

slf4j- log4j12

< / dependency>

org.springframework.cloud

spring-cloud-starter-netflix-hystrix

org.springframework.boot

spring-boot-starter-web

其中, spring-cloud-starter-zookeeper-all为ZooKeeper服务的注册与发现依赖,我们可以发现,其内部通过标签将ZooKeeper依赖包去掉,在后面又通过标签添加ZooKeeper依赖。看似有点矛盾,其实不然,Spring Cloud 官方默认依赖的ZooKeeper版本是3.5.x,该版本目前为 beta@版本,而 3.4.x版本为稳定版,可以用于生产环境,因此我们需要手动添加3.4.x版本的ZooKeeper。

(2)创建应用入口类并编写启动代码。(具体代码可以参照前面Application类,本节不再列出。)

(3)新建配置文件application.yml并编写以下代码:

spring:

cloud:

zookeeper:

connect-string: localhost: 2181

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

原文地址: https://outofmemory.cn/zaji/5682406.html

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

发表评论

登录后才能评论

评论列表(0条)

保存