Eureka服务注册与发现

水深无声 2024-03-17 21:00 121阅读 0赞

服务治理

在这里插入图片描述

服务注册

注意:CS是客户端和服务端
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

两组件

Eureka Server

在这里插入图片描述
Eureka Server 相当于一个商场,商场里的各个店铺都需要在商场进行签合同(注册)来在商场里开店

注意:在 Eureka Server 注册中心的启动类上添加 @EnableEurekaServer 注解

  1. package com.atguigu.springcloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  5. @SpringBootApplication
  6. // @EnableEurekaServer 服务注册中心
  7. @EnableEurekaServer
  8. public class EurekaMain7001 {
  9. public static void main(String[] args) {
  10. SpringApplication.run(EurekaMain7001.class, args);
  11. }
  12. }

在 pom.xml 添加以下依赖

  1. <!-- Eureka Server -->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  5. </dependency>

在 application.yml 添加以下配置

  1. server:
  2. port: 7001
  3. eureka:
  4. instance:
  5. hostname: eureka7001.com # eureka 服务端的实例名称
  6. client:
  7. # false 表示不向注册中心注册自己
  8. register-with-eureka: false
  9. # false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
  10. fetch-registry: false
  11. service-url:
  12. #设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址
  13. # defaultZone: http://eureka7002.com:7002/eureka/ # 集群版
  14. defaultZone: http://eureka7001.com:7001/eureka/ # 单机版

Eureka Client

在这里插入图片描述
Eureka Client 相当于商场的店铺,每隔一个月交租金(每隔30秒向Eureka Server发送心跳),如果商场几个月没有收到某个店铺的租金(Eureka在多个心跳周期内没有收到某个节点的心跳),商场将会与该店铺结束合同(Eureka Server 将会从服务注册表中把这个服务节点移除)
注意:在 Eureka Client 客户端(在 Eureka Server 注册中心注册的各个服务)的启动类上添加 @EnableEurekaClient 注解

  1. package com.atguigu.springcloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  6. @SpringBootApplication
  7. @EnableEurekaClient
  8. @EnableDiscoveryClient // 该注解用于将服务注册进注册中心
  9. public class PaymentMain8001 {
  10. public static void main(String[] args) {
  11. SpringApplication.run(PaymentMain8001.class, args);
  12. }
  13. }

在 pom.xml 添加以下依赖

  1. <!--eureka-client-->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  5. </dependency>

在 application.yml 里添加以下配置

  1. server:
  2. port: 8001
  3. eureka:
  4. client:
  5. # 表示将自己注册进EurekaServer默认为true
  6. register-with-eureka: true
  7. # 是否从EurekaServer抓取已有的注册信息,默认为true,单节点无所谓,集群必须设置为true才能配合Ribbon使用负载均衡
  8. fetch-registry: true
  9. service-url:
  10. # 单机版
  11. defaultZone: http://eureka7001.com:7001/eureka
  12. # 集群版
  13. # defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

Eureka集群

在这里插入图片描述
原理:多个Eureka相互注册,端口7001的Eureka注册到端口7002的Eureka里,端口7002的Eureka注册到端口7001的Eureka里如下图:
在这里插入图片描述
集群的配置yml文件

  1. server:
  2. port: 7002
  3. eureka:
  4. instance:
  5. hostname: eureka7002.com # eureka 服务端的实例名称
  6. client:
  7. # false 表示不向注册中心注册自己
  8. register-with-eureka: false
  9. # false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
  10. fetch-registry: false
  11. service-url:
  12. #设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址
  13. defaultZone: http://eureka7001.com:7001/eureka
  14. # defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #单机
  15. # defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 多个服务器

服务发现 Discovery

对于注册进eureka里面的微服务,可以通过服务发现来获得该服务的信息
在 Controller 类里

  1. /**
  2. * DiscoveryClient
  3. * 服务发现,客户端(Client)
  4. */
  5. @Resource
  6. private DiscoveryClient discoveryClient;
  7. @GetMapping("/payment/discovery")
  8. public Object discovery() {
  9. List<String> services = discoveryClient.getServices();
  10. services.forEach(element -> {
  11. log.info("****element:"+element);
  12. });
  13. List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
  14. instances.forEach(instance -> {
  15. log.info(instance.getInstanceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
  16. });
  17. return this.discoveryClient;
  18. }

在这里插入图片描述
在这里插入图片描述

Eureka 的自我保护

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

为什么产生自我保护机制

在这里插入图片描述

什么是自我保护机制

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

禁止自我保护

在这里插入图片描述

注册中心,在 Eureka Server 端的 yml 文件修改
在这里插入图片描述
在这里插入图片描述
生产者客户端 Eureka Client 端在 yml 修改
在这里插入图片描述
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,121人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Eureka服务注册发现

    注册中心可以说是微服务架构中的"通讯录",它记录了服务和服务之间地址的映射关系。在分布式架构中, 服务会注册到这里(注册中心),当服务需要调用其它服务时,就这里找到服务的...

    相关 服务注册发现---eureka

    eureka简介:云端服务发现,一个基于 REST 的服务,用于定位服务,以实现云端中间层服务发现和故障转移。 话不多说直接上代码 首先新建一个springcloud eu