美文网首页
spring-cloud Eureka使用

spring-cloud Eureka使用

作者: Jimhou | 来源:发表于2018-08-15 13:59 被阅读112次

什么是Eureka?

Eureka 是Netfilx 开源的一个基于rest服务实现的分布式系统的服务发现组件。它包含Server 和Client两部分。Spring Cloud 将它集成在了Spring Cloud Netfilx中,用来实现微服务的注册与发现 。启动时需要依赖spring boot服务。

Eureka Server

Eureka Server 提供了服务发现的功能,集群内的每个微服务在启动时,都会将服务自身的信息(IP、端口、名称等)注册到Eureka Server中,Eureka Server会存储这些信息。

Eureka Client

  • Eureka Client 是一个java客户端,用来简化微服务与Eureka Server的交互。微服务在启动后,会周期性的(默认30s)向Eureka Server发送心跳并获取eureka server内的服务信息,这个心跳操作也叫“续约”。
  • 在默认情况下,Eureka Server 同时也是Eureka Client,在多个Eureka Server实例中,互相之间通过复制的方式,来实现服务注册表中数据的同步。
  • Euerka Client会缓存服务注册表中的信息,这种方式可以减轻Eureka Server 的压力,无须每次请求都查询Eureka Server;同时也可以保证在所有的Eureka Server 宕机后,微服务能够使用缓存中的信息找到服务的提供者完成调用;。

使用方式

step1 引入依赖

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

step2 配置

编辑application.yml

server:
  port: 8761

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/


配置文件说明
  • 该服务充当两个角色,一个是eureka server ,一个是eureka client,充当server 的配置为eureka.server.*,充当client的配置eureka.client.*
  • 该服务的端口是8761
  • eureka.client.registerWithEureka: 表示是否将自己注册到Eureka Server。因为当前服务就是Eureka Server是,所以此服务不需注册到Eureka Server。
  • eureka.client.registerWithEureka: 表示是否从Eureka Server中获取服务的注册信息,默认是true,因为这是个单点服务,所以不需要从Eureka server拉去服务信息
  • eureka.client.serviceUrl.defaultZone: 与Eureka 交互的地址,查询服务和注册服务都使用这个地址,默认是http://localhost:8761/eureka;多个地址可用逗号分隔。
  • eureka server只是提供了可以注册、获取服务等相关api、也可以与其他业务的服务放在一起,但是为了架构的清晰性,还是独立作为一个注册中心服务较好。

step3 启动Spring boot服务

@SpringBootApplication
@EnableEurekaServer     // 说明该服务将作为一个注册中心,用来注册其他服务
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

将微服务注册到Eureka Server 上

step1 新创建一个maven 项目,步骤省略

step2 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>lenovo.test.houjb</groupId>
    <artifactId>eureka-client</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
   <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
</project>

step3 更新配置文件

application.properties

eureka:
  instance:
    preferIpAddress: true
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

step4 启动服务

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

step5 访问eureka UI

image.png

eureka server 的高可用

原理

eureka server集群内没有master、slave 的概念区分,eureka server集群的每个eureka server都是注册中心(eureka-server)、也同时都拉取服务列表到本地(eureka-client),如下图所示。


eureka-ha.png

实现

  • 新建application-peer1.yml、application-peer2.yml、application-peer3.yml

applcation-peer1.yml

spring:
  application:
    name: eureka-server
server:
  port: 8761
eureka:
  instance:
    hostname: peer1

  client:
    serviceUrl:
      defaultZone: http://peer2:8762/eureka/,http://peer3:8763/eureka/

application-peer2.yml

spring:
  application:
    name: eureka-server
server:
  port: 8762
eureka:
  instance:
    hostname: peer2

  client:
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/,http://peer3:8763/eureka/

application-peer3.yml

spring:
  application:
    name: eureka-server
server:
  port: 8763
eureka:
  instance:
    hostname: peer3

  client:
    serviceUrl:
      defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/
  • 将服务打包
mvn clean install -DskipTests
  • 启动集群
nohup java -jar  eureka-server-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1 > peer1.nohup 2>&1 &
nohup java -jar  eureka-server-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2 > peer2.nohup 2>&1 &
nohup java -jar  eureka-server-ha-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer3 > peer3.nohup 2>&1 &
  • 服务启动成功,访问 eureka ui
image.png

相关文章

网友评论

      本文标题:spring-cloud Eureka使用

      本文链接:https://www.haomeiwen.com/subject/sebibftx.html