一、版本

Spring全家桶中最让人烦的便是版本问题,如果是随便从网上找的依赖,可能版本过低maven下载不下来,要想找到合适的版本不可避免地得去官网查找。
(1)https://github.com/alibaba/spring-cloud-alibaba/wiki/该链接是Spring Cloud Alibaba的项目地址,应为目前使用SpringCloud主要使用3个依赖:SpringCloud,SpringBoot,SpringCloudAlibaba。在该项目地址的版本说明中,很清楚地给出了这三个依赖之间推荐使用地依赖版本,例如:
在这里插入图片描述
本文使用的是红线部分。
(2)https://spring.io/projects该地址是SpringCloud的相关文档,在该网站,可以查找SpringCloud所有组件最近的几个版本,例如:
在这里插入图片描述
GA: General Availability,正式发布的版本,官方推荐使用此版本。在国外都是用GA来说明release版本的。

PRE: 预览版,内部测试版. 主要是给开发人员和测试人员测试和找BUG用的,不建议使用;

SNAPSHOT: 快照版,可以稳定使用,且仍在继续改进版本。


二、Eureka注册中心

1.基本架构

(1)SpringCloud封装了NetFlix公司开发的Eureka模块来实现服务注册和发现
(2)Eureka采用了C-S架构设计,EurekaServer作为服务注册功能的服务器,他是服务注册中心
(3)而系统中的其他微服务,使用Eureka的客户端连接到EurekaServer并维持心跳连接。这样系统的维护人员就可以通过EurekaServer来监控系统中各个微服务是否正常运行,SpringCloud的一些其他模块(比如Zuul)就可以通过EurekaServer来发现系统中的其他微服务,并执行相关逻辑;

2.基本组成

Eureka包含两个组件:Eureka Server和Eureka Client
EurekaServer提供服务注册服务,各个节点启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息。
EurekaClient是一个java客户端,用于简化EurekaServer的交互,客户端同时也具备一个内置的,使用轮询负载算法的负载均衡器,在应用启动后,将会向EurekaServer发送心跳(默认周期为30秒),如果EurekaServer在多个心跳周期内没有收到某个节点的心跳,EurekaServer将会从服务注册列表中把这个服务节点移除掉(默认周期为90秒)

3.Eureka服务端使用示例

(1)父项目管理依赖

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.3.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR9</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.6.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

(2)Eureka服务添加依赖

		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

(3)编写配置文件

server:
  port: 8761

eureka:
  instance:
    hostname: eureka
    lease-expiration-duration-in-seconds: 20   #服务过期时间配置,超过这个时间没有接收到心跳EurekaServer就会将这个实例剔除(默认90秒)
    lease-renewal-interval-in-seconds: 5       #服务刷新时间配置,每隔这个时间会主动心跳一次(默认30秒)
  client:
    # false表示不向注册中心注册自己
    register-with-eureka: false
    # false表示自己就是注册中心
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
	registry-fetch-interval-seconds: 10  #重新刷新服务地址的时间

(4)在启动类上加上@EnableEurekaServer

启动后,访问本地8761端口即可进入Eureka界面

4.Eureka客户端使用示例

(1)添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

(2)编写配置文件


eureka:
  client:
    fetch-registry: false
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    # 定义Status处的名字
    instance-id: cloudtest
    # 点击eureka网页上该服务的名字,可以显示服务的ip地址
    prefer-ip-address: true 

(3)在启动类上添加注解EnableEurekaClient

5.Eureka集群使用实例

单一的Eureka服务端如果挂掉了,那么所有服务基本上等于都挂掉了,所以要对Eureka服务端进行集群化

(1)对于Eureka服务端

其余不变只改配置文件

server:
  port: 8761

eureka:
  instance:
   # 改值默认为spring.application.name的值,注意不能重复
    hostname: eureka 
  client:
    # false表示不向注册中心注册自己
    register-with-eureka: false
    # false表示自己就是注册中心
    fetch-registry: false
    service-url:
      defaultZone: http://另外一个eureka服务器ip:端口号/eureka/

多个eureka集群之间service-url互相注册

(1)对于Eureka客户端

eureka:
  client:
  	register-with-eureka: true
  	fetchRegistry: true
    service-url:
      defaultZone: http://服务端的ip地址:服务端的port/eureka/,http://服务端的ip地址2:服务端的port2/eureka/

对于客户端要注册到eureka集群所有服务端上

三、写在后话

目前github上https://github.com/Netflix/eureka/wiki宣布停止Eureka的后续更新,所以不建议新的项目继续使用Eureka作为注册中心,也并不建议对Eureka进行更深入的学习。


以上便是Eureka的基本使用,欢迎讨论

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐