> For the complete documentation index, see [llms.txt](https://potoyang.gitbook.io/spring-in-action-v5/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://potoyang.gitbook.io/spring-in-action-v5/di-13-zhang-fu-wu-fa-xian/13.2-pei-zhi-fu-wu-zhu-ce/13.2.2-kuo-zhan-eureka.md).

# 13.2.2 扩展 Eureka

虽然在开发环境中，只用一个 Eureka 实例很方便，但是在将应用程序部署到生产环境中时，您需要至少两个 Eureka 实例以确保服务高可用。

## 生产环境中的 Spring Cloud 服务

在生产环境中部署微服务时，需要考虑很多问题。虽然 Eureka 服务的高可用性和安全性，在开发环境不那么重要，但在生产环境中是至关重要的。如果您是 `Pivotal Cloud Foundry` 或 `Pivotal Web Services` 的客户，那么您可以让其他人处理那些事。

Pivotal 公司的 Spring Cloud 服务，不仅提供了适用于生产环境的 Eureka 实现，还有配置服务和断路器仪表盘。您要做的就是从服务市场选择 `p-service-registry` 服务，然后绑定到微服务上。对于配置服务和断路器仪表盘（我们将在接下来的几章中讨论），在服务市场中选择名称为 `p-config-server` 和 `p-circuit-breaker-dashboard` 的服务。

配置两个（或更多）Eureka 实例的最简单、最直接的方法，就是在 application.yml 中使用 profiles 配置。然后多次启动 Eureka 服务，每次使用一个不同的 profile。例如，下面清单中的配置显示了，如何配置两个相互充当对等点的 Eureka 服务器。

{% code title="清单 13.1 使用 Spring profiles 配置包含两个对等点的 Eureka" %}

```
eureka:
  client:
    service-url:
      defaultZone: http://${other.eureka.host}:${other.eureka.port}/eureka

---
spring:
  profiles: eureka-1
    application:
      name: eureka-1

server:
  port: 8761

eureka:
  instance:
    hostname: eureka1.tacocloud.com

other:
  eureka:
    host: eureka2.tacocloud.com
    port: 8762

---
spring:
  profiles: eureka-2
    application:
      name: eureka-2

server:
  port: 8762

eureka:
  instance:
    hostname: eureka2.tacocloud.com

other:
  eureka:
    host: eureka1.tacocloud.com
    port: 8761
```

{% endcode %}

在默认 profile（在清单13.1的顶部）中，设置了 `eureka.client.serviceurl` 属性。其中的占位符变量，引用了在每个 profile 中特殊指定的属性。

在默认 profile 之后，配置了两个 profile，一个命名为 `eureka-1`，另一个命名为 `eureka-2`。每个 profile 都为自己的配置指定自己的端口和实例名。在每个 profile 中设置了 `other.eureka.host` 和 `other.eureka.port` 两个属性，以引用另一个 Eureka 实例。这些属性是默认 profile 中的占位符所引用的属性，其他就没有什么特别之处了。

请注意，没有设置 `eureka.client.fetch-registry` 或 `eureka.client.register-with-ereake`。默认值确保 Eureka 服务相互注册，并从另一个 Eureka 服务获取注册信息。

虽然我们的 Eureka 服务现在已经启动运行了，不过本质上现在就像是一个没有人会看的空白的电话簿。没有微服务在 Eureka 中注册，也没有其他服务来查找服务，一切还都没什么实际用处。让我们看看如何启用一些微服务来使用 Eureka。
