高可用注册中心
在微服务中,注册中心非常核心,可以实现服务治理,如果一旦注册出现故障的时候,可能会导致整个微服务无法访问,在这时候就需要对注册中心实现高可用集群模式。
Eureka高可用原理
默认情况下Eureka是让服务注册中心,不注册自己
###因为该应用为注册中心,不会注册自己
register-with-eureka: true
###不需要去注册中心上检索服务
fetch-registry: true
Eureka高可用实际上将自己作为服务向其他服务注册中心注册自己,这样就可以形成一组相互注册的服务注册中心,从而实现服务清单的互相同步,达到高可用效果。
Eureka集群环境搭建
Eureka01配置
###服务端口号
server:
port: 8100
###eureka 基本信息配置
spring:
application:
name: eureka-server
eureka:
instance:
###注册到eurekaip地址
hostname: 127.0.0.1
client:
serviceUrl:
defaultZone: http://127.0.0.1:8200/eureka/
###因为自己是为注册中心,不需要自己注册自己
register-with-eureka: true
###因为自己是为注册中心,不需要检索服务
fetch-registry: true
Eureka02配置
###服务端口号
server:
port: 8200
###eureka 基本信息配置
spring:
application:
name: eureka-server
eureka:
instance:
###注册到eurekaip地址
hostname: 127.0.0.1
client:
serviceUrl:
defaultZone: http://127.0.0.1:8100/eureka/
###因为自己是为注册中心,不需要自己注册自己
register-with-eureka: true
###因为自己是为注册中心,不需要检索服务
fetch-registry: true
客户端集成Eureka集群
server:
port: 8000
spring:
application:
name: app-itmayiedu-member
#eureka:
# client:
# service-url:
# defaultZone: http://localhost:8100/eureka
###集群地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka,http://localhost:8200/eureka
register-with-eureka: true
fetch-registry: true
Maven配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<!-- 管理依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--SpringCloud eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
使用Consul来替换Eureka
Consul简介
Consul 是一套开源的分布式服务发现和配置管理系统,由 HashiCorp 公司用 Go 语言开发。
它具有很多优点。包括: 基于 raft 协议,比较简洁; 支持健康检查, 同时支持 HTTP 和 DNS 协议 支持跨数据中心的 WAN 集群 提供图形界面 跨平台,支持 Linux、Mac、Windows
Consul 下载地址https://springcloud.cc/spring-cloud-consul.html
Consul环境搭建
下载window版,解压得到一个可执行文件。
设置环境变量,让我们直接在cmd里可直接使用consul使命。在path后面添加consul所在目录例如D:\soft\consul_1.1.0_windows_amd64
启动consul命
consul agent -dev -ui -node=cy
-dev开发服务器模式启动,-node结点名为cy,-ui可以用界面访问,默认能访问。
测试访问地址:http://localhost:8500
Consul客户端
Maven依赖信息
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<!-- 管理依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--SpringCloud consul-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
</dependencies>
<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
客户端配置文件
###eureka 服务端口号
server:
port: 8502
spring:
application:
name: consul-order
####consul注册中心地址
cloud:
consul:
host: localhost
port: 8500
discovery:
hostname: 192.168.18.220
使用Zookeeper来替换Eureka
Eureka源码分析
客户端负载均衡器
客户端负载均衡器
RestTemplate
负载均衡器源码分析
负载均衡器重试机制
服务保护机制SpringCloud Hystrix
服务降级、熔断、限流概念
Hystrix工作原理
Hystrix属性配置
Hystrix仪表盘
Turbine集群监控
声明式服务调用SpringCloud Fegin
Fegin操作用法
Fegin参数绑定
Fegin继承特性
Ribbon配置
Hystrix配置
Api网关服务SrpingCloud Zuul
快速入门
路由详解
过滤器详解
动态加载
分布式配置中心SrpingCloud config
快速入门
Config架构
服务端详解
客户端详解
分布式服务跟踪SpringCloud sleuth
快速入门
跟踪原理
抽样收集
与Logstash整合
与Zipkin整合