Skip to content

SpringCloud_组件基本使用_Eureka

1、Eureka 是什么

Eureka 是一个Netflix 开源的服务发现组件,包括 Server 和 Client 两部分。在 Spring Cloud 子项目 Spring Cloud Netflix 中。

服务注册与发现

Eureka采用了CS的设计架构,Eureka Server作为服务注册功能的服务器,它是服务注册中心。

而系统中的其他微服务,使用Eureka的客户端连接到Eureka Server并维持心跳连接。

这样系统的维护人员就可以通过Eureka Server来监控系统中各个微服务是否正常运行。

在服务注册与发现中,有一个注册中心。当服务器启动的时候,会把当前自己服务器的信息比如服务地址通讯地址等以别名方式注册到注册中心上。另一方(消费者服务提供者),以该别名的方式去注册中心上获取到实际的服务通讯地址,然后再实现本地RPC调用。

RPC远程调用框架核心设计思想:在于注册中心,因为使用注册中心管理每个服务与服务之间的一个依赖关系(服务治理概念)。在任何 rpc 远程框架中,都会有一个注册中心(存放服务地址相关信息(接口地址))

Eureka 系统架构(右图是Dubbo的架构) image.png

Eureka Server 与 Eureka Client

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

Eureka Client通过注册中心进行访问, Eureka Client 是一个Java客户端,用于简化Eureka Server的交互,客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器。

在应用启动后,将会向Eureka Server发送心跳(默认周期为30秒)。如果Eureka Server 在多个心跳周期内没有接收到某个节点的心跳,EurekaServer将会从服务注册表中把这个服务节点移除(默认90秒)

2、基本使用

服务端

pom

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
        <!--热部署工具-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

application.yml

server:
  port: 7001

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

启动类

@EnableEurekaServer //启动服务发现,接受注册
@SpringBootApplication
public class EurekaServer_7001 {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServer_7001.class, args);
    }
}

启动项目,访问 http://localhost:7001/

客户端

pom

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <version>1.4.7.RELEASE</version>
</dependency>

application

#Eureka配置
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/
  instance:
    instance-id: springcloud-provider-dept-8001   # 修改eureka上的默认描述信息
    prefer-ip-address: true  # true,可以显示服务的IP地址

启动类

@SpringBootApplication
@EnableEurekaClient
public class DeptProvider_8001 {

    public static void main(String[] args) {
        SpringApplication.run(DeptProvider_8001.class,args);
    }
}

to be contined....

参考