文章28 | 阅读 8930 | 点赞0
Eureka是Netflix的一个子模块 ,也是核心模块之一。Eureka是一个基于REST的服务,用于定位服务,以实现云端中间服务发现和故障转移。服务注册于发现对于微服务架构来说是非常重要的,有了服务发现与注册 ,只需要使用服务的标识,就可以访问到服务,而不需要修改服务调用的配置文件 功能类似于 Dubbo 的注册中心 比如zookeeper
Eureka采用了 C-S 的设计架构。Eureka server作为服务注册功能 的服务器,它是服务注册中心
而系统中的其他微服务,使用Eureka的客户端连接到 Eureka Server 并维持心跳连接, 这样 系统的维护人员就可以通过Eureka Server 来监控系统中各个为服务是否正常运行。 SpringCloud 的一些其他 模块 (比如 zoo)就可以通过Eureka Server 来发现系统中的其他为服务 ,并执行相关的逻辑
Eureka Server提供服务注册服务
各个节点启动后,会在EurekaServer中进行注册,这样 EurekaServer中的服务注册表中将会存储所有可用的 服务节点的信息,服务节点的信息可以在界面上可以看的到
Eureka Client 是一Java客户端,用于简化Eureka Server的交互,客户端同时也具备一个内置的,使用轮询(round-robin)负载算法的负载均衡器。在应用启动后 ,将会向Eureka Server 发送心跳(默认周期30秒)如果Eureka Server 在多个心跳周期内没有接收到某个节点的心跳 EurekaServer 将会从服务注册中把这个服务节点移除(默认90秒)
Eureka Server 提供服务注册发现
Eureka Server provider 服务提供方将自身服务注册到Eureka 从而使服务消费方找到、、
Eureka Consumer 服务消费方 从Eureka 获取注册服务列表,从而能够消费服务
1)
从创建好的父 级工程里添加注册的模块
microservicecloud-eureka-7001 Pom文件
<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>
<parent>
<groupId>com.xxx</groupId>
<artifactId>microservicecloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microservicecloud-eureka-7001</artifactId>
<dependencies>
<!--eureka-server服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己。
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
#单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
# 相当与 http://localhost:7001
package com.xxx.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer//这个注解是代表了扫描EurekaServer的注解 开启EurekaServer接受其他 为服务注册进来
//@Enable.... 是一个开启的注解
public class EurekaServerApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(EurekaServerApp.class, args);
}
}
下一步 启动 项目 启动成功 端口就是7001
打开浏览器 就可访问了
看到这个页面说明已经可以了
<!-- 将微服务provider侧注册进eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
eureka:
client: #客户端注册进eureka服务列表内
service-url:
defaultZone: http://localhost:7001/eureka
代表的是本服务启动自动 注册到Eureka 服务中
启动项目 需要把两个 都启动 一个是 注册 和提供者
1): eurekaServer注册7001
2):8083
3)现在打开页面 会发现多了一条
对外暴漏的微服务的名字就是 在yml 文件里自定义的
1)
需要在8083的pom添加依赖
<!-- actuator监控信息完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2)在父级项目也加上 依赖加上
microservicecloud是你父级项目的名
<build>
<finalName>microservicecloud</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>
<delimit>$</delimit>
</delimiters>
</configuration>
</plugin>
</plugins>
</build>
3)在8083 项目里.yml 文件 添加
修改 连接名 与 显示 ip 和配置 info
eureka:
client: #客户端注册进eureka服务列表内
service-url:
defaultZone: http://localhost:7001/eureka
instance:
instance-id: microservicecloud-dept8083
prefer-ip-address: true # 显示ip地址
info:
app.name: atguigu-microservicecloud
company.name: www.baidu.com
build.artifactId: $project.artifactId$
build.version: $project.version$
修改之后 启动项目
点进去
点进去 就不 是404了 你的内网也显示了 显示的就是一个 json 字符串
默认情况下,如果EurekaServer 在定期的时间内没有接收到某个微服务心跳,EurekaServer将会注销该实例(默认90秒)但是当网络分区故障发生时 微服务与EurekaServer之间无法正常通信,以上行为可能变得非常危险了-因为微服务本身其实是健康的,此时不应该注销这个服务。Eureka通过 自我保护模式 来解决问题 当EurekaServer节点在短期时间内丢失过多客户端时(可能发生了网络分区 故障)那么这个节点就会进入自我保护模式 ,一旦进入该模式 EurekaServer就会保护服务注册表中的信息,不在删除服务注册表中的数据 (也就是不会注销任何微服务)。当网络恢复后,该EurekaServer节点会自动退出自我保护模式
在自我保护模式中,EurekaServer会保护服务注册表中的信息, 不再注任何服务实例 当他收到的心跳重新恢复到閥值以上时 该EurekaServer节点就会自动退出自我保护模式 ,它的设计哲学是宁可保留错误的服务注册信息,也不盲目的注销任何 可能健康的服务实例。
自我 保护是一种 应对网络异常的安全保护措施 他的架构哲学是保留所有的服务(健康和不健康的 服务都会保留)也不盲目注册任何健康的微服务 使用自我保护模式,可以让Eureka集群更稳定
在Eureka yml 文件添加
在Spring cloud中 可以使用 eureka.server.enable-self-preservation= false
自我保护的体现
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_44520739/article/details/89344717
内容来源于网络,如有侵权,请联系作者删除!