当前位置:   article > 正文

SpringCloud Netflix-Eureka使用_eureka github

eureka github

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

2、springBoot主类添加@EnableEurekaServer注解

@EnableEurekaServer
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
客户端

详细演示代码: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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

2、springBoot主类添加@EnableDiscoveryClient注解

@EnableDiscoveryClient
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

4、eureka-client客户端使用

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

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

3、集群

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/1000191
推荐阅读
相关标签
  

闽ICP备14008679号