如何激活
要激活 Consul Service Discovery,请使用带有 group 和 artifact id 的启动器。有关使用当前 Spring Cloud 发布系列设置构建系统的详细信息,请参阅 Spring Cloud 项目页面。org.springframework.cloud
spring-cloud-starter-consul-discovery
在领事处注册
当客户端向 Consul 注册时,它会提供有关自身的元数据,例如主机和端口、ID、名称和标记。默认情况下,会创建一个 HTTP 检查,即 Consul 每 10 秒命中一次终结点。如果运行状况检查失败,则服务实例将标记为严重。/actuator/health
Consul 客户示例:
@SpringBootApplication
@RestController
public class Application {
@RequestMapping("/")
public String home() {
return "Hello world";
}
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
(即完全正常的 Spring Boot 应用程序)。如果 Consul 客户端位于 以外的位置,则需要配置才能找到客户端。例:localhost:8500
spring: cloud: consul: host: localhost port: 8500
如果您使用 Spring Cloud Consul Config,并且您已经设置了 或 或使用 ,则需要将上述值放在 中而不是 。spring.cloud.bootstrap.enabled=true spring.config.use-legacy-processing=true spring-cloud-starter-bootstrap bootstrap.yml application.yml |
默认服务名称、实例 ID 和端口分别取自 、Spring Context ID 和 。Environment
${spring.application.name}
${server.port}
要禁用 Consul Discovery Client,您可以设置为 。当设置为 时,Consul Discovery Client 也将被禁用。spring.cloud.consul.discovery.enabled
false
spring.cloud.discovery.enabled
false
若要禁用服务注册,可以设置为 。spring.cloud.consul.discovery.register
false
将管理注册为单独的服务
当管理服务器端口设置为与应用程序端口不同的内容时,通过设置属性,管理服务将注册为与应用程序服务不同的服务。例如:management.server.port
spring: application: name: myApp management: server: port: 4452
上述配置将注册以下 2 项服务:
-
申请服务:
ID: myApp Name: myApp
-
管理服务:
ID: myApp-management Name: myApp-management
管理服务将继承其 和 从应用程序服务继承。例如:instanceId
serviceName
spring: application: name: myApp management: server: port: 4452 spring: cloud: consul: discovery: instance-id: custom-service-id serviceName: myprefix-${spring.application.name}
上述配置将注册以下 2 项服务:
-
申请服务:
ID: custom-service-id Name: myprefix-myApp
-
管理服务:
ID: custom-service-id-management Name: myprefix-myApp-management
可以通过以下属性进行进一步自定义:
/** Port to register the management service under (defaults to management port) */ spring.cloud.consul.discovery.management-port /** Suffix to use when registering management service (defaults to "management") */ spring.cloud.consul.discovery.management-suffix /** Tags to use when registering management service (defaults to "management") */ spring.cloud.consul.discovery.management-tags
HTTP 运行状况检查
Consul 实例的运行状况检查默认为“/actuator/health”,这是 Spring Boot Actuator 应用程序中运行状况终结点的默认位置。即使对于执行器应用程序,如果您使用非默认上下文路径或 servlet 路径(例如)或管理端点路径(例如 ),您也需要更改此设置。server.servletPath=/foo
management.server.servlet.context-path=/admin
还可以配置 Consul 用于检查运行状况终结点的时间间隔。“10s”和“1m”分别代表 10 秒和 1 分钟。
此示例演示了上述内容(有关更多选项,请参阅附录页中的属性)。spring.cloud.consul.discovery.health-check-*
spring: cloud: consul: discovery: healthCheckPath: ${management.server.servlet.context-path}/actuator/health healthCheckInterval: 15s
您可以通过设置 来完全禁用 HTTP 运行状况检查。spring.cloud.consul.discovery.register-health-check=false
应用标头
标头可以应用于运行状况检查请求。例如,如果您尝试注册使用 Vault Backend 的 Spring Cloud Config 服务器:
spring: cloud: consul: discovery: health-check-headers: X-Config-Token: 6442e58b-d1ea-182e-cfa5-cf9cddef0722
根据 HTTP 标准,每个标头可以有多个值,在这种情况下,可以提供一个数组:
spring: cloud: consul: discovery: health-check-headers: X-Config-Token: - "6442e58b-d1ea-182e-cfa5-cf9cddef0722" - "Some other value"
TTL 健康检查
可以使用领事 TTL 检查代替默认配置的 HTTP 检查。 主要区别在于应用程序向 Consul 代理发送检测信号,而不是向应用程序发送请求的 Consul 代理。
还可以配置应用程序用于发送 ping 的间隔。“10s”和“1m”分别代表 10 秒和 1 分钟。 默认值为 30 秒。
此示例演示了上述内容(有关更多选项,请参阅附录页中的属性)。spring.cloud.consul.discovery.heartbeat.*
spring: cloud: consul: discovery: heartbeat: enabled: true ttl: 10s
TTL申请状态
对于 Spring Boot Actuator 应用程序,状态由其可用的运行状况终结点确定。 当运行状况终结点不可用(禁用或不是 Spring Boot Actuator 应用程序)时,它假定应用程序处于良好运行状况。
查询运行状况终端节点时,默认使用根运行状况组。 通过设置以下属性,可以使用不同的运行状况组:
spring: cloud: consul: discovery: heartbeat: actuator-health-group: <your-custom-group-goes-here>
可以通过设置以下属性完全禁用运行状况终结点的使用:
spring: cloud: consul: discovery: heartbeat: use-actuator-health: false
自定义 TTL 应用程序状态
如果要配置自己的应用程序状态机制,只需实现接口即可ApplicationStatusProvider
@Bean public class MyCustomApplicationStatusProvider implements ApplicationStatusProvider { public CheckStatus currentStatus() { return yourMethodToDetermineAppStatusGoesHere(); } }
并使其可用于应用程序上下文:
@Bean public CustomApplicationStatusProvider customAppStatusProvider() { return new MyCustomApplicationStatusProvider(); }
执行器健康指示器
如果服务实例是 Spring Boot Actuator 应用程序,则可能会为其提供以下执行器运行状况指示器。
DiscoveryClientHealth指标
当 Consul Service Discovery 处于活动状态时,将配置 DiscoverClientHealthIndicator 并将其提供给 Actuator 运行状况终结点。 有关配置选项,请参阅此处。
领事健康指标
配置了一个指示器,用于验证 的运行状况。ConsulClient
默认情况下,它检索 Consul 领导节点状态和所有已注册的服务。
在具有许多已注册服务的部署中,在每次运行状况检查时检索所有服务的成本可能很高。
要跳过服务检索,仅检查领导节点状态集。spring.cloud.consul.health-indicator.include-services-query=false
要禁用指标集 。management.health.consul.enabled=false
当应用程序在引导上下文模式(默认)下运行时, 此指示器已加载到引导上下文中,并且不可用于执行器运行状况终结点。 |
元数据
Consul 支持服务的元数据。Spring Cloud 有一个从服务字段填充的字段。若要填充字段,请设置值 on 或属性。ServiceInstance
Map<String, String> metadata
meta
meta
spring.cloud.consul.discovery.metadata
spring.cloud.consul.discovery.management-metadata
spring: cloud: consul: discovery: metadata: myfield: myvalue anotherfield: anothervalue
上述配置将导致 meta 字段包含 和 的服务。myfield→myvalue
anotherfield→anothervalue
生成的元数据
领事自动注册将自动生成一些条目。
钥匙 | 价值 |
---|---|
“组” |
财产。仅当不为空时,才会生成此值。 |
“安全” |
如果属性等于“https”,则为 true,否则为 false。 |
属性,默认为“zone” |
财产。仅当不为空时,才会生成此值。 |
旧版本的 Spring Cloud Consul 通过解析属性从 Spring Cloud Commons 填充该方法。不再支持此功能,请迁移到使用地图。ServiceInstance.getMetadata() spring.cloud.consul.discovery.tags spring.cloud.consul.discovery.metadata |
使 Consul 实例 ID 唯一
默认情况下,consul 实例的注册 ID 等于其 Spring Application Context ID。默认情况下,Spring Application Context ID 为 。在大多数情况下,这将允许一个服务的多个实例在一台计算机上运行。如果需要进一步的唯一性,可以使用 Spring Cloud 通过在 中提供唯一标识符来覆盖它。例如:${spring.application.name}:comma,separated,profiles:${server.port}
spring.cloud.consul.discovery.instanceId
spring: cloud: consul: discovery: instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
有了这个元数据,并在 localhost 上部署了多个服务实例,随机值将在那里启动,使实例是唯一的。在Cloudfoundry中,将自动填充到Spring Boot应用程序中,因此不需要随机值。vcap.application.instance_id
如果您使用 Spring Cloud Consul Config,并且您已经设置了 或 或使用 ,则需要将上述值放在 中而不是 。spring.cloud.bootstrap.enabled=true spring.config.use-legacy-processing=true spring-cloud-starter-bootstrap bootstrap.yml application.yml |
当应用程序在引导上下文模式(默认)下运行时, 此指示器已加载到引导上下文中,并且不可用于执行器运行状况终结点。 |
钥匙 | 价值 |
---|---|
“组” |
财产。仅当不为空时,才会生成此值。 |
“安全” |
如果属性等于“https”,则为 true,否则为 false。 |
属性,默认为“zone” |
财产。仅当不为空时,才会生成此值。 |
旧版本的 Spring Cloud Consul 通过解析属性从 Spring Cloud Commons 填充该方法。不再支持此功能,请迁移到使用地图。ServiceInstance.getMetadata() spring.cloud.consul.discovery.tags spring.cloud.consul.discovery.metadata |
查找服务
使用负载均衡器
Spring Cloud 支持 Feign(REST 客户端构建器)和 Spring RestTemplate
,用于使用逻辑服务名称/ID 而不是物理 URL 查找服务。Feign 和发现感知的 RestTemplate 都利用 Spring Cloud LoadBalancer 进行客户端负载均衡。
如果要使用 RestTemplate 访问服务 STORES,只需声明:
@LoadBalanced @Bean public RestTemplate loadbalancedRestTemplate() { return new RestTemplate(); }
并像这样使用它(注意我们如何使用 Consul 的 STORES 服务名称/ID,而不是完全限定的域名):
@Autowired RestTemplate restTemplate; public String getFirstProduct() { return this.restTemplate.getForObject("https://STORES/products/1", String.class); }
如果在多个数据中心有 Consul 群集,并且想要访问另一个数据中心中的服务,则仅使用服务名称/ID 是不够的。在这种情况下
You Use 属性,其中是服务名称/ID,是数据中心
STORES 服务所在的位置。spring.cloud.consul.discovery.datacenters.STORES=dc-west
STORES
dc-west
Spring Cloud 现在还提供对 Spring Cloud LoadBalancer 的支持。 |
使用 DiscoveryClient
您还可以使用它为发现客户端提供一个简单的 API,该 API 并非特定于 Netflix,例如org.springframework.cloud.client.discovery.DiscoveryClient
@Autowired private DiscoveryClient discoveryClient; public String serviceUrl() { List<ServiceInstance> list = discoveryClient.getInstances("STORES"); if (list != null && list.size() > 0 ) { return list.get(0).getUri(); } return null; }
Spring Cloud 现在还提供对 Spring Cloud LoadBalancer 的支持。 |
领事目录手表
领事目录监视利用了领事监视服务的能力。目录监视会阻止 Consul HTTP API 调用,以确定是否有任何服务已更改。如果有新的服务数据,则发布检测信号事件。
要更改 Config Watch 被调用的频率,请更改 .默认值为 1000,以毫秒为单位。延迟是上一次调用结束和下一次调用开始后的时间量。spring.cloud.consul.config.discovery.catalog-services-watch-delay
要禁用目录监视集 。spring.cloud.consul.discovery.catalogServicesWatch.enabled=false
手表使用弹簧来安排与领事的通话。默认情况下,它是 a 为 1 的 a。要更改 ,请创建一个以常量命名的 bean。TaskScheduler
ThreadPoolTaskScheduler
poolSize
TaskScheduler
TaskScheduler
ConsulDiscoveryClientConfiguration.CATALOG_WATCH_TASK_SCHEDULER_NAME