服务注册中心,netfix公司的,出道即巅峰,可惜没人维护了。
1.x 可用,2.x 使用后果自负。本文讲eureka服务中心的搭建,分为单机版本和eureka集群版本。以及服务消费者,服务提供者如何入驻注册中心。
eureka 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>cloud2020</artifactId>
<groupId>top.bitqian</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<!-- 127.0.0.1 eureka7001.com -->
<artifactId>cloud-eureka-server7001</artifactId>
<dependencies>
<!-- eureka server~ -->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>top.bitqian</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
eureka 服务server端的注册中心, single
server:
port: 7001
# 单机版eureka
eureka:
instance:
hostname: localhost #eureka服务端的实例名字
client:
register-with-eureka: false #表识不向注册中心注册自己
fetch-registry: false #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://${
eureka.instance.hostname}:${
server.port}/eureka/
#设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
eureka的集群搭建
- 更改host文件
127.0.0.1 http://eureka7001.com
127.0.0.1 http://eureka7002.com
- yaml 相互注册,相互守望
server:
port: 7001
# 服务注册集群 eureka服务注册中心地址7001
eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名字
client:
register-with-eureka: false #表识不向注册中心注册自己
fetch-registry: false #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://eureka7002.com:7002/eureka/ #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
server:
port: 7002
# 服务注册集群 eureka服务注册中心地址7002
eureka:
instance:
hostname: eureka7002.com #eureka服务端的实例名字
client:
register-with-eureka: false #表识不向注册中心注册自己
fetch-registry: false #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
service-url:
defaultZone: http://eureka7001.com:7001/eureka/ #设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
客户端如何入驻eureka集群
- 消费者
# 暴露80端口,消费者调用8001 支付接口
server:
port: 80
# 服务消费者 client
spring:
application:
name: cloud-order-service
eureka:
client:
register-with-eureka: true # 是否注册到服务中心~
fetchRegistry: true
service-url:
# 将80消费者服务 发布到7001, 7002端口的eureka集群服务里面
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
- 两个服务提供者
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
# 将支付服务8001发布到7001, 7002 两台eureka集群配置中
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
# eureka客户端 可以又多个
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
# 将支付服务8001发布到7001, 7002 两台eureka集群配置中
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
eureka的自我保护机制
eureak 不会删掉,干掉已经挂掉的服务。
# 自我保护机制出现现象: 服务客户端和eureka server之间在网络分区下进行的保护
# 一旦进入保护模式,eureka server将会尝试保护其注册表中的信息, 不会注销掉任何微服务
# 某个时刻某一个微服务不可用了, eureka不会进行服务清理, 会对微服务信息进行保存。
# 为什么进行保护: 网络是不稳定的, 可能某个服务可能因为短暂的网络拥堵, 导致服务暂停了, 但是服务是健康的。 cap --> ap