SpringCloud Netflix-Eureka使用

1、介绍

Eureka是Netflix公司开发的一个框架,主要提供服务自动注册与发现、服务健康状态监管等功能,还提供了一个方便查看管理的后端管理系统界面。

Eureka作为SpringCloudNetflix的一个组件,在微服务架构中充当注册中心的作用。

Eureka包含两个部分:Eureka Server、Eureka Client。

在这里插入图片描述

  • Eureka:就是服务注册中心(可以是一个集群),对外暴露自己的地址。
  • 提供者:启动后向Eureka注册自己信息(ip、port、服务名等)。
  • 消费者:向Eureka订阅服务,Eureka会将对应服务的所有提供者地址列表发送给消费者,并且定期更新(默认30s)。
  • 心跳(续约):提供者定期通过http方式向Eureka刷新自己的状态(默认周期为30s,EurekaServer默认90s未收到心跳将移除节点)。

项目github地址:https://github.com/Netflix/eureka

master分支

在这里插入图片描述

1.4.x分支

在这里插入图片描述

从Eureka的github项目页面可以发现,master分支最后一次更新为2021-11,常用的1.4.x分支最后一次更新为2016-11,当前时间2022-08-24,所以传言Eureka项目停止维护并非空穴来风,所以新互联网项目应尽量避免使用Eureka作为注册中心。

2、使用

SpringCloud是基于SpringBoot为基础实现的,并且有版本的兼容关系,如图,本文档演示,springBoot采用2.3.9.RELEASE、springCloud采用Hoxton.SR10。

在这里插入图片描述

服务端

详细演示代码:https://gitee.com/micro_service_xk/netflix-cloud-demo.git eureka-server模块

1、pom.xml文件

<!-- springboot父工程 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.9.RELEASE</version>
    <relativePath/>
</parent>

<dependencyManagement>
    <dependencies>
        <!-- springCloud父工程 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR10</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--eureka服务端-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>

2、springBoot主类添加@EnableEurekaServer注解

@EnableEurekaServer
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}

3、application.yml文件

server:
  port: 10086 # 服务端口
spring:
  application:
    name: eurekaserver # eureka的服务名称
eureka:
  client:
    service-url:  # eureka的地址信息(eureka默认需要多个实例相互注册形成集群,这里自己注册到自己,避免报错)
      defaultZone: http://127.0.0.1:10086/eureka
客户端

详细演示代码:https://gitee.com/micro_service_xk/netflix-cloud-demo.git user-service、order-service模

1、pom.xml文件

<!-- springboot父工程 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.9.RELEASE</version>
    <relativePath/>
</parent>

<dependencyManagement>
    <dependencies>
        <!-- springCloud父工程 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Hoxton.SR10</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--eureka客户端-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>

2、springBoot主类添加@EnableDiscoveryClient注解

@EnableDiscoveryClient
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}

3、application.yml文件

server:
  port: 8500 # 服务端口
spring:
  application:
    name: user-service # 服务名,将注册到eureka-server中
eureka:
  client:
    service-url:  # eureka-server的地址信息,多个使用英文逗号分割
      defaultZone: http://127.0.0.1:10086/eureka

4、eureka-client客户端使用

@Autowired
private DiscoveryClient discoveryClient;// Eureka客户端,可以获取到服务实例信息

//eureka-client根据服务名,从eureka-server获取注册的服务列表信息
 List<ServiceInstance> instances = discoveryClient.getInstances("order-service");

3、集群

注册中心在分布式架构中充当相当重要的角色,所以必须搭建高可用的集群模式。

多个Eureka Server之间互相注册为服务,当服务提供者注册到Eureka Server集群中的某个节点时,该节点会把服务的信息同步给集群中的每个节点,从而实现数据同步。因此,无论Eureka Client访问到Eureka Server集群中的任意一个节点,都可以获取到完整的服务列表信息。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值