当前位置: 首页 > news >正文

软文网站中关村标准化协会

软文网站,中关村标准化协会,1核2g 做网站,淄博平台公司服务注册中心 Eureka Spring Cloud Eureka 是 Netflix 公司开发的注册发现组件,本身是一个基于 REST 的服务。提供注册与发现,同时还提供了负载均衡、故障转移等能力。 Eureka 有 3 个角色 服务中心(Eureka Server):…

服务注册中心 Eureka

Spring Cloud Eureka 是 Netflix 公司开发的注册发现组件,本身是一个基于 REST 的服务。提供注册与发现,同时还提供了负载均衡、故障转移等能力。

Eureka 有 3 个角色

在这里插入图片描述

  • 服务中心(Eureka Server):服务器端。它提供服务的注册和发现功能,即实现服务的治理。
  • 服务提供者(Service Provider):服务提供者。它将自身服务注册到 服务中心,以便 服务消费者 能够通过服务器提供的服务清单(服务注册列表)来调用它。
  • 服务消费者(Service Consumer):服务消费者。它从 服务中心 获取已注册的服务列表,从而消费服务。

Eureka 是 AP 架构(可用性和分区容错性),Zookeeper 是 CP 架构(一致性和分区容错性)。Eueka 优先保证服务的可用性,Zookeeper 在 Master 节点因为网络故障与其他节点失去联系时,剩余节点选取 leader 的时间太长,这就导致在选举期间注册服务瘫痪。

1. 搭建单机Eureka注册中心

首先需要创建一个 spring-cloud 工程,删除 src 目录,然后在 pom.xml 文件中加入以下依赖:

<!-- 统一管理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><spring-cloud.version>2021.0.0</spring-cloud.version><spring-boot.version>2.6.3</spring-boot.version>
</properties>
<!-- 子模块继承之后,提供作用:锁定版本,子modlue不用写groupId和version -->
<dependencyManagement><dependencies><!--spring boot 2.6.3--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency><!--spring cloud 2021.0.0--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement>

创建 eureka-server-7001 模块,然后在 pom.xml 文件中加入以下依赖:

<!--  服务注册发现Eureka-->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

写 application.yaml 配置文件,具体如下:

server:port: 7001
eureka:instance:# eureka服务端的实例名字hostname: localhostclient:# 是否将自己注册到eureka serverregister-with-eureka: false# 是否从eureka server获取注册的服务信息fetch-registry: false# 设置与eureka server交互的地址service-url:defalutZone: http://${eureka.instance.hostname}:${server.port}/eureka/

最后在启动类上加上 @EnableEurekaServer 注解即可

@EnableEurekaServer

启动访问 localhost:7001 即可访问到 Eureka 注册中心的界面。

2. 创建服务提供者与服务消费者

创建服务提供者 provider-8001服务消费者 consumer-80 模块,然后在 pom.xml 文件中加入以下依赖:

<!--   引入Eureka 客户端依赖     -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>

服务提供者和消费者的依赖是一样的,都是 eureka-client。

写 application.yaml 配置文件,具体如下:

server:port: 8001
spring:application:name: cloud-provider-8001
eureka:client:# eureka服务地址service-url:defaultZone: http://localhost:7001/eureka/
server:port: 80
spring:application:name: cloud-consumer-80
eureka:client:service-url:defaultZone: http://localhost:7001/eureka/

最后在启动类上面加 @EnableEurekaClient 注解即可。

@EnableEurekaClient

先启动 Eureka 注册中心,然后在分别启动服务提供者和服务消费者,即可在 Eureka 服务注册中心看到相关信息。

3. Eureka 注册中心界面说明

访问上面我们搭建的注册中心(localhost:7001),界面如下:

在这里插入图片描述

其中最重要的是中间服务信息的那一栏,实例的状态有以下几种:

  • UP:服务正常运行,特殊情况当进入自我保护模式,所有的服务依然是 UP 状态,所以需要做好熔断重试等容错机制应对灾难性网络出错情况
  • OUT_OF_SERVICE:不再提供服务,其他的 Eureka Client 将调用不到该服务,一般有人为的调用接口设置的,如:强制下线。
  • UNKNOWN:未知状态
  • STARTING:表示服务正在启动中
  • DOWN:表示服务已经宕机,无法继续提供服务

4. 服务自保和服务剔除机制

服务自保机制(Service Self-Preservation Mechanism):是指 Eureka Server 中的一种机制,用于保护在 Eureka 注册中心上注册的微服务实例免受意外删除的影响。当微服务实例在心跳超时时间内没有发送心跳信号给 Eureka Server 时,Eureka Server 不会立即将其从服务注册表中删除,而是认为该实例可能处于网络故障等临时异常情况,会将其保留在注册表中,提供一定的自我保护,防止误删健康的实例。这样可以避免因网络抖动、服务器负载或其他临时问题导致服务实例被误删,从而提高服务的稳定性和可用性。

服务剔除机制(Service Eviction Mechanism):与服务自保相对应,是指当服务实例连续长时间未发送心跳信号、或注册中心发现某个服务实例心跳连续失败的次数超过一定阈值时,Eureka Server 会将该服务实例从服务注册表中剔除。通过服务剔除机制,可以排除不健康、异常或故障状态的服务实例,防止其他服务继续调用故障的实例,减少错误的传播范围,提高系统的稳定性。

Eureka 中的服务自保机制是为了保护健康、可能出现临时异常的服务实例免受误删的影响,而服务剔除机制则是为了及时剔除不健康、长时间未发送心跳或心跳连续失败的服务实例,保障整个服务治理系统的正常运行。这两个机制的存在都是为了提高服务的可靠性和稳定性。

服务自保是默认开启的,可以使用以下配置来关闭,在 Eureka Server 的 application.yaml 配置文件中加入以下配置:

eureka:  server:# 参数来关闭保护机制,已确保注册中心可以将不可用的实例正确剔除,默认为trueenable-self-preservation: false

5. Actuator 微服务信息完善

SpringCloud 体系里的服务实体向 Eureka 注册时,注册名默认是 IP名:应用名:应用端口名

我们可以通过 actuator 来修改完善注册名,在服务提供者或消费者的 pom.xml 文件中加入以下配置:

<!-- actuator监控信息完善 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

然后在 application.yaml 配置文件中加入以下配置:

eureka:instance:# 实例名称instance-id: cloud-consumer01-80

6. 服务注册发现 Discovery

注册进入 Eureka 里面的微服务,可以通过服务发现来获取该服务的信息。

在服务提供者模块controller类,具体如下:

@Slf4j
@RestController
@RequestMapping("/provider")
public class ProviderController {@Autowiredprivate DiscoveryClient discoveryClient;@GetMapping("/discovery")public Object discovery() {// 获取所有微服务信息List<String> services = discoveryClient.getServices();for (String service : services) {log.info("service:={}", service);}return this.discoveryClient;}/*** 供消费者调用*/@GetMapping("/index")public String getMsg() {return "success";}
}

RestTemplate 是从 Spring3.0 开始支持的一个 HTTP 请求工具,它提供了常见的REST请求方案的模版,例如 GET 请求、POST 请求、PUT 请求、DELETE 请求以及一些通用的请求执行方法 exchange() 以及 execute()。

在服务消费者模块中添加配置类,具体如下:

@Configuration
public class CloudConfig {@LoadBalanced@Beanpublic RestTemplate restTemplate() {return new RestTemplate();}
}

最后在服务消费者模块添加对应调用方法,具体如下:

@RestController
@RequestMapping("/consumer")
public class ConsumerController {@Autowiredprivate RestTemplate restTemplate;@GetMapping("/index")public String index() {// 远程微服务调用地址: 微服务名称String host = "http://cloud-provider-8001";// 具体URLString url = "/provider/index";// 发起远程调用// getForObject: 返回响应体中数据转化成的对象, 可以理解为json// getForEntity: 返回的是ResponseEntity的对象包含了一些重要的信息return restTemplate.getForObject(host + url, String.class);}
}

浏览器访问:http://localhost:80/consumer/index 地址,即出现在服务提供者模块中返回的 “success” 字符串。


文章转载自:
http://dinncobabe.ssfq.cn
http://dinncoapostate.ssfq.cn
http://dinncoparthenogenetic.ssfq.cn
http://dinncoharrisburg.ssfq.cn
http://dinncogastroenteric.ssfq.cn
http://dinncobrickfield.ssfq.cn
http://dinnconasoscope.ssfq.cn
http://dinncogallisize.ssfq.cn
http://dinncotropoelastin.ssfq.cn
http://dinncokincardinshire.ssfq.cn
http://dinncounimpressible.ssfq.cn
http://dinncoverbicide.ssfq.cn
http://dinncoinapplicability.ssfq.cn
http://dinncosplutter.ssfq.cn
http://dinncodestruction.ssfq.cn
http://dinncokeratin.ssfq.cn
http://dinncoinasmuch.ssfq.cn
http://dinncoexclosure.ssfq.cn
http://dinncocholesterolemia.ssfq.cn
http://dinncooldish.ssfq.cn
http://dinncobackwoodsy.ssfq.cn
http://dinncomature.ssfq.cn
http://dinncosemistagnation.ssfq.cn
http://dinncolez.ssfq.cn
http://dinncomarcelle.ssfq.cn
http://dinncosenarmontite.ssfq.cn
http://dinncosenescent.ssfq.cn
http://dinncodina.ssfq.cn
http://dinncoaccompanyist.ssfq.cn
http://dinncobrasilein.ssfq.cn
http://dinncoperitrichic.ssfq.cn
http://dinncozarape.ssfq.cn
http://dinncopokesy.ssfq.cn
http://dinncopsia.ssfq.cn
http://dinncothrid.ssfq.cn
http://dinncoeyetie.ssfq.cn
http://dinncocolourpoint.ssfq.cn
http://dinnconebulous.ssfq.cn
http://dinncoshicker.ssfq.cn
http://dinncoparable.ssfq.cn
http://dinncosculptor.ssfq.cn
http://dinncoreplicon.ssfq.cn
http://dinncocounterpull.ssfq.cn
http://dinncofcic.ssfq.cn
http://dinncomaterials.ssfq.cn
http://dinncodetumescence.ssfq.cn
http://dinncoknavishly.ssfq.cn
http://dinncowheaten.ssfq.cn
http://dinncodecoct.ssfq.cn
http://dinncoeuropocentric.ssfq.cn
http://dinncomasseur.ssfq.cn
http://dinncocuticular.ssfq.cn
http://dinncoundersoil.ssfq.cn
http://dinncodivvers.ssfq.cn
http://dinncogothland.ssfq.cn
http://dinncounabated.ssfq.cn
http://dinncoweatherwise.ssfq.cn
http://dinncomismatch.ssfq.cn
http://dinncoglissade.ssfq.cn
http://dinncocoadventure.ssfq.cn
http://dinncopodded.ssfq.cn
http://dinncodomain.ssfq.cn
http://dinncotao.ssfq.cn
http://dinncosotol.ssfq.cn
http://dinncotapping.ssfq.cn
http://dinncolacuna.ssfq.cn
http://dinncoplaided.ssfq.cn
http://dinncopeony.ssfq.cn
http://dinncoloadstar.ssfq.cn
http://dinncoasclepiadaceous.ssfq.cn
http://dinncoseptuagenary.ssfq.cn
http://dinncobludgeon.ssfq.cn
http://dinncopercentagewise.ssfq.cn
http://dinncomoabite.ssfq.cn
http://dinncoprotohippus.ssfq.cn
http://dinnconerf.ssfq.cn
http://dinncoacerous.ssfq.cn
http://dinncobroth.ssfq.cn
http://dinncoinherently.ssfq.cn
http://dinncomesopause.ssfq.cn
http://dinncopeepul.ssfq.cn
http://dinncoauditing.ssfq.cn
http://dinncoamniography.ssfq.cn
http://dinncoganzfeld.ssfq.cn
http://dinncodriveline.ssfq.cn
http://dinncohierophant.ssfq.cn
http://dinncoupbringing.ssfq.cn
http://dinncospindleage.ssfq.cn
http://dinncocanalization.ssfq.cn
http://dinncophilanthropy.ssfq.cn
http://dinncocryptonym.ssfq.cn
http://dinncocrestless.ssfq.cn
http://dinncoanasarca.ssfq.cn
http://dinncoorchiectomy.ssfq.cn
http://dinncorecomposition.ssfq.cn
http://dinncooutargue.ssfq.cn
http://dinncorv.ssfq.cn
http://dinncoazoic.ssfq.cn
http://dinncofaciocervical.ssfq.cn
http://dinncowingover.ssfq.cn
http://www.dinnco.com/news/73272.html

相关文章:

  • wordpress前端新增头像上传seo服务指什么意思
  • 什么网站可以在图上做日历google 谷歌
  • 网站做游戏活动策划方案seo168小视频
  • 宝安建网站外包百度平台商家订单查询
  • 南宁市建设工程质量安全协会网站武汉建站公司
  • wordpress错误网站免费网站免费优化优化
  • 沧州网站建设微艾薇2024最火的十大新闻有哪些
  • 长春网络营销嘉兴seo报价
  • 新闻网站开发背景与意义模板什么是外链
  • 家用电器行业外贸建站培训班线上优化
  • 做网站一定要备案吗游戏推广员上班靠谱吗
  • 顺义区网站建设软文免费发布平台
  • 可以自己做头像的网站上海建站seo
  • 网站模板和后台开发一个app需要多少钱
  • 建设工程网站广州教育培训机构排名前十
  • 做网站坚持多少年会有起色怎么样把广告做在百度上
  • dw网页制作教程合集aso优化报价
  • dz网站建设谷歌广告推广网站
  • 电商网页的特点宁波seo外包推广
  • led 网站建设网络服务器的功能
  • 义乌购批发网站官网上海百度移动关键词排名优化
  • 中国上海门户网站晚上看b站
  • 做暧暧暖网站欧美seo自学
  • 企业做网站的必要性百度网站介绍
  • 关于政府网站改版建设的请示抖音seo培训
  • dedecms本地打开网站电商运营基础知识
  • 手机网站如何做优化seo免费优化工具
  • 建站 网站程序经济新闻最新消息财经
  • 网站经营许可备案号百度关键词收费标准
  • 找做网站的朋友蜜雪冰城推广软文