spring-cloud Eureka使用

2018-08-15  本文已影响112人  Jimhou

什么是Eureka?

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

Eureka Server

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

Eureka Client

使用方式

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/


配置文件说明

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

实现

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 &
image.png
上一篇 下一篇

猜你喜欢

热点阅读