ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SpringCloud(三)——Eureka服务注册与发现

2021-07-09 01:03:04  阅读:174  来源: 互联网

标签:服务 SpringCloud eureka EurekaServer 注册 7001 Eureka


SpringCloud(三)——Eureka服务注册与发现

Eureka简介

Eureka是Netflix的一个子模块,也是核心模块之一。Eureka是一个基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移,服务注册与发现对于微服务来说是非常重要的,有了服务发现与注册,只需要使用服务的标识符,就可以访问到服务,而不需要修改服务调用的配置文件了,功能类似于Dubbo的注册中心,比如Zookeeper;

特点

  • CAP原则又称CAP定理,指的是在一个分布式系统中
  • 一致性(Consistency)
  • 可用性(Availability)
  • 分区容错性(Partition tolerance)
  • CAP 原则指的是,这三个要素最多只能同时实现两点,不可能三者兼顾。

Eureka原理

Eureka的基本架构:

SpringCloud 封装了NetFlix公司开发的Eureka模块来实现服务注册和发现
Eureka采用了C-S的架构设计,EurekaServer 作为服务注册功能的服务器,他是服务注册中心

而系统中的其他微服务。使用Eureka的客户端连接到EurekaServer并维持心跳连接。这样系统的维护人员就可以通过EurekaServer来监控系统中各个微服务是否正常运行,SpringCloud的一些其他模块(比如Zuul)就可以通过EurekaServer来发现系统中的其他微服务,并执行相关的逻辑;和Dubbo架构对比:

Eureka
imge
Dubbo
imge

Eureka 包含两个组件:Eureka ServerEureka Client

Eureka Server 提供服务注册服务,各个节点启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。

Eureka Client是一个Java客户端,用于简化EurekaServer的交互,客户端同时也具备一个内置的,使用轮询负载算法的负载均衡器。在应用启动后,将会向EurekaServer发送心跳(默认周期为30秒)。如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,EurekaServer将会从服务注册表中把这个服务节点移除掉(默认周期为90秒)

三大角色

  • Eureka Server:提供服务的注册于发现。
  • Service Provider:将自身服务注册到Eureka中,从而使消费方能够找到。
  • Service Consumer:服务消费方从Eureka中获取注册服务列表,从而找到消费服务。

Eureka服务注册

1、新建一个子模块:springcloud-eureka-7001

image

2、添加Euruka服务依赖

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        <version>2.2.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <version>2.3.3.RELEASE</version>
    </dependency>
</dependencies>

3、创建application.yaml配置文件

server:
  port: 7001

#Eureka配置
eureka:
  instance:
    hostname: localhost #Eureka服务端的实例名称
  client:
    register-with-eureka: false #表示是否向Eureka注册中心注册自己
    fetch-registry: false #如果为false,则表示自己为注册中心
    service-url: #监控页面
      #eureka.instance.hostname对应上面的localhost,    server.port 7001
      #http://localhost:7001/eureka/
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

4、编写主启动类

package com.study.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer //开启Eureka服务,可以接收别人注册进来
public class EurekaServer_7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer_7001.class,args);
    }
}
启动测试

image

5、在需要注册的模块添加eureka客户依赖(springcloud-provider-dept-8001)

<!--Eureka-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>2.2.5.RELEASE</version>
</dependency>

6、在yaml配置文件中添加Eureka配置

#Eureka配置,服务注册到哪里
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/
  instance:
    instance-id: springcloud-provider-dept8001  #修改Eureka默认描述信息

7、在主启动类中添加@EnableEurekaClient注解,标注这个模块是Eureka客户

image

8、启动测试,查看是否注册成功

  • 1)先启动springcloud-eureka-7001,eureka服务
  • 2)再启动springloud-provider-dept-8001,eureka客户
  • 3)然后访问 localhost:7001 进入到eureka页面
    image
  • 4)出现红字是出发eureka自我保护机制
    image

Eureka自我保护机制

某时刻某一个微服务不可用,eureka不会立即清理,依旧会对该微服务的信息进行保存!

完善监控信息

1、在eureka客户(springloud-provider-dept-8001)中添加actuator依赖

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

2、配置eureka客户yaml

#info配置
info:
  app.name: zhangsan-springcloud
  company.name: blog.study.com

3、启动eureka客户,继续访问eureka服务

image
显示信息
image

actuator与注册微服务信息完善

1、在eureka客户模块(springcloud-provider-dept-8001)中添加监管信息依赖

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

2、编写yaml监管信息

#info配置
info:
  app.name: zhangsan-springcloud
  company.name: blog.study.com

3、主启动类中添加@EnableDiscoveryClient注解

@EnableDiscoveryClient //服务发现

image

4、在eureka客户模块编写controller

//用来获取一些注册进来的微服务的消息
@GetMapping("/discovery")
public Object discovery(){
    //获取微服务列表的清单
    List<String> services = discoveryClient.getServices();
    //输出微服务的清单
    System.out.println("discovery=>:"+services);
    //得到一个具体的服务的信息,通过具体的微服务id,applicationName
    List<ServiceInstance> instances = discoveryClient.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
    for (ServiceInstance instance : instances) {
        System.out.println(
                instance.getHost()+"\t"+
                        instance.getPort()+"\t"+
                        instance.getUri()+"\t"+
                        instance.getServiceId()
        );
    }
    return this.discoveryClient;
}

5、重启7001和8001测试

image

image

标签:服务,SpringCloud,eureka,EurekaServer,注册,7001,Eureka
来源: https://www.cnblogs.com/luoxiao1104/p/14983917.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有