zoukankan      html  css  js  c++  java
  • Spring Cloud(二):服务注册与发现 Eureka【Finchley 版】

    Spring Cloud(二):服务注册与发现 Eureka【Finchley 版】

    上一篇主要介绍了相关理论,这一篇开始我们来一个个的实践一下。

    Just code it.

    本系列介绍的配置均基于 Spring Boot 2.0.1.RELEASE 版本和 Spring Cloud Finchley.RC1 版本

    服务注册中心

    Spring Cloud 已经帮我们实现了服务注册中心,我们只需要很简单的几个步骤就可以完成。

    首先我们创建一个 Spring Boot 工程,名字就叫 eureka-server,可以直接使用 Spring Initializr 创建

    也可以直接在 pom.xml 中引入以下依赖

    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
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Finchley.RC1</spring-cloud.version>
    </properties>

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

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
    </dependencies>

    <dependencyManagement>
    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>${spring-cloud.version}</version>
    <type>pom</type>
    <scope>import</scope>
    </dependency>
    </dependencies>
    </dependencyManagement>

    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>

    通过@EnableEurekaServer注解启动一个服务注册中心提供给其他应用进行对话。这一步非常的简单,只需要在一个普通的 Spring Boot 应用中添加这个注解就能开启此功能,比如

    1
    2
    3
    4
    5
    6
    7
    8
    @EnableEurekaServer
    @SpringBootApplication
    public class EurekaServerApplication {

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

    在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为,只需要在 application.yml 配置文件中增加如下信息:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    spring:
    application:
    name: eureka-server
    server:
    port: 7000
    eureka:
    instance:
    hostname: localhost
    client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
    defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    • server.port:为了与后续要进行注册的服务区分,这里将服务注册中心的端口设置为 7000。
    • eureka.client.register-with-eureka:表示是否将自己注册到 Eureka Server,默认为 true。
    • eureka.client.fetch-registry:表示是否从 Eureka Server 获取注册信息,默认为 true。
    • eureka.client.service-url.defaultZone:设置与 Eureka Server 交互的地址,查询服务和注册服务都需要依赖这个地址。默认是 http://localhost:8761/eureka ;多个地址可使用英文逗号(,)分隔。

    启动工程后,访问 http://localhost:7000/,可以看到下面的页面,其中还没有发现任何服务

    集群

    注册中心这么关键的服务,如果是单点话,遇到故障就是毁灭性的。在一个分布式系统中,服务注册中心是最重要的基础部分,理应随时处于可以提供服务的状态。为了维持其可用性,使用集群是很好的解决方案。Eureka 通过互相注册的方式来实现高可用的部署,所以我们只需要将 Eureke Server 配置其他可用的 service-url 就能实现高可用部署。

    双节点注册中心

    首先我们尝试一下双节点的注册中心的搭建。

    1、我们将之前的 application.yml 复制一份并命名为 application-peer1.yml,作为 peer1 服务中心的配置,并将 service-url 指向 peer2

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    spring:
    application:
    name: eureka-server
    server:
    port: 7001
    eureka:
    instance:
    hostname: peer1
    client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
    defaultZone: http://peer2:7002/eureka/

    2、将之前的 application-peer1.yml 复制一份并命名为 application-peer2.yml,作为 peer2 服务中心的配置,并将 service-url 指向 peer1

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    spring:
    application:
    name: eureka-server
    server:
    port: 7002
    eureka:
    instance:
    hostname: peer2
    client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
    defaultZone: http://peer1:7001/eureka/

    3、配本地 host:
    在 hosts 文件中加入如下配置

    1
    127.0.0.1 peer1 peer2

    4、打包启动
    依次执行下面命令

    1
    2
    3
    4
    5
    6
    # 打包
    mvn clean package -Dmaven.test.skip=true

    # 分别以 peer1 和 peer2 配置信息启动 Eureka
    java -jar target/eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1
    java -jar target/eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2

    在刚启动 peer1 的时候,启动完成后会在控制台看到一些异常信息,大致就是拒绝连接、请求超时这一类的,这个不用管,启动 peer2 后就好了。

    依次启动完成后,访问 http://localhost:7001/,效果如下

    根据图可以看出 peer1 的注册中心 DS Replicas 已经有了 peer2 的相关配置信息,并且出现在 available-replicas 中。我们手动停止 peer2 来观察,发现 peer2 就会移动到 unavailable-replicas 一栏中,表示 peer2 不可用。

    到此双节点的配置已经完成。

    注意事项

    • 在搭建 Eureka Server 双节点或集群的时候,要把eureka.client.register-with-eurekaeureka.client.fetch-registry均改为true(默认)。否则会出现实例列表为空,且 peer2 不在 available-replicas 而在 unavailable-replicas 的情况(这时其实只是启动了两个单点实例)。如果是像我这样图省事把之前的单节点配置和双节点的配置放在一个工程里,双节点的配置里要显示设置以上两个参数,直接删除是用不了默认配置的——Spring profile 会继承未在子配置里设置的父配置(application.yml)中的配置。
    • 在注册的时候,配置文件中的spring.application.name必须一致,否则情况会是这样的

    Eureka 集群使用

    在生产中我们可能需要三台或者大于三台的注册中心来保证服务的稳定性,配置的原理其实都一样,将注册中心分别指向其它的注册中心。这里只介绍三台集群的配置情况,其实和双节点的注册中心类似,每台注册中心分别又指向其它两个节点即可。

    application-peer1.yml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    spring:
    application:
    name: eureka-server
    server:
    port: 7001
    eureka:
    instance:
    hostname: peer1
    client:
    service-url:
    defaultZone: http://peer2:7002/eureka/,http://peer3:7003/eureka/

    application-peer2.yml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    spring:
    application:
    name: eureka-server
    server:
    port: 7002
    eureka:
    instance:
    hostname: peer2
    client:
    service-url:
    defaultZone: http://peer1:7001/eureka/,http://peer3:7003/eureka/

    application-peer3.yml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    spring:
    application:
    name: eureka-server
    server:
    port: 7003
    eureka:
    instance:
    hostname: peer3
    client:
    service-url:
    defaultZone: http://peer1:7001/eureka/,http://peer2:7002/eureka/

    修改 hosts 文件中的配置,添加 peer3

    1
    127.0.0.1 peer1 peer2 peer3

    分别以 peer1、peer2、peer3 的配置参数启动 Eureka 注册中心

    1
    2
    3
    java -jar target/eureka-server-0.0.1-SNAPSHOT.jar  --spring.profiles.active=peer1
    java -jar target/eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer2
    java -jar target/eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer3

    依次启动完成后,访问 http://localhost:7001/,效果如下

    可以在 peer1 中看到了 peer2、peer3 的相关信息,至此 Eureka 集群也已经完成了。

    注册中心 Eureka 就介绍到这里,下一节我们将利用我们搭建的 Eureka Server 来为服务提供者 / 调用者提供注册 / 发现服务

    相关阅读

    Spring Cloud(一):服务治理技术概览
    Spring Cloud(二):服务注册与发现 Eureka
    Spring Cloud(三):服务提供与调用 Eureka
    Spring Cloud(四):服务容错保护 Hystrix
    Spring Cloud(五):Hystrix 监控面板
    Spring Cloud(六):Hystrix 监控数据聚合 Turbine
    Spring Cloud(七):配置中心(Git 版与动态刷新)
    Spring Cloud(八):配置中心(服务化与高可用)
    Spring Cloud(九):配置中心(消息总线)
    Spring Cloud(十):服务网关 Zuul(路由)
    Spring Cloud(十一):服务网关 Zuul(过滤器)
    Spring Cloud(十二):分布式链路跟踪(Sleuth 与 Zipkin)

    示例代码:GitHub

    参考

    springcloud(二):注册中心 Eureka
    Spring Cloud 构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston 版】
    Spring Cloud 技术分析(1)——服务治理
    Spring Cloud - Peer Awareness

  • 相关阅读:
    K8s(2)-部署应用
    Docker-常用命令(7)
    Docker-堆栈stack(6)
    Docker-集群swarm(5)
    Docker-服务(4)
    Docker的概念术语(2)
    k8s(1)-使用kubeadm安装Kubernetes
    Celery-分布式任务队列
    使用Python管理压缩包
    jQuery基础
  • 原文地址:https://www.cnblogs.com/chenweida/p/9025615.html
Copyright © 2011-2022 走看看